|
1 | 1 | # 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 |
3 | 3 | # - Adding labels and comments to non-compliant PRs |
4 | 4 | # - Automatically cleaning up when PRs are updated to comply |
5 | 5 |
|
@@ -31,39 +31,33 @@ jobs: |
31 | 31 | name: Check branch pattern |
32 | 32 | runs-on: ubuntu-latest |
33 | 33 | steps: |
34 | | - # Step 1: Use GitHub Script for branch pattern validation (safer than shell) |
| 34 | + # Step 1: Check branch pattern |
35 | 35 | - name: Check branch pattern |
36 | 36 | 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 |
63 | 57 |
|
64 | 58 | # Step 2: If the branch pattern is invalid, add a label and comment to the PR |
65 | 59 | - name: Handle invalid branch (label + comment) |
66 | | - if: steps.branch_check.outputs.result == 'invalid' |
| 60 | + if: failure() |
67 | 61 | uses: actions/github-script@v7 |
68 | 62 | with: |
69 | 63 | github-token: ${{ secrets.GITHUB_TOKEN }} |
|
79 | 73 | const message = `❌ **${messageIdentifier}** |
80 | 74 |
|
81 | 75 | 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 |
83 | 77 | - Regular feature development and other changes should target the \`develop\` branch |
84 | 78 |
|
85 | 79 | 📝 **Action required:** Please update your PR to target the \`develop\` branch instead. |
@@ -126,7 +120,7 @@ jobs: |
126 | 120 |
|
127 | 121 | # Step 3: If the branch pattern is corrected, remove label and comment |
128 | 122 | - 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() |
130 | 124 | uses: actions/github-script@v7 |
131 | 125 | with: |
132 | 126 | github-token: ${{ secrets.GITHUB_TOKEN }} |
|
0 commit comments