From 994bf2f20b0fa9f51ba561af494233c27858d50f Mon Sep 17 00:00:00 2001 From: "beplus-agent-claude[bot]" <300068699+beplus-agent-claude[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:43:56 +0000 Subject: [PATCH] =?UTF-8?q?feat(pr-gate):=20run=20each=20PR=20check=20once?= =?UTF-8?q?,=20where=20it=20counts=20=E2=80=94=20draft,=20stack=20top,=20R?= =?UTF-8?q?ush=20impact?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A composite action, pr-gate/, decides whether a pull request's checks run: never on a draft; in a stack, only on the top PR (its tree holds every PR below it); and, given Rush projects or paths, only when the stack changes them, by Rush's own --impacted-by from where the stack left its root branch. It fails open. Every reusable workflow takes it as `pr-gate: true`, off by default, so moving v2 changes no caller (pr-gate/PR-GATE.md, PG-D1 to PG-D7). - app-ci: gate in `plan` (the project's impact, or gate-paths); `checks` skips. - library-ci: a `gate` job; the environment and the package diff come from the stack's root, so a stack's top PR runs with dev's secrets. - docs-pages: a `gate` job for pull requests; deploy and package spell out their status, since the skipped gate would otherwise stop every publish. - db-migrations-pr: the database project's impact plus gate-paths. - infra-diff: top-only with gate-paths, else every ready PR. - No reusable workflow names pull-requests: read (asking for more than a caller grants fails it); workflow-wide permissions moved to the jobs. - Caches are restored on a PR and saved only off one; rush-cache.yml saves the one every PR reads, from the default branch, under the checks' keys. - cli: BE_NPM_CHECK probes ONE @beplus package (one request) instead of each dependency (71 in bepluscloud's console, about a minute of every run). - test/pr-gate/run.sh + test-pr-gate.yml: 20 decisions against a fake gh. Co-Authored-By: Igor Lamos --- .github/workflows/app-ci.yml | 67 ++++++- .github/workflows/db-migrations-pr.yml | 64 ++++++- .github/workflows/docs-pages.yml | 47 ++++- .github/workflows/infra-diff.yml | 33 +++- .github/workflows/library-ci.yml | 54 +++++- .github/workflows/rush-cache.yml | 135 ++++++++++++++ .github/workflows/test-pr-gate.yml | 29 +++ README.md | 14 ++ cli/README.md | 20 ++- cli/action.yml | 8 + pr-gate/PR-GATE.md | 135 ++++++++++++++ pr-gate/action.yml | 233 +++++++++++++++++++++++++ scripts/verify-callers.mjs | 3 +- test/pr-gate/run.sh | 126 +++++++++++++ 14 files changed, 933 insertions(+), 35 deletions(-) create mode 100644 .github/workflows/rush-cache.yml create mode 100644 .github/workflows/test-pr-gate.yml create mode 100644 pr-gate/PR-GATE.md create mode 100644 pr-gate/action.yml create mode 100755 test/pr-gate/run.sh diff --git a/.github/workflows/app-ci.yml b/.github/workflows/app-ci.yml index 82feeb6..668ac17 100644 --- a/.github/workflows/app-ci.yml +++ b/.github/workflows/app-ci.yml @@ -126,6 +126,26 @@ on: required: false type: number default: 40 + pr-gate: + description: >- + The PR gate (pr-gate/, PR-GATE.md): a draft runs nothing, only the top PR of a + stack runs (its tree holds the PRs below), and only when the stack impacts + `project` (Rush's --impacted-by, from where the stack left its root branch) or + changes one of `gate-paths`. A skipped `checks` job reports "skipped". Off: + every run runs, as before. Turn it on together with, in the caller: + `types: [opened, synchronize, reopened, ready_for_review]`, no `paths:` filter + (pass them as `gate-paths`), and `pull-requests: read`. + required: false + type: boolean + default: false + gate-paths: + description: >- + With `pr-gate`: globs, one per line (`*` crosses `/`), that run the checks + INSTEAD of the project's Rush impact — for a check that reads a few files + only (an i18n coverage check). Empty: `project`'s impact decides. + required: false + type: string + default: "" secrets: BE_NPM_TOKEN: description: >- @@ -133,9 +153,10 @@ on: an environment secret, `github-environment`) is all a caller passes. required: false -permissions: - contents: read - +# No workflow-wide `permissions`: `plan` takes the caller's token as it is, so the PR gate +# reads the repository's pull requests where the caller grants `pull-requests: read` (and +# fails open, with a warning, where it does not). A called workflow cannot ask for more than +# its caller grants — asking would fail every caller that does not — so it never names it. jobs: # What a caller would otherwise write out, read from the files that already # hold it. Every value comes from the pull request's own tree, so each is @@ -146,6 +167,7 @@ jobs: outputs: directory: ${{ steps.project.outputs.directory }} database: ${{ steps.project.outputs.database }} + run: ${{ steps.gate.outputs.run || 'true' }} steps: - uses: actions/checkout@v4 @@ -231,10 +253,26 @@ jobs: echo "database=$database" } >> "$GITHUB_OUTPUT" + # Last in `plan`: with a project to weigh it checks out the PR's head with its whole + # history (blobless), which the steps above must not read. + - name: PR gate + id: gate + if: inputs.pr-gate + uses: beplus/setup-beplus/pr-gate@v2 + with: + stack: top-only + projects: ${{ inputs.gate-paths == '' && inputs.project || '' }} + paths: ${{ inputs.gate-paths }} + node-version: ${{ inputs.node-version }} + checks: name: checks needs: plan + # The PR gate's answer; without it, always. Skipped reports "skipped", never pending. + if: needs.plan.outputs.run == 'true' runs-on: ubuntu-latest + permissions: + contents: read # Where BE_NPM_TOKEN is an environment secret the job has to claim the # environment to see it, and the environment's branch policy must then admit # every branch the caller runs on. Empty: no environment. @@ -268,8 +306,14 @@ jobs: # Keyed by the project too: `install --to` installs a subset, and # db-migrations-pr.yml saves its own, smaller one under the plain key. - - name: Cache Rush and pnpm - uses: actions/cache@v4 + # + # RESTORED here, SAVED only off a pull request. A cache a PR saves is scoped to that + # PR's ref: no other PR can read it, so saving it (~40 s) bought nothing but the next + # push of the same PR. rush-cache.yml saves the one every PR reads, from the default + # branch (its key is this one). + - name: Restore the Rush and pnpm cache + id: cache + uses: actions/cache/restore@v4 with: path: | common/temp @@ -293,8 +337,9 @@ jobs: # ⚠️ AND PROVE IT WORKED. npm.beplus.cloud answers a read it cannot # authenticate with HTTP 200 and a stub body, never 401, so a token # that did not reach this job fails nothing until pnpm, minutes later. - # This asks the registry for every @beplus package the project depends - # on, and fails here, naming the cause. + # This asks the registry for ONE @beplus package the project depends on + # (one request, about a second) and fails here, naming the cause. A + # package that is not published is the install's to report. BE_NPM_CHECK: ${{ needs.plan.outputs.directory }} - name: Rush install @@ -302,6 +347,14 @@ jobs: PROJECT: ${{ inputs.project }} run: node common/scripts/install-run-rush.js install --to "$PROJECT" + - name: Save the Rush and pnpm cache + if: github.event_name != 'pull_request' && steps.cache.outputs.cache-hit != 'true' + uses: actions/cache/save@v4 + with: + path: | + common/temp + key: ${{ steps.cache.outputs.cache-primary-key }} + - name: Rush build if: inputs.build != 'none' env: diff --git a/.github/workflows/db-migrations-pr.yml b/.github/workflows/db-migrations-pr.yml index 410d674..ca2a0b0 100644 --- a/.github/workflows/db-migrations-pr.yml +++ b/.github/workflows/db-migrations-pr.yml @@ -83,6 +83,28 @@ on: required: false type: string default: "" + pr-gate: + description: >- + The PR gate (pr-gate/, PR-GATE.md): a draft checks nothing, and only the top PR + of a stack checks (its tree holds every PR below it), and only when the stack + impacts the database project (Rush's --impacted-by, from where the stack left its + root branch) or changes one of `gate-paths`. Off: every run the caller's + triggers start runs, as before. Turn it on together with, in the caller: + `types: [opened, synchronize, reopened, ready_for_review]` on `pull_request`, + NO `paths:` filter there (it sees only the top PR's own commits; a `push` + filter may stay), and `pull-requests: read`. + required: false + type: boolean + default: false + gate-paths: + description: >- + With `pr-gate`: globs, one per line (`*` crosses `/`), that run the checks + INSTEAD of the database project's Rush impact — the paths a caller's + `paths:` filter named (beplus.estate.json and the workflows always run them). + Empty: the database project's impact decides. + required: false + type: string + default: "" secrets: BE_NPM_TOKEN: description: >- @@ -90,9 +112,9 @@ on: an environment secret, `github-environment`) is all a caller passes. required: false -permissions: - contents: read - +# No workflow-wide `permissions`: `plan` takes the caller's token as it is, so the PR gate +# reads pull requests where the caller grants `pull-requests: read` (a called workflow +# asking for more than its caller grants would fail every caller that does not). jobs: # What the copies wrote out by hand, read from the manifest that already # holds it. Both values come from the pull request's own tree, so both are @@ -104,6 +126,7 @@ jobs: package: ${{ steps.database.outputs.package }} project: ${{ steps.database.outputs.project }} database: ${{ steps.database.outputs.database }} + run: ${{ steps.gate.outputs.run || 'true' }} steps: - uses: actions/checkout@v4 @@ -155,10 +178,25 @@ jobs: echo "database=$database" } >> "$GITHUB_OUTPUT" + # Last in `plan`: it checks out the PR's head with its history. + - name: PR gate + id: gate + if: inputs.pr-gate + uses: beplus/setup-beplus/pr-gate@v2 + with: + stack: top-only + projects: ${{ inputs.gate-paths == '' && steps.database.outputs.project || '' }} + paths: | + beplus.estate.json + ${{ inputs.gate-paths }} + checks: name: migration integrity + rebuild needs: plan + if: needs.plan.outputs.run == 'true' runs-on: ubuntu-latest + permissions: + contents: read # Where BE_NPM_TOKEN is an environment secret (MRKIT: `dev`) the job has to # claim the environment to see it, and the environment's branch policy must # then admit every branch this gate runs on — it has no `branches` filter @@ -200,8 +238,12 @@ jobs: with: node-version: 22.14 - - name: Cache Rush and pnpm - uses: actions/cache@v4 + # Restored on every run, saved only off a pull request (the push to dev this + # workflow also runs on): a PR's cache is scoped to its own ref, and no other PR + # can read it. + - name: Restore the Rush and pnpm cache + id: cache + uses: actions/cache/restore@v4 with: path: | common/temp @@ -225,13 +267,21 @@ jobs: # authenticate with HTTP 200 and a stub body, never 401, so a token # that did not reach this job fails nothing until pnpm, minutes later, # reports ERR_PNPM_TARBALL_INTEGRITY, then ERR_PNPM_NO_VERSIONS. This - # asks the registry for every @beplus package the database package - # depends on, and fails here, naming the cause. + # asks the registry for ONE @beplus package the database package + # depends on (one request), and fails here, naming the cause. BE_NPM_CHECK: ${{ needs.plan.outputs.package }} - name: Rush install run: node common/scripts/install-run-rush.js install --to ${{ needs.plan.outputs.project }} + - name: Save the Rush and pnpm cache + if: github.event_name != 'pull_request' && steps.cache.outputs.cache-hit != 'true' + uses: actions/cache/save@v4 + with: + path: | + common/temp + key: ${{ steps.cache.outputs.cache-primary-key }} + # ⚠️ INSTALL IS NOT ENOUGH. drizzle-kit reads dist/, including workspace # dependencies' — Mountaineer's @beplus/postmark contributes tables. # Without a build its dist/db is absent, drizzle-kit prints diff --git a/.github/workflows/docs-pages.yml b/.github/workflows/docs-pages.yml index 06e710f..a9ac57f 100644 --- a/.github/workflows/docs-pages.yml +++ b/.github/workflows/docs-pages.yml @@ -120,9 +120,28 @@ on: required: false type: string default: "" + pr-gate: + description: >- + The PR gate (pr-gate/, PR-GATE.md) for a pull request's build: a draft builds + nothing, and only the top PR of a stack builds (its tree holds every PR below it). + A push builds and publishes as before. Off: every pull request builds. Turn it on + together with, in the caller: `types: [opened, synchronize, reopened, + ready_for_review]`, no `branches:`/`paths:` filter on `pull_request` (pass the + paths as `gate-paths`), and `pull-requests: read`. + required: false + type: boolean + default: false + gate-paths: + description: >- + With `pr-gate`: globs, one per line (`*` crosses `/`): a pull request builds only + when its stack changes one (the workflows, rush.json and the like always count). + Empty: every ready top PR builds. + required: false + type: string + default: "" -permissions: - contents: read +# No workflow-wide `permissions`: every job names its own, and `gate` takes the caller's +# token as it is (a called workflow asking for more than its caller grants fails them all). # One deployment at a time, and never cancel one half-done: a partly uploaded site is a broken one. # A pull request's build deploys nothing, so a newer push to it replaces the one in flight. @@ -131,8 +150,24 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: + gate: + name: PR gate + if: inputs.pr-gate && github.event_name == 'pull_request' + runs-on: ubuntu-latest + outputs: + run: ${{ steps.gate.outputs.run }} + steps: + - id: gate + uses: beplus/setup-beplus/pr-gate@v2 + with: + stack: top-only + paths: ${{ inputs.gate-paths }} + build: name: Build the docs site + needs: gate + # Without the gate (off, or not a pull request), always; with it, its answer. + if: ${{ !cancelled() && (needs.gate.result == 'skipped' || needs.gate.outputs.run == 'true') }} runs-on: ubuntu-latest environment: ${{ inputs.environment }} # `pages: read` is for asking Pages where the site will be served, below. @@ -349,7 +384,10 @@ jobs: deploy: name: Publish to GitHub Pages needs: build - if: needs.build.outputs.publish == 'true' + # ⚠️ Status spelled out: `gate`, above `build`, is skipped on every push, and an implicit + # success() counts a skipped job anywhere up the chain as not succeeded — so without + # `!cancelled()` here nothing would ever publish. + if: ${{ !cancelled() && needs.build.result == 'success' && needs.build.outputs.publish == 'true' }} runs-on: ubuntu-latest # Pages needs to write the deployment and to mint the token that proves it came from this run. permissions: @@ -366,7 +404,8 @@ jobs: package: name: Publish the docs package needs: build - if: needs.build.outputs.publish == 'true' && inputs.package + # Status spelled out, as for `deploy`. + if: ${{ !cancelled() && needs.build.result == 'success' && needs.build.outputs.publish == 'true' && inputs.package }} runs-on: ubuntu-latest environment: ${{ inputs.package-environment }} # The site is already up: a package that cannot be published fails here, never the deploy. diff --git a/.github/workflows/infra-diff.yml b/.github/workflows/infra-diff.yml index 829e71d..51bd261 100644 --- a/.github/workflows/infra-diff.yml +++ b/.github/workflows/infra-diff.yml @@ -62,6 +62,24 @@ name: infra diff on: workflow_call: + inputs: + pr-gate: + description: >- + The PR gate (pr-gate/, PR-GATE.md): a draft diffs nothing, and only the top PR of + a stack diffs (cdk diff compares its tree, which holds every PR below it, with + what is deployed), and only when the stack changes one of `gate-paths`. Off, or + on without `gate-paths`: every ready PR the caller's triggers start diffs. + Turn it on together with, in the caller: `types: [opened, synchronize, + reopened, ready_for_review]`, and its `paths:` filter moved into `gate-paths` + (a filter on the caller sees only the top PR's own commits). + required: false + type: boolean + default: false + gate-paths: + description: "With `pr-gate`: globs, one per line (`*` crosses `/`): what the diff reads." + required: false + type: string + default: "" secrets: BE_NPM_TOKEN: description: >- @@ -87,6 +105,7 @@ jobs: packages: ${{ steps.plan.outputs.packages }} region: ${{ steps.estate.outputs.region }} marker: ${{ steps.estate.outputs.marker }} + run: ${{ steps.gate.outputs.run || 'true' }} steps: - uses: actions/checkout@v4 # `beplus infra` reads beplus.estate.json and nothing @@ -140,9 +159,21 @@ jobs: echo "region=$region" >> "$GITHUB_OUTPUT" echo "marker=$marker" >> "$GITHUB_OUTPUT" + # Last in `plan`: with `gate-paths` it checks out the PR's head with its history. + # Without `gate-paths` a stack's lower PR still diffs: the caller's own `paths:` + # filter may have started only it. + - name: PR gate + id: gate + if: inputs.pr-gate + uses: beplus/setup-beplus/pr-gate@v2 + with: + stack: ${{ inputs.gate-paths != '' && 'top-only' || 'every' }} + paths: ${{ inputs.gate-paths }} + diff: name: ${{ matrix.package }} · ${{ matrix.env }} needs: plan + if: needs.plan.outputs.run == 'true' runs-on: ubuntu-latest # Claiming the environment is how the job reaches BE_NPM_TOKEN, which is an # environment secret. On GitHub Free it adds no gate; with Environment @@ -176,7 +207,7 @@ jobs: comment: name: comment needs: [plan, diff] - if: always() && github.event_name == 'pull_request' + if: always() && github.event_name == 'pull_request' && needs.plan.outputs.run == 'true' runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/library-ci.yml b/.github/workflows/library-ci.yml index 8e26166..ed8c284 100644 --- a/.github/workflows/library-ci.yml +++ b/.github/workflows/library-ci.yml @@ -69,6 +69,17 @@ on: required: false type: boolean default: false + pr-gate: + description: >- + The PR gate (pr-gate/, PR-GATE.md): a draft runs nothing, and only the top PR + of a stack runs (its tree holds every PR below it), claiming the environment of + the branch the stack leaves from (dev) rather than its own base. Off: every run + runs, as before. Turn it on together with, in the caller: + `types: [opened, synchronize, reopened, ready_for_review]` and no `branches:` + filter (a stack's top PR is based on the PR below it, never on dev). + required: false + type: boolean + default: false permissions: id-token: write @@ -79,11 +90,29 @@ env: AWS_REGION: us-east-1 jobs: + gate: + name: PR gate + if: inputs.pr-gate + runs-on: ubuntu-latest + outputs: + run: ${{ steps.gate.outputs.run }} + root: ${{ steps.gate.outputs.root }} + steps: + - id: gate + uses: beplus/setup-beplus/pr-gate@v2 + with: + stack: top-only + validate: name: Validate Changes + needs: gate + # Without the gate (skipped), always; with it, its answer. + if: ${{ !cancelled() && (needs.gate.result == 'skipped' || needs.gate.outputs.run == 'true') }} runs-on: ubuntu-latest - environment: ${{ github.base_ref }} + # The environment of the branch the stack leaves from: a stack's top PR is based on + # the PR below it, whose branch is no environment. + environment: ${{ needs.gate.outputs.root || github.base_ref }} env: BE_ENVIRONMENT: ${{ vars.BE_ENVIRONMENT }} @@ -107,15 +136,19 @@ jobs: with: node-version: ${{ inputs.node-version }} - - name: Cache Rush and pnpm + # Restored on every run, saved only off a pull request: a PR's cache is scoped to its + # own ref, so no other PR reads it (rush-cache.yml saves the one they all read). + - name: Restore the Rush and pnpm cache if: inputs.cache - uses: actions/cache@v4 + id: cache + uses: actions/cache/restore@v4 with: path: | common/temp - key: ${{ runner.os }}-rush-${{ hashFiles('common/config/rush/pnpm-lock.yaml', 'rush.json') }} + # rush-cache.yml's plain key (until 2.17.0 it had no arch: one miss, then the same). + key: ${{ runner.os }}-${{ runner.arch }}-rush-${{ hashFiles('common/config/rush/pnpm-lock.yaml', 'rush.json') }} restore-keys: | - ${{ runner.os }}-rush- + ${{ runner.os }}-${{ runner.arch }}-rush- - uses: beplus/setup-beplus/cli@v2 env: @@ -128,6 +161,14 @@ jobs: - name: Install the Dependencies run: node ./common/scripts/install-run-rush.js install + - name: Save the Rush and pnpm cache + if: inputs.cache && github.event_name != 'pull_request' && steps.cache.outputs.cache-hit != 'true' + uses: actions/cache/save@v4 + with: + path: | + common/temp + key: ${{ steps.cache.outputs.cache-primary-key }} + - name: Check for package changes if: inputs.change-file-check id: package-changes @@ -136,7 +177,8 @@ jobs: HAS_CHANGE_FILES=false # Check for changes in packages directory - if git diff origin/${{ github.base_ref }}...HEAD --name-only | grep -q "^packages/"; then + # From the stack's root (the gate's), so a stack's top PR sees every PR below it. + if git diff origin/${{ needs.gate.outputs.root || github.base_ref }}...HEAD --name-only | grep -q "^packages/"; then HAS_PACKAGE_CHANGES=true echo "📦 Package code changes detected" fi diff --git a/.github/workflows/rush-cache.yml b/.github/workflows/rush-cache.yml new file mode 100644 index 0000000..d695ed1 --- /dev/null +++ b/.github/workflows/rush-cache.yml @@ -0,0 +1,135 @@ +# The Rush and pnpm cache every pull request reads. +# -------------------------------------------------- +# A cache is scoped to the ref that saved it. A pull request reads its own, its base +# branch's and the DEFAULT branch's — never another PR's. So the caches app-ci.yml, +# library-ci.yml and db-migrations-pr.yml used to save from a pull request (~40 s each) +# served only that PR's next push; they now only restore on a pull request, and this +# saves the one they all read: +# +# - from the DEFAULT branch, because a stack's upper PRs are based on the PR below +# them and read only their base's caches and the default branch's. In a repository +# whose default branch is `main` while work lands on `dev` (bepluscloud/ai, +# Mountaineer), run it on a schedule, which runs on the default branch, and name +# `ref: dev`: it installs dev's lockfile and saves it in the default branch's scope. +# - under the key the checks restore: with `project`, app-ci.yml's +# (`--rush--`, `rush install --to `); without, +# the plain one db-migrations-pr.yml and library-ci.yml use (`--rush-`, +# a full `rush install`). +# +# A run whose key is already saved does nothing but look (a few seconds). A PR that +# changes the lockfile restores the nearest older cache by prefix, and pnpm fetches the +# difference. +# +# on: +# schedule: +# - cron: "40 4 * * *" +# workflow_dispatch: +# jobs: +# console: +# uses: beplus/setup-beplus/.github/workflows/rush-cache.yml@v2 +# with: +# ref: dev +# project: "@bepluscloud/console" +# github-environment: dev +# secrets: inherit + +name: Rush cache + +on: + workflow_call: + inputs: + ref: + description: "The branch whose lockfile is installed. Default: the ref the caller runs on." + required: false + type: string + default: "" + project: + description: >- + The Rush project app-ci.yml checks: install `--to` it and save under app-ci's key. + Empty: a full `rush install` under the plain key. + required: false + type: string + default: "" + node-version: + description: "Node.js version." + required: false + type: string + default: "22.14" + github-environment: + description: "The environment that holds BE_NPM_TOKEN, where it is an environment secret." + required: false + type: string + default: "" + secrets: + BE_NPM_TOKEN: + description: "npm.beplus.cloud token for the @beplus scope." + required: false + +permissions: + contents: read + +jobs: + save: + name: save ${{ inputs.project || 'rush' }} + runs-on: ubuntu-latest + environment: ${{ inputs.github-environment }} + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ inputs.ref }} + + - uses: actions/setup-node@v6 + with: + node-version: ${{ inputs.node-version }} + + - name: Is it saved already + id: cache + uses: actions/cache/restore@v4 + with: + path: | + common/temp + key: ${{ runner.os }}-${{ runner.arch }}-rush-${{ inputs.project && format('{0}-', inputs.project) || '' }}${{ hashFiles('common/config/rush/pnpm-lock.yaml', 'rush.json') }} + lookup-only: true + + - name: Authenticate the @beplus scope + if: steps.cache.outputs.cache-hit != 'true' + uses: beplus/setup-beplus/cli@v2 + env: + BE_NPM_TARGET: ${{ vars.BE_NPM_TARGET }} + BE_NPM_TOKEN: ${{ secrets.BE_NPM_TOKEN }} + with: + BE_CLI_VERSION: ${{ vars.BE_CLI_VERSION }} + BE_NPM_AUTH: true + + - name: Rush install + if: steps.cache.outputs.cache-hit != 'true' + env: + PROJECT: ${{ inputs.project }} + run: | + set -euo pipefail + if [ -n "$PROJECT" ]; then + node common/scripts/install-run-rush.js install --to "$PROJECT" + else + node common/scripts/install-run-rush.js install + fi + + - name: Save the cache + if: steps.cache.outputs.cache-hit != 'true' + uses: actions/cache/save@v4 + with: + path: | + common/temp + key: ${{ steps.cache.outputs.cache-primary-key }} + + - name: Say what it did + env: + HIT: ${{ steps.cache.outputs.cache-hit }} + KEY: ${{ steps.cache.outputs.cache-primary-key }} + run: | + if [ "$HIT" = true ]; then + echo "### Rush cache: already saved" >> "$GITHUB_STEP_SUMMARY" + else + echo "### Rush cache: saved" >> "$GITHUB_STEP_SUMMARY" + fi + echo "\`$KEY\` in ${GITHUB_REF}'s scope." >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/test-pr-gate.yml b/.github/workflows/test-pr-gate.yml new file mode 100644 index 0000000..5a57057 --- /dev/null +++ b/.github/workflows/test-pr-gate.yml @@ -0,0 +1,29 @@ +# The PR gate's decisions (pr-gate/action.yml), against a fake `gh` and a throwaway git +# repository: drafts, a stack's bottom, middle and top, a lone PR, a fork, a push, paths, +# `always`, and the answers that fail open. See test/pr-gate/run.sh. +name: Test pr-gate + +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + paths: + - pr-gate/** + - test/pr-gate/** + - .github/workflows/test-pr-gate.yml + push: + branches: [main] + paths: + - pr-gate/** + - test/pr-gate/** + - .github/workflows/test-pr-gate.yml + +permissions: + contents: read + +jobs: + decisions: + if: github.event_name != 'pull_request' || !github.event.pull_request.draft + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: test/pr-gate/run.sh diff --git a/README.md b/README.md index 3e3ce0c..1fdd1a4 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,20 @@ Configures Git for secure operations using a GitHub App token. ```yaml - uses: beplus/setup-beplus/git@v2 ``` + +### [`pr-gate`](./pr-gate/PR-GATE.md) +Decides whether a pull request's checks run: never on a draft, only on the top PR +of a stack (its tree holds every PR below it), and only when the stack impacts the +Rush projects (or changes the paths) the check reads. Every reusable workflow below +takes it as `pr-gate: true`, off by default. See [PR-GATE.md](./pr-gate/PR-GATE.md). + +```yaml +- id: gate + uses: beplus/setup-beplus/pr-gate@v2 + with: + projects: "@bepluscloud/console" +``` + ### Reusable workflows The thirteen `beplus/*` library repositories each carried a copy of the same PR diff --git a/cli/README.md b/cli/README.md index a3a4f4d..d936e3f 100644 --- a/cli/README.md +++ b/cli/README.md @@ -23,7 +23,7 @@ Composite GitHub Action that installs [@beplus/be](https://github.com/beplus/be) |------|----------|---------|-------------| | `BE_CLI_VERSION` | No | empty | Version or channel of beplus CLI to install (passed to `be`). Empty installs the repository's pin (`be auto`), or `latest` with a warning when there is none. A value here wins over the pin. | | `BE_NPM_AUTH` | No | `false` | If `true`, runs `beplus npm auth` | -| `BE_NPM_CHECK` | No | empty | Package directories (space- or newline-separated, relative to the workspace) whose `@beplus` dependencies must come back from the registry as real packages. Needs `npm` and `jq`. Empty: no check. | +| `BE_NPM_CHECK` | No | empty | Package directories (space- or newline-separated, relative to the workspace) whose `@beplus` dependencies name the probe: ONE of them must come back from the registry as a real package (the token reached the job and was taken). Needs `npm` and `jq`. Empty: no check. | | `BE_PREFIX` | No | empty | Install prefix for beplus binaries. If empty, defaults to `$HOME/.beplus`. Must be writable. | | `BE_WORKING_DIRECTORY` | No | the workspace | Where the pin is looked up: the nearest `beplus.estate.json` at or above this directory, exactly as `be auto` finds it. Relative to the workspace. | @@ -96,15 +96,17 @@ fork's pull request — fails nothing here: `npm view` prints nothing and exits and pnpm fails minutes later with `ERR_PNPM_TARBALL_INTEGRITY`, then `ERR_PNPM_NO_VERSIONS`. -`BE_NPM_CHECK` closes that gap. After the auth, it asks the registry for every -`@beplus` dependency of the packages you name (`dependencies`, -`devDependencies`, `optionalDependencies`; `workspace:` ones are skipped, and an -npm alias such as `"@beplus/docs-legacy": "npm:@beplus/docs@0.4.2"` is asked for -as the package after `npm:`) — +`BE_NPM_CHECK` closes that gap. After the auth, it asks the registry for ONE +`@beplus` dependency of the packages you name (the first, sorted, of +`dependencies`, `devDependencies`, `optionalDependencies`; `workspace:` ones are +skipped, and an npm alias such as `"@beplus/docs-legacy": "npm:@beplus/docs@0.4.2"` +is asked for as the package after `npm:`) — through the registry Rush hands pnpm (`common/config/rush/.npmrc`), else npm's -own — and fails the step unless each comes back as a real package, naming the -cause: the stub (no token, or a refused one), or npm's own error (a 404 for a -package that is not published). +own — and fails the step unless it comes back as a real package, naming the +cause: the stub (no token, or a refused one), or npm's own error. One request +is enough, because the stub answers every package alike; a dependency that is +simply not published is left to the install to report (until 2.17.0 it asked +for every dependency, a request apiece: about a minute in a large app). ```yaml - uses: actions/setup-node@v6 diff --git a/cli/action.yml b/cli/action.yml index 5f0da60..02398f0 100644 --- a/cli/action.yml +++ b/cli/action.yml @@ -229,6 +229,14 @@ runs: echo "Nothing in @beplus to check ($CHECK)." exit 0 fi + # ONE package proves what this step is for: the token reached the job and the + # registry took it (the stub answers every package alike). Asking for each + # dependency cost a request apiece (71 in bepluscloud's console: a minute of + # every run), and a package that is not published is the install's to report. + if [ "${#wanted[@]}" -gt 1 ]; then + echo "Probing the registry with ${wanted[0]} (one of ${#wanted[@]} @beplus dependencies)." + wanted=("${wanted[0]}") + fi failed=0 for pkg in "${wanted[@]}"; do diff --git a/pr-gate/PR-GATE.md b/pr-gate/PR-GATE.md new file mode 100644 index 0000000..7a2661a --- /dev/null +++ b/pr-gate/PR-GATE.md @@ -0,0 +1,135 @@ +# The PR gate: run each check once, where it counts + +One mechanism, in every beplus estate (bepluscloud/ai, every `beplus/*` library, +MRKIT, Mountaineer), that decides whether a pull request's checks run. Every +reusable workflow here takes it as `pr-gate: true`. A repository's own workflow +adds it as one job (see [Your own workflow](#your-own-workflow)). + +## Why + +Measured on bepluscloud/ai over 11 days (2026-11-05 to 11-16, every run): +console CI was 3,823 minutes of `checks`, about 13 minutes a run. + +| What the run was on | Share of console minutes | +|---|---| +| a PR that changes the console, top of its stack or alone | 53% | +| a PR that changes the console, but **lower in a stack** | 29% | +| a PR lower in a stack that **does not touch the console** | 11% | +| a PR alone or on top that **does not touch the console** (desktop, docs) | 7% | + +So about 47% ran for nothing. The library repositories had the opposite +problem: `ci.yml` ran on PRs into `dev` only, so a stack's bottom PR ran and its +top never did. + +## The rules (PG-D1 to PG-D7; say if one is wrong) + +- **PG-D1: A draft runs nothing.** Agents open drafts, test each branch locally, + 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…"). + - **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. + - **A PR merged on its own** (it leaves the stack, or the stack is merged part + way) has nothing above it, so its next run is its own. It needs a run before + it merges: push to it, or mark it ready again (`beplus gh pr ready --undo`, + then `beplus gh pr ready `). + - **Which PR broke it** is usually clear from the failing test. Otherwise run + that suite on the lower branches locally, as every branch is tested before it + is marked ready. +- **PG-D3: Only what the stack changes.** With `projects`, Rush's own + `rush list --impacted-by git:` decides. That covers dependencies + between workspace projects and the lockfile, project by project: a lockfile + change marks only the projects whose dependencies moved. With `paths`, a + changed file matching one of the globs decides. Either one is enough. +- **PG-D4: The diff is the whole stack's.** It runs from where the stack left its + root branch (`dev`, `main`, `_versions/…`): the gate follows each PR's base down + while an open PR has that branch as its head. Never from the PR's own base, + which for the top of a stack is the PR below it. +- **PG-D5: Files every build reads run everything** (`always`): workflows, + `rush.json`, the root `package.json`, `beplus.estate.json`, Rush's config JSON + and `.npmrc`, and `common/scripts/`. The lockfile is not among them, because + Rush attributes it per project. +- **PG-D6: It fails open.** If GitHub cannot be asked (the caller did not grant + `pull-requests: read`), or Rush cannot answer, the checks run, with a warning + saying why. The gate never stops a check it could not reason about. +- **PG-D7: Skipped inside the workflow, never by `on.*.paths`.** A job skipped by + `if:` reports "skipped", which a required check accepts. A workflow a `paths:` + filter left out reports nothing, and a required check waits forever. A + `paths:` filter also sees only the top PR's own commits, so with PG-D2 it + would skip the one run that covers the stack. **Turning the gate on means + removing the caller's `pull_request` `paths:`/`branches:` filter**, and + passing the paths as `gate-paths` where the workflow takes them. + +## Rolling it out + +`pr-gate` is **off by default in every reusable workflow**, so moving `v2` +changes no caller. Each caller turns it on in one change: + +```yaml +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] # PG-D1 + # no `paths:` and no `branches:` (PG-D7) + +permissions: + contents: read + pull-requests: read # PG-D2: which PRs are based on this one + +jobs: + console: + uses: beplus/setup-beplus/.github/workflows/app-ci.yml@v2 + with: + project: "@bepluscloud/console" + pr-gate: true # draft, top of the stack, and @bepluscloud/console's impact + # gate-paths: | # instead of the project's impact, for a check that reads a few files + # packages/apps/console/app/* +``` + +| Workflow | What `pr-gate: true` decides by | +|---|---| +| `app-ci.yml` | draft, top-only, `project`'s Rush impact (or `gate-paths` instead) | +| `library-ci.yml` | draft, top-only. Claims the environment of the stack's root, not the PR's base | +| `docs-pages.yml` | draft, top-only, and `gate-paths` when given, for pull requests (a push builds and publishes as before) | +| `db-migrations-pr.yml` | draft, top-only, the database project's Rush impact (or `gate-paths` instead) | +| `infra-diff.yml` | draft, top-only with `gate-paths` (else every ready PR, as its `paths:` caller had it) | + +A called workflow cannot ask for more than its caller grants, and asking fails +every caller that does not grant it. So none of these names +`pull-requests: read`. Where a workflow had a workflow-wide `permissions`, that +block moved to the jobs, and the gate's job takes the caller's token as it is. + +## Your own workflow + +```yaml +jobs: + gate: + runs-on: ubuntu-latest + outputs: + run: ${{ steps.gate.outputs.run }} + steps: + - id: gate + uses: beplus/setup-beplus/pr-gate@v2 + with: + projects: "@mcl/console" # or paths:, or neither (draft + top-only) + test: + needs: gate + if: needs.gate.outputs.run == 'true' + # … +``` + +A job further down that `needs: test` and has an `if:` of its own must spell out +its status (`!cancelled() && needs.test.result == 'success' && …`). An implicit +`success()` treats a skipped job anywhere up the chain as not succeeded. + +## The cache every PR reads + +A cache is scoped to the ref that saved it. A PR reads its own, its base's and +the default branch's, never another PR's. Saving from a PR (about 40 s a run) +therefore served only that PR's next push. `app-ci.yml`, `library-ci.yml` and +`db-migrations-pr.yml` now **restore on a PR and save only off one**, and +[`rush-cache.yml`](../.github/workflows/rush-cache.yml) saves the one they all +read, from the default branch, under the key they restore. diff --git a/pr-gate/action.yml b/pr-gate/action.yml new file mode 100644 index 0000000..62a8ffb --- /dev/null +++ b/pr-gate/action.yml @@ -0,0 +1,233 @@ +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 + 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). + +# Why (pr-gate/PR-GATE.md has the whole of it): +# +# 1. A draft runs nothing. Agents open drafts, test locally, link a stack, then mark the +# 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 +# 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 +# base, which for the top of a stack is the PR below) says whether any of them is +# impacted — through its dependencies and the lockfile, as Rush computes it. With +# `paths`, a changed file matching one of them. Files in `always` run everything. +# +# It fails open: if GitHub cannot be asked (no `pull-requests: read`), or Rush cannot +# answer, the checks run, with a warning saying why. +# +# A job skipped by `if:` reports "skipped", which a required check accepts; a workflow a +# `paths:` filter left out reports nothing. So filter here, never with `on.*.paths`. + +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. + required: false + default: top-only + projects: + description: >- + Rush projects, space- or newline-separated. Run only when the stack impacts one of + them (`rush list --impacted-by git:`). Empty: no project test. + required: false + default: "" + paths: + description: >- + Globs, one per line (bash patterns; `*` crosses `/`). Run when a changed file + matches one. With `projects` too, either one is enough. Empty: no path test. + required: false + default: "" + always: + description: >- + Globs that run the checks whatever else changed: what every project's build reads. + Only consulted when `projects` or `paths` is set. + required: false + default: | + .github/workflows/* + .github/actions/* + rush.json + package.json + beplus.estate.json + common/config/rush/*.json + common/config/rush/.npmrc* + common/scripts/* + node-version: + description: "Node.js for Rush, with `projects` (rush.json's nodeSupportedVersionRange holds it)." + required: false + default: "22.14" + github-token: + description: "A token that reads the repository's pull requests (`pull-requests: read`)." + required: false + default: ${{ github.token }} + +outputs: + run: + description: "`true` when the checks should run, `false` when they skip." + value: ${{ steps.decide.outputs.run }} + reason: + description: "Why, in a sentence." + value: ${{ steps.decide.outputs.reason }} + root: + description: "The branch the stack leaves from (`dev`, `main`, `_versions/…`), on a pull request." + value: ${{ steps.stack.outputs.root }} + +runs: + using: composite + steps: + # Draft, the stack's top, and the stack's root: GitHub's answers alone, no checkout. + - name: Where this PR stands in its stack + id: stack + shell: bash + env: + GH_TOKEN: ${{ inputs.github-token }} + EVENT: ${{ github.event_name }} + REPO: ${{ github.repository }} + OWNER: ${{ github.repository_owner }} + DRAFT: ${{ github.event.pull_request.draft }} + NUMBER: ${{ github.event.pull_request.number }} + HEAD_REF: ${{ github.event.pull_request.head.ref }} + HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }} + BASE_REF: ${{ github.event.pull_request.base.ref }} + STACK: ${{ inputs.stack }} + run: | + set -uo pipefail + out() { echo "$1=$2" >> "$GITHUB_OUTPUT"; } + + if [[ "$EVENT" != pull_request && "$EVENT" != pull_request_target ]]; then + out decided true; out run true; out reason "Not a pull request ($EVENT): it runs." + exit 0 + fi + if [[ "$DRAFT" == true ]]; then + out decided true; out run false + out reason "#$NUMBER is a draft: nothing runs until it is marked ready." + exit 0 + fi + case "$STACK" in top-only | every) ;; *) + echo "::error::the \`stack\` input: \"$STACK\" is not top-only or every."; exit 1 ;; + esac + + # 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 + 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 + 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." + exit 0 + fi + fi + + # The stack's root: follow each base down while an open PR has it as its head. + root="$BASE_REF" + for _ in $(seq 1 20); do + below="$(gh api -X GET "repos/$REPO/pulls" -f state=open -f head="$OWNER:$root" -f per_page=1 --jq '.[0].base.ref // empty' 2>/dev/null)" || break + [[ -n "$below" && "$below" != "$root" ]] || break + root="$below" + done + out root "$root" + out decided false + + # What the stack changes, from where it left its root, by git and Rush. + - name: Fetch the stack's history + if: steps.stack.outputs.decided == 'false' && (inputs.projects != '' || inputs.paths != '') + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + fetch-depth: 0 + filter: blob:none + persist-credentials: false + + - name: Node for Rush + if: steps.stack.outputs.decided == 'false' && inputs.projects != '' + uses: actions/setup-node@v6 + with: + node-version: ${{ inputs.node-version }} + + - name: Decide + id: decide + shell: bash + env: + DECIDED: ${{ steps.stack.outputs.decided }} + RUN: ${{ steps.stack.outputs.run }} + REASON: ${{ steps.stack.outputs.reason }} + ROOT: ${{ steps.stack.outputs.root }} + NUMBER: ${{ github.event.pull_request.number }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + PROJECTS: ${{ inputs.projects }} + PATHS: ${{ inputs.paths }} + ALWAYS: ${{ inputs.always }} + run: | + set -uo pipefail + decide() { + echo "run=$1" >> "$GITHUB_OUTPUT" + echo "reason=$2" >> "$GITHUB_OUTPUT" + if [[ "$1" == true ]]; then echo "▶ $2"; else echo "⏭ $2"; fi + { echo "### PR gate: $([[ "$1" == true ]] && echo runs || echo skipped)"; echo; echo "$2"; } >> "$GITHUB_STEP_SUMMARY" + exit 0 + } + [[ "$DECIDED" == true ]] && decide "$RUN" "$REASON" + if [[ -z "$PROJECTS" && -z "$PATHS" ]]; then + decide true "#$NUMBER is the top of its stack (or alone): it runs." + fi + + if ! git fetch --quiet --no-tags --filter=blob:none origin "+refs/heads/$ROOT:refs/remotes/origin/$ROOT"; then + echo "::warning title=PR gate::Could not fetch $ROOT; the checks run." + decide true "#$NUMBER: its root branch $ROOT could not be fetched, so it runs." + fi + fork="$(git merge-base "$HEAD_SHA" "origin/$ROOT")" || decide true "#$NUMBER shares no history with $ROOT, so it runs." + mapfile -t changed < <(git diff --name-only "$fork" "$HEAD_SHA") + echo "The stack changes ${#changed[@]} file(s) since it left $ROOT at ${fork:0:8}." + + # Globs, one per line; `*` crosses `/` in [[ == ]]. + matches() { + local list="$1" f g + for f in "${changed[@]}"; do + while IFS= read -r g || [[ -n "$g" ]]; do + g="${g#"${g%%[![:space:]]*}"}"; g="${g%"${g##*[![:space:]]}"}" + [[ -n "$g" && "${g:0:1}" != "#" ]] || continue + # shellcheck disable=SC2053 + if [[ "$f" == $g ]]; then echo "$f"; return 0; fi + done <<< "$list" + done + return 1 + } + if hit="$(matches "$ALWAYS")"; then + decide true "#$NUMBER changes $hit, which every check reads: it runs." + fi + if [[ -n "$PATHS" ]] && hit="$(matches "$PATHS")"; then + decide true "#$NUMBER changes $hit: it runs." + fi + + if [[ -n "$PROJECTS" ]]; then + if [[ ! -f rush.json ]]; then + echo "::warning title=PR gate::\`projects\` names Rush projects, but there is no rush.json; the checks run." + decide true "#$NUMBER: no rush.json to read the projects from, so it runs." + fi + json="$(node common/scripts/install-run-rush.js list --impacted-by "git:$fork" --json 2>/tmp/pr-gate.err | sed -n '/^{/,$p')" + impacted="$(printf '%s' "$json" | node -e ' + let s = ""; process.stdin.on("data", (d) => (s += d)).on("end", () => { + try { process.stdout.write(JSON.parse(s).projects.map((p) => p.name).join(" ")); } + catch { process.exit(1); } + });')" || { + echo "::warning title=PR gate::rush list --impacted-by failed ($(tail -c 300 /tmp/pr-gate.err)); the checks run." + decide true "#$NUMBER: Rush could not say what it impacts, so it runs." + } + echo "Impacted: ${impacted:-nothing}." + for p in $PROJECTS; do + for i in $impacted; do + [[ "$p" == "$i" ]] && decide true "#$NUMBER's stack impacts $p: it runs." + done + done + decide false "#$NUMBER's stack impacts none of $(echo $PROJECTS) (it impacts ${impacted:-nothing}): skipped." + fi + decide false "#$NUMBER's stack changes none of the paths this check reads: skipped." diff --git a/scripts/verify-callers.mjs b/scripts/verify-callers.mjs index 125d9ca..360af74 100755 --- a/scripts/verify-callers.mjs +++ b/scripts/verify-callers.mjs @@ -110,7 +110,8 @@ const libraries = (ORG) => { * anything else leaves it in, with its own runtime condition intact. */ const enabledSteps = (workflow, withInputs) => { - const job = Object.values(workflow.jobs)[0]; + // The work's job, not the PR gate's (`gate`, pr-gate/PR-GATE.md), which only decides. + const job = Object.entries(workflow.jobs).find(([name]) => name !== 'gate')[1]; return job.steps .filter((step) => { const named = [...String(step.if ?? '').matchAll(/inputs\.([a-z-]+)/g)].map((m) => m[1]); diff --git a/test/pr-gate/run.sh b/test/pr-gate/run.sh new file mode 100755 index 0000000..f062e14 --- /dev/null +++ b/test/pr-gate/run.sh @@ -0,0 +1,126 @@ +#!/usr/bin/env bash +# The PR gate's two scripts (pr-gate/action.yml: `stack`, `decide`), run against a fake `gh` +# and a throwaway git repository. Needs bash ≥ 4, git and yq. Rush's impact is not run here +# (it needs a Rush repository); the paths, `always` and the fail-open answers are. +# +# test/pr-gate/run.sh +set -uo pipefail + +here="$(cd "$(dirname "$0")" && pwd)" +action="$here/../../pr-gate/action.yml" +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT + +yq '.runs.steps[] | select(.id == "stack") | .run' "$action" > "$work/stack.sh" +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. +mkdir -p "$work/bin" +cat > "$work/bin/gh" << 'EOF' +#!/usr/bin/env bash +[[ -n "${FAIL_GH:-}" ]] && { echo "HTTP 403: Resource not accessible by integration" >&2; exit 1; } +base="" head="" jq="" +while [[ $# -gt 0 ]]; do + case "$1" in + -f) case "$2" in base=*) base="${2#base=}" ;; head=*) head="${2#*:}" ;; esac; shift 2 ;; + --jq) jq="$2"; shift 2 ;; + *) shift ;; + esac +done +json="[" +sep="" +while read -r n b h r; 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\"}}}" + sep="," + fi +done < "$PRS" +json+="]" +printf '%s' "$json" | jq -r "$jq" +EOF +chmod +x "$work/bin/gh" +export PATH="$work/bin:$PATH" PRS="$work/prs" + +failures=0 +expect() { # + local got + got="$(sed -n "s/^$2=//p" "$4" | tail -n 1)" + if [[ "$got" == "$3" ]]; then echo "✓ $1 ($2=$got)"; else echo "✗ $1: $2 is \"$got\", want \"$3\""; failures=$((failures + 1)); fi +} + +stack() { # [head repo] + local out="$work/out" + : > "$out" + GITHUB_OUTPUT="$out" EVENT=pull_request REPO=o/r OWNER=o DRAFT="$2" NUMBER=9 HEAD_REF="$3" \ + HEAD_REPO="${7:-o/r}" BASE_REF="$4" STACK=top-only bash "$work/stack.sh" > /dev/null 2>&1 + expect "$1" decided "$5" "$out" + if [[ "$5" == true ]]; then expect "$1" run "$6" "$out"; else expect "$1" root "$6" "$out"; fi +} + +# The stack: #1 dev <- a, #2 a <- b, #3 b <- c (the top); #4 dev <- x, alone; #5 a fork's PR. +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 +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 +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 + +out="$work/out"; : > "$out" +GITHUB_OUTPUT="$out" EVENT=push bash "$work/stack.sh" > /dev/null 2>&1 +expect "a push runs" run true "$out" + +: > "$out" +FAIL_GH=1 GITHUB_OUTPUT="$out" EVENT=pull_request REPO=o/r OWNER=o DRAFT=false NUMBER=1 HEAD_REF=a \ + HEAD_REPO=o/r BASE_REF=dev STACK=top-only bash "$work/stack.sh" > /dev/null 2>&1 +expect "GitHub refusing the list fails open" decided false "$out" + +# Decide: a repository whose dev has one commit, and a stack two commits ahead of it. +repo="$work/repo" +git init -q -b dev "$repo" +git -C "$repo" -c user.email=t@t -c user.name=t commit -q --allow-empty -m root +git -C "$repo" update-ref refs/remotes/origin/dev HEAD +git -C "$repo" checkout -q -b c +mkdir -p "$repo/packages/apps/desktop" "$repo/.github/workflows" +echo a > "$repo/packages/apps/desktop/main.ts" +git -C "$repo" add -A && git -C "$repo" -c user.email=t@t -c user.name=t commit -q -m desktop +head_sha="$(git -C "$repo" rev-parse HEAD)" +# `git fetch origin` in decide: point origin at the repository itself. +git -C "$repo" remote add origin "$repo" + +decide() { # + : > "$out" + (cd "$repo" && GITHUB_OUTPUT="$out" GITHUB_STEP_SUMMARY=/dev/null DECIDED=false ROOT=dev NUMBER=9 \ + HEAD_SHA="$head_sha" PROJECTS="$2" PATHS="$3" ALWAYS="$always" bash "$work/decide.sh" > /dev/null 2>&1) + expect "$1" run "$4" "$out" +} + +decide "the top with no project or path runs" "" "" true +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 + +echo b > "$repo/.github/workflows/ci.yml" +git -C "$repo" add -A && git -C "$repo" -c user.email=t@t -c user.name=t commit -q -m workflow +head_sha="$(git -C "$repo" rev-parse HEAD)" +decide "a workflow change runs whatever the paths" "" "packages/apps/console/*" true + +: > "$out" +GITHUB_OUTPUT="$out" GITHUB_STEP_SUMMARY=/dev/null DECIDED=true RUN=false REASON=draft bash "$work/decide.sh" > /dev/null 2>&1 +expect "an answer the stack step gave stands" run false "$out" + +if [[ "$failures" -gt 0 ]]; then + echo "$failures case(s) failed." + exit 1 +fi +echo "All cases passed."