diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index df63956a2..a7b0c0c65 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -192,6 +192,7 @@ jobs: tests/test_bootstrap_new_repo.bats \ tests/test_seed_repo_template.bats \ tests/test_sweep_stuck_reviews.bats \ + tests/test_dependabot_provenance.bats \ tests/test_delayed_rate_limit_retry.bats \ tests/test_standing_approval.bats \ tests/test_validate_cases.bats \ @@ -239,6 +240,7 @@ jobs: tests/test_pr_review_force_review_wiring.bats \ tests/test_review_one_pr_nonrequired_gate.bats \ tests/test_review_one_pr_force_re_review.bats \ + tests/test_review_one_pr_dependabot_provenance.bats \ tests/test_review_one_pr_escalation_hold.bats \ tests/test_review_one_pr_self_dismissed_reeval.bats \ tests/test_review_one_pr_metadata_rearm.bats \ diff --git a/docs/architecture/adr/0012-comment-gate-exemption-set.md b/docs/architecture/adr/0012-comment-gate-exemption-set.md index 80f65bc85..3e562f8ef 100644 --- a/docs/architecture/adr/0012-comment-gate-exemption-set.md +++ b/docs/architecture/adr/0012-comment-gate-exemption-set.md @@ -75,6 +75,12 @@ We will narrow #1813's "every PR issue comment" scope to every PR issue comment pre-merge check badges. The author must be that GitHub App (`Bot`). The match is on the structured state, never on prose, so it holds however often the bot edits the comment (`_MAINTAINER_GATE_NO_FINDING_JQ_DEFS`). + CodeRabbit's **"Review skipped"** notice (#2481, e.g. "Bot user detected" on + a Dependabot PR) is cleared the same way: the body opens with the + `skip review by coderabbit.ai` marker, carries it and its closing marker + exactly once, and carries no `final_review_risk_label` and no + finding-bearing section. It reviews nothing, and dev-lead never dispositions + comments on a Dependabot PR, so without this the gate deadlocked there. We will never exempt by author login alone, because `don-petry` is also the account dev-lead and the personas post from. A steering comment stays in scope diff --git a/docs/pr-review-agent/maintainer-comment-gate.md b/docs/pr-review-agent/maintainer-comment-gate.md index 91ab88311..20fb53167 100644 --- a/docs/pr-review-agent/maintainer-comment-gate.md +++ b/docs/pr-review-agent/maintainer-comment-gate.md @@ -155,6 +155,16 @@ minimized with classifier `RESOLVED`. In practice: kind `reviewed`, and only `passed` pre-merge check badges. The author must be the GitHub App (`Bot`), and a finding-bearing section still blocks. Any other state — failed, in progress, findings, unparseable, a second state block — still blocks. +- **CodeRabbit's "Review skipped" notice (#2481).** On a Dependabot PR CodeRabbit + posts "Review skipped — Bot user detected" instead of a review. dev-lead never + dispositions comments on a Dependabot PR, so the notice deadlocked the gate. It + now needs no disposition when the author is the `coderabbitai` GitHub App (`Bot`), + the body opens with the `` marker (after CodeRabbit's optional header markers) and carries + it and its closing marker exactly once, and the body has no + `final_review_risk_label` and no finding-bearing section. The prose is never + matched. The same marker from a User account, another bot, below other text, or + beside a finding still blocks. - **A maintainer clears it without dev-lead (#1910/#1918).** When dev-lead is suppressed, rate-limited, cancelled, or never dispatched, a maintainer can run [`scripts/maintainer-resolve-comment.sh`](../../scripts/maintainer-resolve-comment.sh) diff --git a/scripts/lib/dependabot-provenance.sh b/scripts/lib/dependabot-provenance.sh new file mode 100755 index 000000000..73690beaf --- /dev/null +++ b/scripts/lib/dependabot-provenance.sh @@ -0,0 +1,147 @@ +#!/usr/bin/env bash +# dependabot-provenance.sh — the Dependabot commit-provenance rule, for pr-review (#2481). +# +# pr-review reviews and approves a PR opened by Dependabot (the sweep dispatches +# it, scripts/sweep-stuck-reviews.sh). It keeps the rule dependabot-automerge +# applies before it approves (combsmith/.github +# .github/scripts/dependabot/lib/gates.sh, dependabot_provenance_gate, #1400): +# every commit on the PR is Dependabot's, or a branch-update merge commit the +# automerge App made through the update-branch API. A commit pushed by anyone +# else blocks the approval. The two copies must stay in step; the gate below is +# a port of the central one with the same verdicts. +# +# Source this file; it defines functions only. +# +# dependabot_is_author +# dependabot_provenance_gate [trusted_login] [expected_sha] +# dependabot_provenance_check [expected_sha] +# +# Verdicts: 0 pass, 1 block (a verdict about the commits), 2 unreadable (the +# reads cannot be trusted; no verdict). Callers treat 1 and 2 as "do not +# approve", and must not report 2 as a foreign commit. + +# REST spelling of the Dependabot App's login (commit and PR author). +DEPENDABOT_LOGIN="dependabot[bot]" + +# The automerge App whose update-branch merge commits are allowed. Same login as +# PR_BUDGET_MECHANICAL_LOGINS in pr-automation-budget.sh. Set but empty allows +# Dependabot's own commits only (hence `=`, not `:=`). +: "${DEPENDABOT_AUTOMERGE_LOGIN=combsmith-dependabot-automrg[bot]}" + +# dependabot_is_author +# 0 when is the Dependabot App: `app/dependabot` (gh pr view/list) or +# `dependabot[bot]` (REST). A bare `dependabot` is GraphQL's spelling but also +# a valid User login, so it is not accepted. +dependabot_is_author() { + case "${1:-}" in + "app/dependabot" | "$DEPENDABOT_LOGIN") return 0 ;; + esac + return 1 +} + +# dependabot_provenance_gate [trusted_login] [expected_sha] +# pr_json REST `GET /repos/{r}/pulls/{n}` (head.sha, commits). +# commits_json REST `GET /repos/{r}/pulls/{n}/commits`: a flat array, or the +# pages `gh api --paginate --slurp` produces. +# trusted_login the automerge App; its verified web-flow merge commits (2+ +# parents) are allowed. Empty allows Dependabot's commits only. +# expected_sha optional; block unless the PR head is this commit. +# Passes only when the whole commit list was read (its length is the PR's +# commit count and it holds the head), at least one commit is Dependabot's, +# and every commit is Dependabot's (committed by web-flow or Dependabot, +# GitHub-verified) or a trusted merge. Prints the reason. +dependabot_provenance_gate() { + local pr_json="${1:-}" commits_json="${2:-}" trusted="${3:-}" expected="${4:-}" out verdict reason + + # Both documents go through stdin: a long commit list can exceed the + # per-argument size limit of --argjson. + out=$({ printf '%s\n' "$pr_json"; printf '%s\n' "$commits_json"; } | jq -rs \ + --arg trusted "$trusted" --arg expected "$expected" --arg bot "$DEPENDABOT_LOGIN" ' + if length != 2 then error("expected two documents") else . end + | .[0] as $pr | .[1] as $pages + | ($pr.head.sha // "") as $head + | ($pr.commits // -1) as $count + | ([$pages | if type == "array" then flatten[] else empty end + | select(type == "object")]) as $commits + | def verified: (.commit.verification.verified // false) == true; + def author: (.author.login // ""); + def committer: (.committer.login // ""); + def from_dependabot: + author == $bot and (committer | IN("web-flow", $bot)) and verified; + def trusted_merge: + $trusted != "" and author == $trusted and committer == "web-flow" + and ((.parents // []) | length) >= 2 and verified; + ([$commits[] | select((from_dependabot or trusted_merge) | not) + | "\(.sha // "?" | .[0:7]) (author \(author | if . == "" then "unknown" else . end), committer \(committer | if . == "" then "unknown" else . end)\(if verified then "" else ", unverified" end))"]) as $foreign + | if $head == "" then "unreadable\tPR head commit unknown" + elif ($expected != "" and $head != $expected) then + "block\thead moved to \($head[0:7]) (expected \($expected[0:7]))" + elif ($commits | length) == 0 then "unreadable\tno commits read" + elif ($commits | length) != $count then + "unreadable\tread \($commits | length) of \($count) commits — cannot verify them all" + elif ([$commits[] | .sha] | index($head)) == null then + "unreadable\thead \($head[0:7]) not in the commit list" + elif ($foreign | length) > 0 then + "block\t\($foreign | length) commit(s) not from Dependabot: \($foreign | join(", "))" + elif ([$commits[] | select(from_dependabot)] | length) == 0 then + "block\tno Dependabot commit on the PR" + else + "pass\tall \($commits | length) commit(s) from Dependabot\(if $trusted != "" then " or \($trusted) branch updates" else "" end) (head \($head[0:7]))" + end + ' 2>/dev/null) || out="" + + verdict="${out%%$'\t'*}" + reason="${out#*$'\t'}" + case "$verdict" in + pass) echo "$reason"; return 0 ;; + block) echo "$reason"; return 1 ;; + unreadable) echo "$reason"; return 2 ;; + esac + echo "could not evaluate commit provenance (malformed input)" + return 2 +} + +# dependabot_provenance_check [expected_sha] +# Reads the PR and all its commits with gh and applies +# dependabot_provenance_gate, trusting DEPENDABOT_AUTOMERGE_LOGIN's merges. The +# PR itself must have been opened by Dependabot. Sets DEPENDABOT_GATE_REASON. +# Returns 0 pass, 1 block, 2 unreadable. The head is read again after the +# commit list; if it moved, the verdict describes nothing that exists (2). +# shellcheck disable=SC2034 # DEPENDABOT_GATE_REASON is read by the caller +dependabot_provenance_check() { + local repo="${1:-}" pr="${2:-}" expected="${3:-}" pr_json commits_json head user again rc=0 + DEPENDABOT_GATE_REASON="" + + if ! [[ "$repo" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] || ! [[ "$pr" =~ ^[0-9]+$ ]]; then + DEPENDABOT_GATE_REASON="malformed repository or pull request number" + return 2 + fi + if ! pr_json=$(gh api "repos/${repo}/pulls/${pr}" 2>/dev/null) || [[ -z "$pr_json" ]]; then + DEPENDABOT_GATE_REASON="could not read the pull request" + return 2 + fi + user=$(jq -r '.user.login // ""' <<< "$pr_json" 2>/dev/null) || user="" + if [[ -z "$user" ]]; then + DEPENDABOT_GATE_REASON="could not read the pull request author" + return 2 + fi + if [[ "$user" != "$DEPENDABOT_LOGIN" ]]; then + DEPENDABOT_GATE_REASON="the pull request was not opened by Dependabot (author $user)" + return 1 + fi + head=$(jq -r '.head.sha // ""' <<< "$pr_json" 2>/dev/null) || head="" + if ! commits_json=$(gh api "repos/${repo}/pulls/${pr}/commits?per_page=100" --paginate --slurp 2>/dev/null) \ + || [[ -z "$commits_json" ]]; then + DEPENDABOT_GATE_REASON="could not read the pull request's commits" + return 2 + fi + DEPENDABOT_GATE_REASON=$(dependabot_provenance_gate "$pr_json" "$commits_json" \ + "$DEPENDABOT_AUTOMERGE_LOGIN" "$expected") || rc=$? + (( rc == 0 )) || return "$rc" + if ! again=$(gh api "repos/${repo}/pulls/${pr}" 2>/dev/null) \ + || [[ "$(jq -r '.head.sha // ""' <<< "$again" 2>/dev/null)" != "$head" ]]; then + DEPENDABOT_GATE_REASON="the head changed while it was being read" + return 2 + fi + return 0 +} diff --git a/scripts/lib/maintainer-comment-gate.sh b/scripts/lib/maintainer-comment-gate.sh index 33555fbcc..7f3647a44 100755 --- a/scripts/lib/maintainer-comment-gate.sh +++ b/scripts/lib/maintainer-comment-gate.sh @@ -189,6 +189,15 @@ readonly _MAINTAINER_GATE_INFO_JQ_DEFS=' # follow-ups, so it is not cleared), exactly one # `final_review_risk_coverage` JSON with kind "reviewed", and # pre-merge check badges that are all "passed" (at least one). +# coderabbit_skip_notice — CodeRabbit's "Review skipped" notice (#2481), posted +# in place of a review (e.g. "Bot user detected" on a +# Dependabot PR): the body opens (after the optional +# `coderabbit-ai-generated:v1` and summarize markers) with the +# `skip review by coderabbit.ai` marker, carries that marker +# and its closing `end of auto-generated comment` marker +# exactly once each, and carries no `final_review_risk_label` +# (a reviewed summary is coderabbit_no_finding's to judge). +# The prose is never matched. # no_finding_summary — ALL of: a GitHub App author (author.__typename "Bot", # as info_status_clear requires); its bare login is the bot the # recognizer belongs to; the recognizer matches; and the body @@ -230,12 +239,18 @@ readonly _MAINTAINER_GATE_NO_FINDING_JQ_DEFS=' | (type == "object") and .kind == "reviewed" and (.sourceCommitId | nf_sha) and (.coveredCommitId | nf_sha)) and ([$b | scan("pre-merge-checks-(?:counter-)?([a-z]+)[-.]") | .[0]] | unique) == ["passed"]; + def coderabbit_skip_notice: + ((.body // "") | tostring) as $b + | ($b | test("\\A(?:\\s*)?(?:\\s*)?")) + and ([$b | scan("")] | length) == 1 + and ([$b | scan("")] | length) == 1 + and (($b | contains("final_review_risk_label")) | not); def no_finding_summary: ((.author?.__typename // "") == "Bot") and (((.body // "") | tostring | test($findre)) | not) and ((.author?.login // "" | tostring | if endswith("[bot]") then .[0:-5] else . end) as $l | ($l == "chatgpt-codex-connector" and codex_no_finding) - or ($l == "coderabbitai" and coderabbit_no_finding)); + or ($l == "coderabbitai" and (coderabbit_no_finding or coderabbit_skip_notice))); ' # This library's own directory, resolved ONCE at source time. dev-lead-fix-reviews.sh diff --git a/scripts/review-one-pr.sh b/scripts/review-one-pr.sh index 289733f83..0b0c873da 100755 --- a/scripts/review-one-pr.sh +++ b/scripts/review-one-pr.sh @@ -118,6 +118,12 @@ source "$SCRIPT_DIR/lib/pr-metadata-digest.sh" # shellcheck source=lib/self-dismissed-approval.sh source "$SCRIPT_DIR/lib/self-dismissed-approval.sh" +# Dependabot commit-provenance rule (#2481): dependabot_is_author / +# dependabot_provenance_check, the gate pr-review applies before reviewing a PR +# opened by Dependabot. +# shellcheck source=lib/dependabot-provenance.sh +source "$SCRIPT_DIR/lib/dependabot-provenance.sh" + # Per-PR review-claim primitive (issue #1589, slice 1 — OBSERVE-ONLY). Provides # the claim key = (head SHA, metadata digest), the claim marker format, and # concurrent_claim_present so the #1551 same-SHA re-review race is observable @@ -288,7 +294,7 @@ echo " output_channel=$REVIEW_OUTPUT_CHANNEL" # NEUTRAL covers informational checks that don't gate merging. # failing — anything else (FAILURE, ACTION_REQUIRED, TIMED_OUT, CANCELLED, # STALE, STARTUP_FAILURE, or unknown conclusions) -PR_SNAPSHOT=$(gh pr view "$PR_URL" --json headRefOid,baseRefName,statusCheckRollup,reviewDecision,reviews,labels,comments,body,closingIssuesReferences) +PR_SNAPSHOT=$(gh pr view "$PR_URL" --json headRefOid,baseRefName,statusCheckRollup,reviewDecision,reviews,labels,comments,body,closingIssuesReferences,author) PR_HEAD_SHA=$(echo "$PR_SNAPSHOT" | jq -r '.headRefOid') export PR_HEAD_SHA echo " head SHA: $PR_HEAD_SHA" @@ -405,7 +411,7 @@ if [ "$CI_STATUS" = "pending" ]; then echo " force-review: CI pending, waiting ${_FORCE_POLL_SEC}s for checks to settle (attempt ${_poll}/${_FORCE_POLL_MAX})" sleep "$_FORCE_POLL_SEC" _gh_poll_err=$(mktemp 2>/dev/null || echo "/tmp/cascade/gh-poll-$$.err") - if ! PR_SNAPSHOT=$(gh pr view "$PR_URL" --json headRefOid,baseRefName,statusCheckRollup,reviewDecision,reviews,labels,comments,body,closingIssuesReferences 2>"$_gh_poll_err"); then + if ! PR_SNAPSHOT=$(gh pr view "$PR_URL" --json headRefOid,baseRefName,statusCheckRollup,reviewDecision,reviews,labels,comments,body,closingIssuesReferences,author 2>"$_gh_poll_err"); then _gh_poll_err_content=$(cat "$_gh_poll_err" 2>/dev/null || true) rm -f "$_gh_poll_err" if is_rate_limited "$_gh_poll_err_content"; then @@ -487,6 +493,34 @@ if [ -n "$NON_REQUIRED_CI_FAILURES" ]; then export NON_REQUIRED_CI_FAILURES fi +# Dependabot provenance gate (#2481). pr-review approves Dependabot PRs (the +# sweep dispatches them, since nothing else reviews them), so it keeps the rule +# dependabot-automerge applies before approving (#1400): every commit on a PR +# opened by Dependabot is Dependabot's, or the automerge App's update-branch +# merge. A commit pushed by anyone else gets no approval from here; a human +# code owner reviews it instead. No bypass: FORCE_REVIEW does not apply, as the +# automerge rule has none. An unreadable commit list fails closed (exit 1, the +# next sweep retries). It runs after the CI gate, so a failing required check +# skips first and costs no read. +if dependabot_is_author "$(jq -r '.author.login // ""' <<< "$PR_SNAPSHOT" 2>/dev/null || true)"; then + _dp_rc=0 + _dp_num="" + [[ "$PR_URL" =~ /pull/([0-9]+) ]] && _dp_num="${BASH_REMATCH[1]}" + dependabot_provenance_check "$_OWNER_REPO" "$_dp_num" "$PR_HEAD_SHA" || _dp_rc=$? + if (( _dp_rc == 0 )); then + echo " Dependabot provenance: $DEPENDABOT_GATE_REASON" + elif (( _dp_rc == 1 )); then + echo " skip: Dependabot provenance — $DEPENDABOT_GATE_REASON (#2481)" + emit_verdict skip dependabot-provenance "every commit on the PR is Dependabot's or an automerge update-branch merge (Dependabot recreates the PR), or a human code owner reviews it" + exit 100 + else + echo " error: Dependabot provenance could not be read — $DEPENDABOT_GATE_REASON; failing closed (#2481)" + emit_verdict error dependabot-provenance-unreadable "the PR and its full commit list can be read at one head; the next scheduled pr-review sweep retries this PR" + exit 1 + fi + unset _dp_rc _dp_num +fi + # Advisory bot review gate — instant check for advisory bot reviews (Gemini, Copilot, SonarCloud, Codex) # This ensures valid code reviews are incorporated before pr-review posts approval (issue #457). # diff --git a/scripts/sweep-stuck-reviews.sh b/scripts/sweep-stuck-reviews.sh index a6ef59128..962c32d36 100755 --- a/scripts/sweep-stuck-reviews.sh +++ b/scripts/sweep-stuck-reviews.sh @@ -65,6 +65,9 @@ source "$SCRIPT_DIR/lib/pr-automation-budget.sh" # approval, so it must never satisfy the stop condition. # shellcheck source=lib/standing-approval.sh source "$SCRIPT_DIR/lib/standing-approval.sh" +# Dependabot provenance (#2481): dependabot_is_author / dependabot_provenance_check. +# shellcheck source=lib/dependabot-provenance.sh +source "$SCRIPT_DIR/lib/dependabot-provenance.sh" AGENT_REPO="${AGENT_REPO:-combsmith/.github-private}" TRIGGER_WORKFLOW="${TRIGGER_WORKFLOW:-pr-review-trigger.yml}" @@ -178,6 +181,26 @@ stuck=0 dispatched=0 armed=0 +# dependabot_dispatchable +# Dependabot PRs (#2481) are dispatched only when every commit passes the +# Dependabot provenance rule at this head: Dependabot's own commits, or the +# automerge App's update-branch merges. A foreign commit (1) or an unreadable +# commit list (2) is skipped and logged; review-one-pr.sh applies the same gate +# again before it approves, so this check only saves a dispatch. +dependabot_dispatchable() { + local _url="$1" _repo="$2" _sha="$3" _rc=0 + dependabot_provenance_check "$_repo" "${_url##*/}" "$_sha" || _rc=$? + if [ "$_rc" -eq 0 ]; then + return 0 + fi + if [ "$_rc" -eq 1 ]; then + echo " skip $_url — Dependabot provenance: $DEPENDABOT_GATE_REASON (#2481)" + else + echo " skip $_url — Dependabot provenance unreadable: $DEPENDABOT_GATE_REASON (#2481)" + fi + return 1 +} + # dispatch_review