Skip to content

Commit 59ca6c7

Browse files
committed
More secure
1 parent c0d3fc2 commit 59ca6c7

File tree

3 files changed

+156
-106
lines changed

3 files changed

+156
-106
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Auto spotless, part 1
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: Check out PR branch
31+
env:
32+
GH_TOKEN: ${{ github.token }}
33+
run: gh pr checkout ${{ github.event.pull_request.number }}
34+
35+
- name: Spotless
36+
run: ./gradlew spotlessApply
37+
38+
- id: create-patch-file
39+
name: Create patch file
40+
run: |
41+
git diff > patch
42+
if [ -s patch ]; then
43+
echo "non-empty=true" >> "$GITHUB_OUTPUT"
44+
fi
45+
46+
- name: Upload patch file
47+
if: steps.create-patch-file.outputs.non-empty == 'true'
48+
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
49+
with:
50+
path: patch
51+
name: patch-${{ github.event.pull_request.number }}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Auto spotless
2+
on:
3+
workflow_run:
4+
workflows:
5+
- "Auto spotless, part 1"
6+
types:
7+
- completed
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
11+
cancel-in-progress: true
12+
13+
permissions:
14+
contents: read
15+
16+
jobs:
17+
apply:
18+
runs-on: ubuntu-latest
19+
needs: check
20+
permissions:
21+
contents: write
22+
pull-requests: write
23+
steps:
24+
- id: download-patch
25+
name: Download patch
26+
uses: actions/[email protected]
27+
with:
28+
# this script copied from
29+
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#using-data-from-the-triggering-workflow
30+
script: |
31+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
32+
owner: context.repo.owner,
33+
repo: context.repo.repo,
34+
run_id: context.payload.workflow_run.id
35+
});
36+
let patchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
37+
return artifact.name.startsWith("patch-")
38+
})[0];
39+
if (!patchArtifact) {
40+
core.info('No patch to apply.');
41+
return;
42+
}
43+
let download = await github.rest.actions.downloadArtifact({
44+
owner: context.repo.owner,
45+
repo: context.repo.repo,
46+
artifact_id: patchArtifact.id,
47+
archive_format: 'zip'
48+
});
49+
const fs = require('fs');
50+
const path = require('path');
51+
const temp = '${{ runner.temp }}/artifacts';
52+
if (!fs.existsSync(temp)){
53+
fs.mkdirSync(temp);
54+
}
55+
fs.writeFileSync(path.join(temp, 'patch.zip'), Buffer.from(download.data));
56+
core.setOutput("pr-num", patchArtifact.name.substring("patch-".length));
57+
58+
- name: Unzip patch
59+
if: steps.download-patch.outputs.pr-num != ''
60+
run: unzip patch.zip -d "${{ runner.temp }}/artifacts"
61+
62+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
63+
if: steps.download-patch.outputs.pr-num != ''
64+
65+
- name: Check out PR branch
66+
if: steps.download-patch.outputs.pr-num != ''
67+
env:
68+
GH_TOKEN: ${{ github.token }}
69+
run: gh pr checkout ${{ steps.download-patch.outputs.pr-num }}
70+
71+
- name: Use CLA approved github bot
72+
if: steps.download-patch.outputs.pr-num != ''
73+
# IMPORTANT do not call the .github/scripts/use-cla-approved-bot.sh
74+
# since that script could have been compromised in the PR branch
75+
run: |
76+
git config user.name otelbot
77+
git config user.email [email protected]
78+
79+
- uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6
80+
if: steps.download-patch.outputs.pr-num != ''
81+
id: otelbot-token
82+
with:
83+
app-id: ${{ vars.OTELBOT_APP_ID }}
84+
private-key: ${{ secrets.OTELBOT_PRIVATE_KEY }}
85+
86+
- name: Apply patch and push
87+
if: steps.download-patch.outputs.pr-num != ''
88+
env:
89+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
90+
run: |
91+
git apply "${{ runner.temp }}/artifacts/patch"
92+
git commit -a -m "./gradlew spotlessApply"
93+
git push
94+
95+
- if: steps.download-patch.outputs.pr-num != '' && success()
96+
env:
97+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
98+
run: |
99+
gh pr comment ${{ steps.download-patch.outputs.pr-num }} --body "🔧 The result from \`./gradlew spotlessApply\` was committed to the PR branch."
100+
101+
- if: steps.download-patch.outputs.pr-num != '' && failure()
102+
env:
103+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
104+
run: |
105+
gh pr comment ${{ steps.download-patch.outputs.pr-num }} --body "❌ The result from \`./gradlew 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.yml

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)