Skip to content

Commit 907b5cb

Browse files
authored
Update GitFlow_Check-pull-request-source-branch.yml
1 parent f7e44c2 commit 907b5cb

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

.github/workflows/GitFlow_Check-pull-request-source-branch.yml

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This workflow enforces GitFlow branch patterns by:
2-
# - Ensuring only hotfix/* branches can target main (release branches are handled automatically)
2+
# - Ensuring only hotfix/* or release/* branches can target main
33
# - Adding labels and comments to non-compliant PRs
44
# - Automatically cleaning up when PRs are updated to comply
55

@@ -31,39 +31,33 @@ jobs:
3131
name: Check branch pattern
3232
runs-on: ubuntu-latest
3333
steps:
34-
# Step 1: Use GitHub Script for branch pattern validation (safer than shell)
34+
# Step 1: Check branch pattern
3535
- name: Check branch pattern
3636
id: branch_check
37-
uses: actions/github-script@v7
38-
with:
39-
github-token: ${{ secrets.GITHUB_TOKEN }}
40-
result-encoding: string
41-
script: |
42-
// Get branch information from context
43-
const headRef = context.payload.pull_request.head.ref;
44-
const baseRef = context.payload.pull_request.base.ref;
45-
const mainBranch = process.env.MAIN_BRANCH;
46-
const validPattern = new RegExp(process.env.VALID_PATTERNS);
47-
48-
console.log(`Checking PR from '${headRef}' to '${baseRef}'`);
49-
50-
// Perform the validation in JavaScript instead of shell
51-
if (baseRef === mainBranch) {
52-
if (!validPattern.test(headRef)) {
53-
console.log(`::error::❌ Invalid branch! PRs to main must come from hotfix/* or release/* branches. Please target the develop branch instead.`);
54-
return 'invalid';
55-
} else {
56-
console.log(`::notice::✅ Branch pattern is valid: '${headRef}' → '${mainBranch}'`);
57-
return 'valid';
58-
}
59-
} else {
60-
console.log(`::notice::✅ Not targeting main branch, no pattern restrictions apply.`);
61-
return 'not-main';
62-
}
37+
run: |
38+
# Get branch information from context
39+
HEAD_REF="${{ github.head_ref }}"
40+
BASE_REF="${{ github.base_ref }}"
41+
MAIN_BRANCH="${{ env.MAIN_BRANCH }}"
42+
VALID_PATTERN="${{ env.VALID_PATTERNS }}"
43+
44+
echo "Checking PR from '$HEAD_REF' to '$BASE_REF'"
45+
46+
# Perform the validation
47+
if [[ "$BASE_REF" == "$MAIN_BRANCH" ]]; then
48+
if [[ ! "$HEAD_REF" =~ $VALID_PATTERN ]]; then
49+
echo "::error::❌ Invalid branch! PRs to main must come from hotfix/* or release/* branches. Please target the develop branch instead."
50+
exit 1
51+
else
52+
echo "::notice::✅ Branch pattern is valid: '$HEAD_REF' → '$MAIN_BRANCH'"
53+
fi
54+
else
55+
echo "::notice::✅ Not targeting main branch, no pattern restrictions apply."
56+
fi
6357
6458
# Step 2: If the branch pattern is invalid, add a label and comment to the PR
6559
- name: Handle invalid branch (label + comment)
66-
if: steps.branch_check.outputs.result == 'invalid'
60+
if: failure()
6761
uses: actions/github-script@v7
6862
with:
6963
github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -79,7 +73,7 @@ jobs:
7973
const message = `❌ **${messageIdentifier}**
8074
8175
According to our GitFlow workflow:
82-
- Pull requests to the \`main\` branch are only allowed from \`hotfix/*\` branches
76+
- Pull requests to the \`main\` branch are only allowed from \`hotfix/*\` or \`release/*\` branches
8377
- Regular feature development and other changes should target the \`develop\` branch
8478
8579
📝 **Action required:** Please update your PR to target the \`develop\` branch instead.
@@ -126,7 +120,7 @@ jobs:
126120
127121
# Step 3: If the branch pattern is corrected, remove label and comment
128122
- name: Clean up if branch is corrected
129-
if: steps.branch_check.outputs.result == 'valid' || steps.branch_check.outputs.result == 'not-main'
123+
if: success()
130124
uses: actions/github-script@v7
131125
with:
132126
github-token: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)