package wire import ( "regexp" "sort" "strings" ) // CIUnit is one failing hosted CI unit (a step, or a sharded step's // shard) and its log, as hosted checks output carries it: a header line // CIUnitHeader(Name), then the log. type CIUnit struct{ Name, Log string } // ciUnitPrefix starts a unit's header: distinct from Go's own "=== RUN" // lines, so the instance finds every unit without the binding's capped // list of names. A log could forge one; that only moves budget between // parts of the same quoted data. const ciUnitPrefix = "=== CI unit: " // CIUnitHeader is the line that starts a unit's part of the output. func CIUnitHeader(name string) string { return ciUnitPrefix + strings.ReplaceAll(name, "\n", " ") } // ciCut marks where a unit's log was cut. const ciCut = "[...]\n" // JoinCIUnits is every unit's part within limit bytes (stopped-jobs // audit, fix 4: the tail of all the logs joined kept only the last // failing unit). Every unit is named first: its header is reserved before // any log takes a share (when even the headers do not fit, as many units // as fit are named). The logs then get equal shares of what is left; what // a shorter one leaves goes to the others. Within its share a unit keeps // its failure lines first (ciFailure, with the indented lines after // each), then its tail. func JoinCIUnits(units []CIUnit, limit int) string { heads := make([]string, len(units)) named, left := 0, limit for i, u := range units { heads[i] = CIUnitHeader(u.Name) + "\n" if len(heads[i]) > left { break } left -= len(heads[i]) named++ } units, heads = units[:named], heads[:named] logs := make([]string, len(units)) order := make([]int, len(units)) for i := range order { order[i] = i } sort.SliceStable(order, func(a, b int) bool { return len(units[order[a]].Log) < len(units[order[b]].Log) }) for n, i := range order { logs[i] = clipCILog(units[i].Log, left/(len(units)-n)) left -= len(logs[i]) } var b strings.Builder for i := range units { b.WriteString(heads[i] + logs[i]) } return b.String() } // ClipCIOutput is output within limit bytes: with units in it, the end // of what comes before the first (at most a third of limit), then every // unit's share (JoinCIUnits); without, its tail. func ClipCIOutput(output string, limit int) string { head, units := SplitCIUnits(output) if len(units) == 0 { return tailLines(output, limit) } head = tailLines(head, limit/3) return head + JoinCIUnits(units, limit-len(head)) } // SplitCIUnits finds the units' parts in output: head is what comes // before the first header, and each unit runs to the next. Output with no // headers (an older runner's) is all head. func SplitCIUnits(output string) (head string, units []CIUnit) { var b strings.Builder cur := -1 flush := func() { if cur < 0 { head = b.String() } else { units[cur].Log = b.String() } b.Reset() } for _, line := range strings.SplitAfter(output, "\n") { if name, ok := strings.CutPrefix(strings.TrimSuffix(line, "\n"), ciUnitPrefix); ok { flush() units = append(units, CIUnit{Name: name}) cur = len(units) - 1 continue } b.WriteString(line) } flush() return head, units } // clipCILog is log within n bytes: whole when it fits; else its failure // lines first, within half of n, then a cut, then its tail. func clipCILog(log string, n int) string { if !strings.HasSuffix(log, "\n") { log += "\n" } if len(log) <= n { return log } if n <= 2*len(ciCut) { return "" } lines := strings.SplitAfter(log, "\n") keep := make([]bool, len(lines)) for i, l := range lines { if !ciFailure(l) { continue } keep[i] = true for j := i + 1; j < len(lines) && j <= i+3 && (strings.HasPrefix(lines[j], " ") || strings.HasPrefix(lines[j], "\t")); j++ { keep[j] = true } } pick := func(before int) string { var b strings.Builder offset := 0 for i, l := range lines { if offset+len(l) > before { break } offset += len(l) if keep[i] && b.Len()+len(l) <= n/2 { b.WriteString(l) } } return b.String() } fails := pick(len(log)) tail := tailLines(log, n-len(fails)-2*len(ciCut)) // A failure line the tail keeps is not given twice. fails = pick(len(log) - len(tail)) out := fails if fails != "" || len(tail) < len(log) { out += ciCut } return out + tail } // ciFailure: a line that says what failed (Go's test output and the // usual error words). func ciFailure(line string) bool { t := strings.TrimSpace(line) return strings.HasPrefix(t, "--- FAIL") || strings.HasPrefix(t, "FAIL") || strings.HasPrefix(t, "panic:") || ciError.MatchString(t) || ciDiagnostic.MatchString(t) } // ciError is an error word in any case; ciDiagnostic a compiler's or // checker's "file.go:12:3: message" (and a test's "x_test.go:12: ..."). var ( ciError = regexp.MustCompile(`(?i)\berror\b`) ciDiagnostic = regexp.MustCompile(`^[^\s:]+\.[A-Za-z0-9]+:\d+(:\d+)?: `) ) // tailLines is the end of s within limit bytes, starting on a line. func tailLines(s string, limit int) string { if limit <= 0 { return "" } if len(s) <= limit { return s } s = s[len(s)-limit:] if i := strings.IndexByte(s, '\n'); i >= 0 { s = s[i+1:] } return strings.ToValidUTF8(s, "") }