PR Commenter #11
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
| --- | |
| # ============================================================================= | |
| # pr_comment.yml | |
| # | |
| # Triggers: | |
| # workflow_run → CI — Development (completed) | |
| # | |
| # Resolves the GitHub Actions fork permission issue. Workflows triggered | |
| # via `pull_request` from a fork do not have `write` permissions to post | |
| # comments. `workflow_run` executes in the context of the base repository. | |
| # ============================================================================= | |
| name: "PR Commenter" | |
| on: | |
| workflow_run: | |
| workflows: | |
| - "CI — Development" | |
| types: | |
| - completed | |
| permissions: | |
| pull-requests: write | |
| actions: read | |
| jobs: | |
| post_comment: | |
| name: "Post PR Status Comment" | |
| runs-on: ubuntu-latest | |
| if: > | |
| github.event.workflow_run.event == 'pull_request' | |
| steps: | |
| - name: "Download PR Number Artifact" | |
| # Wait, how does it know the PR number? The `development.yml` needs to upload the PR number as an artifact | |
| # so this workflow can read it. Let's write the script to find the PR associated with the commit. | |
| uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7 | |
| with: | |
| script: |- | |
| const workflowRun = context.payload.workflow_run; | |
| // Get the pull request associated with the commit | |
| const response = await github.rest.search.issuesAndPullRequests({ | |
| q: `is:pr repo:${context.repo.owner}/${context.repo.repo} sha:${workflowRun.head_sha}` | |
| }); | |
| if (response.data.total_count === 0) { | |
| console.log('No PR found for this commit.'); | |
| return; | |
| } | |
| const prNumber = response.data.items[0].number; | |
| const runUrl = workflowRun.html_url; | |
| const status = workflowRun.conclusion; | |
| let body = `## CI Development Pipeline Status\n\n`; | |
| if (status === 'success') { | |
| body += `✅ **Pipeline**: Completed successfully. [View Run Details](${runUrl})\n`; | |
| } else { | |
| body += `❌ **Pipeline**: Failed or was cancelled. [Check logs](${runUrl})\n`; | |
| } | |
| // Post comment | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: body | |
| }); |