Skip to content

Commit 25737fa

Browse files
authored
[5.x] Enforce correct prefix in PR title workflow (#13330)
1 parent c34ef9b commit 25737fa

1 file changed

Lines changed: 53 additions & 3 deletions

File tree

.github/workflows/pr-title.yml

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,56 @@ jobs:
88
pr-title:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: deepakputhraya/action-pr-title@master
12-
with:
13-
regex: '^\[\d+\.x\]\s'
11+
- name: Validate PR title matches target branch
12+
env:
13+
PR_TITLE: ${{ github.event.pull_request.title }}
14+
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
15+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
16+
run: |
17+
# Validates PR title against target branch
18+
# Returns error message if invalid, empty string if valid
19+
validate_pr_title() {
20+
local target_branch="$1"
21+
local pr_title="$2"
22+
local default_branch="$3"
23+
24+
# Check if target branch is a version branch (e.g., 5.x, 4.x)
25+
if [[ $target_branch =~ ^([0-9]+)\.x$ ]]; then
26+
local version="${BASH_REMATCH[1]}"
27+
if [[ ! $pr_title =~ ^\[$version\.x\][[:space:]] ]]; then
28+
echo "PR targeting '$target_branch' must have title starting with '[$version.x] '"
29+
return
30+
fi
31+
32+
# Check if target branch is master (next major version)
33+
elif [[ $target_branch == "master" ]]; then
34+
local current_version="${default_branch//\.x/}"
35+
local next_version=$((current_version + 1))
36+
if [[ ! $pr_title =~ ^\[$next_version\.x\][[:space:]] ]]; then
37+
echo "PR targeting 'master' must have title starting with '[$next_version.x] '"
38+
return
39+
fi
40+
41+
# For other branches, just enforce that there's a version prefix
42+
else
43+
if [[ ! $pr_title =~ ^\[[0-9]+\.x\][[:space:]] ]]; then
44+
echo "PR title must start with a version prefix like '[5.x] '"
45+
return
46+
fi
47+
fi
48+
49+
echo ""
50+
}
51+
52+
echo "PR Title: $PR_TITLE"
53+
echo "Base Branch: $BASE_BRANCH"
54+
echo "Default Branch: $DEFAULT_BRANCH"
55+
56+
ERROR=$(validate_pr_title "$BASE_BRANCH" "$PR_TITLE" "$DEFAULT_BRANCH")
57+
58+
if [[ -n $ERROR ]]; then
59+
echo $ERROR
60+
exit 1
61+
fi
62+
63+
echo "PR title validation passed"

0 commit comments

Comments
 (0)