From 36082f9a8cd11e6fbd25fef5c28615407dd70382 Mon Sep 17 00:00:00 2001 From: Chester Sim Date: Tue, 15 Sep 2026 14:10:59 +0800 Subject: [PATCH 1/2] fix(dependency-gate): compare against the merge base, not the base branch tip `github.event.pull_request.base.sha` is the tip of the base branch at event time, not the merge base. Diffing a pull request's lockfile against the tip replays every base-branch commit made since the branch forked, inverted: what the base branch dropped reads as something the pull request added. A branch that is merely behind gets findings for packages it never touched, and the only way through is a `deps-reviewed` label signing off on nothing real. velocity-v1#511 hit this. It changes no `bun.lock`, but was five commits behind master, and master had re-resolved the tree: `@types/protobufjs` disappeared from the lockfile and the hoisted `@velocity-exchange/common/@velocity-exchange/sdk` entry collapsed. Against the tip those two read as "new package" and "source changed". Running the parser over the real blobs reproduces the run's findings and its digest exactly with base = tip, and reports nothing with base = merge base. Resolve the merge base through the compare API rather than `git merge-base`. The head commit is not in the checkout on a fork pull request -- actions/checkout fetches refs/heads/* and refs/tags/* of the base repository only -- so `git merge-base` would fail exactly where the gate matters most. The API needs nothing fetched and only the `contents: read` the action already requires. The merge base is an ancestor of base.sha, so it is still base-branch content: the "never trust pull request content" invariant is unchanged. The existing tip candidates stay on as fallbacks behind it, so a shallow checkout or a rewritten base degrades to the old over-reporting behaviour instead of failing the run. Both `prt` and `pr` modes had the bug and both are fixed. --- dependency-gate/README.md | 6 ++++-- dependency-gate/action.yml | 34 +++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/dependency-gate/README.md b/dependency-gate/README.md index 566b42a..3ffdb04 100644 --- a/dependency-gate/README.md +++ b/dependency-gate/README.md @@ -57,8 +57,10 @@ jobs: lockfiles: bun.lock ui/bun.lock ``` -`fetch-depth: 0` is required: the action compares against the base commit, which a -shallow clone does not contain. +`fetch-depth: 0` is required: the action compares against the **merge base** of the +pull request and its target branch, which a shallow clone does not contain. The merge +base, not the target branch's tip, is what keeps a branch that has fallen behind from +being blamed for dependency changes that landed on the target branch after it forked. In this mode the action emits a `::warning::` on every run, because the workflow it is running from is the pull request's own copy. That is not paranoia, it is the residual diff --git a/dependency-gate/action.yml b/dependency-gate/action.yml index 479dd18..0f55db5 100644 --- a/dependency-gate/action.yml +++ b/dependency-gate/action.yml @@ -68,10 +68,31 @@ runs: HEAD_SHA: ${{ github.event.pull_request.head.sha }} PUSH_BEFORE: ${{ github.event.before }} REQUIRE_PRT: ${{ inputs.require-pull-request-target }} + GH_TOKEN: ${{ inputs.token }} + REPO: ${{ github.repository }} run: | set -euo pipefail resolve() { git rev-parse --verify --quiet "$1^{commit}"; } + # base.sha is the base branch *tip*, not the merge base. Diffing against the + # tip replays every base-branch commit made since the fork point, inverted: + # what the base branch dropped reads as something the pull request added. A + # branch that is merely behind then gets findings for packages it never + # touched. + # + # The compare API answers this without the head commit being in the checkout, + # which for a fork's head it never is: actions/checkout fetches refs/heads/* + # and refs/tags/* of the base repository only, so `git merge-base` would fail + # there. The merge base is an ancestor of base.sha, so it is still + # base-branch content and nothing of the pull request's is trusted. + merge_base() { + [ -n "${REPO:-}" ] || return 1 + [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]] || return 1 + [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]] || return 1 + gh api "repos/$REPO/compare/$BASE_SHA...$HEAD_SHA" \ + --jq '.merge_base_commit.sha' 2>/dev/null + } + # prt: the workflow comes from the base branch and the pull request's files are # never checked out. This is the mode to run in. # pr: the workflow is the pull request's own copy, so it can be edited in the @@ -104,15 +125,18 @@ runs: base="" case "$mode" in prt) - # Every candidate here is base-branch content. The last one is the tip of - # the base branch, which is what actions/checkout leaves in the tree under - # pull_request_target, so a default shallow checkout still works. - base=$(resolve "$BASE_SHA" || resolve "origin/$GITHUB_BASE_REF" || resolve HEAD || true) + # Every candidate here is base-branch content. The tip candidates behind + # the merge base keep a shallow checkout working, and are what + # actions/checkout leaves in the tree under pull_request_target; they + # over-report on a branch that is behind rather than failing the run. + mb=$(merge_base || true) + base=$(resolve "${mb:-}" || resolve "$BASE_SHA" || resolve "origin/$GITHUB_BASE_REF" || resolve HEAD || true) ;; pr) # Not HEAD here: under pull_request that is the merge commit, which # contains the pull request's own changes. - base=$(resolve "$BASE_SHA" || resolve "origin/$GITHUB_BASE_REF" || true) + mb=$(merge_base || true) + base=$(resolve "${mb:-}" || resolve "$BASE_SHA" || resolve "origin/$GITHUB_BASE_REF" || true) ;; *) if [ -n "${PUSH_BEFORE:-}" ] && [ "$PUSH_BEFORE" != "0000000000000000000000000000000000000000" ]; then From 29d14531e5e1b21bfdf79f53f261874fdc7a7277 Mon Sep 17 00:00:00 2001 From: Chester Sim Date: Tue, 15 Sep 2026 14:23:15 +0800 Subject: [PATCH 2/2] fix(dependency-gate): keep the base tip in pull_request mode, harden the API call Review found that the previous commit was right for `prt` and wrong for `pr`. The two modes read the head lockfiles from different places. `prt` reads them through the contents API at `HEAD_SHA`, a real commit on the pull request's branch, so the merge base is the correct other side. `pr` reads them off the workspace, which under `pull_request` is GitHub's merge commit (`refs/pull/N/merge`, "Merge into "). That tree already carries the base branch through `base.sha`, so comparing it to the merge base attributes the base branch's own dependency changes to the pull request -- the same class of false finding this branch set out to remove, in the opposite direction. `pr` mode never had the bug for that reason, and is restored to `BASE_SHA`. Also harden the helper, both from the same review: - Validate the compare API's answer with the same regex as its inputs. `gh api` writes its error body to *stdout* and exits 1, and `--jq` yields "null" for a missing field, so `mb` could hold a non-empty non-SHA and the `${mb:-}` guard would never fire. `git rev-parse --verify --quiet` rejected those anyway, so this changes no behaviour; it stops the one externally-sourced string in the file reaching git unchecked. - Warn when the API call fails instead of degrading silently. The fallback still gates, but it over-reports on a branch that is behind, and over-reporting changes the digest, which deletes a sign-off label that was correctly applied. A network blip should not do that without saying so. Document the residual behaviour change under "Known limits": a package the target branch removed after the fork point, which the branch still carries, is no longer reported. That is what makes the gate agree with the three-dot diff a reviewer reads, and the removal wins on merge in any case. --- dependency-gate/README.md | 18 ++++++++++++++---- dependency-gate/action.yml | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/dependency-gate/README.md b/dependency-gate/README.md index 3ffdb04..530ced7 100644 --- a/dependency-gate/README.md +++ b/dependency-gate/README.md @@ -57,10 +57,8 @@ jobs: lockfiles: bun.lock ui/bun.lock ``` -`fetch-depth: 0` is required: the action compares against the **merge base** of the -pull request and its target branch, which a shallow clone does not contain. The merge -base, not the target branch's tip, is what keeps a branch that has fallen behind from -being blamed for dependency changes that landed on the target branch after it forked. +`fetch-depth: 0` is required: the action compares against the base commit, which a +shallow clone does not contain. In this mode the action emits a `::warning::` on every run, because the workflow it is running from is the pull request's own copy. That is not paranoia, it is the residual @@ -91,6 +89,13 @@ jobs: Set `require-pull-request-target: "true"` at the same time. It makes the action fail if it is ever triggered by anything else, so the trigger cannot be quietly downgraded. +`fetch-depth: 0` is required here too, and for a second reason. In this mode the action +compares against the **merge base** of the pull request and its target branch, not the +target branch's tip, which is what keeps a branch that has fallen behind from being +blamed for dependency changes that landed on the target branch after it forked. Step 1 +needs no such adjustment: `pull_request` hands the action GitHub's merge commit, which +already carries the target branch, so there the tip is the correct comparison. + **Why both modes exist.** Under `pull_request_target` the workflow is read from the base branch, so a pull request that *introduces* the gate does not run it at all: there is no gate on the base branch yet to run. Shipping `pull_request_target` alone would mean the @@ -224,6 +229,11 @@ Known limits, stated rather than hidden: - `pnpm-lock.yaml` records no per-package registry in v9, so a registry swap made only in `.npmrc` is invisible here. Tarball and git overrides in a `resolution:` block *are* read. +- A package the target branch has **removed** since the fork point, which this branch + still carries, is not reported. Comparing against the merge base is what makes the + gate agree with the three-dot diff a reviewer reads, and the target branch's removal + wins on merge anyway, unless the two sides edited the same lockfile hunks, in which + case the merge conflicts and the author resolves it by hand. ## Supported lockfiles diff --git a/dependency-gate/action.yml b/dependency-gate/action.yml index 0f55db5..c7968ef 100644 --- a/dependency-gate/action.yml +++ b/dependency-gate/action.yml @@ -74,11 +74,17 @@ runs: set -euo pipefail resolve() { git rev-parse --verify --quiet "$1^{commit}"; } - # base.sha is the base branch *tip*, not the merge base. Diffing against the - # tip replays every base-branch commit made since the fork point, inverted: - # what the base branch dropped reads as something the pull request added. A - # branch that is merely behind then gets findings for packages it never - # touched. + # For prt only. base.sha is the base branch *tip*, not the merge base, and prt + # reads the head lockfiles at HEAD_SHA, a real commit off the pull request's + # branch. Diffing that against the tip replays every base-branch commit made + # since the fork point, inverted: what the base branch dropped reads as + # something the pull request added, so a branch that is merely behind gets + # findings for packages it never touched. + # + # pr is the other shape and wants the tip. There the head lockfiles come from + # the workspace, which is GitHub's merge commit, already carrying the base + # branch through base.sha; the merge base would blame the pull request for the + # base branch's own changes. # # The compare API answers this without the head commit being in the checkout, # which for a fork's head it never is: actions/checkout fetches refs/heads/* @@ -89,8 +95,19 @@ runs: [ -n "${REPO:-}" ] || return 1 [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ ]] || return 1 [[ "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]] || return 1 - gh api "repos/$REPO/compare/$BASE_SHA...$HEAD_SHA" \ - --jq '.merge_base_commit.sha' 2>/dev/null + local sha + if ! sha=$(gh api "repos/$REPO/compare/$BASE_SHA...$HEAD_SHA" \ + --jq '.merge_base_commit.sha' 2>/dev/null); then + # Not fatal, the candidates below still gate. Say so anyway: falling back + # to the tip resurrects the over-reporting, which changes the digest, which + # drops a sign-off label that was correctly applied. + echo "::warning::Could not reach the compare API to resolve the merge base; falling back to the base branch tip, which over-reports on a branch that is behind." >&2 + return 1 + fi + # gh writes its error body to stdout, and --jq yields "null" for a missing + # field, so the answer is checked the same way the inputs are. + [[ "$sha" =~ ^[0-9a-f]{40}$ ]] || return 1 + printf '%s\n' "$sha" } # prt: the workflow comes from the base branch and the pull request's files are @@ -134,9 +151,10 @@ runs: ;; pr) # Not HEAD here: under pull_request that is the merge commit, which - # contains the pull request's own changes. - mb=$(merge_base || true) - base=$(resolve "${mb:-}" || resolve "$BASE_SHA" || resolve "origin/$GITHUB_BASE_REF" || true) + # contains the pull request's own changes. The tip, not the merge base: + # the collect step reads this mode's head lockfiles out of that merge + # commit, so the base branch is already on both sides of the comparison. + base=$(resolve "$BASE_SHA" || resolve "origin/$GITHUB_BASE_REF" || true) ;; *) if [ -n "${PUSH_BEFORE:-}" ] && [ "$PUSH_BEFORE" != "0000000000000000000000000000000000000000" ]; then