Skip to content

Commit 7820cba

Browse files
Combine access token and pull request checks in workflow (#380)
# Motivation In #368 I changed to an earlier version of `EndBug/add-and-commit` to avoid having to pass `ref` to the `checkout` action depending on whether the run if for a PR to avoid having to have 4 different checkout steps. I realized that I only need to pass the token if I plan to make changes, so I can combine the checkout steps and still only have 2 while also using newer versions of `actions/checkout` and `EndBug/add-and-commit`. # Changes 1. Check whether commits can be added based on both the presence of the PAT token and the even being for a PR. 2. Use `actions/checkout@v4` and `EndBug/[email protected]`. # Tested [PR](#381) from a forked repo: * Without formatting changes required: https://github.com/dfinity/snsdemo/actions/runs/10040798640/job/27747555323?pr=381 * With formatting changes required: https://github.com/dfinity/snsdemo/actions/runs/10040830499/job/27747653098?pr=381 From this PR: * Without formatting changes required: https://github.com/dfinity/snsdemo/actions/runs/10040798640/job/27747555323?pr=380 * With formatting changes required: https://github.com/dfinity/snsdemo/actions/runs/10040829788/job/27747650644 * Automatic follow-up after commit: https://github.com/dfinity/snsdemo/actions/runs/10040837356/job/27747673570?pr=380 --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 4f2eaa1 commit 7820cba

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

.github/workflows/checks.yml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,21 @@ jobs:
1515
# users. So on PRs where the token is not available, we don't commit
1616
# changes and instead just fail if the formatting is incorrect.
1717
steps:
18-
- name: Check if personal access token is available
19-
id: check_pat
18+
- name: Check if commits can be added
19+
id: check_can_add_commit
2020
run: |
21-
echo "has_pat=${{ secrets.GIX_CREATE_PR_PAT != '' }}" >> $GITHUB_OUTPUT
21+
echo "can_add_commit=${{ secrets.GIX_CREATE_PR_PAT != '' && github.event_name == 'pull_request' }}" >> $GITHUB_OUTPUT
2222
2323
- name: Checkout
24-
if: steps.check_pat.outputs.has_pat == 'true'
25-
uses: actions/checkout@v3
24+
if: steps.check_can_add_commit.outputs.can_add_commit == 'true'
25+
uses: actions/checkout@v4
2626
with:
27+
repository: ${{ github.event.pull_request.head.repo.full_name }}
28+
ref: ${{ github.event.pull_request.head.ref }}
2729
token: ${{ secrets.GIX_CREATE_PR_PAT }}
28-
- name: Checkout without personal access token
29-
if: steps.check_pat.outputs.has_pat == 'false'
30-
uses: actions/checkout@v3
30+
- name: Checkout
31+
if: steps.check_can_add_commit.outputs.can_add_commit == 'false'
32+
uses: actions/checkout@v4
3133

3234
- name: Install shfmt
3335
run: sudo snap install --classic shfmt
@@ -45,8 +47,8 @@ jobs:
4547
echo "formatting_needed=true" >> $GITHUB_OUTPUT
4648
fi
4749
- name: Commit Formatting changes
48-
if: steps.check_pat.outputs.has_pat == 'true' && steps.check_format.outputs.formatting_needed == 'true'
49-
uses: EndBug/add-and-commit@v7.2.0
50+
if: steps.check_can_add_commit.outputs.can_add_commit == 'true' && steps.check_format.outputs.formatting_needed == 'true'
51+
uses: EndBug/add-and-commit@v9.1.4
5052
with:
5153
add: .
5254
default_author: github_actions
@@ -56,9 +58,9 @@ jobs:
5658
pull_strategy: "NO-PULL"
5759

5860
- name: Fail for formatting issues without personal access token
59-
if: steps.check_pat.outputs.has_pat == 'false' && steps.check_format.outputs.formatting_needed == 'true'
61+
if: steps.check_can_add_commit.outputs.can_add_commit == 'false' && steps.check_format.outputs.formatting_needed == 'true'
6062
run: |
61-
echo "Formatting changes are needed but couldn't be committed because the personal access token isn't available."
63+
echo "Formatting changes are needed but couldn't be committed because the personal access token isn't available or this isn't a pull request."
6264
exit 1
6365
shell-checks:
6466
needs: formatting

0 commit comments

Comments
 (0)