Skip to content

Commit e27afad

Browse files
committed
Make it safe
1 parent c0d3fc2 commit e27afad

File tree

3 files changed

+171
-106
lines changed

3 files changed

+171
-106
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
name: Auto spotless apply
2+
on:
3+
workflow_run:
4+
workflows:
5+
- "Auto spotless check"
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+
permissions:
20+
contents: write
21+
pull-requests: write
22+
steps:
23+
- id: download-patch
24+
name: Download patch
25+
uses: actions/github-script@v7.0.1
26+
with:
27+
# this script copied from
28+
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#using-data-from-the-triggering-workflow
29+
script: |
30+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
31+
owner: context.repo.owner,
32+
repo: context.repo.repo,
33+
run_id: context.payload.workflow_run.id
34+
});
35+
let patchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
36+
return artifact.name == "patch"
37+
})[0];
38+
if (!patchArtifact) {
39+
core.info('No patch to apply.');
40+
return;
41+
}
42+
let download = await github.rest.actions.downloadArtifact({
43+
owner: context.repo.owner,
44+
repo: context.repo.repo,
45+
artifact_id: patchArtifact.id,
46+
archive_format: 'zip'
47+
});
48+
const fs = require('fs');
49+
const path = require('path');
50+
const temp = '${{ runner.temp }}/artifacts';
51+
if (!fs.existsSync(temp)){
52+
fs.mkdirSync(temp);
53+
}
54+
fs.writeFileSync(path.join(temp, 'patch.zip'), Buffer.from(download.data));
55+
core.setOutput("exists", "true");
56+
57+
- id: get-pr-number
58+
name: Get PR number
59+
uses: actions/github-script@v7.0.1
60+
with:
61+
script: |
62+
const workflowRunUrl = context.payload.workflow_run.url;
63+
const response = await github.request(`${workflowRunUrl}`);
64+
const prNumber = response.data.pull_requests && response.data.pull_requests[0] ? response.data.pull_requests[0].number : null;
65+
core.setOutput('pr-number', prNumber);
66+
67+
- name: Unzip patch
68+
if: steps.download-patch.outputs.exists == 'true'
69+
working-directory: ${{ runner.temp }}/artifacts
70+
run: unzip patch.zip
71+
72+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
73+
if: steps.download-patch.outputs.exists == 'true'
74+
75+
- name: Check out PR branch
76+
if: steps.download-patch.outputs.exists == 'true'
77+
env:
78+
GH_TOKEN: ${{ github.token }}
79+
run: gh pr checkout ${{ steps.get-pr-number.outputs.pr-number }}
80+
81+
- name: Use CLA approved github bot
82+
if: steps.download-patch.outputs.exists == 'true'
83+
# IMPORTANT do not call the .github/scripts/use-cla-approved-bot.sh
84+
# since that script could have been compromised in the PR branch
85+
run: |
86+
git config user.name otelbot
87+
git config user.email 197425009+otelbot@users.noreply.github.com
88+
89+
- uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6
90+
if: steps.download-patch.outputs.exists == 'true'
91+
id: otelbot-token
92+
with:
93+
app-id: ${{ vars.OTELBOT_APP_ID }}
94+
private-key: ${{ secrets.OTELBOT_PRIVATE_KEY }}
95+
96+
- name: Apply patch and push
97+
if: steps.download-patch.outputs.exists == 'true'
98+
env:
99+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
100+
run: |
101+
git apply "${{ runner.temp }}/artifacts/patch"
102+
git commit -a -m "./gradlew spotlessApply"
103+
git push
104+
105+
- if: steps.download-patch.outputs.exists == 'true' && success()
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 \`./gradlew spotlessApply\` was committed to the PR branch."
110+
111+
- if: steps.download-patch.outputs.exists == 'true' && failure()
112+
env:
113+
GH_TOKEN: ${{ steps.otelbot-token.outputs.token }}
114+
run: |
115+
gh pr comment ${{ steps.get-pr-number.outputs.pr-number }} --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."
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

.github/workflows/auto-spotless.yml

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

0 commit comments

Comments
 (0)