Skip to content

Commit

Permalink
FIX Set fetch-depth for parent-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jul 11, 2022
1 parent 775ca10 commit 0434f60
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,42 @@ jobs:

steps:

- name: Set fetch depth
id: set-fetch-depth
env:
GITHUB_REPOSITORY: ${{ github.repository }}
shell: bash
run: |
# Default fetch-depth of 1 will only fetch the latest commit
# This is fine for non-pull-request events where standard major.minor naming conventions
# are used so we do not need to attempt to get the name of the parent branch
FETCH_DEPTH=1
# For pull-requests, set fetch-depth to one more than the number of commits
# so we can later make a best attempt at getting the name of the parent branch
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
PR_NUMBER=${{ github.event.pull_request.number }}
# https://docs.github.com/en/rest/pulls/pulls#list-commits-on-a-pull-request
RESP_CODE=$(curl -w %{http_code} -s -o __pr_commits.json \
-X GET https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/commits \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${{ github.token }}" \
)
if [[ $RESP_CODE == "200" ]]; then
NUM_COMMITS=$(cat __pr_commits.json | jq '. | length')
FETCH_DEPTH=$(( NUM_COMMITS + 1 ))
rm __pr_commits.json
else
echo "Failed to fetch commits for pull-request - HTTP response code was $RESP_CODE"
exit 1
fi
fi
echo "FETCH_DEPTH is $FETCH_DEPTH"
echo "::set-output name=fetch-depth::$FETCH_DEPTH"
- name: Checkout code
uses: actions/checkout@7884fcad6b5d53d10323aee724dc68d8b9096a2e # v2.4.2
with:
fetch-depth: ${{ steps.set-fetch-depth.outputs.fetch-depth }}

- name: Install PHP
# SHA will need to be updated to support new php version when they are released
Expand Down

0 comments on commit 0434f60

Please sign in to comment.