Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Owner list read by .github/auto-merge/decide.sh (review requests to anyone else hold auto-merge).
* @todie
21 changes: 21 additions & 0 deletions .github/auto-merge-paths.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Auto-merge allow-list, read from the BASE ref by .github/auto-merge/decide.sh, never from the PR.
# One bash pattern per line; "*" also matches "/". No inline comments.
#
# A non-draft PR from a branch of this repo merges on green (see decide.sh for the holds). A file
# outside the lines below also needs the `automerge-deploy` label from someone with write access:
# these are the paths that change what is built, published or run with secrets (Cargo.toml,
# Dockerfiles, helm, deploy, scripts, Makefile). This list fails closed: add a line only for paths
# that cannot change what ships without a separate release.
docs/*
specs/*
tests/*
CHANGELOG.md
README.md
#
# NEVER: a PR touching any of these is never auto-merged, whatever its labels.
# Workflows (including this machinery), CODEOWNERS and this file:
!.github/*
# Agent hooks and settings, and the secret-scan config and its allow file:
!.claude/*
!.gitleaks.toml
!.gitleaksignore
112 changes: 112 additions & 0 deletions .github/auto-merge/decide.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#!/usr/bin/env bash
# Decide whether a pull request gets GitHub auto-merge, and enable it or disarm it.
# Run by .github/workflows/auto-merge.yml from the BASE checkout; it reads the PR through
# the API and never runs PR content. All PR-controlled values arrive as environment variables.
#
# Armed by default, no label (Christian 2026-10-27, as unsigned-paas): a non-draft PR to main from a
# branch of this repo whose author has write access, or a Dependabot patch/minor update.
# Held, any one of: draft; HOLD in the title; labels hold, do-not-merge, needs-review,
# review-requested; a CHANGES_REQUESTED review; a review request to anyone who is not a
# CODEOWNERS owner. Paths: .github/auto-merge-paths.txt (NEVER lines are absolute; a file outside
# the safe lines needs the `automerge-deploy` label from a writer).
# GitHub merges when every required check passes on the exact head. This script never decides green.
set -euo pipefail

ROOT="${ROOT:-.}"
: "${REPO:?}" "${PR:?}" "${BASE_REF:?}" "${HEAD_REF:?}" "${HEAD_REPO:?}" "${AUTHOR:?}" "${HEAD_SHA:?}"
TITLE="${TITLE:-}" AUTHOR_TYPE="${AUTHOR_TYPE:-User}" DRAFT="${DRAFT:-false}" CHANGED_FILES="${CHANGED_FILES:-0}"
LABELS="${LABELS:-[]}" DEP_UPDATE_TYPE="${DEP_UPDATE_TYPE:-}" APP_SLUG="${APP_SLUG:-}"

disarm() {
local by
by=$(gh pr view "$PR" -R "$REPO" --json autoMergeRequest --jq '.autoMergeRequest.enabledBy.login // empty')
case "$by" in
"$APP_SLUG"|"${APP_SLUG}[bot]"|"app/$APP_SLUG")
[ -n "$APP_SLUG" ] || return 0
gh pr merge "$PR" -R "$REPO" --disable-auto
echo "::notice::disabled the auto-merge this workflow enabled earlier" ;;
esac
}
skip() { echo "::notice::auto-merge not enabled: $1"; disarm; exit 0; }
has_label() { jq -e --arg l "$1" 'index($l) != null' <<<"$LABELS" >/dev/null; }
is_writer() {
local perm
perm=$(gh api "repos/$REPO/collaborators/$1/permission" --jq .permission 2>/dev/null) || perm=none
case "$perm" in admin|maintain|write) return 0 ;; *) return 1 ;; esac
}
label_by_writer() {
local who
who=$(gh api --paginate --slurp "repos/$REPO/issues/$PR/events" \
| jq -r --arg l "$1" '[.[][] | select(.event=="labeled" and .label.name==$l)] | last | .actor.login // empty')
[ -n "$who" ] && is_writer "$who"
}

[ "$BASE_REF" = "main" ] || skip "base is $BASE_REF, not main"

# Inert until main has a required-status-checks rule: without one a clean PR merges at once.
n=$(gh api "repos/$REPO/rules/branches/main" --jq '[.[] | select(.type=="required_status_checks")] | length')
[ "$n" -ge 1 ] || skip "main has no required_status_checks rule (merging would not wait for CI)"

[ "$DRAFT" = "true" ] && skip "draft"
shopt -s nocasematch
[[ "$TITLE" =~ (^|[^[:alpha:]])hold([^[:alpha:]]|$) ]] && skip "title contains HOLD"
shopt -u nocasematch
[[ "$HEAD_REF" == release-please--* ]] && skip "release-please PR is merged by the operator"
for l in hold do-not-merge needs-review review-requested; do
has_label "$l" && skip "label $l"
done
changes=$(gh pr view "$PR" -R "$REPO" --json latestReviews --jq '[.latestReviews[] | select(.state=="CHANGES_REQUESTED")] | length')
[ "$changes" -eq 0 ] || skip "a reviewer requested changes"
[ "$CHANGED_FILES" -lt 3000 ] || skip "diff too large to inspect (API lists 3000 files at most)"

[ "$HEAD_REPO" = "$REPO" ] || skip "PR from a fork"
if [ "$AUTHOR" = "dependabot[bot]" ]; then
case "$DEP_UPDATE_TYPE" in
version-update:semver-patch|version-update:semver-minor) ;;
*) skip "dependabot update type '${DEP_UPDATE_TYPE:-unknown}' is not patch or minor" ;;
esac
elif [ "$AUTHOR_TYPE" = "Bot" ]; then
skip "bot author $AUTHOR"
else
is_writer "$AUTHOR" || skip "author $AUTHOR has no write access"
fi

# A review request to anyone who is not a CODEOWNERS owner is an opt-in to review. Owners come
# from the base ref; their automatic requests do not hold.
owners=$(grep -vE '^\s*(#|$)' "$ROOT/.github/CODEOWNERS" | grep -oE '@[A-Za-z0-9_./-]+' | tr 'A-Z' 'a-z' | sort -u || true)
requested=$(gh pr view "$PR" -R "$REPO" --json reviewRequests --jq '.reviewRequests[] | "@" + (.login // .slug) | ascii_downcase')
extra=$(comm -23 <(sort -u <<<"$requested" | grep -v '^$' || true) <(printf '%s\n' "$owners") | paste -sd, -)
[ -z "$extra" ] || skip "review requested from $extra"

# Paths. The allow-list is read from the base checkout, never from the PR.
mapfile -t safe < <(grep -vE '^\s*(#|!|$)' "$ROOT/.github/auto-merge-paths.txt")
mapfile -t never < <(grep -E '^!' "$ROOT/.github/auto-merge-paths.txt" | sed 's/^!//')
mapfile -t files < <(gh api --paginate "repos/$REPO/pulls/$PR/files" --jq '.[] | .filename, (.previous_filename // empty)')
[ "${#files[@]}" -gt 0 ] || skip "no changed files returned"
deploy=false
for f in "${files[@]}"; do
for p in "${never[@]}"; do
# shellcheck disable=SC2053
[[ "$f" == $p ]] && skip "protected path $f"
done
hit=false
for p in "${safe[@]}"; do
# shellcheck disable=SC2053
[[ "$f" == $p ]] && { hit=true; break; }
done
$hit || deploy=true
done
if $deploy; then
has_label automerge-deploy || skip "touches paths outside the allow-list; needs the automerge-deploy label"
label_by_writer automerge-deploy || skip "automerge-deploy label not applied by a writer"
fi

if out=$(gh pr merge "$PR" -R "$REPO" --auto --merge --match-head-commit "$HEAD_SHA" 2>&1); then
echo "$out"
elif grep -qi 'clean status' <<<"$out"; then
# Already mergeable with the required rule present: the required checks passed.
gh pr merge "$PR" -R "$REPO" --merge --match-head-commit "$HEAD_SHA"
else
echo "$out" >&2
exit 1
fi
74 changes: 74 additions & 0 deletions .github/auto-merge/test-decide.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
# Logic test for decide.sh with a fake `gh`. Each case names the facts, runs the script, and
# asserts whether `gh pr merge --auto` (arm) or `--disable-auto` (disarm) was called.
# Run: bash .github/auto-merge/test-decide.sh [path-to-decide.sh]
set -uo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DECIDE="${1:-$HERE/decide.sh}"
ROOT_REAL="$(cd "$HERE/../.." && pwd)"
T="$(mktemp -d)"; trap 'rm -rf "$T"' EXIT
mkdir -p "$T/bin" "$T/root/.github"
cp "$ROOT_REAL/.github/auto-merge-paths.txt" "$ROOT_REAL/.github/CODEOWNERS" "$T/root/.github/"

cat > "$T/bin/gh" <<'FAKE'
#!/usr/bin/env bash
echo "gh $*" >> "$FAKE_LOG"
a="$*"
case "$a" in
*"rules/branches/main"*) echo "${F_RULES:-1}" ;;
*"--json autoMergeRequest"*) echo "${F_AUTOMERGE_BY:-}" ;;
*"--json latestReviews"*) echo "${F_CHANGES:-0}" ;;
*"--json reviewRequests"*) printf '%s' "${F_REVIEW_REQ:-}" ;;
*"collaborators/"*) case "$a" in *"/collaborators/${F_LABEL_ACTOR:-nobody}/"*) echo "${F_LABEL_PERM:-write}" ;; *) echo "${F_PERM:-write}" ;; esac ;;
*"issues/"*"/events"*) printf '[[{"event":"labeled","label":{"name":"automerge-deploy"},"actor":{"login":"%s"}}]]' "${F_LABEL_ACTOR:-nobody}" ;;
*"/files"*) printf '%s' "${F_FILES:-docs/a.md}" ;;
*"pr merge"*"--disable-auto"*) : ;;
*"pr merge"*) : ;;
*) echo "unexpected gh call: $a" >&2; exit 99 ;;
esac
FAKE
chmod +x "$T/bin/gh"

pass=0; fail=0
run() { # name expect(arm|none|disarm) -- env assignments...
local name="$1" expect="$2"; shift 2
: > "$T/log"
( export PATH="$T/bin:$PATH" FAKE_LOG="$T/log" ROOT="$T/root" REPO=o/r PR=7 BASE_REF=main HEAD_REF=feat/x HEAD_REPO=o/r \
AUTHOR=alice HEAD_SHA=abc123 TITLE="docs: a" AUTHOR_TYPE=User DRAFT=false CHANGED_FILES=1 LABELS='[]' APP_SLUG=relbot
for kv in "$@"; do export "$kv"; done
bash "$DECIDE" > "$T/out" 2>&1 ) ; local rc=$?
local got=none
grep -q 'pr merge .*--auto' "$T/log" && got=arm
grep -q 'pr merge .*--disable-auto' "$T/log" && got=disarm
if [ "$got" = "$expect" ] && [ "$rc" -eq 0 ]; then pass=$((pass+1)); printf 'ok %s\n' "$name"
else fail=$((fail+1)); printf 'FAIL %s: expected %s, got %s (rc=%s)\n' "$name" "$expect" "$got" "$rc"; sed 's/^/ /' "$T/out" | head -5; fi
}

run "docs PR by a writer arms" arm
run "no required-checks rule: inert" none F_RULES=0
run "base is not main" none BASE_REF=release
run "draft" none DRAFT=true
run "HOLD in the title" none 'TITLE=fix: HOLD this'
run "hold label" none 'LABELS=["hold"]'
run "needs-review label" none 'LABELS=["needs-review"]'
run "do-not-merge label" none 'LABELS=["do-not-merge"]'
run "release-please branch" none HEAD_REF=release-please--branches--main
run "changes requested" none F_CHANGES=1
run "fork PR" none HEAD_REPO=evil/r
run "bot author" none AUTHOR=some-bot 'AUTHOR_TYPE=Bot'
run "author without write access" none F_PERM=read
run "dependabot major" none AUTHOR='dependabot[bot]' AUTHOR_TYPE=Bot DEP_UPDATE_TYPE=version-update:semver-major
run "dependabot minor arms" arm AUTHOR='dependabot[bot]' AUTHOR_TYPE=Bot DEP_UPDATE_TYPE=version-update:semver-minor
run "review requested from a non-owner" none F_REVIEW_REQ=$'@mallory\n'
run "review requested from the owner arms" arm F_REVIEW_REQ=$'@todie\n'
# NEVER cases carry a valid automerge-deploy label, so only the NEVER rule can stop them.
run "NEVER path (workflow)" none F_FILES=$'.github/workflows/ci.yml\n' 'LABELS=["automerge-deploy"]' F_LABEL_ACTOR=bob
run "NEVER path among safe ones" none F_FILES=$'docs/a.md\n.github/CODEOWNERS\n' 'LABELS=["automerge-deploy"]' F_LABEL_ACTOR=bob
run "NEVER path (agent hooks)" none F_FILES=$'.claude/settings.json\n' 'LABELS=["automerge-deploy"]' F_LABEL_ACTOR=bob
run "outside the allow-list, no label" none F_FILES=$'Dockerfile\n'
run "outside the allow-list, label by writer arms" arm F_FILES=$'Dockerfile\n' 'LABELS=["automerge-deploy"]' F_LABEL_ACTOR=bob
run "outside the allow-list, label by reader" none F_FILES=$'Dockerfile\n' 'LABELS=["automerge-deploy"]' F_LABEL_ACTOR=bob F_LABEL_PERM=read
run "held PR disarms an App-enabled auto-merge" disarm DRAFT=true F_AUTOMERGE_BY=app/relbot
run "held PR leaves a person's auto-merge" none DRAFT=true F_AUTOMERGE_BY=todie
echo "passed $pass, failed $fail"
[ "$fail" -eq 0 ]
78 changes: 78 additions & 0 deletions .github/workflows/auto-merge-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Auto-merge review hold

# A CHANGES_REQUESTED review is an opt-in to review before merge.
# Reviews fire `pull_request_review`, which auto-merge.yml (pull_request_target)
# does not receive, so this workflow disables an auto-merge the release App
# enabled when a reviewer requests changes. It never arms anything; the next
# auto-merge.yml run re-evaluates once the author pushes or relabels.
#
# ┌─────────────────────────────────────────────────────────────────────────┐
# │ SECURITY: under pull_request_review the workflow file and GITHUB_SHA │
# │ come from the PR merge ref, i.e. PR content. This job therefore checks │
# │ nothing out and runs nothing from the repository: it installs a pinned │
# │ gh, mints the App token and disables auto-merge. Fork PRs and Dependabot │
# │ PRs get no repository secrets here; forks are never armed, and a │
# │ Dependabot PR is held with the `hold` label instead. │
# └─────────────────────────────────────────────────────────────────────────┘
on:
pull_request_review:
types: [submitted]

permissions: {}

# Same group as auto-merge.yml, so the two never interleave: this run waits for
# an in-flight auto-merge run, and a later auto-merge run re-reads the reviews.
concurrency:
group: auto-merge-${{ github.event.pull_request.number }}
cancel-in-progress: false

jobs:
disarm:
name: Disarm on changes requested
if: >-
github.event.review.state == 'changes_requested' &&
github.event.pull_request.head.repo.full_name == github.repository &&
github.event.pull_request.user.login != 'dependabot[bot]'
runs-on: {group: ent-default, labels: [self-hosted]}
timeout-minutes: 5
permissions: {}
steps:
- name: Install gh (pinned, checksum-verified)
env:
GH_CLI_VERSION: "2.101.0"
GH_CLI_SHA256: "9bca2d1c16825f109907a23307628a2f0698fbf99662b73a5cf0b020293072b8"
run: |
set -euo pipefail
tgz="gh_${GH_CLI_VERSION}_linux_amd64.tar.gz"
curl -fsSL -o "/tmp/$tgz" "https://github.com/cli/cli/releases/download/v${GH_CLI_VERSION}/$tgz"
echo "${GH_CLI_SHA256} /tmp/$tgz" | sha256sum -c -
mkdir -p "$HOME/.local/bin"
tar -xzf "/tmp/$tgz" -C /tmp
install -m 0755 "/tmp/gh_${GH_CLI_VERSION}_linux_amd64/bin/gh" "$HOME/.local/bin/gh"
echo "$HOME/.local/bin" >> "$GITHUB_PATH"

- name: Mint release App token
id: app-token
uses: unsigned-gg/actions/vendor/actions-create-github-app-token@fd981ada61381db6416ba67b2bbd2d779823d0b5 # v3.2.0
with:
app-id: ${{ vars.RELEASE_BOT_APP_ID }}
private-key: ${{ secrets.RELEASE_BOT_PRIVATE_KEY }}

- name: Disable an App-enabled auto-merge
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
REPO: ${{ github.repository }}
PR: ${{ github.event.pull_request.number }}
APP_SLUG: ${{ steps.app-token.outputs.app-slug }}
shell: bash
run: |
set -euo pipefail
by=$(gh pr view "$PR" -R "$REPO" --json autoMergeRequest --jq '.autoMergeRequest.enabledBy.login // empty')
# gh reports an App as app/<slug>; the API also uses <slug> and <slug>[bot].
case "$by" in
"$APP_SLUG"|"${APP_SLUG}[bot]"|"app/$APP_SLUG")
gh pr merge "$PR" -R "$REPO" --disable-auto
echo "::notice::changes requested: disabled the auto-merge the release App enabled" ;;
*)
echo "::notice::changes requested: no App-enabled auto-merge to disable (enabled by '${by:-nobody}')" ;;
esac
31 changes: 31 additions & 0 deletions .github/workflows/auto-merge-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Auto-merge logic test

# Tests .github/auto-merge/decide.sh with a fake gh. Not a required check.
on:
pull_request:
paths:
- '.github/auto-merge/**'
- '.github/auto-merge-paths.txt'
- '.github/CODEOWNERS'

permissions:
contents: read

jobs:
test-decide:
name: Auto-merge logic test
runs-on: {group: ent-default, labels: [self-hosted]}
timeout-minutes: 5
steps:
- name: Checkout
uses: unsigned-gg/actions/vendor/actions-checkout@fd981ada61381db6416ba67b2bbd2d779823d0b5 # v7.0.1
with:
persist-credentials: false

- name: Syntax check
run: |
bash -n .github/auto-merge/decide.sh
bash -n .github/auto-merge/test-decide.sh

- name: Run decide.sh tests
run: bash .github/auto-merge/test-decide.sh
Loading
Loading