internal/control/output.go

4effb29e572777a68b56b21da1b1733fb6219e89
gitbay/internal/control/output.go history · blame · raw

106 lines · 3869 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	FastForward        bool        `json:"fast_forward"`
 62	Unmet              []string    `json:"unmet,omitempty"`
 63}
 64
 65// ReviewOut is one review on a merge request.
 66type ReviewOut struct {
 67	Reviewer string `json:"reviewer"`
 68	Verdict  string `json:"verdict"`
 69	Stale    bool   `json:"stale"`
 70	// Counts reports whether this verdict decides the merge gates. A
 71	// reader may review a public merge request; only someone who can
 72	// write to the repository decides whether it merges. Without this the
 73	// page would show an approval the gate ignores, and the difference
 74	// would be invisible until a merge was refused (#147).
 75	Counts    bool   `json:"counts"`
 76	CreatedAt string `json:"created_at"`
 77}
 78
 79// CheckOut is one commit status on a merge request head.
 80type CheckOut struct {
 81	Context   string `json:"context"`
 82	State     string `json:"state"`
 83	URL       string `json:"url,omitempty"`
 84	UpdatedAt string `json:"updated_at"`
 85	Duration  string `json:"duration,omitempty"` // CI checks only, once finished
 86}
 87
 88// CommitOut is one commit a merge request carries.
 89type CommitOut struct {
 90	SHA     string `json:"sha"`
 91	Subject string `json:"subject"`
 92}
 93
 94// jsonStrings renders a string slice as a JSON array, for the event data
 95// the webhook payload carries. A nil slice is [], never null: a consumer
 96// iterating the field should not have to special-case one of them.
 97func jsonStrings(ss []string) string {
 98	if ss == nil {
 99		ss = []string{}
100	}
101	raw, err := json.Marshal(ss)
102	if err != nil {
103		return "[]"
104	}
105	return string(raw)
106}