|
| 1 | +# File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details. |
| 2 | +name: Castiron custom code comment |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_run: |
| 6 | + workflows: [Castiron custom code] |
| 7 | + types: [completed] |
| 8 | + |
| 9 | +permissions: {} |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: castiron-custom-code-comment-${{ github.event.workflow_run.head_repository.id }}-${{ github.event.workflow_run.head_branch }} |
| 13 | + cancel-in-progress: false |
| 14 | + |
| 15 | +jobs: |
| 16 | + comment: |
| 17 | + name: Update custom-code comment |
| 18 | + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.path == '.github/workflows/castiron-custom-code.yml' |
| 19 | + runs-on: ubuntu-latest |
| 20 | + timeout-minutes: 5 |
| 21 | + permissions: |
| 22 | + contents: read |
| 23 | + actions: read |
| 24 | + pull-requests: write |
| 25 | + steps: |
| 26 | + - name: Check out the trusted publisher |
| 27 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 |
| 28 | + with: |
| 29 | + ref: ${{ github.workflow_sha }} |
| 30 | + persist-credentials: false |
| 31 | + |
| 32 | + - name: Download the completed run's report |
| 33 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 34 | + with: |
| 35 | + github-token: ${{ github.token }} |
| 36 | + run-id: ${{ github.event.workflow_run.id }} |
| 37 | + name: castiron-custom-code-${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }} |
| 38 | + path: ${{ runner.temp }}/castiron-custom-code |
| 39 | + |
| 40 | + - name: Validate report context |
| 41 | + id: context |
| 42 | + env: |
| 43 | + REPOSITORY: ${{ github.repository }} |
| 44 | + RUN_ID: ${{ github.event.workflow_run.id }} |
| 45 | + RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }} |
| 46 | + run: | |
| 47 | + number=$(jq -er --arg repository "$REPOSITORY" --argjson run "$RUN_ID" \ |
| 48 | + --argjson attempt "$RUN_ATTEMPT" \ |
| 49 | + 'select(.repository == $repository and .run == $run and .attempt == $attempt) | .pr | select(type == "number" and . > 0 and . == floor)' \ |
| 50 | + "$RUNNER_TEMP/castiron-custom-code/context.json") |
| 51 | + printf 'number=%s\n' "$number" >> "$GITHUB_OUTPUT" |
| 52 | +
|
| 53 | + - name: Create or update the single report comment |
| 54 | + id: publish |
| 55 | + env: |
| 56 | + GH_TOKEN: ${{ github.token }} |
| 57 | + REPOSITORY: ${{ github.repository }} |
| 58 | + PR_NUMBER: ${{ steps.context.outputs.number }} |
| 59 | + RUN_ID: ${{ github.event.workflow_run.id }} |
| 60 | + RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }} |
| 61 | + run: | |
| 62 | + python3 -I scripts/castiron/custom_code_report.py comment \ |
| 63 | + --report "$RUNNER_TEMP/castiron-custom-code/report.json" \ |
| 64 | + --repository "$REPOSITORY" --pr "$PR_NUMBER" --run-id "$RUN_ID" \ |
| 65 | + --run-attempt "$RUN_ATTEMPT" |
| 66 | +
|
| 67 | + - name: Publish a trusted failure status |
| 68 | + if: always() && !cancelled() && steps.publish.outcome != 'success' |
| 69 | + uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 |
| 70 | + with: |
| 71 | + script: | |
| 72 | + const marker = '<!-- castiron:custom-code-report:v1 -->'; |
| 73 | + const run = context.payload.workflow_run; |
| 74 | + if (run.event !== 'pull_request' || run.path !== '.github/workflows/castiron-custom-code.yml') return; |
| 75 | + const pulls = run.pull_requests?.length ? run.pull_requests : await github.paginate(github.rest.repos.listPullRequestsAssociatedWithCommit, {...context.repo, commit_sha: run.head_sha}); |
| 76 | + for (const pull of pulls) { |
| 77 | + const {data: current} = await github.rest.pulls.get({...context.repo, pull_number: pull.number}); |
| 78 | + if (current.state !== 'open' || current.head.sha !== run.head_sha) continue; |
| 79 | + const comments = await github.paginate(github.rest.issues.listComments, {...context.repo, issue_number: pull.number}); |
| 80 | + const previous = comments.find(c => c.user?.type === 'Bot' && c.user?.login === 'github-actions[bot]' && c.body?.startsWith(marker)); |
| 81 | + const prior = previous?.body?.match(/<!-- castiron:run:v1:(\d+):(\d+) -->/); |
| 82 | + if (prior && (Number(prior[1]) > run.id || (Number(prior[1]) === run.id && Number(prior[2]) > run.run_attempt))) continue; |
| 83 | + const url = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${run.id}`; |
| 84 | + const body = `${marker}\n\n## Castiron custom code\n\n⚠️ Report unavailable for \`${run.head_sha.slice(0, 12)}\`.\n\nThe report setup or validation failed. [Inspect the workflow run](${url}).\n\n<!-- castiron:run:v1:${run.id}:${run.run_attempt} -->`; |
| 85 | + if (previous) await github.rest.issues.updateComment({...context.repo, comment_id: previous.id, body}); |
| 86 | + else await github.rest.issues.createComment({...context.repo, issue_number: pull.number, body}); |
| 87 | + } |
0 commit comments