Add Github action that verify PR and commit message prefix #5
Workflow file for this run
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
--- | |
name: Check PR Prefix | |
on: | |
pull_request: | |
types: [opened, synchronize, edited, reopened] | |
jobs: | |
check-prefix: | |
runs-on: ubuntu-latest | |
permissions: | |
pull-requests: read | |
contents: read | |
issues: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
- name: Authenticate GitHub CLI | |
env: | |
GH_TOKEN: ${{ github.token }} | |
run: gh auth status || gh auth login --with-token <<< "$GH_TOKEN" | |
- name: Check for required prefixes | |
env: | |
GH_TOKEN: ${{ github.token }} | |
PR_NUMBER: ${{ github.event.pull_request.number }} | |
run: | | |
set -euo pipefail | |
echo "Checking PR #$PR_NUMBER for required role prefixes..." | |
# Fetch PR title and commit messages | |
PR_TITLE=$(gh pr view "$PR_NUMBER" --json title -q '.title') | |
COMMIT_MSGS=$(gh pr view "$PR_NUMBER" --json commits -q '.commits[].messageHeadline' | tr '\n' ' ') | |
ALL_TEXT="$PR_TITLE $COMMIT_MSGS" | |
# Fetch changed files and extract unique role names | |
CHANGED_FILES=$(gh pr diff "$PR_NUMBER" --name-only) | |
AFFECTED_ROLES=$(echo "$CHANGED_FILES" | grep '^roles/' | awk -F/ 'NF>2 {print $2}' | sort -u) | |
if [ -z "$AFFECTED_ROLES" ]; then | |
echo "No roles changed - skipping check." | |
exit 0 | |
fi | |
echo "Roles affected: $AFFECTED_ROLES" | |
echo "PR title: $PR_TITLE" | |
MISSING=() | |
for ROLE in $AFFECTED_ROLES; do | |
# Sanitize role name for grep (in case of special characters) | |
SANITIZED_ROLE=$(echo "$ROLE" | sed 's/[^a-zA-Z0-9_-]/-/g') | |
if ! echo "$ALL_TEXT" | grep -Fiq "\[$SANITIZED_ROLE\]"; then | |
MISSING+=("[$ROLE]") | |
fi | |
done | |
if [ ${#MISSING[@]} -gt 0 ]; then | |
echo "::error::Missing required prefix(es): ${MISSING[*]} in PR title or commit messages." | |
# Optionally comment on PR | |
gh pr comment "$PR_NUMBER" --body "Missing required prefix(es): ${MISSING[*]}. Please include these in the PR title or commit messages." | |
exit 1 | |
fi | |
echo "All required prefixes are present." |