Castiron custom code comment #7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # File generated from our OpenAPI spec by Castiron. See CONTRIBUTING.md for details. | |
| name: Castiron custom code comment | |
| on: | |
| workflow_run: | |
| workflows: [Castiron custom code] | |
| types: [completed] | |
| permissions: {} | |
| concurrency: | |
| group: castiron-custom-code-comment-${{ github.event.workflow_run.head_repository.id }}-${{ github.event.workflow_run.head_branch }} | |
| cancel-in-progress: false | |
| jobs: | |
| comment: | |
| name: Update custom-code comment | |
| if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.path == '.github/workflows/castiron-custom-code.yml' | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 5 | |
| permissions: | |
| contents: read | |
| actions: read | |
| pull-requests: write | |
| steps: | |
| - name: Check out the trusted publisher | |
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 | |
| with: | |
| ref: ${{ github.workflow_sha }} | |
| persist-credentials: false | |
| - name: Download the completed run's report | |
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 | |
| with: | |
| github-token: ${{ github.token }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| name: castiron-custom-code-${{ github.event.workflow_run.id }}-${{ github.event.workflow_run.run_attempt }} | |
| path: ${{ runner.temp }}/castiron-custom-code | |
| - name: Validate report context | |
| id: context | |
| env: | |
| REPOSITORY: ${{ github.repository }} | |
| RUN_ID: ${{ github.event.workflow_run.id }} | |
| RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }} | |
| run: | | |
| number=$(jq -er --arg repository "$REPOSITORY" --argjson run "$RUN_ID" \ | |
| --argjson attempt "$RUN_ATTEMPT" \ | |
| 'select(.repository == $repository and .run == $run and .attempt == $attempt) | .pr | select(type == "number" and . > 0 and . == floor)' \ | |
| "$RUNNER_TEMP/castiron-custom-code/context.json") | |
| printf 'number=%s\n' "$number" >> "$GITHUB_OUTPUT" | |
| - name: Create or update the single report comment | |
| id: publish | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| REPOSITORY: ${{ github.repository }} | |
| PR_NUMBER: ${{ steps.context.outputs.number }} | |
| RUN_ID: ${{ github.event.workflow_run.id }} | |
| RUN_ATTEMPT: ${{ github.event.workflow_run.run_attempt }} | |
| run: | | |
| python3 -I scripts/castiron/custom_code_report.py comment \ | |
| --report "$RUNNER_TEMP/castiron-custom-code/report.json" \ | |
| --repository "$REPOSITORY" --pr "$PR_NUMBER" --run-id "$RUN_ID" \ | |
| --run-attempt "$RUN_ATTEMPT" | |
| - name: Publish a trusted failure status | |
| if: always() && !cancelled() && steps.publish.outcome != 'success' | |
| uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 | |
| with: | |
| script: | | |
| const marker = '<!-- castiron:custom-code-report:v1 -->'; | |
| const run = context.payload.workflow_run; | |
| if (run.event !== 'pull_request' || run.path !== '.github/workflows/castiron-custom-code.yml') return; | |
| const pulls = run.pull_requests?.length ? run.pull_requests : await github.paginate(github.rest.repos.listPullRequestsAssociatedWithCommit, {...context.repo, commit_sha: run.head_sha}); | |
| for (const pull of pulls) { | |
| const {data: current} = await github.rest.pulls.get({...context.repo, pull_number: pull.number}); | |
| if (current.state !== 'open' || current.head.sha !== run.head_sha) continue; | |
| const comments = await github.paginate(github.rest.issues.listComments, {...context.repo, issue_number: pull.number}); | |
| const previous = comments.find(c => c.user?.type === 'Bot' && c.user?.login === 'github-actions[bot]' && c.body?.startsWith(marker)); | |
| const prior = previous?.body?.match(/<!-- castiron:run:v1:(\d+):(\d+) -->/); | |
| if (prior && (Number(prior[1]) > run.id || (Number(prior[1]) === run.id && Number(prior[2]) > run.run_attempt))) continue; | |
| const url = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${run.id}`; | |
| 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} -->`; | |
| if (previous) await github.rest.issues.updateComment({...context.repo, comment_id: previous.id, body}); | |
| else await github.rest.issues.createComment({...context.repo, issue_number: pull.number, body}); | |
| } |