gunk-dev/the-valley

queue.go

source171dbbb226c0main's published tip history
6.6 KiB
1package main
2
3// The operator's queue: the ready changes that wait on an approval, in the
4// order they would land, and the ready changes that move without one.
5// valley pending prints it (pending.go).
6//
7// A ready change whose active version has not landed is moving when a
8// current restack report says its restack stopped short of complete, when
9// one of its required checks failed, or when its active version has an
10// integration request at its head (approvedVersion). Every other one waits
11// on an approval. Waiting changes are stacked by ancestry among themselves
12// (stack): a waiting change that another's head contains folds into that
13// change's row, since approving the top lands every layer. A moving change
14// is a row of its own whatever it is stacked on, since it moves on its own.
15//
16// Rows are in landing order. The check worker builds ready versions in
17// the order it first saw them ready, then by project, then by change ID
18// (worker.go), and the integrator lands an approved request once its
19// evidence is complete. So a row's place is when its top's active version
20// became ready, oldest first, then its project and change ID. A record
21// carries no time, so when a version became ready is read as the commit
22// time of the change's record: the last write to the record of a ready
23// change is the one that readied its active version. The worker's own
24// order is the scan in which its pass first saw each version, which only
25// that pass holds, so versions readied between the same two scans may
26// land in another order than their rows.
27
28import (
29 "fmt"
30 "sort"
31 "strings"
32
33 "the-valley/change"
34)
35
36// queue is a repository's rows: the stacks of changes that wait on an
37// approval, each by its top, and the moving changes, each with its glyph
38// and what it says. Neither is in landing order yet (byLanding). With
39// measured, each entry is measured for its block (measure).
40func (r listed) queue(view jsonRepo, h handleMap, l listing, measured bool) (waiting, moving []*entry) {
41 states := r.states()
42 var wait []*entry
43 for _, c := range view.Changes {
44 e := r.changeEntry("ready", c, h, l, states)
45 v := c.Versions[c.Active-1]
46 switch {
47 case c.Restack != nil && c.Restack.Current && c.Restack.Report.Result != change.RestackComplete:
48 name := func(id string) string { return h[handleKey{r.name, c.Target, id}] }
49 e.glyph, e.colour = "◇", colourWaiting
50 if c.Restack.Report.Result == change.RestackConflict {
51 e.glyph, e.colour = "✗", colourRefused
52 }
53 e.says = "restack: " + readable(restackSays(c.Restack.Report, name))
54 case v.Checks != nil && v.Checks.State == checkFailed:
55 e.glyph, e.colour, e.says = "✗", colourRefused, failedNames(v.Checks)+" failed"
56 if c.Approved != nil && *c.Approved {
57 e.says = "approved · " + e.says
58 }
59 case c.Approved != nil && *c.Approved:
60 e.glyph, e.colour, e.says = "◐", colourWorking, "approved"
61 if n := checkCount(v.Checks); n != "" {
62 e.says += " · " + n
63 }
64 default:
65 wait = append(wait, e)
66 continue
67 }
68 moving = append(moving, e)
69 }
70 r.dated(append(append([]*entry{}, wait...), moving...))
71 waiting = r.snap.stack(wait, measured)
72 for _, e := range moving {
73 if measured {
74 r.snap.measure(e)
75 }
76 }
77 return waiting, moving
78}
79
80// byLanding orders rows as they would land: by when the top's active
81// version became ready, oldest first, then by project and change ID.
82func byLanding(rows []*entry) {
83 sort.SliceStable(rows, func(i, j int) bool {
84 a, b := rows[i], rows[j]
85 if a.since != b.since {
86 return a.since < b.since
87 }
88 if a.repo != b.repo {
89 return a.repo < b.repo
90 }
91 return a.id < b.id
92 })
93}
94
95// checkCount is a version's checks as a row says them: passed out of
96// required when every nix check passed, and otherwise how many are
97// pending or failed. A check of another runner is not counted, since the
98// host worker does not run it. It is "" when the checks were not read.
99func checkCount(c *jsonVersionChecks) string {
100 if c == nil {
101 return ""
102 }
103 count := map[string]int{}
104 for _, q := range c.Required {
105 count[q.State]++
106 }
107 built := len(c.Required) - count[checkOther]
108 switch {
109 case built == 0:
110 return "no checks required"
111 case count[checkFailed] > 0:
112 return counted(count[checkFailed], "check") + " failed"
113 case count[checkPending] > 0:
114 return counted(count[checkPending], "check") + " pending"
115 }
116 return fmt.Sprintf("checks %d/%d", count[checkPassed], built)
117}
118
119// failedNames names the checks that failed, up to three.
120func failedNames(c *jsonVersionChecks) string {
121 var names []string
122 for _, q := range c.Required {
123 if q.State == checkFailed {
124 names = append(names, q.Name)
125 }
126 }
127 return firstFew(names)
128}
129
130// states is the state of every change a repository records, by its
131// handle key.
132func (r listed) states() map[handleKey]string {
133 states := map[handleKey]string{}
134 for _, n := range r.snap.all {
135 states[handleKey{r.name, n.record.Target, n.id}] = n.record.State
136 }
137 return states
138}
139
140// changeEntry is a recorded change as an entry of section.
141func (r listed) changeEntry(section string, c jsonChange, h handleMap, l listing, states map[handleKey]string) *entry {
142 v := c.Versions[c.Active-1]
143 e := &entry{section: section, repo: r.name, id: c.ChangeID, record: c.Record,
144 name: fmt.Sprintf("%s v%d", c.Handle, v.N), ref: c.Handle, head: v.Head, title: readable(v.Title)}
145 if len(c.Knots) > 0 {
146 e.knot = readable(c.Knots[0].ID)
147 }
148 if section == "drafts" && v.Branch != "" {
149 e.name += " on " + branchName(v.Branch)
150 }
151 for _, id := range c.Includes {
152 k := handleKey{r.name, c.Target, id}
153 if st := states[k]; st != change.Ready && st != change.Draft {
154 e.includes = append(e.includes, fmt.Sprintf("includes %s (%s)", h[k], st))
155 }
156 }
157 if v.Landing == uncertain {
158 e.notes = append(e.notes, "uncertain: it may have landed without its evidence being stored")
159 }
160 if c.Restack != nil && c.Restack.Current {
161 name := func(id string) string { return h[handleKey{r.name, c.Target, id}] }
162 e.notes = append(e.notes, "restack: "+readable(restackSays(c.Restack.Report, name)))
163 }
164 if l.checks && section != "drafts" {
165 e.requires = r.snap.checks(v.Head)
166 }
167 if v.Checks != nil {
168 e.state, e.checks = v.Checks, checksLine(v.Checks)
169 }
170 return e
171}
172
173// dated gives each entry its head's commit time, age and author, and a
174// change's entry the commit time of its record.
175func (r listed) dated(all []*entry) {
176 for _, e := range all {
177 out, _ := r.g.output(true, "log", "-1", "--format=%ct%x00%cr%x00%an", e.head)
178 if fields := strings.SplitN(out, "\x00", 3); len(fields) == 3 {
179 e.when = parseUnix(fields[0])
180 e.age, e.author = fields[1], readable(fields[2])
181 }
182 if e.record != "" {
183 out, _ := r.g.output(true, "log", "-1", "--format=%ct", e.record)
184 e.since = parseUnix(out)
185 }
186 }
187}