internal/control/output.go
79 lines · 2567 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}
37
38// ReviewOut is one review on a merge request.
39type ReviewOut struct {
40 Reviewer string `json:"reviewer"`
41 Verdict string `json:"verdict"`
42 Stale bool `json:"stale"`
43 // Counts reports whether this verdict decides the merge gates. A
44 // reader may review a public merge request; only someone who can
45 // write to the repository decides whether it merges. Without this the
46 // page would show an approval the gate ignores, and the difference
47 // would be invisible until a merge was refused (#147).
48 Counts bool `json:"counts"`
49 CreatedAt string `json:"created_at"`
50}
51
52// CheckOut is one commit status on a merge request head.
53type CheckOut struct {
54 Context string `json:"context"`
55 State string `json:"state"`
56 URL string `json:"url,omitempty"`
57 UpdatedAt string `json:"updated_at"`
58 Duration string `json:"duration,omitempty"` // CI checks only, once finished
59}
60
61// CommitOut is one commit a merge request carries.
62type CommitOut struct {
63 SHA string `json:"sha"`
64 Subject string `json:"subject"`
65}
66
67// jsonStrings renders a string slice as a JSON array, for the event data
68// the webhook payload carries. A nil slice is [], never null: a consumer
69// iterating the field should not have to special-case one of them.
70func jsonStrings(ss []string) string {
71 if ss == nil {
72 ss = []string{}
73 }
74 raw, err := json.Marshal(ss)
75 if err != nil {
76 return "[]"
77 }
78 return string(raw)
79}