|
| 1 | +name: Release Branch Name Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - 'release/**' |
| 9 | + |
| 10 | +jobs: |
| 11 | + check-release-branch-name: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + if: startsWith(github.ref, 'refs/heads/release/') || (github.event_name == 'pull_request' && startsWith(github.head_ref, 'release/')) |
| 14 | + steps: |
| 15 | + - name: Check release branch naming convention |
| 16 | + id: check |
| 17 | + run: | |
| 18 | + # Get the branch name |
| 19 | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then |
| 20 | + BRANCH_NAME="${{ github.head_ref }}" |
| 21 | + else |
| 22 | + BRANCH_NAME="${GITHUB_REF#refs/heads/}" |
| 23 | + fi |
| 24 | +
|
| 25 | + echo "Checking branch: $BRANCH_NAME" |
| 26 | +
|
| 27 | + # Check if it's a release branch |
| 28 | + if [[ "$BRANCH_NAME" == release/* ]]; then |
| 29 | + # Extract the version part after "release/" |
| 30 | + VERSION="${BRANCH_NAME#release/}" |
| 31 | +
|
| 32 | + # Check if the version starts with 'v' and has all three version |
| 33 | + # components |
| 34 | + if [[ "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+ ]]; then |
| 35 | + echo "✅ Branch name follows correct format: $BRANCH_NAME" |
| 36 | + echo "Version: $VERSION" |
| 37 | + echo "valid=true" >> $GITHUB_OUTPUT |
| 38 | + else |
| 39 | + echo "❌ ERROR: Release branch must follow the format 'release/vX.Y.Z'" |
| 40 | + echo "Current branch: $BRANCH_NAME" |
| 41 | + echo "Expected format examples:" |
| 42 | + echo " - release/v1.0.0" |
| 43 | + echo " - release/v1.2.3" |
| 44 | + echo " - release/v2.0.0" |
| 45 | + echo " - release/v2.0.0-beta.1" |
| 46 | + echo "" |
| 47 | + if [[ "$VERSION" =~ ^[0-9] ]]; then |
| 48 | + echo "Your branch should be named: release/v${VERSION}" |
| 49 | + echo "suggestion=release/v${VERSION}" >> $GITHUB_OUTPUT |
| 50 | + else |
| 51 | + echo "Your branch should be named: release/v<major>.<minor>.<patch>" |
| 52 | + echo "suggestion=release/v<major>.<minor>.<patch>" >> $GITHUB_OUTPUT |
| 53 | + fi |
| 54 | + echo "valid=false" >> $GITHUB_OUTPUT |
| 55 | + echo "branch=$BRANCH_NAME" >> $GITHUB_OUTPUT |
| 56 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + else |
| 60 | + echo "Not a release branch, skipping check" |
| 61 | + echo "valid=skip" >> $GITHUB_OUTPUT |
| 62 | + fi |
| 63 | +
|
| 64 | + - name: Comment PR on validation failure |
| 65 | + if: failure() && github.event_name == 'pull_request' && steps.check.outputs.valid == 'false' |
| 66 | + uses: actions/github-script@v7 |
| 67 | + with: |
| 68 | + script: | |
| 69 | + const branch = '${{ steps.check.outputs.branch }}'; |
| 70 | + const version = '${{ steps.check.outputs.version }}'; |
| 71 | + const suggestion = '${{ steps.check.outputs.suggestion }}'; |
| 72 | +
|
| 73 | + const body = `## ❌ Release Branch Naming Convention Error |
| 74 | +
|
| 75 | + The branch name **\`${branch}\`** does not follow the required naming convention. |
| 76 | +
|
| 77 | + ### Required Format |
| 78 | + Release branches must follow the format: **\`release/vX.Y.Z\`** |
| 79 | +
|
| 80 | + Where: |
| 81 | + - The version must start with a lowercase \`v\` |
| 82 | + - X, Y, and Z are version numbers (all three components required) |
| 83 | +
|
| 84 | + ### Valid Examples |
| 85 | + - \`release/v1.0.0\` |
| 86 | + - \`release/v1.2.3\` |
| 87 | + - \`release/v2.0.0\` |
| 88 | + - \`release/v2.0.0-beta.1\` |
| 89 | +
|
| 90 | + ### Your Branch Issue |
| 91 | + ${version.startsWith('v') ? |
| 92 | + 'Missing patch version component (Z in vX.Y.Z)' : |
| 93 | + version.match(/^[0-9]/) ? |
| 94 | + 'Missing the required \`v\` prefix' : |
| 95 | + 'Invalid version format'} |
| 96 | +
|
| 97 | + ### Suggested Fix |
| 98 | + Your branch should be named: **\`${suggestion}\`** |
| 99 | +
|
| 100 | + Please rename your branch to follow the convention and push again.`; |
| 101 | +
|
| 102 | + // Check if we already commented |
| 103 | + const { data: comments } = await github.rest.issues.listComments({ |
| 104 | + owner: context.repo.owner, |
| 105 | + repo: context.repo.repo, |
| 106 | + issue_number: context.issue.number, |
| 107 | + }); |
| 108 | +
|
| 109 | + const botComment = comments.find(comment => |
| 110 | + comment.user.type === 'Bot' && |
| 111 | + comment.body.includes('Release Branch Naming Convention Error') |
| 112 | + ); |
| 113 | +
|
| 114 | + if (botComment) { |
| 115 | + // Update existing comment |
| 116 | + await github.rest.issues.updateComment({ |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + comment_id: botComment.id, |
| 120 | + body: body |
| 121 | + }); |
| 122 | + } else { |
| 123 | + // Create new comment |
| 124 | + await github.rest.issues.createComment({ |
| 125 | + owner: context.repo.owner, |
| 126 | + repo: context.repo.repo, |
| 127 | + issue_number: context.issue.number, |
| 128 | + body: body |
| 129 | + }); |
| 130 | + } |
0 commit comments