|  | 
|  | 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 | + | 
|  | 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("exists", "true"); | 
|  | 57 | +            core.setOutput("pr-number", patchArtifact.name.substring("patch-".length)); | 
|  | 58 | +
 | 
|  | 59 | +      - name: Unzip patch | 
|  | 60 | +        if: steps.download-patch.outputs.exists == 'true' | 
|  | 61 | +        run: unzip patch.zip -d "${{ runner.temp }}/artifacts" | 
|  | 62 | + | 
|  | 63 | +      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | 
|  | 64 | +        if: steps.download-patch.outputs.exists == 'true' | 
|  | 65 | + | 
|  | 66 | +      - name: Check out PR branch | 
|  | 67 | +        if: steps.download-patch.outputs.exists == 'true' | 
|  | 68 | +        env: | 
|  | 69 | +          GH_TOKEN: ${{ github.token }} | 
|  | 70 | +        run: gh pr checkout ${{ steps.download-patch.outputs.pr-number }} | 
|  | 71 | + | 
|  | 72 | +      - name: Use CLA approved github bot | 
|  | 73 | +        if: steps.download-patch.outputs.exists == 'true' | 
|  | 74 | +        # IMPORTANT do not call the .github/scripts/use-cla-approved-bot.sh | 
|  | 75 | +        # since that script could have been compromised in the PR branch | 
|  | 76 | +        run: | | 
|  | 77 | +          git config user.name otelbot | 
|  | 78 | +          git config user.email [email protected] | 
|  | 79 | +
 | 
|  | 80 | +      - uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v2.0.6 | 
|  | 81 | +        if: steps.download-patch.outputs.exists == 'true' | 
|  | 82 | +        id: otelbot-token | 
|  | 83 | +        with: | 
|  | 84 | +          app-id: ${{ vars.OTELBOT_APP_ID }} | 
|  | 85 | +          private-key: ${{ secrets.OTELBOT_PRIVATE_KEY }} | 
|  | 86 | + | 
|  | 87 | +      - name: Apply patch and push | 
|  | 88 | +        if: steps.download-patch.outputs.exists == 'true' | 
|  | 89 | +        env: | 
|  | 90 | +          GH_TOKEN: ${{ steps.otelbot-token.outputs.token }} | 
|  | 91 | +        run: | | 
|  | 92 | +          git apply "${{ runner.temp }}/artifacts/patch" | 
|  | 93 | +          git commit -a -m "./gradlew spotlessApply" | 
|  | 94 | +          git push | 
|  | 95 | +
 | 
|  | 96 | +      - if: steps.download-patch.outputs.exists == 'true' && success() | 
|  | 97 | +        env: | 
|  | 98 | +          GH_TOKEN: ${{ steps.otelbot-token.outputs.token }} | 
|  | 99 | +        run: | | 
|  | 100 | +          gh pr comment ${{ steps.download-patch.outputs.pr-number }} --body "🔧 The result from \`./gradlew spotlessApply\` was committed to the PR branch." | 
|  | 101 | +
 | 
|  | 102 | +      - if: steps.download-patch.outputs.exists == 'true' && failure() | 
|  | 103 | +        env: | 
|  | 104 | +          GH_TOKEN: ${{ steps.otelbot-token.outputs.token }} | 
|  | 105 | +        run: | | 
|  | 106 | +          gh pr comment ${{ steps.download-patch.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." | 
0 commit comments