Skip to content

Commit 11c3dda

Browse files
authored
Add spotless auto-fix for PRs (#1890)
1 parent 858912d commit 11c3dda

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Auto spotless apply
2+
on:
3+
workflow_run:
4+
workflows:
5+
- "Auto spotless check"
6+
types:
7+
- completed
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
apply:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: write
17+
pull-requests: write
18+
steps:
19+
- name: Download patch
20+
uses: actions/[email protected]
21+
with:
22+
run-id: ${{ github.event.workflow_run.id }}
23+
path: ${{ runner.temp }}
24+
merge-multiple: true
25+
github-token: ${{ github.token }}
26+
27+
- id: unzip-patch
28+
name: Unzip patch
29+
working-directory: ${{ runner.temp }}
30+
run: |
31+
if [ -f patch ]; then
32+
echo "exists=true" >> $GITHUB_OUTPUT
33+
fi
34+
35+
- uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6
36+
if: steps.unzip-patch.outputs.exists == 'true'
37+
id: otelbot-token
38+
with:
39+
app-id: 1296620
40+
private-key: ${{ secrets.OTELBOT_JAVA_CONTRIB_PRIVATE_KEY }}
41+
42+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
43+
if: steps.unzip-patch.outputs.exists == 'true'
44+
with:
45+
token: ${{ steps.otelbot-token.outputs.token }}
46+
47+
- id: get-pr
48+
if: steps.unzip-patch.outputs.exists == 'true'
49+
name: Get PR
50+
env:
51+
PR_BRANCH: |-
52+
${{
53+
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login)
54+
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch)
55+
|| github.event.workflow_run.head_branch
56+
}}
57+
GH_TOKEN: ${{ github.token }}
58+
run: |
59+
echo gh pr view "${PR_BRANCH}" --json number --jq .number
60+
number=$(gh pr view "${PR_BRANCH}" --json number --jq .number)
61+
echo $number
62+
echo "number=$number" >> $GITHUB_OUTPUT
63+
64+
- name: Check out PR branch
65+
if: steps.unzip-patch.outputs.exists == 'true'
66+
env:
67+
GH_TOKEN: ${{ github.token }}
68+
run: gh pr checkout ${{ steps.get-pr.outputs.number }}
69+
70+
- name: Use CLA approved github bot
71+
if: steps.unzip-patch.outputs.exists == 'true'
72+
# IMPORTANT do not call the .github/scripts/use-cla-approved-bot.sh
73+
# since that script could have been compromised in the PR branch
74+
run: |
75+
git config user.name otelbot
76+
git config user.email [email protected]
77+
78+
- name: Apply patch and push
79+
if: steps.unzip-patch.outputs.exists == 'true'
80+
run: |
81+
git apply "${{ runner.temp }}/patch"
82+
git commit -a -m "./gradlew spotlessApply"
83+
git push
84+
85+
- if: steps.unzip-patch.outputs.exists == 'true' && success()
86+
env:
87+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
88+
run: |
89+
gh pr comment ${{ steps.get-pr.outputs.number }} --body "🔧 The result from spotlessApply was committed to the PR branch."
90+
91+
- if: steps.unzip-patch.outputs.exists == 'true' && failure()
92+
env:
93+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
94+
run: |
95+
gh pr comment ${{ steps.get-pr.outputs.number }} --body "❌ The result from spotlessApply could not be committed to the PR branch, see logs: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID."
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Auto spotless check
2+
on:
3+
pull_request:
4+
types:
5+
- opened
6+
- synchronize
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
10+
cancel-in-progress: true
11+
12+
permissions:
13+
contents: read
14+
15+
jobs:
16+
check:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
20+
21+
- name: Set up JDK for running Gradle
22+
uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1
23+
with:
24+
distribution: temurin
25+
java-version: 17
26+
27+
- name: Set up gradle
28+
uses: gradle/actions/setup-gradle@06832c7b30a0129d7fb559bcc6e43d26f6374244 # v4.3.1
29+
with:
30+
cache-read-only: true
31+
32+
- name: Check out PR branch
33+
env:
34+
GH_TOKEN: ${{ github.token }}
35+
run: gh pr checkout ${{ github.event.pull_request.number }}
36+
37+
- name: Spotless
38+
run: ./gradlew spotlessApply
39+
40+
- id: create-patch
41+
name: Create patch file
42+
run: |
43+
git diff > patch
44+
if [ -s patch ]; then
45+
echo "exists=true" >> "$GITHUB_OUTPUT"
46+
fi
47+
48+
- name: Upload patch file
49+
if: steps.create-patch.outputs.exists == 'true'
50+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
51+
with:
52+
path: patch
53+
name: patch

0 commit comments

Comments
 (0)