Skip to content

Commit 48e6e10

Browse files
committed
More secure
1 parent c0d3fc2 commit 48e6e10

File tree

2 files changed

+94
-49
lines changed

2 files changed

+94
-49
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/workflows/auto-spotless.yml renamed to .github/workflows/auto-spotless-part-2.yml

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name: Auto spotless
22
on:
3-
pull_request_target:
3+
workflow_run:
4+
workflows:
5+
- "Auto spotless, part 1"
46
types:
5-
- opened
6-
- synchronize
7+
- completed
78

89
concurrency:
910
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
@@ -13,45 +14,6 @@ permissions:
1314
contents: read
1415

1516
jobs:
16-
check:
17-
runs-on: ubuntu-latest
18-
outputs:
19-
patch-created: ${{ steps.create-patch-file.outputs.nonempty }}
20-
steps:
21-
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
22-
23-
- name: Free disk space
24-
run: .github/scripts/gha-free-disk-space.sh
25-
26-
- name: Set up JDK for running Gradle
27-
uses: actions/setup-java@c5195efecf7bdfc987ee8bae7a71cb8b11521c00 # v4.7.1
28-
with:
29-
distribution: temurin
30-
java-version-file: .java-version
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-file
41-
name: Create patch file
42-
run: |
43-
git diff > patch
44-
if [ -s patch ]; then
45-
echo "nonempty=true" >> "$GITHUB_OUTPUT"
46-
fi
47-
48-
- name: Upload patch file
49-
if: steps.create-patch-file.outputs.nonempty == 'true'
50-
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
51-
with:
52-
path: patch
53-
name: patch
54-
5517
apply:
5618
runs-on: ubuntu-latest
5719
needs: check
@@ -60,17 +22,49 @@ jobs:
6022
contents: write
6123
pull-requests: write
6224
steps:
25+
- id: download-patch
26+
name: Download patch
27+
uses: actions/[email protected]
28+
with:
29+
# this script copied from
30+
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#using-data-from-the-triggering-workflow
31+
script: |
32+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
33+
owner: context.repo.owner,
34+
repo: context.repo.repo,
35+
run_id: context.payload.workflow_run.id
36+
});
37+
let patchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
38+
return artifact.name.startsWith("patch-")
39+
})[0];
40+
if (!patchArtifact) {
41+
core.info('No patch to apply.');
42+
return;
43+
}
44+
let download = await github.rest.actions.downloadArtifact({
45+
owner: context.repo.owner,
46+
repo: context.repo.repo,
47+
artifact_id: patchArtifact.id,
48+
archive_format: 'zip'
49+
});
50+
const fs = require('fs');
51+
const path = require('path');
52+
const temp = '${{ runner.temp }}/artifacts';
53+
if (!fs.existsSync(temp)){
54+
fs.mkdirSync(temp);
55+
}
56+
fs.writeFileSync(path.join(temp, 'patch.zip'), Buffer.from(download.data));
57+
core.setOutput("pr-num", patchArtifact.name.substring("patch-".length));
58+
59+
- name: Unzip patch
60+
run: unzip patch.zip -d "${{ runner.temp }}/artifacts"
61+
6362
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
6463

6564
- name: Check out PR branch
6665
env:
6766
GH_TOKEN: ${{ github.token }}
68-
run: gh pr checkout ${{ github.event.pull_request.number }}
69-
70-
- name: Download patch
71-
uses: actions/download-artifact@v4
72-
with:
73-
name: patch
67+
run: gh pr checkout ${{ steps.download-patch.outputs.pr-num }}
7468

7569
- name: Use CLA approved github bot
7670
# IMPORTANT do not call the .github/scripts/use-cla-approved-bot.sh
@@ -89,7 +83,7 @@ jobs:
8983
env:
9084
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
9185
run: |
92-
git apply patch
86+
git apply "${{ runner.temp }}/artifacts/patch"
9387
git commit -a -m "./gradlew spotlessApply"
9488
git push
9589

0 commit comments

Comments
 (0)