From cf907451b5c8c208f61d026b161bdd79fb0efbbe Mon Sep 17 00:00:00 2001 From: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Tue, 3 Nov 2026 11:51:51 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20chore(actions):=20surface=20stat?= =?UTF-8?q?us-card=20helper=20failures=20and=20add=20its=20regression=20te?= =?UTF-8?q?st?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: scanner <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .github/scripts/hive-status-card.sh | 57 +++++- .github/scripts/integration-governance.cjs | 2 +- .github/scripts/test-hive-status-card.sh | 164 ++++++++++++++++++ changelog.d/fixed-16363-status-card-errors.md | 1 + src/docs/notifications.md | 9 + src/scripts/comment-merged-refs.sh | 22 ++- src/scripts/test-comment-merged-refs.sh | 31 ++++ 7 files changed, 272 insertions(+), 14 deletions(-) create mode 100755 .github/scripts/test-hive-status-card.sh create mode 100644 changelog.d/fixed-16363-status-card-errors.md diff --git a/.github/scripts/hive-status-card.sh b/.github/scripts/hive-status-card.sh index d866cb4c6a..2f1ce5b31d 100755 --- a/.github/scripts/hive-status-card.sh +++ b/.github/scripts/hive-status-card.sh @@ -7,6 +7,15 @@ # hive-status-card.sh has REPO NUMBER SECTION exit 0 if the card has a matching section # hive-status-card.sh upsert REPO NUMBER MARKER BODY_FILE edit the comment carrying MARKER, else create it # +# Exit status of `has`: 0 = the card carries SECTION, 1 = it does not (or no +# card exists), 3 = the comments could not be read (the API call failed, or +# the response was not a JSON list of comment objects). A caller must treat 3 +# as a failure, never as absence: absence makes a workflow post, and posting +# on a read failure is exactly the duplicate this helper exists to prevent. +# `upsert` exits 3 for the same read failure without POSTing or PATCHing; +# a failed PATCH/POST exits non-zero with gh's status. On exit 3 a line +# starting `hive-status-card: error:` is written to stderr. +# # SECTION matches `` by exact KEY (e.g. # `issue-close`, `issue-partial:163`). Only when SECTION ends in `:` is it a # prefix (e.g. `issue-partial:` matches any `issue-partial:`), so @@ -28,24 +37,53 @@ set -euo pipefail GH_BIN="${GH_BIN:-gh}" HIVE_COMMENT_AUTHOR="${HIVE_COMMENT_AUTHOR:-github-actions[bot]}" +READ_ERROR=3 + +read_error() { + echo "hive-status-card: error: $*" >&2 + exit "$READ_ERROR" +} +# Prints the issue's comments as one validated flat JSON array, or exits +# READ_ERROR. Validation happens here so the matchers below never see a shape +# that would make Python exit 1 (which would read as "absent"). comments() { + local raw flat="1" if [ -n "${HIVE_COMMENTS_FILE:-}" ]; then - cat "$HIVE_COMMENTS_FILE" - return + raw=$(cat "$HIVE_COMMENTS_FILE") || read_error "cannot read HIVE_COMMENTS_FILE ${HIVE_COMMENTS_FILE}" + else + raw=$("$GH_BIN" api --paginate --slurp "repos/$1/issues/$2/comments?per_page=100") || + read_error "GET repos/$1/issues/$2/comments failed" + flat="0" fi - "$GH_BIN" api --paginate --slurp "repos/$1/issues/$2/comments?per_page=100" | - python3 -c 'import json,sys -out=[] -for page in json.load(sys.stdin): out.extend(page) -json.dump(out, sys.stdout)' + printf '%s' "$raw" | python3 -c 'import json,sys +flat = sys.argv[1] == "1" +try: + data = json.load(sys.stdin) +except ValueError as e: + sys.exit("response is not JSON: %s" % e) +if not isinstance(data, list): + sys.exit("expected a JSON list, got %s" % type(data).__name__) +pages = [data] if flat else data +out = [] +for page in pages: + if not isinstance(page, list): + sys.exit("expected a list of comment pages, got a %s page" % type(page).__name__) + out.extend(page) +for c in out: + if not isinstance(c, dict): + sys.exit("comment is a %s, not an object" % type(c).__name__) + if not isinstance(c.get("user") or {}, dict) or not isinstance(c.get("body") or "", str): + sys.exit("comment %r has a malformed user or body" % c.get("id")) +json.dump(out, sys.stdout)' "$flat" || read_error "comments for $1#$2 could not be parsed" } cmd="${1:-}"; repo="${2:-}"; number="${3:-}" case "$cmd" in has) section="${4:?section key required}" - comments "$repo" "$number" | python3 -c 'import json,re,sys + list=$(comments "$repo" "$number") + printf '%s' "$list" | python3 -c 'import json,re,sys want=sys.argv[1] section_re=re.compile(r"") def matches(key): @@ -64,7 +102,8 @@ sys.exit(1)' "$section" ;; upsert) marker="${4:?marker required}"; body_file="${5:?body file required}" - id=$(comments "$repo" "$number" | python3 -c 'import json,sys + list=$(comments "$repo" "$number") + id=$(printf '%s' "$list" | python3 -c 'import json,sys marker, author = sys.argv[1], sys.argv[2] for c in json.load(sys.stdin): if (c.get("user") or {}).get("login") != author: diff --git a/.github/scripts/integration-governance.cjs b/.github/scripts/integration-governance.cjs index 847040a5ca..043b217831 100644 --- a/.github/scripts/integration-governance.cjs +++ b/.github/scripts/integration-governance.cjs @@ -9,7 +9,7 @@ const MAX_COMMITS = 1000; const MAX_AUTHOR_BATCH = 50; // This literal is inside the enrolled .github closure. A caller-supplied // preparedBy.sourceSHA must never choose a new executable verifier tree. -const TRUSTED_SCRIPTS_TREE = '9ba1ee4cedf0c05df6d68b5ef905fed8a2924af5'; +const TRUSTED_SCRIPTS_TREE = '25ff55e8bd1ab6da9d717727df35947ab008d146'; const SOURCE = 'https://github.com/kubernetes-sigs/kubebuilder-release-tools/tree/012269a88fa4c034a0acf1ba84c26b195c0dbab4'; const sha = value => crypto.createHash('sha256').update(value).digest('hex'); const git = (args, cwd = process.cwd()) => diff --git a/.github/scripts/test-hive-status-card.sh b/.github/scripts/test-hive-status-card.sh new file mode 100755 index 0000000000..b4425c0dd7 --- /dev/null +++ b/.github/scripts/test-hive-status-card.sh @@ -0,0 +1,164 @@ +#!/usr/bin/env bash +# test-hive-status-card.sh — exercises .github/scripts/hive-status-card.sh with +# a gh stub so no network calls are made (#16363). +# +# Pins: exact section matching, ordinary behaviour when no card exists, that +# an API or parse failure is reported as an error (exit 3) and never as +# absence, and that `upsert` edits only the posting identity's marker comment. +set -u -o pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +HELPER="${HERE}/hive-status-card.sh" +TMP_ROOT="${HERE}/../.test-tmp" +mkdir -p "$TMP_ROOT" +TMP="$(mktemp -d "$TMP_ROOT/hive-status-card.XXXXXX")" +trap 'rm -rf "$TMP"; rmdir "$TMP_ROOT" 2>/dev/null || true' EXIT + +fail=0 +pass() { echo " ok: $*"; } +bad() { echo " FAIL: $*"; fail=1; } + +CALL_LOG="$TMP/gh-calls.log" +: > "$CALL_LOG" + +GH_STUB="$TMP/gh" +cat > "$GH_STUB" <<'STUB' +#!/usr/bin/env bash +set -euo pipefail +method=GET +path="" +prev="" +for arg in "$@"; do + if [ "$prev" = "-X" ]; then method="$arg"; prev=""; continue; fi + if [ "$prev" = "-f" ] || [ "$prev" = "-F" ]; then prev=""; continue; fi + case "$arg" in + -X|-f|-F) prev="$arg" ;; + repos/*) path="$arg" ;; + esac +done +printf '%s %s\n' "$method" "$path" >> "$CALL_LOG" +if [ "$method" != "GET" ]; then + printf '{"id":1}\n' + exit 0 +fi +card='{"id":%s,"user":{"login":"hivecommons-hive[bot]","type":"Bot"},"body":"\\n**Hive status**\\n\\n\\nnote\\n"}' +case "$path" in + repos/o/r/issues/1/comments*) + printf "[[$card]]\n" 101 issue-close ;; + repos/o/r/issues/2/comments*) + printf "[[$card]]\n" 201 issue-partial:1638 ;; + repos/o/r/issues/3/comments*) + printf '[[{"id":301,"user":{"login":"someone","type":"User"},"body":"plain comment"}],[]]\n' ;; + repos/o/r/issues/4/comments*) + printf '[[{"id":401,"user":{"login":"someone","type":"User"},"body":"quoting:\\n\\n"}]]\n' ;; + repos/o/r/issues/5/comments*) + echo "HTTP 502: Bad Gateway" >&2 + exit 1 ;; + repos/o/r/issues/6/comments*) + printf 'rate limited\n' ;; + repos/o/r/issues/7/comments*) + printf '{"message":"Not Found"}\n' ;; + repos/o/r/issues/8/comments*) + printf '[[{"id":801,"user":"not-an-object","body":"x"}]]\n' ;; + repos/o/r/issues/9/comments*) + printf '[[{"id":901,"user":{"login":"someone","type":"User"},"body":"I saw quoted"}],[{"id":902,"user":{"login":"github-actions[bot]","type":"Bot"},"body":"\\nold"}]]\n' ;; + repos/o/r/issues/10/comments*) + printf '[[{"id":1001,"user":{"login":"someone","type":"User"},"body":"I saw quoted"}]]\n' ;; + *) + echo "unexpected gh api path: $path" >&2 + exit 99 ;; +esac +STUB +chmod +x "$GH_STUB" + +# run_has ISSUE SECTION -> sets rc and err +run_has() { + : > "$CALL_LOG" + err=$(CALL_LOG="$CALL_LOG" GH_BIN="$GH_STUB" bash "$HELPER" has o/r "$1" "$2" 2>&1 >/dev/null) + rc=$? +} + +run_has 1 issue-close +if [ "$rc" -eq 0 ]; then pass "has: card carrying issue-close reports present"; else bad "has issue-close on #1: expected 0, got ${rc} (${err})"; fi + +run_has 1 issue-closed +if [ "$rc" -eq 1 ]; then pass "has: section keys match exactly, not by prefix"; else bad "has issue-closed on #1: expected 1, got ${rc}"; fi + +run_has 2 issue-partial:163 +if [ "$rc" -eq 1 ]; then pass "has: issue-partial:163 does not match issue-partial:1638"; else bad "has issue-partial:163 on #2: expected 1, got ${rc}"; fi + +run_has 2 issue-partial: +if [ "$rc" -eq 0 ]; then pass "has: a trailing colon is an explicit prefix match"; else bad "has issue-partial: on #2: expected 0, got ${rc}"; fi + +run_has 3 issue-close +if [ "$rc" -eq 1 ] && [ -z "$err" ]; then pass "has: no card is ordinary absence (exit 1, no error)"; else bad "has on #3 (no card): expected 1 and no stderr, got ${rc} (${err})"; fi + +run_has 4 issue-close +if [ "$rc" -eq 1 ]; then pass "has: a human quoting the card's markers is not the card"; else bad "has on #4 (human quote): expected 1, got ${rc}"; fi + +for case in "5:API failure" "6:non-JSON response" "7:JSON that is not a comment list" "8:malformed comment object"; do + n="${case%%:*}"; what="${case#*:}" + run_has "$n" issue-close + if [ "$rc" -eq 3 ] && printf '%s' "$err" | grep -q '^hive-status-card: error:'; then + pass "has: ${what} exits 3 with an error, not absence" + else + bad "has on #${n} (${what}): expected exit 3 and an error line, got ${rc} (${err})" + fi +done + +: > "$CALL_LOG" +printf 'not json' > "$TMP/bad-comments.json" +CALL_LOG="$CALL_LOG" GH_BIN="$GH_STUB" HIVE_COMMENTS_FILE="$TMP/bad-comments.json" bash "$HELPER" has o/r 1 issue-close >/dev/null 2>&1 +rc=$? +if [ "$rc" -eq 3 ] && [ ! -s "$CALL_LOG" ]; then pass "has: an unparseable HIVE_COMMENTS_FILE is an error and issues no GET"; else bad "bad HIVE_COMMENTS_FILE: expected 3 and no gh call, got ${rc}"; fi + +: > "$CALL_LOG" +CALL_LOG="$CALL_LOG" GH_BIN="$GH_STUB" HIVE_COMMENTS_FILE="$TMP/missing.json" bash "$HELPER" has o/r 1 issue-close >/dev/null 2>&1 +rc=$? +if [ "$rc" -eq 3 ]; then pass "has: a missing HIVE_COMMENTS_FILE is an error"; else bad "missing HIVE_COMMENTS_FILE: expected 3, got ${rc}"; fi + +BODY="$TMP/body.md" +printf '\nnew\n' > "$BODY" + +run_upsert() { + : > "$CALL_LOG" + CALL_LOG="$CALL_LOG" GH_BIN="$GH_STUB" bash "$HELPER" upsert o/r "$1" '' "$BODY" >/dev/null 2>&1 + rc=$? +} + +run_upsert 9 +if [ "$rc" -eq 0 ] && grep -q '^PATCH repos/o/r/issues/comments/902$' "$CALL_LOG" \ + && ! grep -q '901' "$CALL_LOG" && ! grep -q '^POST' "$CALL_LOG"; then + pass "upsert: edits the posting identity's own marker comment (found on a later page)" +else + bad "upsert #9: expected one PATCH of 902 (rc=${rc})"; sed 's/^/ | /' "$CALL_LOG" +fi + +run_upsert 10 +if [ "$rc" -eq 0 ] && grep -q '^POST repos/o/r/issues/10/comments$' "$CALL_LOG" && ! grep -q '^PATCH' "$CALL_LOG"; then + pass "upsert: never edits another author's comment carrying the marker; creates its own" +else + bad "upsert #10: expected a POST and no PATCH (rc=${rc})"; sed 's/^/ | /' "$CALL_LOG" +fi + +run_upsert 3 +if [ "$rc" -eq 0 ] && grep -q '^POST repos/o/r/issues/3/comments$' "$CALL_LOG"; then + pass "upsert: creates the comment when no marker comment exists" +else + bad "upsert #3: expected a POST (rc=${rc})"; sed 's/^/ | /' "$CALL_LOG" +fi + +for n in 5 6; do + run_upsert "$n" + if [ "$rc" -eq 3 ] && ! grep -q -E '^(POST|PATCH)' "$CALL_LOG"; then + pass "upsert: a read failure on #${n} exits 3 without posting a duplicate" + else + bad "upsert #${n}: expected exit 3 and no write (rc=${rc})"; sed 's/^/ | /' "$CALL_LOG" + fi +done + +if [ "$fail" -ne 0 ]; then + echo "test-hive-status-card FAILED" + exit 1 +fi +echo "test-hive-status-card OK" diff --git a/changelog.d/fixed-16363-status-card-errors.md b/changelog.d/fixed-16363-status-card-errors.md new file mode 100644 index 0000000000..c2427e9a9f --- /dev/null +++ b/changelog.d/fixed-16363-status-card-errors.md @@ -0,0 +1 @@ +- The post-merge refs sweep no longer treats an unreadable `hive:status` card as "no card": when the comments API call fails or returns something other than a JSON list of comments, `.github/scripts/hive-status-card.sh` now reports an error (exit 3) instead of absence, `upsert` writes nothing, and the sweep skips that issue with an `::error::` annotation and a non-zero exit rather than posting a possible duplicate note ([#16363](https://github.com/hivecommons/hive/issues/16363)). diff --git a/src/docs/notifications.md b/src/docs/notifications.md index 246a8bb6bd..8a5459117b 100644 --- a/src/docs/notifications.md +++ b/src/docs/notifications.md @@ -686,6 +686,15 @@ paths apply only where a card exists; with `github.notifications.attention_pilot` off there is no issue card, and the workflows behave as before. +A failure to read the card is not treated as "no card": when the comments +API call fails or its response is not a JSON list of comment objects, +`hive-status-card.sh` writes a `hive-status-card: error:` line and exits 3 +(`has` otherwise exits 0 for present, 1 for absent), and `upsert` writes +nothing. The refs sweep then skips that issue with an `::error::` annotation +and exits non-zero instead of posting a possible duplicate. +`.github/scripts/test-hive-status-card.sh` and +`src/scripts/test-comment-merged-refs.sh` pin this behaviour. + **Outside Hive's control.** Comments from external bots, such as Prow, tide and kubestellar-prow, are posted by their own services and cannot be edited, folded or skipped from this repository. Hive does not try to suppress them; diff --git a/src/scripts/comment-merged-refs.sh b/src/scripts/comment-merged-refs.sh index ddbbdb648f..c9f92f1c2a 100755 --- a/src/scripts/comment-merged-refs.sh +++ b/src/scripts/comment-merged-refs.sh @@ -122,6 +122,7 @@ json.dump(obj, sys.stdout)' posted=0 skipped=0 +card_errors=0 while IFS= read -r issue; do [ -n "$issue" ] || continue issue_json=$(api "repos/${REPO}/issues/${issue}") @@ -180,18 +181,26 @@ sys.exit(1)' # The runtime's hive:status card carries the same condition as an # `issue-partial:` section; a second notifying comment would duplicate it. # The comments fetched above are handed to the helper so it does not - # re-fetch the same pages. + # re-fetch the same pages. Exit 1 is "no such section"; anything else is a + # read/parse failure, which must not be mistaken for absence and post. card_helper="$(dirname "${BASH_SOURCE[0]}")/../../.github/scripts/hive-status-card.sh" if [ -x "$card_helper" ]; then comments_file=$(mktemp) printf '%s' "$comments_json" > "$comments_file" - if GH_BIN="$GH_BIN" HIVE_COMMENTS_FILE="$comments_file" "$card_helper" has "$REPO" "$issue" "issue-partial:${PR_NUMBER}" 2>/dev/null; then - rm -f "$comments_file" + card_rc=0 + GH_BIN="$GH_BIN" HIVE_COMMENTS_FILE="$comments_file" "$card_helper" has "$REPO" "$issue" "issue-partial:${PR_NUMBER}" || card_rc=$? + rm -f "$comments_file" + if [ "$card_rc" -eq 0 ]; then echo "Skipping #${issue}: hive:status card already carries the partial-merge note." skipped=$((skipped + 1)) continue fi - rm -f "$comments_file" + if [ "$card_rc" -ne 1 ]; then + echo "::error::Skipping #${issue}: could not read its hive:status card (hive-status-card.sh exit ${card_rc}); not posting a possible duplicate." >&2 + card_errors=$((card_errors + 1)) + skipped=$((skipped + 1)) + continue + fi fi comment_body=$(printf '%s\nPR #%s has merged (%s) and referenced this issue with a non-closing `%s` rather than a closing keyword.\n\nThis issue remains open because the merged PR did not use `Closes` / `Fixes` / `Resolves`. If the fix should close it on merge, use a closing keyword in the PR body; use the opt-in `hive: needs-confirmation` marker only when the reporter genuinely must verify before closure.' \ @@ -308,3 +317,8 @@ $closing_issues EOF_CLOSING printf 'Parent auto-close sweep complete for PR #%s: %s parent(s) closed, %s parent(s) skipped.\n' "$PR_NUMBER" "$parent_closed" "$parent_skipped" + +if [ "$card_errors" -gt 0 ]; then + echo "Refs sweep for PR #${PR_NUMBER}: ${card_errors} issue(s) skipped because their hive:status card could not be read." >&2 + exit 1 +fi diff --git a/src/scripts/test-comment-merged-refs.sh b/src/scripts/test-comment-merged-refs.sh index 12d139e738..66b32943b5 100755 --- a/src/scripts/test-comment-merged-refs.sh +++ b/src/scripts/test-comment-merged-refs.sh @@ -163,6 +163,11 @@ JSON repos/hivecommons/hive/pulls/133) cat <<'JSON' {"number":133,"merged_at":"2026-09-10T21:35:00Z","html_url":"https://github.com/hivecommons/hive/pull/133","body":"Refs #24"} +JSON + ;; + repos/hivecommons/hive/pulls/134) + cat <<'JSON' +{"number":134,"merged_at":"2026-09-10T21:40:00Z","html_url":"https://github.com/hivecommons/hive/pull/134","body":"Refs #25"} JSON ;; repos/hivecommons/hive/issues/10) @@ -192,6 +197,9 @@ JSON repos/hivecommons/hive/issues/24) printf '{"number":24,"state":"open","user":{"login":"maintainer"},"labels":[]}\n' ;; + repos/hivecommons/hive/issues/25) + printf '{"number":25,"state":"open","user":{"login":"maintainer"},"labels":[]}\n' + ;; repos/hivecommons/hive/collaborators/maintainer/permission) printf 'write\n' ;; @@ -221,6 +229,9 @@ JSON repos/hivecommons/hive/issues/24/timeline*) printf '[[]]\n' ;; + repos/hivecommons/hive/issues/25/timeline*) + printf '[[]]\n' + ;; repos/hivecommons/hive/issues/10/comments*) printf '[[]]\n' ;; @@ -243,6 +254,11 @@ JSON repos/hivecommons/hive/issues/24/comments*) cat <<'JSON' [[{"id":2401,"user":{"login":"somebody","type":"User"},"body":"quoting the card:\n\n\nPartially addressed by #133.\n"}]] +JSON + ;; + repos/hivecommons/hive/issues/25/comments*) + cat <<'JSON' +[[{"id":2501,"user":"not-an-object","body":"malformed payload"}]] JSON ;; repos/hivecommons/hive/issues/90/comments*) @@ -543,6 +559,21 @@ else cat "$CALL_LOG" | sed 's/^/ | /' fi +# --- hive:status card: a card read/parse failure is an error, not absence --- +: > "$CALL_LOG" +set +e +card_error_output=$(CALL_LOG="$CALL_LOG" BODY_LOG="$BODY_LOG" GH_BIN="$GH_STUB" bash "$CHECKER" --repo hivecommons/hive --pr 134 2>&1) +card_error_rc=$? +set -e +if [ "$card_error_rc" -ne 0 ] && ! grep -q '^POST repos/hivecommons/hive/issues/25/comments' "$CALL_LOG" \ + && echo "$card_error_output" | grep -q '::error::Skipping #25: could not read its hive:status card'; then + pass "an unreadable hive:status card fails the sweep visibly instead of posting a possible duplicate" +else + bad "expected a non-zero exit, an ::error:: line and no POST on #25 (rc=${card_error_rc})" + echo "$card_error_output" | sed 's/^/ | /' + cat "$CALL_LOG" | sed 's/^/ | /' +fi + # --- hive-status-card.sh upsert: edits only the posting identity's comment --- CARD_HELPER="${HERE}/../../.github/scripts/hive-status-card.sh" UPSERT_BODY="$TMP/upsert-body.md"