internal/control/status.go
134 lines · 4216 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): status set <owner/name> <sha> --context <c> --state pending|success|failure|error [--description <d>] [--url <u>]",
17 Run: runStatusSet})
18 register(Command{Path: []string{"status", "list"},
19 Summary: "statuses on a commit: status list <owner/name> <sha>", ReadOnly: true, Run: runStatusList})
20}
21
22var validStatusState = map[string]bool{"pending": true, "success": true, "failure": true, "error": true}
23
24func runStatusSet(c *Ctx, args []string) int {
25 var path, sha, context, state, description, url string
26 rest := args
27 for i := 0; i < len(rest); i++ {
28 switch rest[i] {
29 case "--context", "--state", "--description", "--url":
30 if i+1 >= len(rest) {
31 return c.fail(protocol.ExitUsage, "%s requires a value", rest[i])
32 }
33 v := rest[i+1]
34 switch rest[i] {
35 case "--context":
36 context = v
37 case "--state":
38 state = v
39 case "--description":
40 description = v
41 case "--url":
42 url = v
43 }
44 i++
45 default:
46 if path == "" {
47 path = rest[i]
48 } else if sha == "" {
49 sha = rest[i]
50 } else {
51 return c.fail(protocol.ExitUsage, "unexpected argument %q", rest[i])
52 }
53 }
54 }
55 if path == "" || sha == "" || context == "" || !validStatusState[state] {
56 return c.fail(protocol.ExitUsage, "usage: status set <owner/name> <sha> --context <c> --state pending|success|failure|error")
57 }
58 if url != "" && !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
59 return c.fail(protocol.ExitUsage, "--url must be http(s)")
60 }
61 // Reporting a status is a write: CI identities need write access (an
62 // API token with full scope, or an account grant).
63 repo, code := resolveRepo(c, path, policy.CanWrite)
64 if code >= 0 {
65 return code
66 }
67 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
68 full, err := gitutil.ResolveRef(dir, sha)
69 if err != nil {
70 return c.fail(protocol.ExitNotFound, "no commit %s in %s", sha, repo.Path())
71 }
72 if err := c.Store.SetCommitStatus(repo.ID, full, context, state, description, url, c.User.ID); err != nil {
73 return c.fail(protocol.ExitFailure, "%v", err)
74 }
75 c.Store.RecordEvent(repo.ID, c.User.ID, "status",
76 fmt.Sprintf(`{"sha":%q,"context":%q,"state":%q}`, full, context, state))
77 return c.emit(map[string]string{"sha": full, "context": context, "state": state}, func(w io.Writer) {
78 fmt.Fprintf(w, "%s on %.10s: %s\n", context, full, state)
79 })
80}
81
82func runStatusList(c *Ctx, args []string) int {
83 if len(args) != 2 {
84 return c.fail(protocol.ExitUsage, "usage: status list <owner/name> <sha>")
85 }
86 repo, code := resolveRepo(c, args[0], policy.CanRead)
87 if code >= 0 {
88 return code
89 }
90 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
91 full, err := gitutil.ResolveRef(dir, args[1])
92 if err != nil {
93 return c.fail(protocol.ExitNotFound, "no commit %s in %s", args[1], repo.Path())
94 }
95 statuses, err := c.Store.ListCommitStatuses(repo.ID, full)
96 if err != nil {
97 return c.fail(protocol.ExitFailure, "%v", err)
98 }
99 type out struct {
100 Context string `json:"context"`
101 State string `json:"state"`
102 Description string `json:"description,omitempty"`
103 URL string `json:"url,omitempty"`
104 Creator string `json:"creator,omitempty"`
105 }
106 var ds []out
107 for _, s := range statuses {
108 ds = append(ds, out{s.Context, s.State, s.Description, s.TargetURL, s.Creator})
109 }
110 d := struct {
111 SHA string `json:"sha"`
112 Combined string `json:"combined"`
113 Statuses []out `json:"statuses"`
114 }{full, combinedOf(statuses), ds}
115 return c.emit(d, func(w io.Writer) {
116 fmt.Fprintf(w, "%.10s: %s\n", d.SHA, orNone(d.Combined))
117 for _, x := range ds {
118 extra := ""
119 if x.Description != "" {
120 extra = "\t" + x.Description
121 }
122 fmt.Fprintf(w, " %s\t%s%s\n", x.Context, x.State, extra)
123 }
124 })
125}
126
127func combinedOf(statuses []store.CommitStatus) string { return store.CombinedStatus(statuses) }
128
129func orNone(s string) string {
130 if s == "" {
131 return "no statuses"
132 }
133 return s
134}