|
| 1 | +name: Verify PR Title |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, edited, synchronize, reopened] |
| 6 | + branches: |
| 7 | + - dev |
| 8 | + - release |
| 9 | + |
| 10 | +jobs: |
| 11 | + verify-pr-title: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Validate PR title format |
| 15 | + uses: actions/github-script@v7 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + const title = context.payload.pull_request.title; |
| 19 | + const errors = []; |
| 20 | +
|
| 21 | + // Rule 1: Title must start with [Component Name] or {Component Name} |
| 22 | + const prefixMatch = title.match(/^(\[([^\]]*)\]|\{([^}]*)\})/); |
| 23 | + if (!prefixMatch) { |
| 24 | + errors.push( |
| 25 | + "PR title must start with `[Component Name]` (customer-facing) or `{Component Name}` (non-customer-facing)." |
| 26 | + ); |
| 27 | + } else { |
| 28 | + const componentName = (prefixMatch[2] || prefixMatch[3] || "").trim(); |
| 29 | + if (!componentName) { |
| 30 | + errors.push( |
| 31 | + "Component name inside the brackets must not be empty." |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | +
|
| 36 | + // Rule 2: If "Fix #" appears after the prefix, it must be followed by a number |
| 37 | + if (prefixMatch) { |
| 38 | + const rest = title.slice(prefixMatch[0].length).trim(); |
| 39 | + const fixMatch = rest.match(/^Fix\s+#/i); |
| 40 | + if (fixMatch) { |
| 41 | + const validFix = rest.match(/^Fix\s+#\d+/i); |
| 42 | + if (!validFix) { |
| 43 | + errors.push( |
| 44 | + "`Fix #` must be followed by a valid issue number (e.g., `Fix #12345`)." |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + if (errors.length > 0) { |
| 51 | + const message = [ |
| 52 | + "## ❌ PR Title Validation Failed\n", |
| 53 | + ...errors.map(e => `- ${e}`), |
| 54 | + "", |
| 55 | + "### Expected format", |
| 56 | + "```", |
| 57 | + "[Component Name] description of the change", |
| 58 | + "{Component Name} description of a non-customer-facing change", |
| 59 | + "[Component Name] BREAKING CHANGE: description", |
| 60 | + "[Component Name] Fix #12345: description", |
| 61 | + "```", |
| 62 | + "", |
| 63 | + "### Examples", |
| 64 | + "```", |
| 65 | + "[Storage] BREAKING CHANGE: `az storage remove`: Remove --auth-mode argument", |
| 66 | + "[ARM] Fix #10246: `az resource tag`: Fix crash when --ids is a resource group ID", |
| 67 | + "{Aladdin} Add help example for dns", |
| 68 | + "[API Management] Add new operation support", |
| 69 | + "```", |
| 70 | + ].join("\n"); |
| 71 | +
|
| 72 | + core.summary.addRaw(message); |
| 73 | + await core.summary.write(); |
| 74 | + core.setFailed("PR title does not follow the required format."); |
| 75 | + } else { |
| 76 | + core.summary.addRaw("## ✅ PR Title Validation Passed"); |
| 77 | + await core.summary.write(); |
| 78 | + core.info("PR title format is valid."); |
| 79 | + } |
0 commit comments