Skip to content

Comment on PR for CI Failure #94

Comment on PR for CI Failure

Comment on PR for CI Failure #94

Workflow file for this run

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
});