Skip to content

Commit f704bf6

Browse files
authored
Add spotless auto-fix for PRs (open-telemetry#13872)
1 parent ec9fbf6 commit f704bf6

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

.github/repository-settings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ settings](https://github.com/open-telemetry/community/blob/main/docs/how-to-conf
132132

133133
- `FOSSA_API_KEY`
134134
- `OTELBOT_PRIVATE_KEY`
135+
- `OTELBOT_JAVA_INSTRUMENTATION_PRIVATE_KEY`
135136

136137
### Organization variables
137138

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
- id: download-patch
20+
name: Download patch
21+
uses: actions/[email protected]
22+
with:
23+
# this script copied from
24+
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#using-data-from-the-triggering-workflow
25+
script: |
26+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
27+
owner: context.repo.owner,
28+
repo: context.repo.repo,
29+
run_id: context.payload.workflow_run.id
30+
});
31+
let patchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
32+
return artifact.name == "patch"
33+
})[0];
34+
if (!patchArtifact) {
35+
core.info('No patch to apply.');
36+
return;
37+
}
38+
let download = await github.rest.actions.downloadArtifact({
39+
owner: context.repo.owner,
40+
repo: context.repo.repo,
41+
artifact_id: patchArtifact.id,
42+
archive_format: 'zip'
43+
});
44+
const fs = require('fs');
45+
const path = require('path');
46+
const temp = '${{ runner.temp }}/artifacts';
47+
if (!fs.existsSync(temp)){
48+
fs.mkdirSync(temp);
49+
}
50+
fs.writeFileSync(path.join(temp, 'patch.zip'), Buffer.from(download.data));
51+
core.setOutput("exists", "true");
52+
53+
- id: get-pr-number
54+
name: Get PR number
55+
uses: actions/[email protected]
56+
with:
57+
script: |
58+
const response = await github.request(context.payload.workflow_run.url);
59+
core.setOutput('pr-number', response.data.pull_requests[0].number);
60+
61+
- name: Unzip patch
62+
if: steps.download-patch.outputs.exists == 'true'
63+
working-directory: ${{ runner.temp }}/artifacts
64+
run: unzip patch.zip
65+
66+
- uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6
67+
if: steps.download-patch.outputs.exists == 'true'
68+
id: otelbot-token
69+
with:
70+
app-id: 1295839
71+
private-key: ${{ secrets.OTELBOT_JAVA_INSTRUMENTATION_PRIVATE_KEY }}
72+
73+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
74+
if: steps.download-patch.outputs.exists == 'true'
75+
with:
76+
token: ${{ steps.otelbot-token.outputs.token }}
77+
78+
- name: Check out PR branch
79+
if: steps.download-patch.outputs.exists == 'true'
80+
env:
81+
GH_TOKEN: ${{ github.token }}
82+
run: gh pr checkout ${{ steps.get-pr-number.outputs.pr-number }}
83+
84+
- name: Use CLA approved github bot
85+
if: steps.download-patch.outputs.exists == 'true'
86+
# IMPORTANT do not call the .github/scripts/use-cla-approved-bot.sh
87+
# since that script could have been compromised in the PR branch
88+
run: |
89+
git config user.name otelbot
90+
git config user.email [email protected]
91+
92+
- name: Apply patch and push
93+
if: steps.download-patch.outputs.exists == 'true'
94+
run: |
95+
git apply "${{ runner.temp }}/artifacts/patch"
96+
git commit -a -m "./gradlew spotlessApply"
97+
git push
98+
99+
- if: steps.download-patch.outputs.exists == 'true' && success()
100+
env:
101+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
102+
run: |
103+
gh pr comment ${{ steps.get-pr-number.outputs.pr-number }} --body "🔧 The result from spotlessApply was committed to the PR branch."
104+
105+
- if: steps.download-patch.outputs.exists == 'true' && failure()
106+
env:
107+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
108+
run: |
109+
gh pr comment ${{ steps.get-pr-number.outputs.pr-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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: Free disk space
22+
run: .github/scripts/gha-free-disk-space.sh
23+
24+
- name: Set up JDK for running Gradle
25+
uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1
26+
with:
27+
distribution: temurin
28+
java-version-file: .java-version
29+
30+
- name: Set up gradle
31+
uses: gradle/actions/setup-gradle@06832c7b30a0129d7fb559bcc6e43d26f6374244 # v4.3.1
32+
with:
33+
cache-read-only: true
34+
35+
- name: Check out PR branch
36+
env:
37+
GH_TOKEN: ${{ github.token }}
38+
run: gh pr checkout ${{ github.event.pull_request.number }}
39+
40+
- name: Spotless
41+
run: ./gradlew spotlessApply
42+
43+
- id: create-patch-file
44+
name: Create patch file
45+
run: |
46+
git diff > patch
47+
if [ -s patch ]; then
48+
echo "non-empty=true" >> "$GITHUB_OUTPUT"
49+
fi
50+
51+
- name: Upload patch file
52+
if: steps.create-patch-file.outputs.non-empty == 'true'
53+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
54+
with:
55+
path: patch
56+
name: patch

0 commit comments

Comments
 (0)