internal/control/dashboard.go

d4aaf96d88e92486a458b150375b2386ac72fe34
gitbay/internal/control/dashboard.go history · blame · raw

214 lines · 6752 bytes

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