Skip to content

Commit d01369f

Browse files
authored
Fix auto spotless (open-telemetry#13879)
1 parent f704bf6 commit d01369f

File tree

2 files changed

+44
-58
lines changed

2 files changed

+44
-58
lines changed

.github/workflows/auto-spotless-apply.yml

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,94 +16,80 @@ jobs:
1616
contents: write
1717
pull-requests: write
1818
steps:
19-
- id: download-patch
20-
name: Download patch
21-
uses: actions/[email protected]
19+
- name: Download patch
20+
uses: actions/[email protected]
2221
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");
22+
run-id: ${{ github.event.workflow_run.id }}
23+
path: ${{ runner.temp }}
24+
merge-multiple: true
25+
github-token: ${{ github.token }}
5226

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
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
6534
6635
- uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6
67-
if: steps.download-patch.outputs.exists == 'true'
36+
if: steps.unzip-patch.outputs.exists == 'true'
6837
id: otelbot-token
6938
with:
7039
app-id: 1295839
7140
private-key: ${{ secrets.OTELBOT_JAVA_INSTRUMENTATION_PRIVATE_KEY }}
7241

7342
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
74-
if: steps.download-patch.outputs.exists == 'true'
43+
if: steps.unzip-patch.outputs.exists == 'true'
7544
with:
7645
token: ${{ steps.otelbot-token.outputs.token }}
7746

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+
7864
- name: Check out PR branch
79-
if: steps.download-patch.outputs.exists == 'true'
65+
if: steps.unzip-patch.outputs.exists == 'true'
8066
env:
8167
GH_TOKEN: ${{ github.token }}
82-
run: gh pr checkout ${{ steps.get-pr-number.outputs.pr-number }}
68+
run: gh pr checkout ${{ steps.get-pr.outputs.number }}
8369

8470
- name: Use CLA approved github bot
85-
if: steps.download-patch.outputs.exists == 'true'
71+
if: steps.unzip-patch.outputs.exists == 'true'
8672
# IMPORTANT do not call the .github/scripts/use-cla-approved-bot.sh
8773
# since that script could have been compromised in the PR branch
8874
run: |
8975
git config user.name otelbot
9076
git config user.email [email protected]
9177
9278
- name: Apply patch and push
93-
if: steps.download-patch.outputs.exists == 'true'
79+
if: steps.unzip-patch.outputs.exists == 'true'
9480
run: |
95-
git apply "${{ runner.temp }}/artifacts/patch"
81+
git apply "${{ runner.temp }}/patch"
9682
git commit -a -m "./gradlew spotlessApply"
9783
git push
9884
99-
- if: steps.download-patch.outputs.exists == 'true' && success()
85+
- if: steps.unzip-patch.outputs.exists == 'true' && success()
10086
env:
10187
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
10288
run: |
103-
gh pr comment ${{ steps.get-pr-number.outputs.pr-number }} --body "🔧 The result from spotlessApply was committed to the PR branch."
89+
gh pr comment ${{ steps.get-pr.outputs.number }} --body "🔧 The result from spotlessApply was committed to the PR branch."
10490
105-
- if: steps.download-patch.outputs.exists == 'true' && failure()
91+
- if: steps.unzip-patch.outputs.exists == 'true' && failure()
10692
env:
10793
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
10894
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."
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."

.github/workflows/auto-spotless-check.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ jobs:
4040
- name: Spotless
4141
run: ./gradlew spotlessApply
4242

43-
- id: create-patch-file
43+
- id: create-patch
4444
name: Create patch file
4545
run: |
4646
git diff > patch
4747
if [ -s patch ]; then
48-
echo "non-empty=true" >> "$GITHUB_OUTPUT"
48+
echo "exists=true" >> "$GITHUB_OUTPUT"
4949
fi
5050
5151
- name: Upload patch file
52-
if: steps.create-patch-file.outputs.non-empty == 'true'
52+
if: steps.create-patch.outputs.exists == 'true'
5353
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
5454
with:
5555
path: patch

0 commit comments

Comments
 (0)