gunk-dev/the-valley

outcome.go

sourceac6512cd4671main's published tip history
1.9 KiB
1package rebase
2
3import "strings"
4
5// Outcome is an integrator outcome record, as the integrator writes it
6// (integrator/refs.go): the head it judged, the target tip it judged that
7// head against, the fingerprint of the evidence it saw, and its decision.
8// A failed pass adds when it failed. The first three are what the
9// integrator compares to decide whether to judge a request again.
10type Outcome struct {
11 Head, Tip, Evidence string
12 Decision string // land, stale, reject, refused-not-ready, refused-merge, voided-withdrawn, failed, or unknown
13 FailedAt string // set for failed alone
14}
15
16// ParseOutcome reads an outcome record. Anything it does not recognise is
17// unknown, never a decision: a damaged record, or one in a format newer
18// than this reader, must not read as a landing or as any other answer the
19// integrator did not give. Fields are separated by spaces or tabs, and
20// trailing newlines are dropped, as bin/valley read it with read(1); a
21// second line, a NUL, or a sixth field makes the record unknown.
22func ParseOutcome(record string) Outcome {
23 line := strings.TrimRight(record, "\n")
24 fields := strings.FieldsFunc(line, func(r rune) bool { return r == ' ' || r == '\t' })
25 field := func(i int) string {
26 if i < len(fields) {
27 return fields[i]
28 }
29 return ""
30 }
31 m := Outcome{Head: field(0), Tip: field(1), Evidence: field(2), Decision: field(3), FailedAt: field(4)}
32 if strings.ContainsAny(line, "\n\x00") || len(fields) > 5 ||
33 !IsObjectID(m.Head) || !IsObjectID(m.Tip) || !isHex(m.Evidence, 16) {
34 m.Decision = "unknown"
35 }
36 switch m.Decision {
37 case "land", "stale", "reject", "refused-not-ready", "refused-merge", "voided-withdrawn":
38 if m.FailedAt != "" {
39 m.Decision = "unknown"
40 }
41 case "failed":
42 if m.FailedAt == "" {
43 m.Decision = "unknown"
44 }
45 default:
46 m.Decision = "unknown"
47 }
48 if m.Decision != "failed" {
49 m.FailedAt = ""
50 }
51 return m
52}