gunk-dev/the-valley

does.go

source672eff5ffaecmain's published tip history
3.5 KiB
1// The knots a change does: every knot in its project's plan graph whose
2// changes field lists valley:<project>/<change ID>. review shows them above
3// a change's diff (reviewpage.go), and changes, pending and status --json
4// include them (list.go, pending.go, render.go).
5//
6// The plan graph in project storage holds the links from knots to changes.
7// Each command reads a project's graph once, however many changes it
8// inspects. When the graph is missing or unreadable, every change gets no
9// knots, and -v warns on stderr.
10
11package main
12
13import (
14 "fmt"
15 "io"
16 "sort"
17 "strings"
18
19 "the-valley/change"
20 "the-valley/knot"
21)
22
23// knotRef is a knot that links a change, as JSON listings and review use it.
24type knotRef struct {
25 ID string `json:"id"`
26 Type string `json:"type"`
27 Status string `json:"status"`
28 Title string `json:"title"`
29
30 active bool
31}
32
33// line formats a knot for review:
34//
35// does oc-6335749 (open): Ready changes stay landable as main moves
36//
37// It strips newlines, tabs and control characters even though the graph
38// schema forbids them, so a malformed knot cannot break a line review
39// shows or write escape sequences to the terminal.
40func (k knotRef) line() string {
41 title := strings.Map(func(r rune) rune {
42 if r == '\n' || r == '\t' {
43 return ' '
44 }
45 return r
46 }, k.Title)
47 return fmt.Sprintf("does %s (%s): %s", visible(k.ID), visible(k.Status), visible(title))
48}
49
50// doers maps each change ID in a project to the knots that link it.
51type doers map[string][]knotRef
52
53// readDoers reads the project's plan graph at refs/the-valley/knots on g's
54// origin and indexes the knots by linked change ID. It keeps only links of
55// the form valley:<project>/<change ID>, ignoring links to other projects
56// or pull requests. When the graph is missing or unreadable, it returns an
57// empty map and, if verbose, warns on stderr.
58func readDoers(g git, project string, verbose bool, stderr io.Writer) doers {
59 return readDoersBy(g, project, verbose, stderr, remote.read)
60}
61
62// readDoersBy is readDoers with the graph read by read.
63func readDoersBy(g git, project string, verbose bool, stderr io.Writer, read func(remote) (knotState, error)) doers {
64 if !verbose {
65 g.stderr = io.Discard
66 }
67 s, err := read(remote{g: g, name: project})
68 switch {
69 case err != nil:
70 if verbose {
71 fmt.Fprintf(stderr, "valley: %s's plan graph could not be read, so no change shows a knot: %v\n", project, err)
72 }
73 return doers{}
74 case s.tip == "":
75 if verbose {
76 fmt.Fprintf(stderr, "valley: %s has no plan graph at %s, so no change shows a knot\n", project, knot.Ref)
77 }
78 return doers{}
79 }
80 prefix := "valley:" + project + "/"
81 d := doers{}
82 for _, id := range s.graph.IDs() {
83 k := s.graph.Knots[id]
84 ref := knotRef{ID: k.ID, Type: k.Type, Status: k.Status, Title: k.Title, active: k.Active()}
85 seen := map[string]bool{}
86 for _, link := range k.Changes {
87 cid, ok := strings.CutPrefix(link, prefix)
88 if !ok || !change.ValidID(cid) || seen[cid] {
89 continue
90 }
91 seen[cid] = true
92 d[cid] = append(d[cid], ref)
93 }
94 }
95 for _, list := range d {
96 // IDs() visits knots in ID order; a stable sort puts active knots
97 // before terminal ones while keeping each group in ID order.
98 sort.SliceStable(list, func(i, j int) bool { return list[i].active && !list[j].active })
99 }
100 return d
101}
102
103// of returns the knots that link change id, or an empty slice so JSON
104// encodes [] instead of null.
105func (d doers) of(id string) []knotRef {
106 if list := d[id]; list != nil {
107 return list
108 }
109 return []knotRef{}
110}