Skip to content

Commit 882a9f8

Browse files
FIX : Branch name validator fixed #2042
Sample Ticket for reference #2042
1 parent 87ba1e9 commit 882a9f8

File tree

1 file changed

+59
-7
lines changed

1 file changed

+59
-7
lines changed

Diff for: .github/workflows/branch-name-check.yml

+59-7
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,79 @@
1-
name: Branch Name Validation
1+
name: Branch and PR Name Validation
22

33
on:
44
push:
55
branches:
66
- master
77
- develop
8+
- console
9+
810
pull_request:
911
branches:
1012
- master
1113
- develop
14+
- console
15+
16+
types:
17+
- opened
18+
- edited
19+
- reopened
1220

1321
jobs:
14-
validate-branch-name:
22+
validate-names:
1523
runs-on: ubuntu-latest
1624
steps:
1725
- name: Checkout code
1826
uses: actions/checkout@v3
19-
27+
2028
- name: Validate branch name
2129
run: |
22-
branch_name=$(echo "${GITHUB_REF#refs/heads/}")
23-
pattern="^(FEATURE|BUGFIX|RELEASE)\/(HCMPRE|DPG|SN)-[0-9]{1,5}$"
24-
if [[ ! "$branch_name" =~ $pattern ]]; then
25-
echo "Branch name '$branch_name' does not follow the correct pattern: FEATURE/HCMPRE-<TICKET_NO>, FEATURE/DPG-<TICKET_NO>, FEATURE/SN-<TICKET_NO>, BUGFIX/SN-<TICKET_NO>, BUGFIX/HCMPRE-<TICKET_NO>, or BUGFIX/DPG-<TICKET_NO> where x is a number between 0 and 99999"
30+
# Determine the branch name
31+
if [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
32+
branch_name="${GITHUB_HEAD_REF}"
33+
else
34+
branch_name="${GITHUB_REF#refs/heads/}"
35+
fi
36+
37+
# Define the branch name pattern
38+
PREFIXES="FEATURE|BUGFIX|RELEASE"
39+
PROJECTS="HCMPRE|DPG|SN"
40+
TICKET_PATTERN="[0-9]{1,5}"
41+
BRANCH_PATTERN="^($PREFIXES)\/($PROJECTS)-$TICKET_PATTERN$"
42+
43+
# Validate the branch name
44+
if [[ ! "$branch_name" =~ $BRANCH_PATTERN ]]; then
45+
echo "Branch name '$branch_name' does not follow the correct pattern: $PREFIXES/$PROJECTS-<TICKET_NO> where <TICKET_NO> is $TICKET_PATTERN"
2646
exit 1
2747
fi
48+
49+
- name: Validate PR title
50+
if: ${{ github.event_name == 'pull_request' }} # Only for PR validation
51+
run: |
52+
# Define constants
53+
PREFIXES="FEATURE|BUGFIX|RELEASE"
54+
PROJECTS="HCMPRE|DPG|SN"
55+
TICKET_PATTERN="[0-9]{1,5}"
56+
TITLE_PATTERN="^($PREFIXES)\/($PROJECTS)-$TICKET_PATTERN.*$"
57+
MIN_TITLE_LENGTH=30
58+
59+
60+
# Fetch the latest PR title dynamically
61+
pr_title=$(curl -s https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }} | jq -r '.title')
62+
echo "Fetched PR title: $pr_title"
63+
64+
# Validate the PR title
65+
if [[ ! "$pr_title" =~ $TITLE_PATTERN ]]; then
66+
echo "PR title '$pr_title' does not follow the correct pattern: $PREFIXES/$PROJECTS-<TICKET_NO> : <Description> where <TICKET_NO> is $TICKET_PATTERN"
67+
exit 1
68+
fi
69+
70+
# Validate the PR title length
71+
if [[ ${#pr_title} -lt $MIN_TITLE_LENGTH ]]; then
72+
echo "PR title '$pr_title' is too short. It must be at least $MIN_TITLE_LENGTH characters long, excluding the default pattern or ticket number."
73+
exit 1
74+
fi
75+
76+
echo "PR title validation passed."
77+
78+
79+

0 commit comments

Comments
 (0)