internal/control/dashboard.go
230 lines · 7297 bytes
1package control
2
3import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "strconv"
8
9 "gitbay.org/gitbay/internal/buildinfo"
10 "gitbay.org/gitbay/internal/gitutil"
11 "gitbay.org/gitbay/internal/policy"
12 "gitbay.org/gitbay/internal/protocol"
13 "gitbay.org/gitbay/internal/store"
14)
15
16func init() {
17 register(Command{Path: []string{"dashboard"},
18 Summary: "one read for the account dashboard: review queue, assigned and open work, pins, activity, builds",
19 Usage: "dashboard",
20 ReadOnly: true, Run: runDashboard})
21 register(Command{Path: []string{"feed"},
22 Summary: "activity on repositories you can reach",
23 Usage: "feed [--limit <n>] [--cursor <c>]",
24 ReadOnly: true, Run: runFeed})
25}
26
27// dashboardItem is one open issue or MR row, with its repo resolved so a
28// client renders the aggregate without further reads.
29type dashboardItem struct {
30 Repo string `json:"repo"`
31 Number int64 `json:"number"`
32 Title string `json:"title"`
33 Author string `json:"author"`
34 State string `json:"state"`
35 UpdatedAt string `json:"updated_at"`
36}
37
38func runDashboard(c *Ctx, args []string) int {
39 if len(args) != 0 {
40 return c.fail(protocol.ExitUsage, "usage: dashboard")
41 }
42 type pinnedOut struct {
43 Path string `json:"path"`
44 Visibility string `json:"visibility"`
45 Description string `json:"description,omitempty"`
46 Archived bool `json:"archived,omitempty"`
47 }
48 type buildOut struct {
49 Repo string `json:"repo"`
50 Number int64 `json:"number"`
51 Job string `json:"job"`
52 Status string `json:"status"`
53 SHA string `json:"sha"`
54 Ref string `json:"ref"`
55 CreatedAt string `json:"created_at"`
56 FinishedAt string `json:"finished_at,omitempty"`
57 }
58 // serverOut is admin-only. The exact build a host is running narrows down
59 // which known issues apply to it, so it is not everyone's to read; the
60 // person who needs it is the operator.
61 type serverOut struct {
62 Commit string `json:"commit"`
63 }
64 type out struct {
65 Reviews []dashboardItem `json:"review_queue"`
66 Assigned []dashboardItem `json:"assigned_issues"`
67 MRs []dashboardItem `json:"open_mrs"`
68 Issues []dashboardItem `json:"open_issues"`
69 Pinned []pinnedOut `json:"pinned"`
70 Activity []feedOut `json:"recent_activity"`
71 Builds []buildOut `json:"builds"`
72 Server *serverOut `json:"server,omitempty"`
73 }
74 d := out{
75 Reviews: []dashboardItem{}, Assigned: []dashboardItem{}, MRs: []dashboardItem{},
76 Issues: []dashboardItem{}, Pinned: []pinnedOut{}, Activity: []feedOut{}, Builds: []buildOut{},
77 }
78
79 pinned, err := c.Store.PinnedRepos(c.User.ID)
80 if err != nil {
81 return c.fail(protocol.ExitFailure, "%v", err)
82 }
83 for _, r := range pinned {
84 grant, err := c.Store.AccessRole(r.ID, c.User.ID)
85 if err != nil {
86 return c.fail(protocol.ExitFailure, "%v", err)
87 }
88 if !policy.CanRead(c.User, r, grant) {
89 continue
90 }
91 desc := gitutil.ReadDescription(RepoDir(c.Cfg.Server.Root, r.OwnerName, r.Name))
92 d.Pinned = append(d.Pinned, pinnedOut{r.Path(), r.Visibility, desc, r.Settings.Archived})
93 }
94
95 mrs, err := c.Store.DashboardMRs(c.User.ID)
96 if err != nil {
97 return c.fail(protocol.ExitFailure, "%v", err)
98 }
99 for _, m := range mrs {
100 d.MRs = append(d.MRs, dashboardItem{m.RepoPath, m.Number, m.Title, m.Author, m.State, m.UpdatedAt})
101 }
102
103 reviews, err := c.Store.ReviewQueue(c.User.ID)
104 if err != nil {
105 return c.fail(protocol.ExitFailure, "%v", err)
106 }
107 for _, m := range reviews {
108 d.Reviews = append(d.Reviews, dashboardItem{m.RepoPath, m.Number, m.Title, m.Author, m.State, m.UpdatedAt})
109 }
110
111 assigned, err := c.Store.AssignedIssues(c.User.ID)
112 if err != nil {
113 return c.fail(protocol.ExitFailure, "%v", err)
114 }
115 for _, i := range assigned {
116 d.Assigned = append(d.Assigned, dashboardItem{i.RepoPath, i.Number, i.Title, i.Author, i.State, i.UpdatedAt})
117 }
118
119 issues, err := c.Store.DashboardIssues(c.User.ID)
120 if err != nil {
121 return c.fail(protocol.ExitFailure, "%v", err)
122 }
123 for _, i := range issues {
124 d.Issues = append(d.Issues, dashboardItem{i.RepoPath, i.Number, i.Title, i.Author, i.State, i.UpdatedAt})
125 }
126
127 events, err := c.Store.RecentEvents(c.User.ID, 20, 0)
128 if err != nil {
129 return c.fail(protocol.ExitFailure, "%v", err)
130 }
131 d.Activity = feedOutputs(events)
132
133 builds, err := c.Store.RecentBuilds(c.User.ID, 20)
134 if err != nil {
135 return c.fail(protocol.ExitFailure, "%v", err)
136 }
137 for _, b := range builds {
138 d.Builds = append(d.Builds, buildOut{b.RepoPath, b.Number, b.Job, b.Status, b.SHA, b.Ref, b.CreatedAt, b.FinishedAt})
139 }
140 if c.User.IsAdmin {
141 d.Server = &serverOut{Commit: buildinfo.String()}
142 }
143
144 return c.emit(d, func(w io.Writer) {
145 fmt.Fprintln(w, "waiting on your review:")
146 printDashboardItems(w, d.Reviews, "!")
147 fmt.Fprintln(w, "assigned to you:")
148 printDashboardItems(w, d.Assigned, "#")
149 fmt.Fprintln(w, "open merge requests:")
150 printDashboardItems(w, d.MRs, "!")
151 fmt.Fprintln(w, "open issues:")
152 printDashboardItems(w, d.Issues, "#")
153 fmt.Fprintln(w, "pinned:")
154 for _, p := range d.Pinned {
155 mark := ""
156 if p.Archived {
157 mark = "\t[archived]"
158 }
159 fmt.Fprintf(w, " %s\t%s\t%s%s\n", p.Path, p.Visibility, p.Description, mark)
160 }
161 fmt.Fprintln(w, "recent activity:")
162 for _, e := range d.Activity {
163 fmt.Fprintf(w, " %s\t%s\t%s\t%s\t%s\n", e.CreatedAt, e.Actor, e.Kind, e.Repo, string(e.Data))
164 }
165 fmt.Fprintln(w, "builds:")
166 for _, b := range d.Builds {
167 fmt.Fprintf(w, " %s\t%d\t%s\t%s\t%.10s\t%s\n", b.Repo, b.Number, b.Job, b.Status, b.SHA, b.Ref)
168 }
169 if d.Server != nil {
170 fmt.Fprintf(w, "server:\n build %s\n", d.Server.Commit)
171 }
172 })
173}
174
175func printDashboardItems(w io.Writer, items []dashboardItem, marker string) {
176 for _, item := range items {
177 fmt.Fprintf(w, " %s%s%d\t%s\t%s\n", item.Repo, marker, item.Number, item.Title, item.Author)
178 }
179}
180
181// feedDefaultLimit caps a bare `feed` call; pagination reaches further
182// back.
183const feedDefaultLimit = 50
184
185type feedOut struct {
186 ID int64 `json:"id"`
187 Repo string `json:"repo"`
188 Actor string `json:"actor,omitempty"`
189 Kind string `json:"kind"`
190 Data json.RawMessage `json:"data,omitempty"`
191 CreatedAt string `json:"created_at"`
192}
193
194func feedOutputs(events []store.FeedEvent) []feedOut {
195 ds := make([]feedOut, 0, len(events))
196 for _, e := range events {
197 d := feedOut{ID: e.ID, Repo: e.RepoPath, Actor: e.Actor, Kind: e.Kind, CreatedAt: e.CreatedAt}
198 if json.Valid([]byte(e.Data)) {
199 d.Data = json.RawMessage(e.Data)
200 }
201 ds = append(ds, d)
202 }
203 return ds
204}
205
206func runFeed(c *Ctx, args []string) int {
207 rest, p, code := parsePageFlags(c, args, "feed", true)
208 if code >= 0 {
209 return code
210 }
211 if len(rest) != 0 {
212 return c.fail(protocol.ExitUsage, "usage: feed [--limit <n>] [--cursor <c>]")
213 }
214 if p.limit == 0 {
215 p.limit = feedDefaultLimit
216 }
217 events, err := c.Store.RecentEvents(c.User.ID, p.queryLimit(), p.keyInt())
218 if err != nil {
219 return c.fail(protocol.ExitFailure, "%v", err)
220 }
221 events, next := trimPage(p, events, "feed", func(e store.FeedEvent) string {
222 return strconv.FormatInt(e.ID, 10)
223 })
224 ds := feedOutputs(events)
225 return c.emitPage(p, ds, next, func(w io.Writer) {
226 for _, d := range ds {
227 fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", d.CreatedAt, d.Actor, d.Kind, d.Repo, string(d.Data))
228 }
229 })
230}