internal/control/status.go
156 lines · 5185 bytes
1package control
2
3import (
4 "fmt"
5 "io"
6 "strings"
7
8 "gitbay.org/gitbay/internal/gitutil"
9 "gitbay.org/gitbay/internal/policy"
10 "gitbay.org/gitbay/internal/protocol"
11 "gitbay.org/gitbay/internal/store"
12)
13
14func init() {
15 register(Command{Path: []string{"status", "set"},
16 Summary: "report a commit status (CI)",
17 Usage: "status set <owner/name> <sha> --context <c> --state pending|success|failure|error [--description <d>] [--url <u>]",
18 Flags: []Flag{
19 {"--context", "<c>", "the check this status reports for; ci/ is reserved for the instance's builds", ""},
20 {"--state", "pending|success|failure|error", "the check's outcome", ""},
21 {"--description", "<d>", "short text shown beside the state", ""},
22 {"--url", "<u>", "link to the check's own output", ""},
23 },
24 Examples: []string{
25 "status set krz/gitbay a1b2c3d --context ext/lint --state success",
26 },
27 Run: runStatusSet})
28 register(Command{Path: []string{"status", "list"},
29 Summary: "statuses on a commit",
30 Usage: "status list <owner/name> <sha>",
31 Examples: []string{"status list krz/gitbay a1b2c3d"},
32 ReadOnly: true, Run: runStatusList})
33}
34
35var validStatusState = map[string]bool{"pending": true, "success": true, "failure": true, "error": true}
36
37func runStatusSet(c *Ctx, args []string) int {
38 var path, sha, context, state, description, url string
39 rest := args
40 for i := 0; i < len(rest); i++ {
41 switch rest[i] {
42 case "--context", "--state", "--description", "--url":
43 if i+1 >= len(rest) {
44 return c.fail(protocol.ExitUsage, "%s requires a value", rest[i])
45 }
46 v := rest[i+1]
47 switch rest[i] {
48 case "--context":
49 context = v
50 case "--state":
51 state = v
52 case "--description":
53 description = v
54 case "--url":
55 url = v
56 }
57 i++
58 default:
59 if path == "" {
60 path = rest[i]
61 } else if sha == "" {
62 sha = rest[i]
63 } else {
64 return c.fail(protocol.ExitUsage, "unexpected argument %q", rest[i])
65 }
66 }
67 }
68 if path == "" || sha == "" || context == "" || !validStatusState[state] {
69 return c.usage()
70 }
71 // ci/<job> statuses are the build subsystem's: queued, reused,
72 // skipped and finished by the server itself. A writer who could post
73 // one could mark ci/test green on their own head before, or instead
74 // of, the build (#258). Case-folded, so CI/test is no way around it.
75 if strings.HasPrefix(strings.ToLower(context), "ci/") {
76 return c.fail(protocol.ExitDenied, "the ci/ prefix is reserved for the instance's builds; report under another name, such as ext/%s",
77 strings.TrimPrefix(strings.ToLower(context), "ci/"))
78 }
79 if url != "" && !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
80 return c.fail(protocol.ExitUsage, "--url must be http(s)")
81 }
82 // Reporting a status is a write: CI identities need write access (an
83 // API token with full scope, or an account grant).
84 repo, code := resolveRepo(c, path, policy.CanWrite)
85 if code >= 0 {
86 return code
87 }
88 if code := refuseArchived(c, repo); code >= 0 {
89 return code
90 }
91 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
92 full, err := gitutil.ResolveRef(dir, sha)
93 if err != nil {
94 return c.fail(protocol.ExitNotFound, "no commit %s in %s", sha, repo.Path())
95 }
96 if err := c.Store.SetCommitStatus(repo.ID, full, context, state, description, url, c.User.ID); err != nil {
97 return c.fail(protocol.ExitFailure, "%v", err)
98 }
99 c.Store.RecordEvent(repo.ID, c.User.ID, "status",
100 fmt.Sprintf(`{"sha":%q,"context":%q,"state":%q}`, full, context, state))
101 return c.emit(map[string]string{"sha": full, "context": context, "state": state}, func(w io.Writer) {
102 fmt.Fprintf(w, "%s on %.10s: %s\n", context, full, state)
103 })
104}
105
106func runStatusList(c *Ctx, args []string) int {
107 if len(args) != 2 {
108 return c.usage()
109 }
110 repo, code := resolveRepo(c, args[0], policy.CanRead)
111 if code >= 0 {
112 return code
113 }
114 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
115 full, err := gitutil.ResolveRef(dir, args[1])
116 if err != nil {
117 return c.fail(protocol.ExitNotFound, "no commit %s in %s", args[1], repo.Path())
118 }
119 statuses, err := c.Store.ListCommitStatuses(repo.ID, full)
120 if err != nil {
121 return c.fail(protocol.ExitFailure, "%v", err)
122 }
123 type out struct {
124 Context string `json:"context"`
125 State string `json:"state"`
126 Description string `json:"description,omitempty"`
127 URL string `json:"url,omitempty"`
128 Creator string `json:"creator,omitempty"`
129 }
130 var ds []out
131 for _, s := range statuses {
132 ds = append(ds, out{s.Context, s.State, s.Description, s.TargetURL, s.Creator})
133 }
134 d := struct {
135 SHA string `json:"sha"`
136 Combined string `json:"combined"`
137 Statuses []out `json:"statuses"`
138 }{full, combinedOf(statuses), ds}
139 return c.emit(d, func(w io.Writer) {
140 tb := c.table(w, "CONTEXT", "STATE", "DESCRIPTION")
141 tb.row(cText("combined"), cState(orNone(d.Combined)), cText(fmt.Sprintf("%.10s", d.SHA)))
142 for _, x := range ds {
143 tb.row(cText(x.Context), cState(x.State), cFlex(x.Description))
144 }
145 tb.flush()
146 })
147}
148
149func combinedOf(statuses []store.CommitStatus) string { return store.CombinedStatus(statuses) }
150
151func orNone(s string) string {
152 if s == "" {
153 return "no statuses"
154 }
155 return s
156}