Skip to content

Comment on PR Title Failure #3

Comment on PR Title Failure

Comment on PR Title Failure #3

name: Comment on PR Title Failure
on:
workflow_run:
workflows: ["Validate PR Title"]
types: [completed]
jobs:
comment:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
# Step taken from: https://github.com/orgs/community/discussions/25220#discussioncomment-11316244
- name: Find PR info
id: pr-context
env:
GH_TOKEN: ${{ github.token }}
PR_TARGET_REPO: ${{ github.repository }}
# If the PR is from a fork, prefix it with `<owner-login>:`, otherwise only the PR branch name is relevant:
PR_BRANCH: |-
${{
(github.event.workflow_run.head_repository.owner.login != github.event.workflow_run.repository.owner.login)
&& format('{0}:{1}', github.event.workflow_run.head_repository.owner.login, github.event.workflow_run.head_branch)
|| github.event.workflow_run.head_branch
}}
# Query the PR number by repo + branch, then assign to step output:
run: |
gh pr view --repo "${PR_TARGET_REPO}" "${PR_BRANCH}" \
--json 'number,title' --jq '"number=\(.number)\ntitle=\(.title)"' \
>> "${GITHUB_OUTPUT}"
- name: Find existing comment
id: find
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ steps.pr-context.outputs.number }}
comment-author: 'github-actions[bot]'
body-includes: PR title does not match the required pattern
- name: Post or update failure comment
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
uses: peter-evans/create-or-update-comment@v4
env:
PR_TITLE: ${{ steps.pr-context.outputs.title }}
with:
comment-id: ${{ steps.find.outputs.comment-id }}
issue-number: ${{ steps.pr-context.outputs.number }}
body: |
PR title does not match the required pattern. Please ensure you follow the [conventional commits](https://www.conventionalcommits.org/) spec.
Your title should start with `feat:`, `fix:`, `chore:`, `docs:`, `perf:`, `refactor:`, `test:`, or `ci:`, and if it's a breaking change that should be suffixed with a `!` (like `feat!:`), and then a 1-72 character brief description of your change.
**Title:** `${{ env.PR_TITLE }}`
- name: Delete comment on success
if: ${{ github.event.workflow_run.conclusion == 'success' && steps.find.outputs.comment-id != '' }}
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api repos/${{ github.repository }}/issues/comments/${{ steps.find.outputs.comment-id }} -X DELETE