|
| 1 | +name: PR Standards |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, edited, synchronize] |
| 6 | + |
| 7 | +jobs: |
| 8 | + check-standards: |
| 9 | + if: | |
| 10 | + github.event.pull_request.user.login != 'actions-user' && |
| 11 | + github.event.pull_request.user.login != 'opencode' && |
| 12 | + github.event.pull_request.user.login != 'rekram1-node' && |
| 13 | + github.event.pull_request.user.login != 'thdxr' && |
| 14 | + github.event.pull_request.user.login != 'kommander' && |
| 15 | + github.event.pull_request.user.login != 'jayair' && |
| 16 | + github.event.pull_request.user.login != 'fwang' && |
| 17 | + github.event.pull_request.user.login != 'adamdotdevin' && |
| 18 | + github.event.pull_request.user.login != 'iamdavidhill' && |
| 19 | + github.event.pull_request.user.login != 'opencode-agent[bot]' |
| 20 | + runs-on: ubuntu-latest |
| 21 | + permissions: |
| 22 | + pull-requests: write |
| 23 | + steps: |
| 24 | + - name: Check PR standards |
| 25 | + uses: actions/github-script@v7 |
| 26 | + with: |
| 27 | + script: | |
| 28 | + const pr = context.payload.pull_request; |
| 29 | + const title = pr.title; |
| 30 | +
|
| 31 | + async function addLabel(label) { |
| 32 | + await github.rest.issues.addLabels({ |
| 33 | + owner: context.repo.owner, |
| 34 | + repo: context.repo.repo, |
| 35 | + issue_number: pr.number, |
| 36 | + labels: [label] |
| 37 | + }); |
| 38 | + } |
| 39 | +
|
| 40 | + async function removeLabel(label) { |
| 41 | + try { |
| 42 | + await github.rest.issues.removeLabel({ |
| 43 | + owner: context.repo.owner, |
| 44 | + repo: context.repo.repo, |
| 45 | + issue_number: pr.number, |
| 46 | + name: label |
| 47 | + }); |
| 48 | + } catch (e) { |
| 49 | + // Label wasn't present, ignore |
| 50 | + } |
| 51 | + } |
| 52 | +
|
| 53 | + async function comment(marker, body) { |
| 54 | + const markerText = `<!-- pr-standards:${marker} -->`; |
| 55 | + const { data: comments } = await github.rest.issues.listComments({ |
| 56 | + owner: context.repo.owner, |
| 57 | + repo: context.repo.repo, |
| 58 | + issue_number: pr.number |
| 59 | + }); |
| 60 | + |
| 61 | + const existing = comments.find(c => c.body.includes(markerText)); |
| 62 | + if (existing) return; |
| 63 | + |
| 64 | + await github.rest.issues.createComment({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + issue_number: pr.number, |
| 68 | + body: markerText + '\n' + body |
| 69 | + }); |
| 70 | + } |
| 71 | +
|
| 72 | + // Step 1: Check title format |
| 73 | + // Matches: feat:, feat(scope):, feat (scope):, etc. |
| 74 | + const titlePattern = /^(feat|fix|docs|chore|refactor|test)\s*(\([a-zA-Z0-9-]+\))?\s*:/; |
| 75 | + const hasValidTitle = titlePattern.test(title); |
| 76 | +
|
| 77 | + if (!hasValidTitle) { |
| 78 | + await addLabel('needs:title'); |
| 79 | + await comment('title', `Hey! Your PR title \`${title}\` doesn't follow conventional commit format. |
| 80 | +
|
| 81 | + Please update it to start with one of: |
| 82 | + - \`feat:\` or \`feat(scope):\` new feature |
| 83 | + - \`fix:\` or \`fix(scope):\` bug fix |
| 84 | + - \`docs:\` or \`docs(scope):\` documentation changes |
| 85 | + - \`chore:\` or \`chore(scope):\` maintenance tasks |
| 86 | + - \`refactor:\` or \`refactor(scope):\` code refactoring |
| 87 | + - \`test:\` or \`test(scope):\` adding or updating tests |
| 88 | +
|
| 89 | + Where \`scope\` is the package name (e.g., \`app\`, \`desktop\`, \`opencode\`). |
| 90 | +
|
| 91 | + See [CONTRIBUTING.md](../blob/dev/CONTRIBUTING.md#pr-titles) for details.`); |
| 92 | + return; |
| 93 | + } |
| 94 | +
|
| 95 | + await removeLabel('needs:title'); |
| 96 | +
|
| 97 | + // Step 2: Check for linked issue (skip for docs/refactor PRs) |
| 98 | + const skipIssueCheck = /^(docs|refactor)\s*(\([a-zA-Z0-9-]+\))?\s*:/.test(title); |
| 99 | + if (skipIssueCheck) { |
| 100 | + await removeLabel('needs:issue'); |
| 101 | + console.log('Skipping issue check for docs/refactor PR'); |
| 102 | + return; |
| 103 | + } |
| 104 | + const query = ` |
| 105 | + query($owner: String!, $repo: String!, $number: Int!) { |
| 106 | + repository(owner: $owner, name: $repo) { |
| 107 | + pullRequest(number: $number) { |
| 108 | + closingIssuesReferences(first: 1) { |
| 109 | + totalCount |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + `; |
| 115 | +
|
| 116 | + const result = await github.graphql(query, { |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + number: pr.number |
| 120 | + }); |
| 121 | +
|
| 122 | + const linkedIssues = result.repository.pullRequest.closingIssuesReferences.totalCount; |
| 123 | +
|
| 124 | + if (linkedIssues === 0) { |
| 125 | + await addLabel('needs:issue'); |
| 126 | + await comment('issue', `Thanks for your contribution! |
| 127 | +
|
| 128 | + This PR doesn't have a linked issue. All PRs must reference an existing issue. |
| 129 | +
|
| 130 | + Please: |
| 131 | + 1. Open an issue describing the bug/feature (if one doesn't exist) |
| 132 | + 2. Add \`Fixes #<number>\` or \`Closes #<number>\` to this PR description |
| 133 | +
|
| 134 | + See [CONTRIBUTING.md](../blob/dev/CONTRIBUTING.md#issue-first-policy) for details.`); |
| 135 | + return; |
| 136 | + } |
| 137 | +
|
| 138 | + await removeLabel('needs:issue'); |
| 139 | + console.log('PR meets all standards'); |
0 commit comments