Skip to content

Commit a71d304

Browse files
authored
Merge pull request #6 from scaleapi/ci/bandit-fetch-head-by-sha
fix(ci): fetch the Bandit scan's PR head by SHA so fork PRs are scanned
2 parents 74394c5 + 8d64a73 commit a71d304

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

‎.github/workflows/bandit-ci.yml‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,25 @@ jobs:
2626
bandit --recursive --aggregate file . -f json -o baseline.json || true
2727
- name: Checkout feature branch
2828
shell: bash
29+
# Fetch the pull request's head COMMIT, not its branch name. The checkout above selected
30+
# the base branch, so `origin` is the base repository -- a fork's head branch exists only
31+
# in the contributor's fork, and `git fetch origin $GITHUB_HEAD_REF` failed there with
32+
# "couldn't find remote ref" before the scan could run. GitHub serves every pull
33+
# request's head commit from the base repository (it publishes refs/pull/<n>/head), the
34+
# same mechanism actions/checkout uses, so fetching by SHA works for forked and
35+
# same-repository pull requests alike.
36+
#
37+
# By SHA rather than refs/pull/<n>/head: the ref moves if the author pushes while this
38+
# runs, whereas the SHA pins the scan to exactly the commit the event fired for.
39+
#
40+
# Deliberately NOT a second actions/checkout: its default `clean: true` runs
41+
# `git clean -ffdx`, which deletes the untracked baseline.json the previous step just
42+
# wrote, so the comparison scan would run with no baseline and nothing would flag it.
43+
env:
44+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
2945
run: |
30-
git fetch origin $GITHUB_HEAD_REF
31-
git checkout $GITHUB_HEAD_REF
46+
git fetch --depth=1 origin "$HEAD_SHA"
47+
git checkout --detach "$HEAD_SHA"
3248
- name: Run Scan off of baseline
3349
shell: bash
3450
run: |

0 commit comments

Comments
 (0)