diff --git a/SECURITY.rst b/SECURITY.rst index 88a716ae..57f9a89c 100644 --- a/SECURITY.rst +++ b/SECURITY.rst @@ -55,7 +55,7 @@ Installation token limited to the test repository and to the permissions the bot uses: - Actions: read and write (reading runs, reruns, dispatching); - - Checks: read (warning annotations); + - Checks: read (warning and error annotations); - Contents: read and write (test branches and recovery refs); - Workflows: read and write (patches may change workflow files); - Metadata: read (always granted). diff --git a/SETUP.rst b/SETUP.rst index 1e591ee6..84fd885a 100644 --- a/SETUP.rst +++ b/SETUP.rst @@ -136,7 +136,7 @@ Fill in the form: - **Repository permissions**: - Actions: read and write (reading runs, reruns, dispatching); - - Checks: read-only (warning annotations, see + - Checks: read-only (warning and error annotations, see ``workflows.warning_annotation`` in ``examples/reference.yaml``); - Contents: read and write (pushing test branches); - Workflows: read and write (patches may change workflow files); diff --git a/examples/reference.yaml b/examples/reference.yaml index 0fdaf56f..62e35e09 100644 --- a/examples/reference.yaml +++ b/examples/reference.yaml @@ -370,9 +370,14 @@ workflows: # annotations with this title, e.g. from # echo "::warning title=leadtest::2 lines are too long" # Their messages are included in the report. This lets a workflow - # pass with warnings, like checkpatch in fork-workflows/. Reading the + # pass with warnings, like checkpatch in fork-workflows/. The report of + # a failed job carries the messages of its error annotations with this + # title, e.g. from + # echo "::error title=leadtest::ERROR: Line is too long" + # instead of the last report.log_lines lines of its log. Either way the + # messages are cut to 100 lines and 10000 characters. Reading the # annotations needs the Checks permission of the GitHub App (see - # SETUP.rst). Empty disables it. + # SETUP.rst). Empty disables both. warning_annotation: leadtest rechecks: diff --git a/fork-workflows/leadtest-checkpatch-dpdk.yml b/fork-workflows/leadtest-checkpatch-dpdk.yml index db81c57d..a6fa7c14 100644 --- a/fork-workflows/leadtest-checkpatch-dpdk.yml +++ b/fork-workflows/leadtest-checkpatch-dpdk.yml @@ -64,8 +64,23 @@ jobs: done chmod +x linux/checkpatch.pl + # On failure the output goes in an error annotation titled like + # workflows.warning_annotation, which LeadTestKit reports instead of + # the end of the log. - name: Run checkpatches.sh run: | cd dpdk + status=0 DPDK_CHECKPATCH_PATH="$GITHUB_WORKSPACE/linux/checkpatch.pl" \ - devtools/checkpatches.sh "$GITHUB_WORKSPACE/patch.mbox" + devtools/checkpatches.sh "$GITHUB_WORKSPACE/patch.mbox" \ + > ../checkpatch.log 2>&1 || status=$? + cat ../checkpatch.log + [ "$status" -eq 0 ] && exit 0 + python3 - ../checkpatch.log <<'EOF' + import sys + text = open(sys.argv[1]).read().strip() + for c, e in (("%", "%25"), ("\r", "%0D"), ("\n", "%0A")): + text = text.replace(c, e) + print("::error title=leadtest::" + text) + EOF + exit "$status" diff --git a/fork-workflows/leadtest-checkpatch-ovs.yml b/fork-workflows/leadtest-checkpatch-ovs.yml index fed34915..d854760a 100644 --- a/fork-workflows/leadtest-checkpatch-ovs.yml +++ b/fork-workflows/leadtest-checkpatch-ovs.yml @@ -59,23 +59,27 @@ jobs: cp upstream/utilities/checkpatch.py checkpatch.py fi - # Errors fail the job. With only warnings the job passes with a - # warning annotation titled like workflows.warning_annotation, and - # LeadTestKit reports a warning with its text. + # The output of checkpatch goes in an annotation titled like + # workflows.warning_annotation, which LeadTestKit reports instead of + # the end of the log. Errors fail the job with an error annotation. + # With only warnings the job passes with a warning annotation, and + # LeadTestKit reports a warning. - name: Run checkpatch run: | status=0 python3 checkpatch.py patch.mbox > checkpatch.log 2>&1 || status=$? cat checkpatch.log - if [ "$status" -ne 0 ] && ! grep -q '^ERROR' checkpatch.log && - grep -q '^WARNING' checkpatch.log; then - python3 - checkpatch.log <<'EOF' + [ "$status" -eq 0 ] && exit 0 + level=error + if ! grep -q '^ERROR' checkpatch.log && grep -q '^WARNING' checkpatch.log; then + level=warning + fi + python3 - "$level" checkpatch.log <<'EOF' import sys - text = open(sys.argv[1]).read().strip() + text = open(sys.argv[2]).read().strip() for c, e in (("%", "%25"), ("\r", "%0D"), ("\n", "%0A")): text = text.replace(c, e) - print("::warning title=leadtest::" + text) + print("::%s title=leadtest::%s" % (sys.argv[1], text)) EOF - exit 0 - fi + [ "$level" = warning ] && exit 0 exit "$status" diff --git a/internal/config/config.go b/internal/config/config.go index ec8b0c36..32d1e5d6 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -268,7 +268,8 @@ type Workflows struct { // RerunJobs is "failed" (GitHub's "Re-run failed jobs") or "all". RerunJobs string `yaml:"rerun_jobs"` // WarningAnnotation is the title of the warning annotations that - // make a successful run a warning. Empty disables the check. + // make a successful run a warning, and of the error annotations + // reported instead of the log of a failed job. Empty disables both. WarningAnnotation string `yaml:"warning_annotation"` } diff --git a/internal/pipeline/annotations.go b/internal/pipeline/annotations.go index dca927df..4466a629 100644 --- a/internal/pipeline/annotations.go +++ b/internal/pipeline/annotations.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "strings" + "unicode/utf8" "leadtestkit/internal/gh" "leadtestkit/internal/httpx" @@ -66,7 +67,7 @@ func (p *Pipeline) successResult(ctx context.Context, pt *state.Patch, r *state. if len(msgs) == 0 { return state.ResultSuccess, true } - warnings := headLines(strings.Join(msgs, "\n"), p.Cfg.Report.LogLines) + warnings := capDetails(strings.Join(msgs, "\n")) r.Details = "Warnings:\n" + warnings p.noteRun(pt, wr.HTMLURL, wr.RunAttempt, "workflow-warnings", "%s run %d, attempt %d passed with %s:\n%s", r.Workflow, wr.ID, wr.RunAttempt, @@ -100,6 +101,64 @@ func (p *Pipeline) warningAnnotations(ctx context.Context, suiteID int64, return msgs, nil } +// errorAnnotations returns the messages of the error annotations of a +// failed job titled workflows.warning_annotation; a workflow uses them to +// report only what matters, like the output of checkpatch, instead of the +// end of its log. It returns "" when the job has none or they cannot be +// read for lack of permission. +func (p *Pipeline) errorAnnotations(ctx context.Context, runID int64, + j gh.Job) (string, error) { + title := p.Cfg.Workflows.WarningAnnotation + if title == "" { + return "", nil + } + // The check run of a job has the ID of the job. + notes, err := p.gh().Annotations(ctx, j.ID) + if code := httpx.StatusCode(err); code == http.StatusForbidden || + code == http.StatusNotFound { + p.warnf("Run %d: cannot read the annotations of job %d (%s); reporting its "+ + "log (the GitHub App needs Checks: read-only): %v", runID, j.ID, j.Name, err) + return "", nil + } + if err != nil { + return "", err + } + var msgs []string + for _, a := range notes { + if a.Level == "failure" && a.Title == title { + msgs = append(msgs, strings.TrimRight(a.Message, "\n")) + } + } + return strings.Join(msgs, "\n"), nil +} + +const ( + maxDetailLines = 100 + maxDetailChars = 10000 +) + +// capDetails cuts text from a workflow to maxDetailLines lines and +// maxDetailChars characters, at a line break when it can. +func capDetails(text string) string { + text = headLines(text, maxDetailLines) + if len(text) <= maxDetailChars { + return text + } + cut := text[:maxDetailChars] + if i := strings.LastIndexByte(cut, '\n'); i > 0 { + cut = cut[:i] + } else { + for i := len(cut); i > 0; i-- { + if utf8.RuneStart(text[i]) { + cut = text[:i] + break + } + } + } + return cut + fmt.Sprintf("\n... longer than %d characters, see the workflow run.", + maxDetailChars) +} + func headLines(text string, n int) string { lines := strings.Split(text, "\n") if len(lines) <= n { diff --git a/internal/pipeline/annotations_test.go b/internal/pipeline/annotations_test.go index 48eb8670..2d4205b2 100644 --- a/internal/pipeline/annotations_test.go +++ b/internal/pipeline/annotations_test.go @@ -4,6 +4,7 @@ package pipeline import ( + "fmt" "strings" "testing" "time" @@ -86,6 +87,155 @@ func TestWarningAnnotationsDisabled(t *testing.T) { } } +func TestWarningAnnotationIsCapped(t *testing.T) { + var lines []string + for i := range 150 { + lines = append(lines, fmt.Sprintf("WARNING: line %d", i)) + } + e, _ := annotatedRun(t, gh.Annotation{Level: "warning", Title: "leadtest", + Message: strings.Join(lines, "\n")}) + e.step() + r := checkpatchRun(e) + if !strings.HasSuffix(r.Details, "WARNING: line 99\n... 50 more lines, see the workflow run.") { + t.Fatalf("details:\n%s", r.Details) + } + + long := strings.Repeat("WARNING: "+strings.Repeat("x", 191)+"\n", 60) + e, _ = annotatedRun(t, gh.Annotation{Level: "warning", Title: "leadtest", + Message: long}) + e.step() + r = checkpatchRun(e) + if !strings.HasSuffix(r.Details, "\n... longer than 10000 characters, see the workflow run.") || + len(r.Details) > len("Warnings:\n")+10000+100 { + t.Fatalf("details: %d bytes, ends %q", len(r.Details), + r.Details[max(0, len(r.Details)-80):]) + } +} + +// failedCheckpatch finishes a failed Checkpatch run of patch 6001 whose +// job has the given log and annotations. +func failedCheckpatch(t *testing.T, log string, notes ...gh.Annotation) *env { + e := newEnv(t, "") + e.pw.addSeries(60, 1, "lorem", e.now) + e.step() + id := e.gh.startRuns("series_60", "sha-6001", "Checkpatch")[0] + e.gh.jobs[id] = []gh.Job{{ID: 61, Name: "checkpatch", Conclusion: "failure", + Steps: []gh.Step{{Name: "Run checkpatch", Conclusion: "failure"}}}} + e.gh.logs[61] = log + e.gh.annotations[61] = notes + e.gh.finishRun(id, "failure") + return e +} + +const checkpatchLog = "2026-03-10T12:00:00.1Z \x1b[36;1mexit \"$status\"\x1b[0m\n" + + "2026-03-10T12:00:00.2Z ERROR: Author should not be mailing list.\n" + + "2026-03-10T12:00:00.3Z ##[error]Process completed with exit code 1.\n" + +func TestErrorAnnotationReplacesLog(t *testing.T) { + e := failedCheckpatch(t, checkpatchLog, + gh.Annotation{Level: "failure", Title: "leadtest", + Message: "== Checking \"patch.mbox\" ==\nERROR: Author should not be mailing " + + "list.\nLines checked: 121, Warnings: 0, Errors: 1\n"}, + gh.Annotation{Level: "failure", Message: "Process completed with exit code 1."}, + gh.Annotation{Level: "warning", Title: "leadtest", Message: "WARNING: ipsum"}) + e.step() + report := string(e.mail.sent[len(e.mail.sent)-2].Data) + reply := string(e.mail.sent[len(e.mail.sent)-1].Data) + for name, mail := range map[string]string{"report": report, "reply": reply} { + if !strings.Contains(mail, "|fail| pw6001") && name == "report" || + !strings.Contains(mail, "checkpatch: failed at step \"Run checkpatch\"") || + !strings.Contains(mail, "Errors of \"checkpatch\":\r\n== Checking \"patch.mbox\""+ + " ==\r\nERROR: Author should not be mailing list.\r\nLines checked: 121, "+ + "Warnings: 0, Errors: 1\r\n") || + strings.Contains(mail, "log lines") || strings.Contains(mail, "exit code") || + strings.Contains(mail, "\x1b") || strings.Contains(mail, "WARNING: ipsum") { + t.Errorf("%s:\n%s", name, mail) + } + } +} + +func TestErrorAnnotationsOfAJobAreJoined(t *testing.T) { + e := failedCheckpatch(t, checkpatchLog, + gh.Annotation{Level: "failure", Title: "leadtest", Message: "ERROR: lorem\n"}, + gh.Annotation{Level: "failure", Title: "leadtest", Message: "ERROR: ipsum"}) + e.step() + report := string(e.mail.sent[len(e.mail.sent)-2].Data) + if !strings.Contains(report, "Errors of \"checkpatch\":\r\nERROR: lorem\r\n"+ + "ERROR: ipsum\r\n") { + t.Errorf("report:\n%s", report) + } +} + +func TestErrorAnnotationIsCapped(t *testing.T) { + var lines []string + for i := range 150 { + lines = append(lines, fmt.Sprintf("ERROR: line %d", i)) + } + e := failedCheckpatch(t, checkpatchLog, gh.Annotation{Level: "failure", + Title: "leadtest", Message: strings.Join(lines, "\n")}) + e.step() + report := string(e.mail.sent[len(e.mail.sent)-2].Data) + if !strings.Contains(report, "ERROR: line 99\r\n... 50 more lines, see the workflow run.") || + strings.Contains(report, "ERROR: line 100") { + t.Errorf("report:\n%s", report) + } + + long := strings.Repeat("ERROR: "+strings.Repeat("x", 193)+"\n", 60) + e = failedCheckpatch(t, checkpatchLog, gh.Annotation{Level: "failure", + Title: "leadtest", Message: long}) + e.step() + // Each line has 201 characters, so 49 of them fit in 10000. + report = string(e.mail.sent[len(e.mail.sent)-2].Data) + if !strings.Contains(report, "x\r\n... longer than 10000 characters, see the workflow "+ + "run.") || strings.Count(report, "ERROR: x") != 49 { + t.Errorf("report (%d errors):\n%s", strings.Count(report, "ERROR: x"), report) + } +} + +func TestErrorAnnotationFallsBackToLog(t *testing.T) { + note := gh.Annotation{Level: "failure", Title: "leadtest", Message: "ERROR: lorem"} + cases := map[string]func(e *env){ + "no annotation": func(e *env) { e.gh.annotations[61] = nil }, + "no permission": func(e *env) { e.gh.annotationErr = &httpx.StatusError{Code: 403} }, + "not found": func(e *env) { e.gh.annotationErr = &httpx.StatusError{Code: 404} }, + "empty message": func(e *env) { + e.gh.annotations[61] = []gh.Annotation{{Level: "failure", Title: "leadtest"}} + }, + "disabled": func(e *env) { e.cfg.Workflows.WarningAnnotation = "" }, + } + for name, setup := range cases { + e := failedCheckpatch(t, checkpatchLog, note) + setup(e) + e.step() + report := string(e.mail.sent[len(e.mail.sent)-2].Data) + if !strings.Contains(report, "Last 25 log lines of \"checkpatch\":") || + strings.Contains(report, "Errors of") { + t.Errorf("%s: report:\n%s", name, report) + } + if name == "disabled" && e.gh.annotationCalls != 0 { + t.Errorf("disabled: %d annotation calls", e.gh.annotationCalls) + } + } +} + +func TestErrorAnnotationRetry(t *testing.T) { + e := failedCheckpatch(t, checkpatchLog, + gh.Annotation{Level: "failure", Title: "leadtest", Message: "ERROR: lorem"}) + e.gh.fail["annotations"] = 1 + e.step() + r := e.series(60).Patches[0].Run(state.RunPush, "Checkpatch") + if r.Final.Email != "" || r.Failure == nil || r.Failure.Op != "logs" { + t.Fatalf("a transient error must delay the report: %+v", r) + } + e.advance(10 * time.Minute) + e.step() + report := string(e.mail.sent[len(e.mail.sent)-2].Data) + if r.Final.Email == "" || !strings.Contains(report, "Errors of \"checkpatch\":\r\n"+ + "ERROR: lorem\r\n") { + t.Errorf("run = %+v, report:\n%s", r, report) + } +} + func TestAnnotationErrors(t *testing.T) { notes := gh.Annotation{Level: "warning", Title: "leadtest", Message: "WARNING: lorem"} e, _ := annotatedRun(t, notes) diff --git a/internal/pipeline/reports.go b/internal/pipeline/reports.go index 13041782..a3227aeb 100644 --- a/internal/pipeline/reports.go +++ b/internal/pipeline/reports.go @@ -568,6 +568,14 @@ func (p *Pipeline) failureLogs(ctx context.Context, runID int64) (string, error) len(failed)-maxJobs) break } + errors, err := p.errorAnnotations(ctx, runID, j) + if err != nil { + return "", err + } + if errors != "" { + fmt.Fprintf(&b, "\nErrors of %q:\n%s\n", j.Name, capDetails(errors)) + continue + } p.infof("Fetching log tail for failed job %d (%s) in run %d.", j.ID, j.Name, runID) log, err := p.gh().JobLog(ctx, j.ID) if err != nil {