Skip to content

Commit 361748e

Browse files
committed
fix(ci): build api-gate detector from base tip when merge base predates it
The API Review Gate builds cmd/api-gate from the PR's merge-base checkout so a PR cannot neuter the detector by editing its own code. When the merge base predates the commit that introduced the gate, cmd/api-gate is absent from both the merge base and the equally old head, so the head fallback also fails to build and the required check errors out before evaluating any API content. This blocks every open PR whose merge base is older than the gate, and neither a re-run nor an approving review clears it. Add the base branch tip as an intermediate build source: prefer the merge-base checkout, fall back to the base tip when the merge base lacks the tool, and bootstrap from head only while the tool exists nowhere on the base branch yet. The base tip is a protected branch, not PR-authored, so it preserves the "analyze head as data, not code" property. The API surface is still diffed against the merge base; only where the detector binary is compiled changes. This unblocks the backlog without forcing every affected PR to rebase. The selection stays inline in the workflow rather than in a helper script: a helper would be read from the untrusted PR head, which both reintroduces the failure for heads that predate the helper and lets a PR steer the selection away from the trusted build. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 1baf126 commit 361748e

1 file changed

Lines changed: 22 additions & 8 deletions

File tree

‎.github/workflows/api-review-gate.yaml‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,30 @@ jobs:
6969
- name: Build api-gate
7070
run: |
7171
set -euo pipefail
72-
# Prefer building the detector from the trusted base checkout so a PR
73-
# cannot neuter the gate by editing its own detector code — the tool
74-
# analyzes the head as *data*, not as code. Fall back to the head
75-
# checkout only while the tool has not yet landed on the base branch
76-
# (bootstrap); in that window CODEOWNERS on cmd/api-gate,
77-
# internal/apigate, and this workflow keeps tampering behind an
78-
# API-owner review.
72+
# Build the detector from a trusted checkout so a PR cannot neuter the
73+
# gate by editing its own detector code — the tool analyzes the head as
74+
# *data*, not as code. This selection stays inline in the workflow (an
75+
# API-owner-protected file that is always present) rather than in a
76+
# helper script: a helper would be read from the untrusted PR head,
77+
# which both breaks PRs whose head predates the helper and lets a PR
78+
# steer the selection.
79+
#
80+
# Precedence: prefer the merge-base checkout; when the merge base
81+
# predates the commit that introduced the gate (cmd/api-gate absent
82+
# there and in the equally old head) fall back to the base branch tip,
83+
# which is equally trusted (protected branch, not PR-authored) and
84+
# current — this unblocks PRs whose merge base is older than the gate
85+
# without forcing a rebase. Bootstrap from head only while the tool
86+
# exists nowhere on the base branch yet; in that window CODEOWNERS on
87+
# cmd/api-gate, internal/apigate, and this workflow keeps tampering
88+
# behind an API-owner review.
7989
if [ -d "${RUNNER_TEMP}/base/cmd/api-gate" ]; then
8090
build_dir="${RUNNER_TEMP}/base"
81-
echo "Building api-gate from the trusted base checkout."
91+
echo "Building api-gate from the trusted merge-base checkout."
92+
elif git cat-file -e "origin/${BASE_REF}:cmd/api-gate" 2>/dev/null; then
93+
git worktree add --detach "${RUNNER_TEMP}/base-tip" "origin/${BASE_REF}"
94+
build_dir="${RUNNER_TEMP}/base-tip"
95+
echo "Building api-gate from the ${BASE_REF} tip; the merge base predates the gate."
8296
else
8397
build_dir="${GITHUB_WORKSPACE}"
8498
echo "::warning::api-gate not present on the base branch yet; building from head (bootstrap). CODEOWNERS enforces API-owner review on the gate's own files."

0 commit comments

Comments
 (0)