| 1 | package main |
| 2 | |
| 3 | // verdict prints what approve and review's prompt say (approve.go, |
| 4 | // review.go), from the facts they already hold. An approval ends in one verdict |
| 5 | // line, the change and what happened to it, with one indented line under |
| 6 | // it only when the person must act: |
| 7 | // |
| 8 | // ✓ nqyw v3 approved · checks passed · landing on main |
| 9 | // ◇ nqyw v3 approved · waiting for 2 checks (no worker building) |
| 10 | // run: valley approve --build-here nqyw |
| 11 | // ◐ nqyw v4 approved · 19 checks running · lands when they pass |
| 12 | // ⊘ nqyw v3 not approved · change is a draft |
| 13 | // run: valley ready nqyw |
| 14 | // |
| 15 | // An approval whose signed note was published with the request opens |
| 16 | // "signed and approved". |
| 17 | // |
| 18 | // The glyphs are plan's (plan.go): ✓ settled, ◐ under way, ◇ waiting on |
| 19 | // nothing that will move by itself, ⊘ refused, and ? unknown. An approval |
| 20 | // goes to stdout, a refusal to stderr. On a terminal the glyph has its |
| 21 | // colour and the change's name is bold; in a pipe, or with NO_COLOR set, |
| 22 | // the text is the same without them. A verdict never says a change landed: |
| 23 | // approve files a request and does not watch it land. An approval whose |
| 24 | // answer carried to a later version names that version, which lands, with |
| 25 | // its checks as the host's worker builds them. |
| 26 | // |
| 27 | // checking runs the checks approve builds here, as one line redrawn in |
| 28 | // place on a terminal and as a start line in a pipe, and keeps the |
| 29 | // builder's output to itself unless a check fails. prompt is review's |
| 30 | // prompt: a header of clauses about the change, and the keys it takes. |
| 31 | |
| 32 | import ( |
| 33 | "bufio" |
| 34 | "fmt" |
| 35 | "io" |
| 36 | "os" |
| 37 | "os/exec" |
| 38 | "strconv" |
| 39 | "strings" |
| 40 | "sync" |
| 41 | "syscall" |
| 42 | "time" |
| 43 | ) |
| 44 | |
| 45 | // The SGR colours of the verdict glyphs, as plan shows its states. |
| 46 | const ( |
| 47 | colourDone = "32" |
| 48 | colourWorking = "33" |
| 49 | colourRefused = "31" |
| 50 | colourWaiting = "36" |
| 51 | ) |
| 52 | |
| 53 | // verdict is the facts one outcome of approve is told from. |
| 54 | type verdict struct { |
| 55 | outcome string // approved, refused or unknown |
| 56 | name string // a change's handle, or a branch |
| 57 | version string |
| 58 | target string |
| 59 | required int // the checks the policy requires; -1 when not given |
| 60 | passed int // how many of them have passed; -1 when not known |
| 61 | took string |
| 62 | waiting []string // required checks no evidence is published for yet |
| 63 | worker bool // a check worker builds the waiting checks |
| 64 | carried bool // the version is the later one the answer carried to, its checks the active version's |
| 65 | signed bool // a signed approval was published with the request |
| 66 | failed []string |
| 67 | why string // what stops the approval, or what is not known |
| 68 | run string // the command the person runs next |
| 69 | see string // where the person reads more |
| 70 | then string // what the person does next, when it is no command |
| 71 | } |
| 72 | |
| 73 | // droppable is w, when it is a file, through a duplicate of its |
| 74 | // descriptor. Go ends a process whose write to its own standard output or |
| 75 | // error finds no reader; through a duplicate the write fails with EPIPE |
| 76 | // and nothing more, so an approval goes on when nobody reads what its |
| 77 | // checks print, as attest run's own output does. |
| 78 | func droppable(w io.Writer) io.Writer { |
| 79 | f, ok := w.(*os.File) |
| 80 | if !ok { |
| 81 | return w |
| 82 | } |
| 83 | syscall.ForkLock.RLock() |
| 84 | fd, err := syscall.Dup(int(f.Fd())) |
| 85 | if err == nil { |
| 86 | syscall.CloseOnExec(fd) |
| 87 | } |
| 88 | syscall.ForkLock.RUnlock() |
| 89 | if err != nil { |
| 90 | return w |
| 91 | } |
| 92 | return os.NewFile(uintptr(fd), f.Name()) |
| 93 | } |
| 94 | |
| 95 | // lines is the verdict: its line, and the action line under it when there |
| 96 | // is one. |
| 97 | func (v verdict) lines(t terminal) string { |
| 98 | var glyph, colour string |
| 99 | var clauses []string |
| 100 | action := v.action(t) |
| 101 | switch v.outcome { |
| 102 | case "refused": |
| 103 | glyph, colour = "⊘", colourRefused |
| 104 | why := v.why |
| 105 | if len(v.failed) > 0 { |
| 106 | why = fmt.Sprintf("%d of %s failed", len(v.failed), countOf(v.required, "check")) |
| 107 | if v.required < 0 { |
| 108 | why = countOf(len(v.failed), "check") + " failed" |
| 109 | } |
| 110 | action = t.tint(colourRefused, "✗") + " " + strings.Join(v.failed, ", ") + " — see: valley status " + v.name |
| 111 | } |
| 112 | clauses = []string{"not approved", why} |
| 113 | case "unknown": |
| 114 | glyph = "?" |
| 115 | why := v.why |
| 116 | if why == "" { |
| 117 | why = "state unknown" |
| 118 | } |
| 119 | clauses = []string{v.approved(), why, "see: valley status " + v.name} |
| 120 | action = "" |
| 121 | case "approved": |
| 122 | clauses = []string{v.approved()} |
| 123 | switch { |
| 124 | case len(v.failed) > 0: |
| 125 | glyph, colour = "✗", colourRefused |
| 126 | clauses = append(clauses, fmt.Sprintf("%d of %s failed", len(v.failed), countOf(v.required, "check"))) |
| 127 | action = t.tint(colourRefused, "✗") + " " + strings.Join(v.failed, ", ") + " — see: valley status " + v.name |
| 128 | case v.required == 0: |
| 129 | glyph, colour = "✓", colourDone |
| 130 | clauses = append(clauses, "no checks required", "landing on "+v.target) |
| 131 | case len(v.waiting) == 0: |
| 132 | glyph, colour = "✓", colourDone |
| 133 | passed := "checks passed" |
| 134 | if v.took != "" && v.required > 0 { |
| 135 | passed = countOf(v.required, "check") + " passed in " + v.took |
| 136 | } |
| 137 | clauses = append(clauses, passed, "landing on "+v.target) |
| 138 | case v.carried && v.worker: |
| 139 | glyph, colour = "◐", colourWorking |
| 140 | pass := "they pass" |
| 141 | if len(v.waiting) == 1 { |
| 142 | pass = "it passes" |
| 143 | } |
| 144 | clauses = append(clauses, countOf(len(v.waiting), "check")+" running", "lands when "+pass) |
| 145 | case v.worker: |
| 146 | glyph, colour = "◐", colourWorking |
| 147 | if v.passed >= 0 && v.required > 0 { |
| 148 | pass := "pass" |
| 149 | if len(v.waiting) == 1 { |
| 150 | pass = "passes" |
| 151 | } |
| 152 | clauses = append(clauses, fmt.Sprintf("lands when %s %s (%d/%d passed)", countOf(len(v.waiting), "check"), pass, v.passed, v.required)) |
| 153 | } else { |
| 154 | clauses = append(clauses, "waiting for "+countOf(len(v.waiting), "check")) |
| 155 | } |
| 156 | default: |
| 157 | glyph, colour = "◇", colourWaiting |
| 158 | clauses = append(clauses, "waiting for "+countOf(len(v.waiting), "check")+" (no worker building)") |
| 159 | } |
| 160 | } |
| 161 | line := t.tint(colour, glyph) + " " + t.strong(v.label()) + " " + strings.Join(clauses, " · ") + "\n" |
| 162 | if action != "" { |
| 163 | line += " " + action + "\n" |
| 164 | } |
| 165 | return line |
| 166 | } |
| 167 | |
| 168 | // approved is the verdict's first clause on a filed request: approved, or |
| 169 | // signed and approved when the approval's note was published with it. |
| 170 | func (v verdict) approved() string { |
| 171 | if v.signed { |
| 172 | return "signed and approved" |
| 173 | } |
| 174 | return "approved" |
| 175 | } |
| 176 | |
| 177 | // action is the line under a verdict that says what the person does next. |
| 178 | func (v verdict) action(t terminal) string { |
| 179 | switch { |
| 180 | case v.run != "": |
| 181 | return "run: " + v.run |
| 182 | case v.see != "": |
| 183 | return "see: " + v.see |
| 184 | } |
| 185 | return v.then |
| 186 | } |
| 187 | |
| 188 | // label is the change as a verdict names it: its handle and version, or |
| 189 | // a branch's name. |
| 190 | func (v verdict) label() string { |
| 191 | if v.version == "" { |
| 192 | return v.name |
| 193 | } |
| 194 | return v.name + " v" + v.version |
| 195 | } |
| 196 | |
| 197 | // countOf is n of a thing, as "1 check" or "3 checks". |
| 198 | func countOf(n int, thing string) string { |
| 199 | if n == 1 { |
| 200 | return "1 " + thing |
| 201 | } |
| 202 | return strconv.Itoa(n) + " " + thing + "s" |
| 203 | } |
| 204 | |
| 205 | // promptLines is review's prompt: the header, its first clause the |
| 206 | // change in bold, and the keys, each with its letter in bold on a |
| 207 | // terminal, left open for the answer. |
| 208 | func promptLines(t terminal, header, keys []string) string { |
| 209 | if len(header) > 0 { |
| 210 | header[0] = t.strong(header[0]) |
| 211 | } |
| 212 | shown := make([]string, len(keys)) |
| 213 | for i, k := range keys { |
| 214 | if open, rest, ok := strings.Cut(k, "["); ok { |
| 215 | if letter, after, ok := strings.Cut(rest, "]"); ok { |
| 216 | k = open + "[" + t.strong(letter) + "]" + after |
| 217 | } |
| 218 | } |
| 219 | shown[i] = k |
| 220 | } |
| 221 | return strings.Join(header, " · ") + "\n" + strings.Join(shown, " ") + ": " |
| 222 | } |
| 223 | |
| 224 | // logTail is how many lines of a failed check's log are shown, and |
| 225 | // logKept how many of the last lines of the run's output are kept to find |
| 226 | // them in. attest run prints a failed check's log after every check has |
| 227 | // ended, so the logs are at the end of its output. |
| 228 | const ( |
| 229 | logTail = 20 |
| 230 | logKept = 2000 |
| 231 | ) |
| 232 | |
| 233 | // checking runs cmd, attest run over the checks named, in order. On a |
| 234 | // terminal one line says which check is building and for how long, and is |
| 235 | // redrawn in place; in a pipe one line says the checks started. The |
| 236 | // command's own output is kept back, unless verbose, and when a check |
| 237 | // fails the end of its log is shown and the refusal follows. On success |
| 238 | // nothing more is printed: approve has not filed yet, and its verdict |
| 239 | // follows. The status is the command's. started, when given, is told of |
| 240 | // the process once it runs. Nothing is said of a run that SIGINT or |
| 241 | // SIGTERM ended, or that stopped, when given, reports was stopped. |
| 242 | func (v verdict) checking(checks []string, cmd *exec.Cmd, verbose bool, stdout, stderr io.Writer, started func(*os.Process), stopped func() bool) int { |
| 243 | t := terminalOf(stdout) |
| 244 | began := time.Now() |
| 245 | of := v.required |
| 246 | if of < len(checks) { |
| 247 | of = len(checks) |
| 248 | } |
| 249 | done := v.passed |
| 250 | if done < 0 { |
| 251 | done = of - len(checks) |
| 252 | } |
| 253 | |
| 254 | var mu sync.Mutex |
| 255 | var output []string |
| 256 | reported, failed := 0, []string(nil) |
| 257 | line := func() string { |
| 258 | mu.Lock() |
| 259 | defer mu.Unlock() |
| 260 | current := "" |
| 261 | if reported < len(checks) { |
| 262 | current = " · " + checks[reported] |
| 263 | } |
| 264 | return fmt.Sprintf("%s %s checking %d/%d%s (%s)…", t.tint(colourWorking, "◐"), t.strong(v.label()), |
| 265 | min(done+reported+1, of), of, current, time.Since(began).Round(time.Second)) |
| 266 | } |
| 267 | if verbose || !t.live { |
| 268 | fmt.Fprintf(stdout, "%s %s checking %s: %s\n", t.tint(colourWorking, "◐"), t.strong(v.label()), |
| 269 | countOf(len(checks), "check"), strings.Join(checks, ", ")) |
| 270 | } |
| 271 | // Both streams are read line by line, kept for a failure's log, and |
| 272 | // passed through as they come when verbose. A check's result is the |
| 273 | // line attest run prints on stdout as each check ends. |
| 274 | var cleanup []func() |
| 275 | var wg sync.WaitGroup |
| 276 | scan := func(echo io.Writer) io.Writer { |
| 277 | r, w := io.Pipe() |
| 278 | wg.Add(1) |
| 279 | go func() { |
| 280 | defer wg.Done() |
| 281 | s := bufio.NewScanner(r) |
| 282 | s.Buffer(make([]byte, 64*1024), 1024*1024) |
| 283 | for s.Scan() { |
| 284 | text := s.Text() |
| 285 | mu.Lock() |
| 286 | output = append(output, text) |
| 287 | if len(output) > 2*logKept { |
| 288 | output = append(output[:0], output[len(output)-logKept:]...) |
| 289 | } |
| 290 | if f := strings.Fields(text); len(f) == 4 && f[0] == "check" { |
| 291 | reported++ |
| 292 | if f[3] != "passed" { |
| 293 | failed = append(failed, f[1]) |
| 294 | } |
| 295 | } |
| 296 | if verbose { |
| 297 | fmt.Fprintln(echo, text) |
| 298 | } |
| 299 | mu.Unlock() |
| 300 | } |
| 301 | io.Copy(io.Discard, r) |
| 302 | }() |
| 303 | cleanup = append(cleanup, func() { w.Close() }) |
| 304 | return w |
| 305 | } |
| 306 | cmd.Stdout, cmd.Stderr = scan(stdout), scan(stderr) |
| 307 | cleanup = append(cleanup, wg.Wait) |
| 308 | if t.live && !verbose { |
| 309 | // The clock in the line moves once a second; what it shows |
| 310 | // otherwise changes only as the command reports a check. |
| 311 | stop := make(chan struct{}) |
| 312 | ticked := make(chan struct{}) |
| 313 | go func() { |
| 314 | defer close(ticked) |
| 315 | tick := time.NewTicker(time.Second) |
| 316 | defer tick.Stop() |
| 317 | for { |
| 318 | fmt.Fprint(stdout, "\r\x1b[K"+t.fit(line(), "")) |
| 319 | select { |
| 320 | case <-stop: |
| 321 | return |
| 322 | case <-tick.C: |
| 323 | } |
| 324 | } |
| 325 | }() |
| 326 | cleanup = append([]func(){func() { close(stop); <-ticked; fmt.Fprint(stdout, "\r\x1b[K") }}, cleanup...) |
| 327 | } |
| 328 | err := cmd.Start() |
| 329 | if err == nil { |
| 330 | if started != nil { |
| 331 | started(cmd.Process) |
| 332 | } |
| 333 | err = cmd.Wait() |
| 334 | } |
| 335 | for _, f := range cleanup { |
| 336 | f() |
| 337 | } |
| 338 | if err == nil { |
| 339 | return 0 |
| 340 | } |
| 341 | status := 1 |
| 342 | if exit, ok := err.(*exec.ExitError); ok && exit.ExitCode() > 0 { |
| 343 | status = exit.ExitCode() |
| 344 | } else if ok { |
| 345 | // A command a signal ended exits as a shell reports it, 128 and |
| 346 | // the signal's number. One that SIGINT or SIGTERM ended was |
| 347 | // stopped, as the person's ctrl-C stops every process in the |
| 348 | // terminal's foreground, this one with it. |
| 349 | if ws, ok := exit.Sys().(syscall.WaitStatus); ok && ws.Signaled() { |
| 350 | status = 128 + int(ws.Signal()) |
| 351 | if ws.Signal() == syscall.SIGINT || ws.Signal() == syscall.SIGTERM { |
| 352 | return status |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | if stopped != nil && stopped() { |
| 357 | return status |
| 358 | } |
| 359 | if !verbose { |
| 360 | fmt.Fprint(stderr, failedLogs(output, failed)) |
| 361 | } |
| 362 | r := v |
| 363 | r.outcome, r.failed = "refused", failed |
| 364 | if len(failed) == 0 { |
| 365 | r.why, r.see = "the checks did not run", "the output above" |
| 366 | if !verbose { |
| 367 | r.run = "valley approve -v " + v.name |
| 368 | } |
| 369 | } |
| 370 | fmt.Fprint(stderr, r.lines(terminalOf(stderr))) |
| 371 | return status |
| 372 | } |
| 373 | |
| 374 | // failedLogs is the end of each failed check's log, as attest run prints |
| 375 | // a failed check's log after a "--- <check> ---" line, or the end of the |
| 376 | // whole output when no check's log is in it. |
| 377 | func failedLogs(output, failed []string) string { |
| 378 | var b strings.Builder |
| 379 | shown := false |
| 380 | for _, name := range failed { |
| 381 | start := -1 |
| 382 | for i, l := range output { |
| 383 | if l == "--- "+name+" ---" { |
| 384 | start = i + 1 |
| 385 | } |
| 386 | } |
| 387 | if start < 0 { |
| 388 | continue |
| 389 | } |
| 390 | end := start |
| 391 | for end < len(output) && !strings.HasPrefix(output[end], "--- ") { |
| 392 | end++ |
| 393 | } |
| 394 | fmt.Fprintf(&b, "--- %s ---\n", name) |
| 395 | for _, l := range output[max(start, end-logTail):end] { |
| 396 | b.WriteString(l + "\n") |
| 397 | } |
| 398 | shown = true |
| 399 | } |
| 400 | if !shown { |
| 401 | for _, l := range output[max(0, len(output)-logTail):] { |
| 402 | b.WriteString(l + "\n") |
| 403 | } |
| 404 | } |
| 405 | return b.String() |
| 406 | } |