eBPF-based security platform
ci: open an issue when a workflow fails on main
Martynas Pumputisbc1dd9492746
diff --git a/.github/scripts/main-status.sh b/.github/scripts/main-status.shnew file mode 100644index 00000000..a6d57139--- /dev/null+++ b/.github/scripts/main-status.sh@@ -0,0 +1,96 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# .github/scripts/main-status.sh -- keep an issue open, labelled ci-main,+# while a workflow fails on main: open it, or add the run to it, when the+# workflow's newest finished push run on main failed, and close it when+# that run passed.+#+# .github/scripts/main-status.sh+#+# The workflow is WORKFLOW_ID and WORKFLOW_NAME, the repository+# GITHUB_REPOSITORY, and gh reads the token from GH_TOKEN.++set -eu++repo=repos/$GITHUB_REPOSITORY+label=ci-main+TITLE="main: $WORKFLOW_NAME fails"+export TITLE++# Runs finish out of order: judge main by the newest run that finished,+# leaving out the cancelled and skipped ones, which say nothing about it.+newest_run() {+ gh api "$repo/actions/workflows/$WORKFLOW_ID/runs?branch=main&event=push&status=completed&per_page=20" \+ --jq '[.workflow_runs[] | select(.conclusion != "cancelled" and .conclusion != "skipped")][0]+ | select(. != null) | [.id, .conclusion, .head_sha, .html_url] | @tsv'+}++open_issue() {+ gh api "$repo/issues?state=open&labels=$label&per_page=100" \+ --jq '[.[] | select(.title == env.TITLE and .pull_request == null)][0].number // empty'+}++failed_jobs() {+ gh api "$repo/actions/runs/$1/jobs?filter=latest&per_page=100" \+ --jq '[.jobs[] | select(.conclusion == "failure" or .conclusion == "timed_out") | .name] | join(", ")'+}++# Two runs of this script can judge main by the same run.+reported() {+ gh api "$repo/issues/$1" --jq .body | grep -qF "$2" ||+ gh api --paginate "$repo/issues/$1/comments" --jq '.[].body' | grep -qF "$2"+}++# Another workflow's run can create the label first.+ensure_label() {+ gh api "$repo/labels/$label" --silent 2>/dev/null ||+ gh api -X POST "$repo/labels" -f name="$label" -f color=d73a4a \+ -f description='A workflow fails on main' --silent ||+ gh api "$repo/labels/$label" --silent+}++report_failure() {+ jobs=$(failed_jobs "$id")+ if [ -n "$jobs" ]; then+ jobs="++Failed jobs: $jobs"+ fi+ if [ -z "$issue" ]; then+ ensure_label+ gh api -X POST "$repo/issues" -f title="$TITLE" -f "labels[]=$label" \+ -f body="\`$WORKFLOW_NAME\` fails on main at $sha: $url$jobs++A later failure on main adds a comment here, and the next pass closes this issue." \+ --silent+ echo "::notice::opened an issue: $TITLE"+ elif ! reported "$issue" "$url"; then+ gh api -X POST "$repo/issues/$issue/comments" \+ -f body="Still failing at $sha: $url$jobs" --silent+ echo "::notice::added the run to issue #$issue"+ fi+}++report_pass() {+ [ -n "$issue" ] || return 0+ gh api -X POST "$repo/issues/$issue/comments" \+ -f body="Passes again at $sha: $url" --silent+ gh api -X PATCH "$repo/issues/$issue" -f state=closed \+ -f state_reason=completed --silent+ echo "::notice::closed issue #$issue"+}++run=$(newest_run)+[ -n "$run" ] || exit 0+tab=$(printf '\t')+IFS=$tab read -r id conclusion sha url <<EOF+$run+EOF+issue=$(open_issue)++case "$conclusion" in+failure | timed_out | startup_failure) report_failure ;;+success) report_pass ;;+esacdiff --git a/.github/workflows/main-status.yml b/.github/workflows/main-status.ymlnew file mode 100644index 00000000..16ccea3e--- /dev/null+++ b/.github/workflows/main-status.yml@@ -0,0 +1,61 @@+name: Main status++# The merge queue, not a person, starts the runs on main, so GitHub tells+# no one when one fails. Keep an issue open, labelled ci-main, for each+# workflow failing on main until it passes there again. The list names+# every workflow that runs on a push to main.+on:+ workflow_run:+ workflows:+ - Build+ - Build every commit+ - Build packages+ - CI suite+ - CodeQL+ - Coverage+ - Coverity+ - DCO+ - Documentation+ - Lint+ - Proofs+ - Sanitizers+ - Scorecard+ types: [completed]+ branches: [main]++permissions: {}++jobs:+ report:+ name: ${{ github.event.workflow_run.name }}+ if: >-+ github.event.workflow_run.event == 'push' &&+ github.event.workflow_run.conclusion != 'cancelled' &&+ github.event.workflow_run.conclusion != 'skipped'+ runs-on: ubuntu-latest+ timeout-minutes: 5+ permissions:+ actions: read+ contents: read+ issues: write+ # One run at a time for each workflow, so that two cannot open its+ # issue twice. GitHub keeps only the newest waiting run, and each run+ # judges main by the workflow's newest finished run, not by its own.+ concurrency:+ group: ${{ github.workflow }}-${{ github.event.workflow_run.workflow_id }}+ cancel-in-progress: false++ steps:+ # The scripts of main, never those of the run that finished.+ - name: Check out the CI scripts+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1+ with:+ persist-credentials: false+ sparse-checkout: .github/scripts++ - name: Open, update or close the workflow's issue+ env:+ GH_TOKEN: ${{ github.token }}+ WORKFLOW_ID: ${{ github.event.workflow_run.workflow_id }}+ WORKFLOW_NAME: ${{ github.event.workflow_run.name }}+ run: .github/scripts/main-status.shdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex adb148b0..e117dee9 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -107,6 +107,13 @@ signed config image.- The prerelease tag, assets, and repository token,and rejection of missing source, mismatched source and a bundlewith the wrong checksum+ * - ``t0011-main-status``+ - ``main-status.sh``: the issue a failure on main opens, with its+ commit, run and failed jobs, the label created once, a later+ failure added to the open issue once, a pass closing it and a+ failure that finishes after it changing nothing, cancelled and+ skipped runs passed over, and the issue of another workflow left+ alone``build`` (1)~~~~~~~~~~~~~diff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex f9300988..00b5cd65 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -97,6 +97,12 @@ Land kernel input changes first. After publishing their bundles, updatethe pin in the same change as any shirudo code that requires the newkernel.+The merge queue starts the runs on ``main``, so GitHub tells no one when+one fails. The ``Main status`` workflow opens an issue labelled+``ci-main`` instead, ``main: <workflow> fails``, with the commit, the run+and the jobs that failed. A later failure on ``main`` adds its run to the+issue, and the workflow's next pass there closes it.+.. raw:: html<details>diff --git a/testing/ci-checks/t0011-main-status.sh b/testing/ci-checks/t0011-main-status.shnew file mode 100755index 00000000..98862523--- /dev/null+++ b/testing/ci-checks/t0011-main-status.sh@@ -0,0 +1,160 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+# shellcheck disable=SC2016,SC2329 # test_expect_success evaluates the body.++cd "$(dirname "$0")/.." || exit 1+# shellcheck source=testing/test-lib.sh+. ./test-lib.sh+command -v jq >/dev/null 2>&1 || test_skip_all "jq is required"++reporter=$(cd .. && pwd)/.github/scripts/main-status.sh+fixture=$(mktemp -d)+trap 'rm -rf "$fixture"' EXIT+export fixture+mkdir "$fixture/bin"++# Each GET answers from a file of the fixture, and each write is recorded.+cat >"$fixture/bin/gh" <<'SH'+#!/bin/sh+set -eu+[ "$1" = api ] || exit 1+shift+method=GET path= filter= fields=+while [ $# -gt 0 ]; do+ case $1 in+ -X) method=$2; shift 2 ;;+ --jq) filter=$2; shift 2 ;;+ -f) fields="$fields $2"; shift 2 ;;+ --paginate | --silent) shift ;;+ *) path=$1; shift ;;+ esac+done+if [ "$method" != GET ]; then+ printf '%s %s%s\n' "$method" "$path" "$fields" >>"$fixture/writes"+ exit 0+fi+case $path in+repos/o/r/actions/workflows/7/runs\?*) file=runs ;;+repos/o/r/actions/runs/*/jobs\?*) file=jobs ;;+repos/o/r/issues\?*) file=issues ;;+repos/o/r/issues/*/comments) file=comments ;;+repos/o/r/issues/*) file=issue ;;+repos/o/r/labels/ci-main) file=label ;;+*) exit 1 ;;+esac+[ -e "$fixture/$file.json" ] || exit 1+if [ -n "$filter" ]; then+ jq -r "$filter" "$fixture/$file.json"+fi+SH+chmod +x "$fixture/bin/gh"++# runs <id>:<conclusion>..., newest first+runs() {+ sep=+ printf '{"workflow_runs":['+ for r; do+ id=${r%%:*}+ printf '%s{"id":%s,"conclusion":"%s","head_sha":"sha%s","html_url":"https://github.com/o/r/actions/runs/%s"}' \+ "$sep" "$id" "${r#*:}" "$id" "$id"+ sep=,+ done+ printf ']}\n'+}++# state <runs> <open issues> <issue body> <comments>+state() {+ rm -f "$fixture"/*.json "$fixture/writes"+ printf '%s\n' "$1" >"$fixture/runs.json"+ printf '%s\n' "$2" >"$fixture/issues.json"+ printf '{"body":"%s"}\n' "$3" >"$fixture/issue.json"+ printf '%s\n' "$4" >"$fixture/comments.json"+ printf '{"jobs":[{"name":"CI functional tests (shard 1)","conclusion":"failure"},{"name":"CI unit tests","conclusion":"success"}]}\n' \+ >"$fixture/jobs.json"+ printf '{"name":"ci-main"}\n' >"$fixture/label.json"+}++report() {+ env PATH="$fixture/bin:$PATH" GITHUB_REPOSITORY=o/r GH_TOKEN=token \+ WORKFLOW_ID=7 WORKFLOW_NAME="CI suite" sh "$reporter" >/dev/null+ touch "$fixture/writes"+}++# The issue a failure of the workflow left open.+ci_suite_issue() {+ printf '[{"number":5,"title":"main: CI suite fails"}]'+}++url=https://github.com/o/r/actions/runs+export url++test_expect_success 'a failure opens an issue with the commit, the run and the failed jobs' '+ state "$(runs 12:failure)" "[]" "" "[]" &&+ rm "$fixture/label.json" &&+ report &&+ grep -Fx "POST repos/o/r/labels name=ci-main color=d73a4a description=A workflow fails on main" "$fixture/writes" &&+ grep -F "POST repos/o/r/issues title=main: CI suite fails labels[]=ci-main body=" "$fixture/writes" &&+ grep -F "fails on main at sha12: $url/12" "$fixture/writes" &&+ grep -F "Failed jobs: CI functional tests (shard 1)" "$fixture/writes" &&+ ! grep -F "CI unit tests" "$fixture/writes"+'++test_expect_success 'a label that exists is not created again' '+ state "$(runs 12:failure)" "[]" "" "[]" &&+ report &&+ ! grep -F "repos/o/r/labels" "$fixture/writes" &&+ grep -F "POST repos/o/r/issues title=" "$fixture/writes"+'++test_expect_success 'a later failure adds its run to the open issue' '+ state "$(runs 12:failure 11:failure)" "$(ci_suite_issue)" "fails on main at sha11: $url/11" "[]" &&+ report &&+ ! grep -F "POST repos/o/r/issues title=" "$fixture/writes" &&+ grep -F "POST repos/o/r/issues/5/comments body=Still failing at sha12: $url/12" "$fixture/writes" &&+ grep -F "Failed jobs: CI functional tests (shard 1)" "$fixture/writes"+'++test_expect_success 'a run the issue already names is not added twice' '+ state "$(runs 12:failure 11:failure)" "$(ci_suite_issue)" "fails on main at sha11: $url/11" \+ "[{\"body\":\"Still failing at sha12: $url/12\"}]" &&+ report &&+ test ! -s "$fixture/writes"+'++test_expect_success 'a pass closes the open issue' '+ state "$(runs 13:success 12:failure)" "$(ci_suite_issue)" "fails on main at sha12: $url/12" "[]" &&+ report &&+ grep -Fx "POST repos/o/r/issues/5/comments body=Passes again at sha13: $url/13" "$fixture/writes" &&+ grep -Fx "PATCH repos/o/r/issues/5 state=closed state_reason=completed" "$fixture/writes"+'++test_expect_success 'a pass with no issue open, or a failure finishing after it, writes nothing' '+ state "$(runs 13:success 12:failure)" "[]" "" "[]" &&+ report &&+ test ! -s "$fixture/writes"+'++test_expect_success 'a cancelled or skipped run leaves main judged by the run before it' '+ state "$(runs 15:cancelled 14:skipped 13:failure)" "[]" "" "[]" &&+ report &&+ grep -F "fails on main at sha13: $url/13" "$fixture/writes"+'++test_expect_success 'the issue of another workflow is left alone' '+ state "$(runs 13:success)" "[{\"number\":6,\"title\":\"main: Lint fails\"}]" "" "[]" &&+ report &&+ test ! -s "$fixture/writes" &&+ state "$(runs 14:failure)" "[{\"number\":6,\"title\":\"main: Lint fails\"}]" "" "[]" &&+ report &&+ grep -F "POST repos/o/r/issues title=main: CI suite fails" "$fixture/writes" &&+ ! grep -F "issues/6" "$fixture/writes"+'++test_expect_success 'no finished run writes nothing' '+ state "$(runs 15:cancelled)" "$(ci_suite_issue)" "" "[]" &&+ report &&+ test ! -s "$fixture/writes"+'++test_done