diff --git a/.github/scripts/hive-status-card.sh b/.github/scripts/hive-status-card.sh new file mode 100755 index 0000000000..d866cb4c6a --- /dev/null +++ b/.github/scripts/hive-status-card.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# hive-status-card.sh — lets a Hive-maintained workflow ask whether the +# runtime's `` card on an issue or PR already carries a +# condition, and find/update the workflow's own marker comment in place. +# +# Usage: +# 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 +# +# 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 +# `issue-partial:163` never matches a card that only carries `issue-partial:1638`. +# +# Authorship: `has` only trusts a card whose author is a Bot account (the +# runtime posts the card from its App identity; a human quoting the card's +# invisible markers must not suppress a workflow). `upsert` only edits a +# comment authored by HIVE_COMMENT_AUTHOR (default `github-actions[bot]`, the +# identity `${{ github.token }}` posts as), so a third party's comment that +# happens to contain MARKER is never overwritten. +# +# Environment: +# GH_BIN gh binary (default: gh) +# HIVE_COMMENT_AUTHOR login whose comments `upsert` may edit (default: github-actions[bot]) +# HIVE_COMMENTS_FILE optional path to the issue's already-fetched comments +# (a flat JSON array); when set, no GET is issued +set -euo pipefail + +GH_BIN="${GH_BIN:-gh}" +HIVE_COMMENT_AUTHOR="${HIVE_COMMENT_AUTHOR:-github-actions[bot]}" + +comments() { + if [ -n "${HIVE_COMMENTS_FILE:-}" ]; then + cat "$HIVE_COMMENTS_FILE" + return + 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)' +} + +cmd="${1:-}"; repo="${2:-}"; number="${3:-}" +case "$cmd" in + has) + section="${4:?section key required}" + comments "$repo" "$number" | python3 -c 'import json,re,sys +want=sys.argv[1] +section_re=re.compile(r"") +def matches(key): + if want.endswith(":"): + return key.startswith(want) + return key == want +for c in json.load(sys.stdin): + if (c.get("user") or {}).get("type") != "Bot": + continue + body=c.get("body") or "" + if "" not in body: + continue + if any(matches(k) for k in section_re.findall(body)): + sys.exit(0) +sys.exit(1)' "$section" + ;; + upsert) + marker="${4:?marker required}"; body_file="${5:?body file required}" + id=$(comments "$repo" "$number" | 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: + continue + if marker in (c.get("body") or ""): + print(c["id"]); break' "$marker" "$HIVE_COMMENT_AUTHOR") + if [ -n "$id" ]; then + "$GH_BIN" api -X PATCH "repos/${repo}/issues/comments/${id}" -F "body=@${body_file}" >/dev/null + else + "$GH_BIN" api -X POST "repos/${repo}/issues/${number}/comments" -F "body=@${body_file}" >/dev/null + fi + ;; + *) echo "usage: $0 has|upsert REPO NUMBER ..." >&2; exit 2 ;; +esac diff --git a/.github/scripts/integration-governance.cjs b/.github/scripts/integration-governance.cjs index 20399176fb..847040a5ca 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 = 'f106bf6d14dcf30c4aba1ed5930b8f5fc86f7e6c'; +const TRUSTED_SCRIPTS_TREE = '9ba1ee4cedf0c05df6d68b5ef905fed8a2924af5'; 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/changelog.d/changed-16363.md b/changelog.d/changed-16363.md new file mode 100644 index 0000000000..c3bad3f54a --- /dev/null +++ b/changelog.d/changed-16363.md @@ -0,0 +1 @@ +- The post-merge refs sweep (`src/scripts/comment-merged-refs.sh`) no longer posts its "PR #N has merged and referenced this issue with a non-closing `Refs`" nudge when the issue's `hive:status` card already carries an `issue-partial:` section for that PR (#16363) diff --git a/src/docs/notifications.md b/src/docs/notifications.md index 78272d1a7b..2bcf78bb32 100644 --- a/src/docs/notifications.md +++ b/src/docs/notifications.md @@ -626,3 +626,36 @@ github: attention: reminder_interval: 24h # default 24h ``` + +### Hive-maintained Actions workflows that defer to the card + +Some comments come from GitHub Actions workflows in this repository rather +than from the runtime. When one of them says what a `hive:status` card +section already says, it now defers (the check is +`.github/scripts/hive-status-card.sh has
`, which +looks for `` inside the comment carrying +``, considering only comments by a Bot account so a +human quoting the card's invisible markers cannot suppress a workflow; a +caller that has already fetched the comments passes them via +`HIVE_COMMENTS_FILE` instead of re-fetching). The companion +`hive-status-card.sh upsert ` edits the +workflow's own earlier comment carrying `` in place, or creates it; +it only edits comments authored by `HIVE_COMMENT_AUTHOR` (default +`github-actions[bot]`, the identity `${{ github.token }}` posts as), never a +third party's comment that happens to contain the marker: + +| Workflow / script | Comment it posts | Behaviour | +| --- | --- | --- | +| Refs sweep (`.github/workflows/refs-sweep.yml`, `src/scripts/comment-merged-refs.sh`) | "PR #N has merged and referenced this issue with a non-closing `Refs`" (``) | **Skips** when the card carries `issue-partial:` for that PR; otherwise unchanged (still once per issue). | +| Issue Closed Hint (`.github/workflows/issue-closed-reopen-hint.yml`) | "@reporter this issue was closed by … comment `/reopen`" | **Pending** (tracked in #16363; the workflow edit must be landed by a maintainer). Once applied: **skips** when the card carries `issue-close`, which already says "Reply /reopen"; otherwise **edits** its earlier comment (``) instead of adding one per closure. Until then it posts a new comment on every closure as before. | + +Editing a comment notifies nobody, so a workflow comment that is edited in +place stops re-mentioning the reporter on every close/reopen cycle. Both +paths apply only where a card exists; with +`github.notifications.attention_pilot` off there is no issue card, and the +workflows behave as before. + +**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; +configure those bots in the project that runs them. diff --git a/src/scripts/comment-merged-refs.sh b/src/scripts/comment-merged-refs.sh index 725a128778..ddbbdb648f 100755 --- a/src/scripts/comment-merged-refs.sh +++ b/src/scripts/comment-merged-refs.sh @@ -177,6 +177,23 @@ sys.exit(1)' continue fi + # 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. + 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" + echo "Skipping #${issue}: hive:status card already carries the partial-merge note." + skipped=$((skipped + 1)) + continue + fi + rm -f "$comments_file" + 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.' \ "$marker" "$PR_NUMBER" "$pr_url" "Refs #${issue}") if [ "$DRY_RUN" = "1" ]; then diff --git a/src/scripts/test-comment-merged-refs.sh b/src/scripts/test-comment-merged-refs.sh index 8dc4dd27f3..12d139e738 100755 --- a/src/scripts/test-comment-merged-refs.sh +++ b/src/scripts/test-comment-merged-refs.sh @@ -148,6 +148,21 @@ JSON repos/hivecommons/hive/pulls/131) cat <<'JSON' {"number":131,"merged_at":"2026-09-10T21:20:00Z","html_url":"https://github.com/hivecommons/hive/pull/131","body":"Closes #33 and #34"} +JSON + ;; + repos/hivecommons/hive/pulls/132) + cat <<'JSON' +{"number":132,"merged_at":"2026-09-10T21:25:00Z","html_url":"https://github.com/hivecommons/hive/pull/132","body":"Refs #23"} +JSON + ;; + repos/hivecommons/hive/pulls/13) + cat <<'JSON' +{"number":13,"merged_at":"2026-09-10T21:30:00Z","html_url":"https://github.com/hivecommons/hive/pull/13","body":"Refs #23"} +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/issues/10) @@ -171,6 +186,12 @@ JSON repos/hivecommons/hive/issues/22) printf '{"number":22,"state":"open","user":{"login":"reporter"},"labels":[{"name":"needs-reporter-confirmation"}]}\n' ;; + repos/hivecommons/hive/issues/23) + printf '{"number":23,"state":"open","user":{"login":"maintainer"},"labels":[]}\n' + ;; + repos/hivecommons/hive/issues/24) + printf '{"number":24,"state":"open","user":{"login":"maintainer"},"labels":[]}\n' + ;; repos/hivecommons/hive/collaborators/maintainer/permission) printf 'write\n' ;; @@ -194,6 +215,12 @@ JSON repos/hivecommons/hive/issues/22/timeline*) printf '[[]]\n' ;; + repos/hivecommons/hive/issues/23/timeline*) + printf '[[]]\n' + ;; + repos/hivecommons/hive/issues/24/timeline*) + printf '[[]]\n' + ;; repos/hivecommons/hive/issues/10/comments*) printf '[[]]\n' ;; @@ -208,6 +235,26 @@ JSON repos/hivecommons/hive/issues/22/comments*) printf '[[]]\n' ;; + repos/hivecommons/hive/issues/23/comments*) + cat <<'JSON' +[[{"id":2301,"user":{"login":"hivecommons-hive[bot]","type":"Bot"},"body":"\n**Hive status**\n\n\nPartially addressed by #132.\n"}]] +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/90/comments*) + cat <<'JSON' +[[{"id":9001,"user":{"login":"somebody","type":"User"},"body":"I saw this: quoted"},{"id":9002,"user":{"login":"github-actions[bot]","type":"Bot"},"body":"\n@reporter this issue was closed"}]] +JSON + ;; + repos/hivecommons/hive/issues/91/comments*) + cat <<'JSON' +[[{"id":9101,"user":{"login":"somebody","type":"User"},"body":"I saw this: quoted"}]] +JSON + ;; *) echo "unexpected gh api path: $path" >&2 exit 99 @@ -446,6 +493,100 @@ else cat "$CALL_LOG" | sed 's/^/ | /' fi +# --- hive:status card: an `issue-partial:` section for this PR skips the nudge +: > "$CALL_LOG" +set +e +card_output=$(CALL_LOG="$CALL_LOG" BODY_LOG="$BODY_LOG" GH_BIN="$GH_STUB" bash "$CHECKER" --repo hivecommons/hive --pr 132 2>&1) +card_rc=$? +set -e +if [ "$card_rc" -eq 0 ] && ! grep -q '^POST repos/hivecommons/hive/issues/23/comments' "$CALL_LOG" \ + && echo "$card_output" | grep -q 'Skipping #23: hive:status card already carries the partial-merge note'; then + pass "hive:status card with issue-partial: for this PR suppresses the refs-sweep comment" +else + bad "expected no POST on #23 when the card already carries issue-partial:132" + echo "$card_output" | sed 's/^/ | /' + cat "$CALL_LOG" | sed 's/^/ | /' +fi +comment_gets=$(grep -c '^GET repos/hivecommons/hive/issues/23/comments' "$CALL_LOG" || true) +if [ "$comment_gets" -eq 1 ]; then + pass "the card check reuses the sweep's fetched comments instead of re-fetching them" +else + bad "expected exactly one GET of #23 comments (sweep + card check share it), got ${comment_gets}" + cat "$CALL_LOG" | sed 's/^/ | /' +fi + +# --- hive:status card: a human quoting the card's markers must not suppress the nudge +: > "$CALL_LOG" +set +e +card_human_output=$(CALL_LOG="$CALL_LOG" BODY_LOG="$BODY_LOG" GH_BIN="$GH_STUB" bash "$CHECKER" --repo hivecommons/hive --pr 133 2>&1) +card_human_rc=$? +set -e +if [ "$card_human_rc" -eq 0 ] && grep -q '^POST repos/hivecommons/hive/issues/24/comments' "$CALL_LOG"; then + pass "a hive:status card quoted by a non-Bot author does not suppress the refs-sweep comment" +else + bad "expected a POST on #24 (the only card-shaped comment is human-authored)" + echo "$card_human_output" | sed 's/^/ | /' + cat "$CALL_LOG" | sed 's/^/ | /' +fi + +# --- hive:status card: a section for a longer PR number (132) must not match PR 13 +: > "$CALL_LOG" +set +e +card_prefix_output=$(CALL_LOG="$CALL_LOG" BODY_LOG="$BODY_LOG" GH_BIN="$GH_STUB" bash "$CHECKER" --repo hivecommons/hive --pr 13 2>&1) +card_prefix_rc=$? +set -e +if [ "$card_prefix_rc" -eq 0 ] && grep -q '^POST repos/hivecommons/hive/issues/23/comments' "$CALL_LOG"; then + pass "hive:status card with issue-partial:132 does not suppress the refs-sweep comment for PR 13" +else + bad "expected a POST on #23 for PR 13 (card only carries issue-partial:132)" + echo "$card_prefix_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" +printf '\nupdated hint\n' > "$UPSERT_BODY" + +: > "$CALL_LOG" +set +e +CALL_LOG="$CALL_LOG" BODY_LOG="$BODY_LOG" GH_BIN="$GH_STUB" bash "$CARD_HELPER" upsert hivecommons/hive 90 '' "$UPSERT_BODY" >/dev/null 2>&1 +upsert_rc=$? +set -e +if [ "$upsert_rc" -eq 0 ] && grep -q '^PATCH repos/hivecommons/hive/issues/comments/9002$' "$CALL_LOG" \ + && ! grep -q 'issues/comments/9001' "$CALL_LOG" \ + && ! grep -q '^POST repos/hivecommons/hive/issues/90/comments' "$CALL_LOG"; then + pass "upsert edits the github-actions[bot] comment carrying the marker, not a human's quote of it" +else + bad "expected a single PATCH of comment 9002 for upsert on #90 (rc=${upsert_rc})" + cat "$CALL_LOG" | sed 's/^/ | /' +fi + +: > "$CALL_LOG" +set +e +CALL_LOG="$CALL_LOG" BODY_LOG="$BODY_LOG" GH_BIN="$GH_STUB" bash "$CARD_HELPER" upsert hivecommons/hive 91 '' "$UPSERT_BODY" >/dev/null 2>&1 +upsert_new_rc=$? +set -e +if [ "$upsert_new_rc" -eq 0 ] && grep -q '^POST repos/hivecommons/hive/issues/91/comments$' "$CALL_LOG" \ + && ! grep -q '^PATCH' "$CALL_LOG"; then + pass "upsert creates a new comment when only a third party's comment carries the marker" +else + bad "expected a POST on #91 and no PATCH (rc=${upsert_new_rc})" + cat "$CALL_LOG" | sed 's/^/ | /' +fi + +: > "$CALL_LOG" +set +e +CALL_LOG="$CALL_LOG" BODY_LOG="$BODY_LOG" GH_BIN="$GH_STUB" HIVE_COMMENT_AUTHOR=somebody bash "$CARD_HELPER" upsert hivecommons/hive 91 '' "$UPSERT_BODY" >/dev/null 2>&1 +upsert_env_rc=$? +set -e +if [ "$upsert_env_rc" -eq 0 ] && grep -q '^PATCH repos/hivecommons/hive/issues/comments/9101$' "$CALL_LOG"; then + pass "HIVE_COMMENT_AUTHOR overrides the identity whose comment upsert may edit" +else + bad "expected a PATCH of comment 9101 with HIVE_COMMENT_AUTHOR=somebody (rc=${upsert_env_rc})" + cat "$CALL_LOG" | sed 's/^/ | /' +fi + if [ "$fail" -ne 0 ]; then echo "test-comment-merged-refs FAILED" exit 1