Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: improve PR validation with specific error messages
  • Loading branch information
hahirwar-cd committed Mar 10, 2026
commit 635fa97449554d7d70a502e1e8f2f07c7a8f829c
45 changes: 27 additions & 18 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:
subjectPattern:
required: false
type: string
default: '^[A-Za-z].+$'
default: '[A-Za-z].+'
validateSingleCommit:
required: false
type: boolean
Expand All @@ -43,30 +43,39 @@ jobs:
- name: Validate PR title format
run: |
TITLE="${{ github.event.pull_request.title }}"

echo "PR Title: $TITLE"

TYPES=$(echo "${{ inputs.types }}" | tr '\n' '|' | sed 's/|$//')

if [ "${{ inputs.requireScope }}" = "true" ]; then
PATTERN="^(${TYPES}|$(echo ${TYPES} | sed 's/\([^|]*\)/\u\1|\l\1/g'))(\([a-zA-Z0-9_-]+\)): ${{ inputs.subjectPattern }}"
else
PATTERN="^(${TYPES}|$(echo ${TYPES} | sed 's/\([^|]*\)/\u\1|\l\1/g'))(\([a-zA-Z0-9_-]+\))?: ${{ inputs.subjectPattern }}"
# Check valid type
TYPE_REGEX="^(${TYPES}|$(echo ${TYPES} | sed 's/\([^|]*\)/\u\1|\l\1/g'))"

if [[ ! "$TITLE" =~ $TYPE_REGEX ]]; then
echo "::error::Invalid PR type."
echo "Allowed types:"
echo "${{ inputs.types }}"
exit 1
fi

if [[ ! "$TITLE" =~ $PATTERN ]]; then
echo "::error::PR title does not follow Conventional Commits format."
echo "Valid examples:"
# Check scope requirement
if [[ "${{ inputs.requireScope }}" == "true" ]]; then
if [[ ! "$TITLE" =~ ^(${TYPES}|$(echo ${TYPES} | sed 's/\([^|]*\)/\u\1|\l\1/g'))\([a-zA-Z0-9_-]+\): ]]; then
echo "::error::PR title must include a scope because requireScope=true."
echo "Example:"
echo " feat(auth): Add login API"
exit 1
fi
fi

# Extract subject
SUBJECT=$(echo "$TITLE" | sed 's/^.*: //')

# Validate subject pattern
if [[ ! "$SUBJECT" =~ ${{ inputs.subjectPattern }} ]]; then
echo "::error::PR subject does not match required pattern."
echo "Subject must match regex: ${{ inputs.subjectPattern }}"
echo "Example:"
echo " feat: Add login API"
echo " Feat: Add login API"
echo " fix(auth): Resolve token issue"
echo " Fix(auth): Resolve token issue"
echo " docs: Update README"
echo " Docs(readme): Update README formatting"
echo " chore: Update dependencies"
echo " Chore(ci): Update CI pipeline"
echo " refactor(core): Simplify authentication logic"
echo " Refactor(core): Simplify authentication logic"
exit 1
fi

Expand Down