Skip to content

Commit 2a79c85

Browse files
Bill LeoutsakosBill Leoutsakos
authored andcommitted
fix(ci): validate stacked PRs against explicit audit bases
1 parent 23afa2a commit 2a79c85

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ on:
1919
push:
2020
branches: [main, staging, dev]
2121
pull_request:
22-
branches: [main, staging, dev]
2322
# Docs content and markdown don't affect the app build or images; push
2423
# runs stay unfiltered because they feed the deploy pipeline.
2524
paths-ignore:
@@ -36,9 +35,12 @@ permissions:
3635
jobs:
3736
test-build:
3837
name: Test and Build
39-
if: github.ref != 'refs/heads/dev' || github.event_name == 'pull_request'
38+
if: >-
39+
(github.ref != 'refs/heads/dev' || github.event_name == 'pull_request') &&
40+
(github.event_name != 'pull_request' ||
41+
contains(fromJSON('["main", "staging", "dev"]'), github.base_ref) ||
42+
github.event.pull_request.head.repo.full_name == github.repository)
4043
uses: ./.github/workflows/test-build.yml
41-
secrets: inherit
4244

4345
# Detect if this is a version release commit (e.g., "v0.5.24: ...")
4446
# Smallest runner on purpose: a few seconds of pure shell over the commit

.github/workflows/test-build.yml

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ name: Test and Build
33
on:
44
workflow_call:
55
workflow_dispatch:
6+
inputs:
7+
comparison_base:
8+
description: 'Full commit SHA to compare against the selected branch'
9+
required: true
10+
type: string
611

712
permissions:
813
contents: read
@@ -171,7 +176,7 @@ jobs:
171176
# The diff-based audits below need a base commit to read, and the default
172177
# depth of 1 clones a single commit with no parent. They normally fetch
173178
# their base by SHA (see "Resolve base ref"), so this depth only covers the
174-
# `HEAD~1` fallback — but without it that fallback resolves to nothing.
179+
# new-branch push's `HEAD~1` fallback — without it that resolves to nothing.
175180
#
176181
# Worth stating because the failure was invisible for so long: the migration
177182
# audit read the resulting `git diff` failure as "no migrations changed" and
@@ -286,19 +291,33 @@ jobs:
286291
# It is fetched by SHA at depth 1; the audits diff two tips and need no
287292
# common ancestry. An all-zero `before` means the branch is new and has no
288293
# predecessor to diff, so `HEAD~1` remains the fallback there.
294+
# PRs use the event's base SHA, not a branch that can advance while queued.
295+
# Manual runs require an explicit base so earlier commits are audited too.
289296
- name: Resolve base ref for diff-based audits
290297
id: audit_base
298+
env:
299+
EVENT_NAME: ${{ github.event_name }}
300+
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
301+
PUSH_BEFORE_SHA: ${{ github.event.before }}
302+
COMPARISON_BASE: ${{ inputs.comparison_base }}
291303
run: |
292-
if [ "${{ github.event_name }}" = "pull_request" ]; then
293-
git fetch --depth=1 origin "${{ github.base_ref }}"
294-
echo "ref=origin/${{ github.base_ref }}" >> "$GITHUB_OUTPUT"
295-
elif [ -n "${{ github.event.before }}" ] &&
296-
[ "${{ github.event.before }}" != "0000000000000000000000000000000000000000" ]; then
297-
git fetch --depth=1 origin "${{ github.event.before }}"
298-
echo "ref=${{ github.event.before }}" >> "$GITHUB_OUTPUT"
304+
if [ "$EVENT_NAME" = "pull_request" ]; then
305+
base_sha="$PR_BASE_SHA"
306+
elif [ "$EVENT_NAME" = "workflow_dispatch" ]; then
307+
base_sha="$COMPARISON_BASE"
308+
elif [ -n "$PUSH_BEFORE_SHA" ] &&
309+
[ "$PUSH_BEFORE_SHA" != "0000000000000000000000000000000000000000" ]; then
310+
base_sha="$PUSH_BEFORE_SHA"
299311
else
300-
echo "ref=HEAD~1" >> "$GITHUB_OUTPUT"
312+
base_sha="$(git rev-parse --verify 'HEAD~1^{commit}')"
313+
fi
314+
if ! [[ "$base_sha" =~ ^[0-9a-fA-F]{40}$ ]]; then
315+
echo 'Comparison base must be a full commit SHA.' >&2
316+
exit 1
301317
fi
318+
git fetch --no-tags --depth=1 origin "$base_sha"
319+
resolved_base="$(git rev-parse --verify "$base_sha^{commit}")"
320+
echo "ref=$resolved_base" >> "$GITHUB_OUTPUT"
302321
303322
- name: Check block registry invariants
304323
run: bun run apps/sim/scripts/check-block-registry.ts "${{ steps.audit_base.outputs.ref }}"

0 commit comments

Comments
 (0)