internal/control/output.go
107 lines · 3977 bytes
1package control
2
3import "encoding/json"
4
5// Named payloads for the commands other surfaces decode. A command used
6// to declare its output inline, so the web type-asserted map keys and
7// nothing tied what a command emits to what a client reads (#126).
8
9// Created is what issue create, issue comment and mr comment emit.
10type Created struct {
11 Number int64 `json:"number"`
12}
13
14// MRCreated is what mr create emits.
15type MRCreated struct {
16 Number int64 `json:"number"`
17 HeadSHA string `json:"head_sha"`
18 StackedOn *stackRef `json:"stacked_on,omitempty"`
19}
20
21// IssueShow is issue show's payload: the issue and its comments.
22type IssueShow struct {
23 issueOut
24 Comments []commentOut `json:"comments,omitempty"`
25}
26
27// MRShow is mr show's payload.
28type MRShow struct {
29 mrOut
30 Checks []CheckOut `json:"checks,omitempty"`
31 Combined string `json:"checks_combined,omitempty"`
32 UnresolvedThreads int `json:"unresolved_threads,omitempty"`
33 Commits []CommitOut `json:"commits,omitempty"`
34 Comments []commentOut `json:"comments,omitempty"`
35 Reviews []ReviewOut `json:"reviews,omitempty"`
36 // Gates is set while the merge request is open.
37 Gates *GatesOut `json:"gates,omitempty"`
38}
39
40// OwnersOut is a set of changed files still waiting on an approval from
41// one of their CODEOWNERS.
42type OwnersOut struct {
43 Files []string `json:"files"`
44 Owners []string `json:"owners"`
45}
46
47// GatesOut is what a merge request has to pass to merge, and where it
48// stands: the same computation mr merge refuses on, so nothing is
49// learned at the refusal that mr show did not say (#199).
50type GatesOut struct {
51 Draft bool `json:"draft,omitempty"`
52 ApprovalsRequired int `json:"approvals_required"`
53 Approvals []string `json:"approvals,omitempty"` // fresh, from reviewers who count
54 ChangesRequested []string `json:"changes_requested,omitempty"`
55 CodeownersRequired bool `json:"codeowners_required"`
56 OwnersOutstanding []OwnersOut `json:"owners_outstanding,omitempty"`
57 ResolvedRequired bool `json:"resolved_required"`
58 OpenThreads int `json:"open_threads"`
59 ChecksRequired bool `json:"checks_required"`
60 Checks string `json:"checks,omitempty"` // combined status; "" when none reported
61 ChecksMissing []string `json:"checks_missing,omitempty"` // required contexts not reported
62 FastForward bool `json:"fast_forward"`
63 Unmet []string `json:"unmet,omitempty"`
64}
65
66// ReviewOut is one review on a merge request.
67type ReviewOut struct {
68 Reviewer string `json:"reviewer"`
69 Verdict string `json:"verdict"`
70 Stale bool `json:"stale"`
71 // Counts reports whether this verdict decides the merge gates. A
72 // reader may review a public merge request; only someone who can
73 // write to the repository decides whether it merges. Without this the
74 // page would show an approval the gate ignores, and the difference
75 // would be invisible until a merge was refused (#147).
76 Counts bool `json:"counts"`
77 CreatedAt string `json:"created_at"`
78}
79
80// CheckOut is one commit status on a merge request head.
81type CheckOut struct {
82 Context string `json:"context"`
83 State string `json:"state"`
84 URL string `json:"url,omitempty"`
85 UpdatedAt string `json:"updated_at"`
86 Duration string `json:"duration,omitempty"` // CI checks only, once finished
87}
88
89// CommitOut is one commit a merge request carries.
90type CommitOut struct {
91 SHA string `json:"sha"`
92 Subject string `json:"subject"`
93}
94
95// jsonStrings renders a string slice as a JSON array, for the event data
96// the webhook payload carries. A nil slice is [], never null: a consumer
97// iterating the field should not have to special-case one of them.
98func jsonStrings(ss []string) string {
99 if ss == nil {
100 ss = []string{}
101 }
102 raw, err := json.Marshal(ss)
103 if err != nil {
104 return "[]"
105 }
106 return string(raw)
107}