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[@]}"
0 commit comments