internal/control/status.go
137 lines · 4282 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 if code := refuseArchived(c, repo); code >= 0 {
68 return code
69 }
70 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
71 full, err := gitutil.ResolveRef(dir, sha)
72 if err != nil {
73 return c.fail(protocol.ExitNotFound, "no commit %s in %s", sha, repo.Path())
74 }
75 if err := c.Store.SetCommitStatus(repo.ID, full, context, state, description, url, c.User.ID); err != nil {
76 return c.fail(protocol.ExitFailure, "%v", err)
77 }
78 c.Store.RecordEvent(repo.ID, c.User.ID, "status",
79 fmt.Sprintf(`{"sha":%q,"context":%q,"state":%q}`, full, context, state))
80 return c.emit(map[string]string{"sha": full, "context": context, "state": state}, func(w io.Writer) {
81 fmt.Fprintf(w, "%s on %.10s: %s\n", context, full, state)
82 })
83}
84
85func runStatusList(c *Ctx, args []string) int {
86 if len(args) != 2 {
87 return c.fail(protocol.ExitUsage, "usage: status list <owner/name> <sha>")
88 }
89 repo, code := resolveRepo(c, args[0], policy.CanRead)
90 if code >= 0 {
91 return code
92 }
93 dir := RepoDir(c.Cfg.Server.Root, repo.OwnerName, repo.Name)
94 full, err := gitutil.ResolveRef(dir, args[1])
95 if err != nil {
96 return c.fail(protocol.ExitNotFound, "no commit %s in %s", args[1], repo.Path())
97 }
98 statuses, err := c.Store.ListCommitStatuses(repo.ID, full)
99 if err != nil {
100 return c.fail(protocol.ExitFailure, "%v", err)
101 }
102 type out struct {
103 Context string `json:"context"`
104 State string `json:"state"`
105 Description string `json:"description,omitempty"`
106 URL string `json:"url,omitempty"`
107 Creator string `json:"creator,omitempty"`
108 }
109 var ds []out
110 for _, s := range statuses {
111 ds = append(ds, out{s.Context, s.State, s.Description, s.TargetURL, s.Creator})
112 }
113 d := struct {
114 SHA string `json:"sha"`
115 Combined string `json:"combined"`
116 Statuses []out `json:"statuses"`
117 }{full, combinedOf(statuses), ds}
118 return c.emit(d, func(w io.Writer) {
119 fmt.Fprintf(w, "%.10s: %s\n", d.SHA, orNone(d.Combined))
120 for _, x := range ds {
121 extra := ""
122 if x.Description != "" {
123 extra = "\t" + x.Description
124 }
125 fmt.Fprintf(w, " %s\t%s%s\n", x.Context, x.State, extra)
126 }
127 })
128}
129
130func combinedOf(statuses []store.CommitStatus) string { return store.CombinedStatus(statuses) }
131
132func orNone(s string) string {
133 if s == "" {
134 return "no statuses"
135 }
136 return s
137}