internal/control/output.go

73 lines · 2204 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	CreatedAt string `json:"created_at"`
44}
45
46// CheckOut is one commit status on a merge request head.
47type CheckOut struct {
48	Context   string `json:"context"`
49	State     string `json:"state"`
50	URL       string `json:"url,omitempty"`
51	UpdatedAt string `json:"updated_at"`
52	Duration  string `json:"duration,omitempty"` // CI checks only, once finished
53}
54
55// CommitOut is one commit a merge request carries.
56type CommitOut struct {
57	SHA     string `json:"sha"`
58	Subject string `json:"subject"`
59}
60
61// jsonStrings renders a string slice as a JSON array, for the event data
62// the webhook payload carries. A nil slice is [], never null: a consumer
63// iterating the field should not have to special-case one of them.
64func jsonStrings(ss []string) string {
65	if ss == nil {
66		ss = []string{}
67	}
68	raw, err := json.Marshal(ss)
69	if err != nil {
70		return "[]"
71	}
72	return string(raw)
73}