Skip to content

Commit 34b470f

Browse files
authored
ci: Use reviewdog for external PRs (#1663)
1 parent 618e292 commit 34b470f

File tree

2 files changed

+69
-41
lines changed

2 files changed

+69
-41
lines changed

.github/workflows/commit/action.yml

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,76 @@ outputs:
1111
runs:
1212
using: "composite"
1313
steps:
14+
- name: Check for changes
15+
id: check
16+
run: |
17+
set -x
18+
if [ -n "$(git status --porcelain)" ]; then
19+
echo "has_changes=true" | tee -a $GITHUB_OUTPUT
20+
else
21+
echo "has_changes=false" | tee -a $GITHUB_OUTPUT
22+
fi
23+
shell: bash
24+
1425
- name: Commit if changed, create a PR if protected
1526
id: commit
27+
if: steps.check.outputs.has_changes == 'true'
1628
env:
1729
GITHUB_TOKEN: ${{ inputs.token }}
1830
run: |
1931
set -x
20-
if [ -n "$(git status --porcelain)" ]; then
21-
echo "Changed"
22-
protected=${{ github.ref_protected }}
23-
foreign=${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}
24-
if [ "${foreign}" = "true" ]; then
25-
# https://github.com/krlmlr/actions-sync/issues/44
26-
echo "Can't push to foreign branch"
27-
elif [ "${protected}" = "true" ]; then
28-
current_branch=$(git branch --show-current)
29-
new_branch=gha-commit-$(git rev-parse --short HEAD)
30-
git checkout -b ${new_branch}
31-
git add .
32-
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
33-
# Force-push, used in only one place
34-
# Alternative: separate branch names for each usage
35-
git push -u origin HEAD -f
36-
37-
existing_pr=$(gh pr list --state open --base main --head ${new_branch} --json number --jq '.[] | .number')
38-
if [ -n "${existing_pr}" ]; then
39-
echo "Existing PR: ${existing_pr}"
40-
else
41-
gh pr create --base main --head ${new_branch} --title "chore: Auto-update from GitHub Actions" --body "Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
42-
fi
32+
protected=${{ github.ref_protected }}
33+
foreign=${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }}
34+
is_pr=${{ github.event_name == 'pull_request' }}
35+
if [ "${is_pr}" = "true" ]; then
36+
# Running on a PR - will use reviewdog in next step
37+
echo "Code changes detected on PR, will suggest changes via reviewdog"
38+
echo "use_reviewdog=true" | tee -a $GITHUB_OUTPUT
39+
git reset HEAD
40+
git status
41+
elif [ "${foreign}" = "true" ]; then
42+
# https://github.com/krlmlr/actions-sync/issues/44
43+
echo "Can't push to foreign branch"
44+
elif [ "${protected}" = "true" ]; then
45+
current_branch=$(git branch --show-current)
46+
new_branch=gha-commit-$(git rev-parse --short HEAD)
47+
git checkout -b ${new_branch}
48+
git add .
49+
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
50+
# Force-push, used in only one place
51+
# Alternative: separate branch names for each usage
52+
git push -u origin HEAD -f
4353
44-
gh workflow run rcc -f ref=$(git rev-parse HEAD)
45-
gh pr merge --merge --auto
54+
existing_pr=$(gh pr list --state open --base main --head ${new_branch} --json number --jq '.[] | .number')
55+
if [ -n "${existing_pr}" ]; then
56+
echo "Existing PR: ${existing_pr}"
4657
else
47-
git fetch
48-
if [ -n "${GITHUB_HEAD_REF}" ]; then
49-
git add .
50-
git stash save
51-
git switch ${GITHUB_HEAD_REF}
52-
git merge origin/${GITHUB_BASE_REF} --no-edit
53-
git stash pop
54-
fi
55-
git add .
56-
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
57-
git push -u origin HEAD
58+
gh pr create --base main --head ${new_branch} --title "chore: Auto-update from GitHub Actions" --body "Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
59+
fi
5860
59-
# Only set output if changed
60-
echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
61+
gh workflow run rcc -f ref=$(git rev-parse HEAD)
62+
gh pr merge --merge --auto
63+
else
64+
git fetch
65+
if [ -n "${GITHUB_HEAD_REF}" ]; then
66+
git add .
67+
git stash save
68+
git switch ${GITHUB_HEAD_REF}
69+
git merge origin/${GITHUB_BASE_REF} --no-edit
70+
git stash pop
6171
fi
72+
git add .
73+
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
74+
git push -u origin HEAD
75+
76+
# Only set output if changed
77+
echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
6278
fi
6379
shell: bash
80+
81+
- name: Suggest changes via reviewdog
82+
if: steps.commit.outputs.use_reviewdog == 'true'
83+
uses: krlmlr/action-suggester@main
84+
with:
85+
github_token: ${{ inputs.token }}
86+
tool_name: "rcc"

.github/workflows/update-snapshots/action.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,24 @@ runs:
5656
if: ${{ steps.run-tests.outputs.changed }}
5757
id: check-changed
5858
run: |
59-
echo "changed=$(git status --porcelain -- tests/testthat/_snaps | head -n 1)" >> $GITHUB_OUTPUT
59+
set -x
60+
if [ "${{ github.event_name}}" != "pull_request" ] ; then
61+
echo "changed=$(git status --porcelain -- tests/testthat/_snaps | head -n 1)" | tee -a $GITHUB_OUTPUT
62+
fi
6063
shell: bash
6164

6265
- name: Derive branch name
6366
if: ${{ steps.check-changed.outputs.changed }}
6467
id: matrix-desc
6568
run: |
69+
set -x
6670
config=$(echo '${{ toJSON(matrix) }}' | jq -c .)
67-
echo "text=$(echo ${config})" >> $GITHUB_OUTPUT
68-
echo "branch=$(echo ${config} | sed -r 's/[^0-9a-zA-Z]+/-/g;s/^-//;s/-$//')" >> $GITHUB_OUTPUT
71+
echo "text=$(echo ${config})" | tee -a $GITHUB_OUTPUT
72+
echo "branch=$(echo ${config} | sed -r 's/[^0-9a-zA-Z]+/-/g;s/^-//;s/-$//')" | tee -a $GITHUB_OUTPUT
6973
shell: bash
7074

7175
- name: Create pull request
76+
# Fall through if PR, will use reviewdog/action-suggester in the commit action
7277
if: ${{ steps.check-changed.outputs.changed }}
7378
id: cpr
7479
uses: peter-evans/create-pull-request@v6

0 commit comments

Comments
 (0)