diff --git a/pr-gate/PR-GATE.md b/pr-gate/PR-GATE.md index 7a2661a..771483d 100644 --- a/pr-gate/PR-GATE.md +++ b/pr-gate/PR-GATE.md @@ -27,9 +27,24 @@ top never did. link the stack, then mark the PRs ready. `ready_for_review` starts the run. - **PG-D2: In a stack, only the top PR runs.** Its head holds every change of the PRs below it, and a stack merges in one go (`gh stack merge`), so `dev` ends at - the top's tree. A PR is "below" when another open PR (draft or not) is based on - its branch. The checks a lower PR skips report "skipped", with the reason in - the run's summary ("#681 is based on #680's branch…"). + the top's tree. A PR skips only when a **ready** PR is above it: one based on + its branch, or on the branch of a draft that is. The checks a lower PR skips + report "skipped", with the reason in the run's summary ("#681 is based on + #680's branch…"). + - **A draft above covers nothing.** A draft runs nothing (PG-D1), so a PR whose + only PRs above are drafts runs its own checks, and its summary says so + ("Only drafts are above #885 (#886)…"). Before this rule, bepluscloud/ai#885 + reported "skipped" under the draft #886 and merged into `dev` without + console CI. + - **A ready PR above a draft above a PR** (#1 ← draft #2 ← ready #3): #3 is the + top and runs, and its tree holds #2's and #1's changes, so #1 skips ("#3 + (ready) is above #1 in its stack, past the draft #2…") and the draft #2 runs + nothing. The stack cannot merge until #2 is marked ready. Once it is, #3 + above it covers it and it skips. #1 can merge on its own before that, as + any lower PR can (see "A PR merged on its own" below). + - **A top PR turned back into a draft** leaves the PRs below it with their + "skipped". Nothing re-runs them by itself. Push to the PR below, or mark it + ready again, to give it its own run. - **What this gives up:** the commits between the bottom and the top of a stack are not each tested on `dev` (`git bisect` is coarser by a stack). Releases are cut from `dev`'s head, which is what the top tested. diff --git a/pr-gate/action.yml b/pr-gate/action.yml index 62a8ffb..1e6c5c1 100644 --- a/pr-gate/action.yml +++ b/pr-gate/action.yml @@ -1,7 +1,7 @@ name: "beplus PR gate" description: >- Decides whether a pull request's checks run: never on a draft; in a stack, only on the - top PR (its tree holds every change below it); and, given projects or paths, only when + top ready PR (its tree holds every change below it; a draft above covers nothing); and, given projects or paths, only when the stack changes one of them. Every other event runs. One mechanism for every beplus estate's workflows (app-ci, library-ci, docs-pages, db-migrations-pr, infra-diff, and a repository's own). @@ -12,7 +12,9 @@ description: >- # PRs ready. # 2. Only the top PR of a stack runs. Its head holds every change of the PRs below it, # and a stack merges in one go (`gh stack merge`), so dev ends at the top's tree. A -# PR "below" is one another open PR is based on. A PR that leaves the stack has +# PR skips only when a READY PR is above it: based on its branch, or on a draft's +# that is (a ready PR above a draft above it). A draft runs nothing, so drafts +# alone above a PR leave it to run its own checks. A PR that leaves the stack has # nothing above it, so its next run is its own. # 3. Only what the stack changes. With `projects`, Rush's own `--impacted-by` over the # stack's whole diff (from where it left its root branch, never from the PR's own @@ -29,8 +31,9 @@ description: >- inputs: stack: description: >- - `top-only` (the default): a PR another open PR is based on skips; the top PR's run - covers it. `every`: every ready PR runs. + `top-only` (the default): a PR with a ready PR above it (based on its branch, or + on a draft above it) skips; the top PR's run covers it. Drafts alone above a PR do + not cover it, so it runs. `every`: every ready PR runs. required: false default: top-only projects: @@ -114,16 +117,48 @@ runs: echo "::error::the \`stack\` input: \"$STACK\" is not top-only or every."; exit 1 ;; esac + # Only a READY PR above covers this one: a draft runs nothing, so a draft above covers + # nothing. Walk up past drafts: a ready PR above a draft above this one is the stack's + # top and covers it. Drafts alone above: this PR runs its own checks. # A fork's branch is never another PR's base here. if [[ "$STACK" == top-only && "$HEAD_REPO" == "$REPO" ]]; then - above="$(gh api -X GET "repos/$REPO/pulls" -f state=open -f base="$HEAD_REF" -f per_page=20 \ - --jq '[.[] | select(.head.repo.full_name == "'"$REPO"'") | .number] | map(tostring) | join(", #")' 2>/tmp/pr-gate.err)" - if [[ $? -ne 0 ]]; then + level=("$HEAD_REF") seen=" $HEAD_REF " ready="" drafts="" failed="" depth=0 + while [[ ${#level[@]} -gt 0 && $depth -lt 20 ]]; do + depth=$((depth + 1)) next=() these="" + for ref in "${level[@]}"; do + if ! prs="$(gh api -X GET "repos/$REPO/pulls" -f state=open -f base="$ref" -f per_page=20 \ + --jq '.[] | select(.head.repo.full_name == "'"$REPO"'") | "\(.number) \(.draft) \(.head.ref)"' 2>/tmp/pr-gate.err)"; then + failed=1; break 2 + fi + while read -r n draft head; do + [[ -n "$n" ]] || continue + if [[ "$draft" != true ]]; then + ready+="${ready:+, }#$n" + else + these+="${these:+, }#$n" + [[ "$seen" == *" $head "* ]] || { seen+="$head "; next+=("$head"); } + fi + done <<< "$prs" + done + [[ -n "$ready" ]] && break + drafts+="${drafts:+${these:+, }}$these" + level=(${next[@]+"${next[@]}"}) + done + + if [[ -n "$failed" ]]; then echo "::warning title=PR gate::Could not list the PRs above #$NUMBER ($(head -c 200 /tmp/pr-gate.err)); the checks run. Give the workflow \`pull-requests: read\`." - elif [[ -n "$above" ]]; then + elif [[ -n "$ready" && "$depth" -eq 1 ]]; then + out decided true; out run false + out reason "$ready is based on #$NUMBER's branch (above it in its stack), and the top PR's run covers this one." + exit 0 + elif [[ -n "$ready" ]]; then out decided true; out run false - out reason "#$above is based on #$NUMBER's branch (above it in its stack), and the top PR's run covers this one." + out reason "$ready (ready) is above #$NUMBER in its stack, past the draft $drafts, and the top PR's run covers this one." exit 0 + elif [[ -n "$drafts" ]]; then + # The case that let bepluscloud/ai#885 merge untested: draft #886 sat above it. + echo "::notice title=PR gate::Only drafts are above #$NUMBER ($drafts). A draft runs nothing, so #$NUMBER runs its own checks." + out drafts "$drafts" fi fi @@ -161,6 +196,7 @@ runs: RUN: ${{ steps.stack.outputs.run }} REASON: ${{ steps.stack.outputs.reason }} ROOT: ${{ steps.stack.outputs.root }} + DRAFTS: ${{ steps.stack.outputs.drafts }} NUMBER: ${{ github.event.pull_request.number }} HEAD_SHA: ${{ github.event.pull_request.head.sha }} PROJECTS: ${{ inputs.projects }} @@ -177,6 +213,7 @@ runs: } [[ "$DECIDED" == true ]] && decide "$RUN" "$REASON" if [[ -z "$PROJECTS" && -z "$PATHS" ]]; then + [[ -n "${DRAFTS:-}" ]] && decide true "Only drafts are above #$NUMBER ($DRAFTS), and a draft runs nothing: #$NUMBER runs its own checks." decide true "#$NUMBER is the top of its stack (or alone): it runs." fi diff --git a/test/pr-gate/run.sh b/test/pr-gate/run.sh index f062e14..23dfd7a 100755 --- a/test/pr-gate/run.sh +++ b/test/pr-gate/run.sh @@ -16,7 +16,8 @@ yq '.runs.steps[] | select(.id == "decide") | .run' "$action" > "$work/decide.sh always="$(yq '.inputs.always.default' "$action")" # A fake gh: `pulls?base=` answers the PRs based on , `pulls?head=:` the PR -# whose head is , from $work/prs (number base head repo per line). FAIL_GH makes it fail. +# whose head is , from $work/prs (number base head repo [draft] per line; draft defaults to +# false). FAIL_GH makes it fail. mkdir -p "$work/bin" cat > "$work/bin/gh" << 'EOF' #!/usr/bin/env bash @@ -31,10 +32,10 @@ while [[ $# -gt 0 ]]; do done json="[" sep="" -while read -r n b h r; do +while read -r n b h r d; do [[ -z "$n" ]] && continue if { [[ -n "$base" && "$b" == "$base" ]] || [[ -n "$head" && "$h" == "$head" ]]; }; then - json+="$sep{\"number\":$n,\"base\":{\"ref\":\"$b\"},\"head\":{\"ref\":\"$h\",\"repo\":{\"full_name\":\"$r\"}}}" + json+="$sep{\"number\":$n,\"draft\":${d:-false},\"base\":{\"ref\":\"$b\"},\"head\":{\"ref\":\"$h\",\"repo\":{\"full_name\":\"$r\"}}}" sep="," fi done < "$PRS" @@ -45,6 +46,11 @@ chmod +x "$work/bin/gh" export PATH="$work/bin:$PATH" PRS="$work/prs" failures=0 +has() { # + local got + got="$(sed -n "s/^$2=//p" "$4" | tail -n 1)" + if [[ "$got" == *"$3"* ]]; then echo "✓ $1 ($2 has \"$3\")"; else echo "✗ $1: $2 is \"$got\", want it to have \"$3\""; failures=$((failures + 1)); fi +} expect() { # local got got="$(sed -n "s/^$2=//p" "$4" | tail -n 1)" @@ -61,20 +67,50 @@ stack() { # [head re } # The stack: #1 dev <- a, #2 a <- b, #3 b <- c (the top); #4 dev <- x, alone; #5 a fork's PR. +# Drafts above (bepluscloud/ai#885 merged untested under draft #886): +# #6 dev <- p, ready, with only the draft #7 p <- q above it; +# #8 dev <- r, ready, the draft #10 r <- s, and the ready #11 s <- t on top; +# #12 dev <- u, ready, the drafts #13 u <- v and #14 v <- w above it; +# #15 dev <- y, ready, with the draft #16 and the ready #17 both based on y. cat > "$PRS" << 'EOF' 1 dev a o/r 2 a b o/r 3 b c o/r 4 dev x o/r 5 dev c fork/r +6 dev p o/r false +7 p q o/r true +8 dev r o/r false +10 r s o/r true +11 s t o/r false +12 dev u o/r false +13 u v o/r true +14 v w o/r true +15 dev y o/r false +16 y y1 o/r true +17 y y2 o/r false EOF stack "a draft runs nothing" true c b true false stack "the bottom of a stack skips" false a dev true false stack "the middle of a stack skips" false b a true false +has "…and its reason names the PR above" reason "#3 is based on #9's branch" "$work/out" stack "the top of a stack runs, from dev" false c b false dev stack "a PR alone runs, from dev" false x dev false dev stack "a fork's PR is never below another" false c dev false dev fork/r +stack "a ready PR beside a draft above covers it" false y dev true false +has "…and its reason names the ready one" reason "#17 is based on #9's branch" "$work/out" + +stack "a PR with only a draft above runs its own checks" false p dev false dev +expect "…and names the draft" drafts "#7" "$work/out" +stack "the draft above it runs nothing" true q p true false +stack "a PR with two drafts above runs its own checks" false u dev false dev +expect "…and names both" drafts "#13, #14" "$work/out" + +stack "a PR below a draft below a ready top skips" false r dev true false +has "…and its reason names the top past the draft" reason "#11 (ready) is above #9 in its stack, past the draft #10" "$work/out" +stack "the draft in the middle runs nothing" true s r true false +stack "the ready top past a draft runs, from dev" false t s false dev out="$work/out"; : > "$out" GITHUB_OUTPUT="$out" EVENT=push bash "$work/stack.sh" > /dev/null 2>&1 @@ -106,6 +142,11 @@ decide() { # } decide "the top with no project or path runs" "" "" true +: > "$out" +(cd "$repo" && GITHUB_OUTPUT="$out" GITHUB_STEP_SUMMARY=/dev/null DECIDED=false ROOT=dev NUMBER=6 DRAFTS="#7" \ + HEAD_SHA="$head_sha" PROJECTS="" PATHS="" ALWAYS="$always" bash "$work/decide.sh" > /dev/null 2>&1) +expect "a PR with only drafts above runs" run true "$out" +has "…and says why" reason "Only drafts are above #6 (#7)" "$out" decide "a path the stack changes runs" "" "packages/apps/desktop/*" true decide "a path the stack does not change skips" "" "packages/apps/console/*" false decide "projects with no rush.json fail open" "@x/console" "" true