oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package wire
2
3import (
4 "regexp"
5 "sort"
6 "strings"
7)
8
9// CIUnit is one failing hosted CI unit (a step, or a sharded step's
10// shard) and its log, as hosted checks output carries it: a header line
11// CIUnitHeader(Name), then the log.
12type CIUnit struct{ Name, Log string }
13
14// ciUnitPrefix starts a unit's header: distinct from Go's own "=== RUN"
15// lines, so the instance finds every unit without the binding's capped
16// list of names. A log could forge one; that only moves budget between
17// parts of the same quoted data.
18const ciUnitPrefix = "=== CI unit: "
19
20// CIUnitHeader is the line that starts a unit's part of the output.
21func CIUnitHeader(name string) string { return ciUnitPrefix + strings.ReplaceAll(name, "\n", " ") }
22
23// ciCut marks where a unit's log was cut.
24const ciCut = "[...]\n"
25
26// JoinCIUnits is every unit's part within limit bytes (stopped-jobs
27// audit, fix 4: the tail of all the logs joined kept only the last
28// failing unit). Every unit is named first: its header is reserved before
29// any log takes a share (when even the headers do not fit, as many units
30// as fit are named). The logs then get equal shares of what is left; what
31// a shorter one leaves goes to the others. Within its share a unit keeps
32// its failure lines first (ciFailure, with the indented lines after
33// each), then its tail.
34func JoinCIUnits(units []CIUnit, limit int) string {
35 heads := make([]string, len(units))
36 named, left := 0, limit
37 for i, u := range units {
38 heads[i] = CIUnitHeader(u.Name) + "\n"
39 if len(heads[i]) > left {
40 break
41 }
42 left -= len(heads[i])
43 named++
44 }
45 units, heads = units[:named], heads[:named]
46 logs := make([]string, len(units))
47 order := make([]int, len(units))
48 for i := range order {
49 order[i] = i
50 }
51 sort.SliceStable(order, func(a, b int) bool { return len(units[order[a]].Log) < len(units[order[b]].Log) })
52 for n, i := range order {
53 logs[i] = clipCILog(units[i].Log, left/(len(units)-n))
54 left -= len(logs[i])
55 }
56 var b strings.Builder
57 for i := range units {
58 b.WriteString(heads[i] + logs[i])
59 }
60 return b.String()
61}
62
63// ClipCIOutput is output within limit bytes: with units in it, the end
64// of what comes before the first (at most a third of limit), then every
65// unit's share (JoinCIUnits); without, its tail.
66func ClipCIOutput(output string, limit int) string {
67 head, units := SplitCIUnits(output)
68 if len(units) == 0 {
69 return tailLines(output, limit)
70 }
71 head = tailLines(head, limit/3)
72 return head + JoinCIUnits(units, limit-len(head))
73}
74
75// SplitCIUnits finds the units' parts in output: head is what comes
76// before the first header, and each unit runs to the next. Output with no
77// headers (an older runner's) is all head.
78func SplitCIUnits(output string) (head string, units []CIUnit) {
79 var b strings.Builder
80 cur := -1
81 flush := func() {
82 if cur < 0 {
83 head = b.String()
84 } else {
85 units[cur].Log = b.String()
86 }
87 b.Reset()
88 }
89 for _, line := range strings.SplitAfter(output, "\n") {
90 if name, ok := strings.CutPrefix(strings.TrimSuffix(line, "\n"), ciUnitPrefix); ok {
91 flush()
92 units = append(units, CIUnit{Name: name})
93 cur = len(units) - 1
94 continue
95 }
96 b.WriteString(line)
97 }
98 flush()
99 return head, units
100}
101
102// clipCILog is log within n bytes: whole when it fits; else its failure
103// lines first, within half of n, then a cut, then its tail.
104func clipCILog(log string, n int) string {
105 if !strings.HasSuffix(log, "\n") {
106 log += "\n"
107 }
108 if len(log) <= n {
109 return log
110 }
111 if n <= 2*len(ciCut) {
112 return ""
113 }
114 lines := strings.SplitAfter(log, "\n")
115 keep := make([]bool, len(lines))
116 for i, l := range lines {
117 if !ciFailure(l) {
118 continue
119 }
120 keep[i] = true
121 for j := i + 1; j < len(lines) && j <= i+3 && (strings.HasPrefix(lines[j], " ") || strings.HasPrefix(lines[j], "\t")); j++ {
122 keep[j] = true
123 }
124 }
125 pick := func(before int) string {
126 var b strings.Builder
127 offset := 0
128 for i, l := range lines {
129 if offset+len(l) > before {
130 break
131 }
132 offset += len(l)
133 if keep[i] && b.Len()+len(l) <= n/2 {
134 b.WriteString(l)
135 }
136 }
137 return b.String()
138 }
139 fails := pick(len(log))
140 tail := tailLines(log, n-len(fails)-2*len(ciCut))
141 // A failure line the tail keeps is not given twice.
142 fails = pick(len(log) - len(tail))
143 out := fails
144 if fails != "" || len(tail) < len(log) {
145 out += ciCut
146 }
147 return out + tail
148}
149
150// ciFailure: a line that says what failed (Go's test output and the
151// usual error words).
152func ciFailure(line string) bool {
153 t := strings.TrimSpace(line)
154 return strings.HasPrefix(t, "--- FAIL") || strings.HasPrefix(t, "FAIL") || strings.HasPrefix(t, "panic:") || ciError.MatchString(t) || ciDiagnostic.MatchString(t)
155}
156
157// ciError is an error word in any case; ciDiagnostic a compiler's or
158// checker's "file.go:12:3: message" (and a test's "x_test.go:12: ...").
159var (
160 ciError = regexp.MustCompile(`(?i)\berror\b`)
161 ciDiagnostic = regexp.MustCompile(`^[^\s:]+\.[A-Za-z0-9]+:\d+(:\d+)?: `)
162)
163
164// tailLines is the end of s within limit bytes, starting on a line.
165func tailLines(s string, limit int) string {
166 if limit <= 0 {
167 return ""
168 }
169 if len(s) <= limit {
170 return s
171 }
172 s = s[len(s)-limit:]
173 if i := strings.IndexByte(s, '\n'); i >= 0 {
174 s = s[i+1:]
175 }
176 return strings.ToValidUTF8(s, "")
177}
178