Comment on PR for CI Failure #94
This file contains 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
name: Comment on PR for CI Failure | |
on: | |
workflow_run: | |
workflows: [ CI ] | |
types: [ completed ] | |
permissions: | |
pull-requests: write | |
jobs: | |
comment-on-failure: | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'failure' }} | |
steps: | |
- name: Get PR Information | |
uses: actions/github-script@v7 | |
id: get-pr | |
with: | |
script: | | |
const response = await github.rest.repos.listPullRequestsAssociatedWithCommit({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
commit_sha: '${{ github.event.workflow_run.head_sha }}' | |
}); | |
return response.data[0].number; | |
- name: Manage Comments | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const prNumber = ${{ steps.get-pr.outputs.result }}; | |
const workflowUrl = '${{ github.event.workflow_run.html_url }}'; | |
const workflowName = '${{ github.event.workflow_run.name }}'; | |
const comments = await github.rest.issues.listComments({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: prNumber | |
}); | |
for (const comment of comments.data) { | |
if (comment.body.includes('❌ The job') && comment.user.type === 'Bot') { | |
await github.rest.issues.deleteComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
comment_id: comment.id | |
}); | |
break; | |
} | |
} | |
const message = `❌ The workflow \`${workflowName}\` failed during the most recent run. | |
Please check the [workflow run](${workflowUrl}) for more details.`; | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: prNumber, | |
body: message | |
}); |