Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-architecture-mv/blob/main/scripts/pr-gate.sh # aien-architecture-mv Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-architecture-mv / scripts / pr-gate.sh` 132 lines ยท 6520 bytes Raw Blame History 1 #!/usr/bin/env bash 2 # PR merge gate for aien-dev repositories. Reads GitHub state at the moment of the call, checks 3 # docs/REPOSITORY_EXECUTION_RULES.md rules 4 and 5, then (unless --dry-run) squash-merges. Rule 2 4 # (crumb compile/verify) is retired (2026-11-07, DRA-97): step 3 always SKIPs and never runs crumb. 5 # It does not replace a human read of the diff, and it never rebases anything by itself. 6 # 7 # Usage: pr-gate.sh [--worktree DIR] [--dry-run] [--admin-authorized-by ""] 8 # Env: GH (default gh), GIT (default git), SLEEP_SECS (default 10, UNKNOWN retry). 9 # Output: one line per step, "GATE : PASS|FAIL|SKIP ". Exit 1 at the first FAIL. 10 set -u 11 GH=${GH:-gh} 12 GIT=${GIT:-git} 13 SLEEP_SECS=${SLEEP_SECS:-10} 14 15 usage() { echo 'usage: pr-gate.sh [--worktree DIR] [--dry-run] [--admin-authorized-by ""]' >&2; exit 2; } 16 [ $# -ge 2 ] || usage 17 REPO=$1; N=$2; shift 2 18 WT=""; DRY=0; ADMIN="" 19 while [ $# -gt 0 ]; do 20 case "$1" in 21 --worktree) [ $# -ge 2 ] || usage; WT=$2; shift 2 ;; 22 --dry-run) DRY=1; shift ;; 23 --admin-authorized-by) [ $# -ge 2 ] || usage; ADMIN=$2; shift 2 ;; 24 *) usage ;; 25 esac 26 done 27 case "$N" in ''|*[!0-9]*) usage ;; esac 28 FULL="aien-dev/$REPO" 29 30 pass() { echo "GATE $1: PASS $2"; } 31 skip() { echo "GATE $1: SKIP $2"; } 32 fail() { echo "GATE $1: FAIL $2"; exit 1; } 33 34 FIELDS=number,state,isDraft,baseRefName,headRefName,headRefOid,mergeable,mergeStateStatus,reviewDecision,statusCheckRollup 35 view() { $GH pr view "$N" -R "$FULL" --json "$FIELDS" 2>&1; } 36 37 # 1. pr 38 PR=$(view) || fail pr "could not read PR: $(echo "$PR" | head -1)" 39 echo "$PR" | jq -e . >/dev/null 2>&1 || fail pr "gh did not return JSON" 40 f() { echo "$PR" | jq -r "$1"; } 41 STATE=$(f .state); DRAFT=$(f .isDraft); BASE=$(f .baseRefName); HEADREF=$(f .headRefName); HEADOID=$(f .headRefOid) 42 [ "$STATE" = OPEN ] || fail pr "state is $STATE, not OPEN" 43 [ "$DRAFT" = false ] || fail pr "PR is a draft" 44 [ "$BASE" = main ] || fail pr "base is $BASE, not main" 45 pass pr "#$N $HEADREF at ${HEADOID:0:8}, open, not draft, base main" 46 47 # 2. local 48 if [ -z "$WT" ]; then 49 skip local "no worktree given" 50 else 51 G="$GIT -C $WT" 52 $G rev-parse --is-inside-work-tree >/dev/null 2>&1 || fail local "$WT is not a git checkout" 53 $G fetch -q origin 2>/dev/null || fail local "fetch failed in $WT" 54 [ -z "$($G status --porcelain)" ] || fail local "worktree is dirty" 55 CUR=$($G rev-parse --abbrev-ref HEAD) 56 [ "$CUR" = "$HEADREF" ] || fail local "worktree is on $CUR, PR branch is $HEADREF" 57 [ "$($G rev-parse HEAD)" = "$HEADOID" ] || fail local "local HEAD $($G rev-parse --short HEAD) differs from GitHub head ${HEADOID:0:7}" 58 [ "$($G merge-base origin/main HEAD)" = "$($G rev-parse origin/main)" ] || fail local "stale base, branch is not on current origin/main; run scripts/rebase-crumb.sh" 59 pass local "clean, on $CUR, HEAD matches GitHub, based on current origin/main" 60 fi 61 62 # 3. crumb: rule 2 is retired. No crumb verify, no crumb-commit-last check; .crumb files are history. 63 skip crumb "retired 2026-11-07 (DRA-97); .crumb files are history" 64 65 # 4. base 66 TRIES=0 67 while :; do 68 MSS=$(f .mergeStateStatus); MRG=$(f .mergeable) 69 [ "$MSS" = UNKNOWN ] || break 70 TRIES=$((TRIES+1)) 71 [ "$TRIES" -le 5 ] || fail base "GitHub has not computed mergeability" 72 sleep "$SLEEP_SECS" 73 PR=$(view) || fail base "could not re-read PR" 74 [ "$(f .headRefOid)" = "$HEADOID" ] || fail base "PR head changed during mergeability wait; restart inspection" 75 done 76 [ "$MRG" != CONFLICTING ] || fail base "mergeable is CONFLICTING (merge conflicts)" 77 case "$MSS" in 78 CLEAN) pass base "mergeStateStatus CLEAN" ;; 79 BEHIND) fail base "stale, rebase first" ;; 80 DIRTY) fail base "merge conflicts" ;; 81 BLOCKED) fail base "required checks or reviews missing" ;; 82 UNSTABLE) fail base "a check is failing" ;; 83 HAS_HOOKS) 84 if [ "$DRY" = 1 ]; then pass base "HAS_HOOKS (accepted in dry run only)" 85 else fail base "merge queue or hooks present, check by hand"; fi ;; 86 *) fail base "unexpected mergeStateStatus $MSS" ;; 87 esac 88 89 # 5. checks 90 RD=$(f '.reviewDecision // ""') 91 case "$RD" in 92 CHANGES_REQUESTED) fail checks "reviewDecision is CHANGES_REQUESTED" ;; 93 REVIEW_REQUIRED) fail checks "reviewDecision is REVIEW_REQUIRED" ;; 94 esac 95 PROT=$($GH api "repos/$FULL/branches/main/protection/required_status_checks" --jq .contexts 2>&1); PRC=$? 96 if [ "$PRC" -ne 0 ]; then 97 if echo "$PROT" | grep -qE '404|Not Found'; then 98 echo "WARNING: $FULL main has no branch protection; no required contexts to verify" >&2 99 skip checks "no branch protection" 100 else 101 fail checks "could not read required checks: $(echo "$PROT" | head -1)" 102 fi 103 else 104 echo "$PROT" | jq -e 'type=="array"' >/dev/null 2>&1 || fail checks "required checks response is not a list" 105 MISSING=$(echo "$PR" | jq -r --argjson req "$PROT" ' 106 (.statusCheckRollup // []) as $r 107 | $req[] as $c 108 | select(([$r[] | select((.name == $c or .context == $c) and (.conclusion == "SUCCESS" or .state == "SUCCESS"))] | length) == 0) 109 | $c') 110 [ -z "$MISSING" ] || fail checks "required check not green: $(echo "$MISSING" | tr '\n' ',' | sed 's/,$//')" 111 pass checks "$(echo "$PROT" | jq 'length') required contexts green, reviewDecision ${RD:-none}" 112 fi 113 114 # 6. merge 115 if [ "$DRY" = 1 ]; then 116 skip merge "dry run" 117 else 118 ARGS=(pr merge "$N" -R "$FULL" --squash --delete-branch --match-head-commit "$HEADOID") 119 NOTE="squash" 120 if [ -n "$ADMIN" ]; then ARGS+=(--admin); NOTE="squash with --admin, authorized by: $ADMIN"; fi 121 MOUT=$($GH "${ARGS[@]}" 2>&1) || fail merge "gh pr merge failed: $(echo "$MOUT" | head -1)" 122 POST=$($GH pr view "$N" -R "$FULL" --json state,mergedAt,mergeCommit 2>&1) || fail merge "merge ran but state could not be re-read" 123 [ "$(echo "$POST" | jq -r .state)" = MERGED ] || fail merge "after merge, state is $(echo "$POST" | jq -r .state), not MERGED" 124 pass merge "$NOTE; merge commit $(echo "$POST" | jq -r '.mergeCommit.oid // "unknown"')" 125 fi 126 127 # 7. queue 128 LIST=$($GH pr list -R "$FULL" --json number,headRefName,mergeStateStatus 2>&1) || fail queue "could not list open PRs: $(echo "$LIST" | head -1)" 129 echo "$LIST" | jq -r --argjson me "$N" '.[] | select(.number != $me) 130 | "QUEUE #\(.number) \(.headRefName) \(.mergeStateStatus)" + (if (.mergeStateStatus == "BEHIND" or .mergeStateStatus == "DIRTY") then " needs scripts/rebase-crumb.sh" else "" end)' 131 pass queue "$(echo "$LIST" | jq --argjson me "$N" '[.[] | select(.number != $me)] | length') other open PRs listed, none rebased" 132 exit 0