Skip to content

Commit 0ffecae

Browse files
committed
ci: read changed files from git instead of the REST API
The Changes job and all six server shards each made two REST calls per run against the repository's shared 1,000 requests/hour GITHUB_TOKEN quota. Diff the checked-out merge commit against its base parent instead, which needs no API call and no file-count caps.
1 parent cac33e9 commit 0ffecae

2 files changed

Lines changed: 19 additions & 39 deletions

File tree

‎.github/actions/ci-changes/action.yml‎

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ runs:
2525
- id: detect
2626
shell: bash
2727
env:
28-
GH_TOKEN: ${{ github.token }}
29-
PR_NUMBER: ${{ github.event.pull_request.number }}
28+
EVENT_NAME: ${{ github.event_name }}
3029
BEFORE_SHA: ${{ github.event.before }}
3130
run: |
3231
set -uo pipefail
@@ -41,41 +40,24 @@ runs:
4140
decide
4241
}
4342
44-
count_rows() {
45-
printf '%s\n' "$1" | grep -c . || true
46-
}
47-
48-
# One row per changed file, holding the new path and, for a rename,
49-
# the path it replaced: renaming a file out of a package changes that
50-
# package just like editing it.
51-
row='[.filename, (.previous_filename // empty)] | @tsv'
52-
53-
if [[ -n "${PR_NUMBER}" ]]; then
54-
# The PR files endpoint stops at 3000 files and pagination cannot
55-
# extend it, so cross-check against the count the PR itself reports.
56-
expected=$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}" --jq '.changed_files') \
57-
|| fail_open "Could not read the pull request."
58-
rows=$(gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files" --paginate --jq ".[] | ${row}") \
59-
|| fail_open "Could not resolve changed files."
60-
61-
listed=$(count_rows "$rows")
62-
if [[ "$listed" -lt "$expected" ]]; then
63-
fail_open "GitHub listed only ${listed} of ${expected} changed files."
64-
fi
43+
# Reads the change from git rather than the REST API: every job that
44+
# runs this shares the repository's 1,000 requests/hour GITHUB_TOKEN quota.
45+
if [[ "$EVENT_NAME" == pull_request ]]; then
46+
# A pull request checks out GitHub's merge commit with fetch-depth: 2,
47+
# so its first parent is the base branch and the diff is the PR's change.
48+
base=HEAD^1
6549
mode=()
6650
else
67-
rows=$(gh api "repos/${GITHUB_REPOSITORY}/compare/${BEFORE_SHA}...${GITHUB_SHA}" --jq ".files[]? | ${row}") \
68-
|| fail_open "Could not resolve changed files."
69-
70-
# The compare endpoint reports at most 300 files and pagination does
71-
# not extend that list, so a full list may be hiding changes.
72-
listed=$(count_rows "$rows")
73-
if [[ "$listed" -ge 300 ]]; then
74-
fail_open "GitHub listed ${listed} changed files, the compare endpoint maximum."
75-
fi
51+
base=$BEFORE_SHA
52+
git fetch --quiet --no-tags --depth=1 --filter=blob:none origin "$base" \
53+
|| fail_open "Could not fetch ${base}."
7654
# main keeps a full run as the backstop for anything the gate misses.
7755
mode=(--full)
7856
fi
7957
80-
tr '\t' '\n' <<< "$rows" > "$RUNNER_TEMP/changed-paths"
58+
# --no-renames lists a rename as its old and new path: renaming a file out
59+
# of a package changes that package just like editing it. It also keeps
60+
# git from fetching file contents to score renames.
61+
git diff --name-only --no-renames "$base" HEAD > "$RUNNER_TEMP/changed-paths" \
62+
|| fail_open "Could not diff against ${base}."
8163
decide --paths "$RUNNER_TEMP/changed-paths" "${mode[@]}"

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ jobs:
2424
name: Changes
2525
runs-on: blacksmith-2vcpu-ubuntu-2404
2626
timeout-minutes: 5
27-
permissions:
28-
contents: read
29-
pull-requests: read
3027
outputs:
3128
typecheck: ${{ steps.detect.outputs.typecheck }}
3229
build: ${{ steps.detect.outputs.build }}
@@ -39,9 +36,11 @@ jobs:
3936
mobile_native: ${{ steps.detect.outputs.mobile_native }}
4037
steps:
4138
# Only the package manifests the script reads to build the dependency graph.
39+
# Depth 2 brings the base commit the change is diffed against.
4240
- name: Checkout
4341
uses: actions/checkout@v6
4442
with:
43+
fetch-depth: 2
4544
sparse-checkout: |
4645
/pnpm-workspace.yaml
4746
/.github/scripts/ci-changes.cjs
@@ -293,17 +292,16 @@ jobs:
293292
name: Test Server ${{ matrix.shard }}
294293
runs-on: blacksmith-4vcpu-ubuntu-2404
295294
timeout-minutes: 10
296-
permissions:
297-
contents: read
298-
pull-requests: read
299295
strategy:
300296
fail-fast: false
301297
matrix:
302298
shard: [1, 2, 3, 4, 5, 6]
303299
steps:
300+
# Depth 2 brings the base commit the change is diffed against.
304301
- name: Checkout
305302
uses: actions/checkout@v6
306303
with:
304+
fetch-depth: 2
307305
sparse-checkout: |
308306
/*
309307
!/.repos/

0 commit comments

Comments
 (0)