Skip to content

SH-101-kratos

SH-101-kratos #51

name: Validate Branch and PR Naming / your-job (pull_request)
on:
pull_request:
types: [opened, synchronize, reopened]
permissions:
contents: read
pull-requests: write
jobs:
your-job:
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Get PR details and validate ticket ID
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
echo "PR Number: $PR_NUMBER"
# Get the PR title using GitHub CLI
PR_TITLE=$(gh pr view "$PR_NUMBER" --json title -q ".title")
echo "PR title from GitHub: '$PR_TITLE'"
# Trim leading and trailing spaces from the PR title
PR_TITLE=$(echo "$PR_TITLE" | xargs)
echo "Trimmed PR title: '$PR_TITLE'"
# Validate PR title includes a JIRA ticket ID (e.g., SH-234)
if [[ ! "$PR_TITLE" =~ ^[A-Z]+-[0-9]+ ]]; then
echo "PR title must include a JIRA ticket ID (e.g., SH-234)."
exit 1 # Fail the action
fi
# Normalize PR ticket ID to lowercase
PR_TICKET=$(echo "$PR_TITLE" | grep -oE '^[A-Z]+-[0-9]+' | tr '[:upper:]' '[:lower:]')
# Fetch the branch name
BRANCH_NAME="${GITHUB_HEAD_REF:-$(git rev-parse --abbrev-ref HEAD)}"
# Normalize branch ticket ID to lowercase
BRANCH_TICKET=$(echo "$BRANCH_NAME" | grep -oE '^[A-Z]+-[0-9]+' | tr '[:upper:]' '[:lower:]')
# Debugging output for branch and PR ticket IDs
echo "Branch name: '$BRANCH_NAME'"
echo "Extracted branch ticket ID: '$BRANCH_TICKET'"
echo "Extracted PR ticket ID: '$PR_TICKET'"
if [[ "$BRANCH_TICKET" != "$PR_TICKET" ]]; then
echo "Mismatch: Branch ticket ID ($BRANCH_TICKET) does not match PR ticket ID ($PR_TICKET)."
exit 1 # Fail the action
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}