Skip to content

feat(pr-title): Add commitlint scopes #573

feat(pr-title): Add commitlint scopes

feat(pr-title): Add commitlint scopes #573

Workflow file for this run

name: Validate PR title
on:
pull_request:
types: [opened, edited, reopened, synchronize]
branches: [main]
workflow_call: {}
# Disable permissions for all available scopes
permissions: {}
concurrency:
group: ${{ github.workflow }}-${{ github.head_ref }}
cancel-in-progress: true
defaults:
run:
shell: bash
env:
MAX_PR_TITLE_LENGTH: 72
jobs:
pr-title-length:
permissions:
pull-requests: write
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Get PR title length
id: pr-title-length
env:
GH_TOKEN: ${{ github.token }}
run: |
echo "PR_TITLE_LENGTH=$(gh api /repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }} | jq '.title' | wc -c)" >> "$GITHUB_ENV"
- name: Add PR comment on failure
# Only add a comment if the PR title is too long
# fromJson is required to convert the string to a number
if: ${{ fromJson(env.PR_TITLE_LENGTH) > fromJson(env.MAX_PR_TITLE_LENGTH) }}
id: pr-comment
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0
with:
header: pr-title-error
message: |
Hi and thank you for opening this pull request! πŸ‘‹πŸΌ
We require pull request titles be limited to ${{ env.MAX_PR_TITLE_LENGTH }} characters.
This is because we use `Squash and Merge` for pull requests with the title as the commit message.
Please update the title to be ${{ env.MAX_PR_TITLE_LENGTH }} characters or less. If you need help, feel free to ask! 😊
- name: Exit if PR title is too long
if: ${{ fromJson(env.PR_TITLE_LENGTH) > fromJson(env.MAX_PR_TITLE_LENGTH) }}
run: |
echo "### :x: Pull Request title is invalid" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "The pull request title is too long. Please update the title to be ${{ env.MAX_PR_TITLE_LENGTH }} characters or less." >> $GITHUB_STEP_SUMMARY
exit 1
pr-title-convention:
# Run if PR title length job does not exit
needs: [pr-title-length]
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout repository
uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0
- name: Get commitlint scopes from configuration file
id: commit_scope
run: |
{
echo 'COMMIT_SCOPES<<EOF'
jq -r '.rules["scope-enum"][2][]' .commitlintrc.json
echo EOF
} >> "$GITHUB_ENV"
- name: Validate PR title conforms to conventional spec
id: validate-pr-title
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Cannot test with act. See:
# https://github.com/amannn/action-semantic-pull-request/issues/248
uses: amannn/action-semantic-pull-request@0723387faaf9b38adef4775cd42cfd5155ed6017 # v5.5.3
with:
requireScope: true
scopes: |
${{ env.COMMIT_SCOPES }}
subjectPattern: ^([A-Z]).+$ # Subject must start with an upper case character
subjectPatternError: |
The subject "{subject}" found in the pull request title
"{title}" didn't match the configured pattern.
The subject must be sentence case.
Please start the subject with an upper case character.
- name: Add PR comment on failure
# Only add a comment if the error message is not null or the PR title is too long
if: ${{ always() && steps.validate-pr-title.outputs.error_message != null }}
id: pr-comment
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0
with:
header: pr-title-error
message: |
Hi and thank you for opening this pull request! πŸ‘‹πŸΌ
We require pull request titles to follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) and it looks like your proposed title needs to be adjusted.
Details:
```
${{ steps.validate-pr-title.outputs.error_message }}
```
Please update the title to follow the conventional commit specification. If you need help, feel free to ask! 😊
- name: Delete PR comment on resolution
# Delete comment if the error message is null or the PR title is the correct length
if: ${{ always() && steps.validate-pr-title.outputs.error_message == null }}
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 # v2.9.0
with:
header: pr-title-error
delete: true
- name: Summary with valid title
# A length check is not required here because the validate-pr-title step will only run if the title is less than or equal to the max length
if: ${{ always() && steps.validate-pr-title.outputs.error_message == null }}
run: |
echo "### :white_check_mark: Pull Request title is valid" >> $GITHUB_STEP_SUMMARY
echo "The pull request title conforms to the conventional commit specification." >> $GITHUB_STEP_SUMMARY
- name: Summary with invalid title
if: ${{ always() && steps.validate-pr-title.outputs.error_message != null }}
env:
# Set to comment ID if a comment was created, otherwise set to previous comment ID
COMMENT_ID: ${{ steps.pr-comment.outputs.created_comment_id || steps.pr-comment.outputs.previous_comment_id }}
PR_URL: ${{ github.event.pull_request.html_url }}
run: |
echo "### :x: Pull Request title is invalid" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "The pull request title does not conform to the conventional commit specification." >> $GITHUB_STEP_SUMMARY
echo "Please see pull request comments for more details on how to resolve this:" >> $GITHUB_STEP_SUMMARY
echo "[Pull Request Comment](${{ env.PR_URL }}#issuecomment-${{ env.COMMENT_ID }})" >> $GITHUB_STEP_SUMMARY