|
| 1 | +name: PR Formatting Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, reopened, synchronize] |
| 6 | + |
| 7 | +permissions: |
| 8 | + pull-requests: write |
| 9 | + |
| 10 | +jobs: |
| 11 | + check: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Validate PR title and description content |
| 15 | + uses: actions/github-script@v7 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + const title = context.payload.pull_request.title; |
| 19 | + const body = context.payload.pull_request.body; |
| 20 | + const prAuthor = context.payload.pull_request.user.login; |
| 21 | +
|
| 22 | + // Validate title prefix for all contributors |
| 23 | + const validTitlePrefixes = [ |
| 24 | + 'FEAT:', 'CHORE:', 'FIX:', 'DOC:', 'STYLE:', 'REFACTOR:', |
| 25 | + 'RELEASE:' |
| 26 | + ]; |
| 27 | +
|
| 28 | + const hasValidPrefix = validTitlePrefixes.some(prefix => |
| 29 | + title.startsWith(prefix)); |
| 30 | +
|
| 31 | + if (!hasValidPrefix) { |
| 32 | + core.setFailed(`❌ PR title must start with one of the allowed prefixes:\n${validTitlePrefixes.join(', ')}`); |
| 33 | + } |
| 34 | +
|
| 35 | + // Validate that either GitHub issue link or ADO Work Item link is present |
| 36 | + const azureWorkItemLinkPattern = |
| 37 | + /https:\/\/sqlclientdrivers\.visualstudio\.com\/[^\/]+\/_workitems\/edit\/\d+/i; |
| 38 | + const githubIssueLinkPattern = |
| 39 | + /(https:\/\/github\.com\/microsoft\/mssql-python\/issues\/\d+|#\d+)/i; |
| 40 | + |
| 41 | + const hasWorkItemLink = azureWorkItemLinkPattern.test(body); |
| 42 | + const hasGitHubIssueLink = githubIssueLinkPattern.test(body); |
| 43 | +
|
| 44 | + if (!hasWorkItemLink && !hasGitHubIssueLink) { |
| 45 | + core.setFailed(`❌ PR must contain either a valid GitHub issue link OR a valid ADO Work Item link.\nGitHub issue format: https://github.com/microsoft/mssql-python/issues/XXX or #XXX\nADO Work Item format: https://sqlclientdrivers.visualstudio.com/.../_workitems/edit/<ID>\nPlease include at least one reference in the PR description.\nFor more information, see CONTRIBUTING.md.`); |
| 46 | + } |
| 47 | +
|
| 48 | + // Check if PR description contains a meaningful summary section |
| 49 | + // with actual content (for all contributors) |
| 50 | + const summaryPattern = |
| 51 | + /###\s*Summary\s*\r?\n([\s\S]*?)(\r?\n###|$)/; |
| 52 | + const summaryMatch = body.match(summaryPattern); |
| 53 | +
|
| 54 | + let hasValidSummary = false; |
| 55 | +
|
| 56 | + if (summaryMatch && summaryMatch[1]) { |
| 57 | + // Extract the summary content |
| 58 | + const summaryContent = summaryMatch[1]; |
| 59 | +
|
| 60 | + // Remove all HTML comments including the template placeholder |
| 61 | + const contentWithoutComments = |
| 62 | + summaryContent.replace(/<!--[\s\S]*?-->/g, ''); |
| 63 | +
|
| 64 | + // Remove whitespace and check if there's actual text content |
| 65 | + const trimmedContent = contentWithoutComments.trim(); |
| 66 | +
|
| 67 | + // Check if there's at least 10 characters of meaningful content |
| 68 | + hasValidSummary = trimmedContent.length >= 10; |
| 69 | + } |
| 70 | +
|
| 71 | + if (!hasValidSummary) { |
| 72 | + core.setFailed(`❌ PR must contain a meaningful summary section with actual text content (minimum 10 characters).\nPlease add a clear description under the '### Summary' heading in your PR description.`); |
| 73 | + } |
| 74 | + - name: Add size label based on PR diff |
| 75 | + if: github.event.pull_request.head.repo.full_name == github.repository |
| 76 | + uses: actions/github-script@v7 |
| 77 | + with: |
| 78 | + script: | |
| 79 | + const pr = context.payload.pull_request; |
| 80 | + const additions = pr.additions; |
| 81 | + const deletions = pr.deletions; |
| 82 | + const totalChanges = additions + deletions; |
| 83 | +
|
| 84 | + // Threshold constants |
| 85 | + const SMALL_PR_THRESHOLD = 100; |
| 86 | + const MEDIUM_PR_THRESHOLD = 500; |
| 87 | +
|
| 88 | + let labelToAdd = ''; |
| 89 | + if (totalChanges < SMALL_PR_THRESHOLD) { |
| 90 | + labelToAdd = 'pr-size: small'; |
| 91 | + } else if (totalChanges < MEDIUM_PR_THRESHOLD) { |
| 92 | + labelToAdd = 'pr-size: medium'; |
| 93 | + } else { |
| 94 | + labelToAdd = 'pr-size: large'; |
| 95 | + } |
| 96 | +
|
| 97 | + // Remove existing size labels if any |
| 98 | + const existingLabels = pr.labels.map(l => l.name); |
| 99 | + const sizeLabels = ['pr-size: small', 'pr-size: medium', 'pr-size: large']; |
| 100 | + for (const label of existingLabels) { |
| 101 | + if (sizeLabels.includes(label)) { |
| 102 | + await github.rest.issues.removeLabel({ |
| 103 | + ...context.repo, |
| 104 | + issue_number: pr.number, |
| 105 | + name: label, |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | +
|
| 110 | + // Add new size label |
| 111 | + await github.rest.issues.addLabels({ |
| 112 | + ...context.repo, |
| 113 | + issue_number: pr.number, |
| 114 | + labels: [labelToAdd], |
| 115 | + }); |
| 116 | +
|
| 117 | + console.log(`Added label: ${labelToAdd} (Total changes: ${totalChanges})`); |
0 commit comments