Skip to content

Auto spotless, part 2 #6

Auto spotless, part 2

Auto spotless, part 2 #6

name: Auto spotless, part 2
on:
workflow_run:
workflows:
- "Auto spotless, part 1"
types:
- completed
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: read
jobs:
apply:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- id: download-patch
name: Download patch
uses: actions/[email protected]
with:
# this script copied from
# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#using-data-from-the-triggering-workflow
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id
});
let patchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "patch"
})[0];
if (!patchArtifact) {
core.info('No patch to apply.');
return;
}
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: patchArtifact.id,
archive_format: 'zip'
});
const fs = require('fs');
const path = require('path');
const temp = '${{ runner.temp }}/artifacts';
if (!fs.existsSync(temp)){
fs.mkdirSync(temp);
}
fs.writeFileSync(path.join(temp, 'patch.zip'), Buffer.from(download.data));
core.setOutput("exists", "true");
- id: get-pr-number
name: Get PR number
uses: actions/[email protected]
with:
script: |
const workflowRunUrl = context.payload.workflow_run.url;
const response = await github.request(`${workflowRunUrl}`);
const prNumber = response.data.pull_requests && response.data.pull_requests[0] ? response.data.pull_requests[0].number : null;
core.setOutput('pr-number', prNumber);
- name: Unzip patch
if: steps.download-patch.outputs.exists == 'true'
working-directory: ${{ runner.temp }}/artifacts
run: unzip patch.zip
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
if: steps.download-patch.outputs.exists == 'true'
- name: Check out PR branch
if: steps.download-patch.outputs.exists == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: gh pr checkout ${{ steps.get-pr-number.outputs.pr-number }}
- name: Use CLA approved github bot
if: steps.download-patch.outputs.exists == 'true'
# IMPORTANT do not call the .github/scripts/use-cla-approved-bot.sh
# since that script could have been compromised in the PR branch
run: |
git config user.name otelbot
git config user.email [email protected]
- name: Apply patch and push
if: steps.download-patch.outputs.exists == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
git apply "${{ runner.temp }}/artifacts/patch"
git commit -a -m "./gradlew spotlessApply"
git push
- if: steps.download-patch.outputs.exists == 'true' && success()
env:
GH_TOKEN: ${{ github.token }}
run: |
gh pr comment ${{ steps.get-pr-number.outputs.pr-number }} --body "🔧 The result from \`./gradlew spotlessApply\` was committed to the PR branch."
- if: steps.download-patch.outputs.exists == 'true' && failure()
env:
GH_TOKEN: ${{ github.token }}
run: |
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."