pr-standards.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. name: pr-standards
  2. on:
  3. pull_request_target:
  4. types: [opened, edited, synchronize]
  5. jobs:
  6. check-standards:
  7. runs-on: ubuntu-latest
  8. permissions:
  9. contents: read
  10. pull-requests: write
  11. steps:
  12. - name: Check PR standards
  13. uses: actions/github-script@v7
  14. with:
  15. script: |
  16. const pr = context.payload.pull_request;
  17. const login = pr.user.login;
  18. // Skip PRs older than Feb 18, 2026 at 6PM EST (Feb 19, 2026 00:00 UTC)
  19. const cutoff = new Date('2026-02-19T00:00:00Z');
  20. const prCreated = new Date(pr.created_at);
  21. if (prCreated < cutoff) {
  22. console.log(`Skipping: PR #${pr.number} was created before cutoff (${prCreated.toISOString()})`);
  23. return;
  24. }
  25. // Check if author is a team member or bot
  26. if (login === 'opencode-agent[bot]') return;
  27. const { data: file } = await github.rest.repos.getContent({
  28. owner: context.repo.owner,
  29. repo: context.repo.repo,
  30. path: '.github/TEAM_MEMBERS',
  31. ref: 'dev'
  32. });
  33. const members = Buffer.from(file.content, 'base64').toString().split('\n').map(l => l.trim()).filter(Boolean);
  34. if (members.includes(login)) {
  35. console.log(`Skipping: ${login} is a team member`);
  36. return;
  37. }
  38. const title = pr.title;
  39. async function addLabel(label) {
  40. await github.rest.issues.addLabels({
  41. owner: context.repo.owner,
  42. repo: context.repo.repo,
  43. issue_number: pr.number,
  44. labels: [label]
  45. });
  46. }
  47. async function removeLabel(label) {
  48. try {
  49. await github.rest.issues.removeLabel({
  50. owner: context.repo.owner,
  51. repo: context.repo.repo,
  52. issue_number: pr.number,
  53. name: label
  54. });
  55. } catch (e) {
  56. // Label wasn't present, ignore
  57. }
  58. }
  59. async function comment(marker, body) {
  60. const markerText = `<!-- pr-standards:${marker} -->`;
  61. const { data: comments } = await github.rest.issues.listComments({
  62. owner: context.repo.owner,
  63. repo: context.repo.repo,
  64. issue_number: pr.number
  65. });
  66. const existing = comments.find(c => c.body.includes(markerText));
  67. if (existing) return;
  68. await github.rest.issues.createComment({
  69. owner: context.repo.owner,
  70. repo: context.repo.repo,
  71. issue_number: pr.number,
  72. body: markerText + '\n' + body
  73. });
  74. }
  75. // Step 1: Check title format
  76. // Matches: feat:, feat(scope):, feat (scope):, etc.
  77. const titlePattern = /^(feat|fix|docs|chore|refactor|test)\s*(\([a-zA-Z0-9-]+\))?\s*:/;
  78. const hasValidTitle = titlePattern.test(title);
  79. if (!hasValidTitle) {
  80. await addLabel('needs:title');
  81. await comment('title', `Hey! Your PR title \`${title}\` doesn't follow conventional commit format.
  82. Please update it to start with one of:
  83. - \`feat:\` or \`feat(scope):\` new feature
  84. - \`fix:\` or \`fix(scope):\` bug fix
  85. - \`docs:\` or \`docs(scope):\` documentation changes
  86. - \`chore:\` or \`chore(scope):\` maintenance tasks
  87. - \`refactor:\` or \`refactor(scope):\` code refactoring
  88. - \`test:\` or \`test(scope):\` adding or updating tests
  89. Where \`scope\` is the package name (e.g., \`app\`, \`desktop\`, \`opencode\`).
  90. See [CONTRIBUTING.md](../blob/dev/CONTRIBUTING.md#pr-titles) for details.`);
  91. return;
  92. }
  93. await removeLabel('needs:title');
  94. // Step 2: Check for linked issue (skip for docs/refactor/feat PRs)
  95. const skipIssueCheck = /^(docs|refactor|feat)\s*(\([a-zA-Z0-9-]+\))?\s*:/.test(title);
  96. if (skipIssueCheck) {
  97. await removeLabel('needs:issue');
  98. console.log('Skipping issue check for docs/refactor/feat PR');
  99. return;
  100. }
  101. const query = `
  102. query($owner: String!, $repo: String!, $number: Int!) {
  103. repository(owner: $owner, name: $repo) {
  104. pullRequest(number: $number) {
  105. closingIssuesReferences(first: 1) {
  106. totalCount
  107. }
  108. }
  109. }
  110. }
  111. `;
  112. const result = await github.graphql(query, {
  113. owner: context.repo.owner,
  114. repo: context.repo.repo,
  115. number: pr.number
  116. });
  117. const linkedIssues = result.repository.pullRequest.closingIssuesReferences.totalCount;
  118. if (linkedIssues === 0) {
  119. await addLabel('needs:issue');
  120. await comment('issue', `Thanks for your contribution!
  121. This PR doesn't have a linked issue. All PRs must reference an existing issue.
  122. Please:
  123. 1. Open an issue describing the bug/feature (if one doesn't exist)
  124. 2. Add \`Fixes #<number>\` or \`Closes #<number>\` to this PR description
  125. See [CONTRIBUTING.md](../blob/dev/CONTRIBUTING.md#issue-first-policy) for details.`);
  126. return;
  127. }
  128. await removeLabel('needs:issue');
  129. console.log('PR meets all standards');
  130. check-compliance:
  131. runs-on: ubuntu-latest
  132. permissions:
  133. contents: read
  134. pull-requests: write
  135. steps:
  136. - name: Check PR template compliance
  137. uses: actions/github-script@v7
  138. with:
  139. script: |
  140. const pr = context.payload.pull_request;
  141. const login = pr.user.login;
  142. // Skip PRs older than Feb 18, 2026 at 6PM EST (Feb 19, 2026 00:00 UTC)
  143. const cutoff = new Date('2026-02-19T00:00:00Z');
  144. const prCreated = new Date(pr.created_at);
  145. if (prCreated < cutoff) {
  146. console.log(`Skipping: PR #${pr.number} was created before cutoff (${prCreated.toISOString()})`);
  147. return;
  148. }
  149. // Check if author is a team member or bot
  150. if (login === 'opencode-agent[bot]') return;
  151. const { data: file } = await github.rest.repos.getContent({
  152. owner: context.repo.owner,
  153. repo: context.repo.repo,
  154. path: '.github/TEAM_MEMBERS',
  155. ref: 'dev'
  156. });
  157. const members = Buffer.from(file.content, 'base64').toString().split('\n').map(l => l.trim()).filter(Boolean);
  158. if (members.includes(login)) {
  159. console.log(`Skipping: ${login} is a team member`);
  160. return;
  161. }
  162. const body = pr.body || '';
  163. const title = pr.title;
  164. const isDocsRefactorOrFeat = /^(docs|refactor|feat)\s*(\([a-zA-Z0-9-]+\))?\s*:/.test(title);
  165. const issues = [];
  166. // Check: template sections exist
  167. const hasWhatSection = /### What does this PR do\?/.test(body);
  168. const hasTypeSection = /### Type of change/.test(body);
  169. const hasVerifySection = /### How did you verify your code works\?/.test(body);
  170. const hasChecklistSection = /### Checklist/.test(body);
  171. const hasIssueSection = /### Issue for this PR/.test(body);
  172. if (!hasWhatSection || !hasTypeSection || !hasVerifySection || !hasChecklistSection || !hasIssueSection) {
  173. issues.push('PR description is missing required template sections. Please use the [PR template](../blob/dev/.github/pull_request_template.md).');
  174. }
  175. // Check: "What does this PR do?" has real content (not just placeholder text)
  176. if (hasWhatSection) {
  177. const whatMatch = body.match(/### What does this PR do\?\s*\n([\s\S]*?)(?=###|$)/);
  178. const whatContent = whatMatch ? whatMatch[1].trim() : '';
  179. const placeholder = 'Please provide a description of the issue';
  180. const onlyPlaceholder = whatContent.includes(placeholder) && whatContent.replace(placeholder, '').replace(/[*\s]/g, '').length < 20;
  181. if (!whatContent || onlyPlaceholder) {
  182. issues.push('"What does this PR do?" section is empty or only contains placeholder text. Please describe your changes.');
  183. }
  184. }
  185. // Check: at least one "Type of change" checkbox is checked
  186. if (hasTypeSection) {
  187. const typeMatch = body.match(/### Type of change\s*\n([\s\S]*?)(?=###|$)/);
  188. const typeContent = typeMatch ? typeMatch[1] : '';
  189. const hasCheckedBox = /- \[x\]/i.test(typeContent);
  190. if (!hasCheckedBox) {
  191. issues.push('No "Type of change" checkbox is checked. Please select at least one.');
  192. }
  193. }
  194. // Check: issue reference (skip for docs/refactor/feat)
  195. if (!isDocsRefactorOrFeat && hasIssueSection) {
  196. const issueMatch = body.match(/### Issue for this PR\s*\n([\s\S]*?)(?=###|$)/);
  197. const issueContent = issueMatch ? issueMatch[1].trim() : '';
  198. const hasIssueRef = /(closes|fixes|resolves)\s+#\d+/i.test(issueContent) || /#\d+/.test(issueContent);
  199. if (!hasIssueRef) {
  200. issues.push('No issue referenced. Please add `Closes #<number>` linking to the relevant issue.');
  201. }
  202. }
  203. // Check: "How did you verify" has content
  204. if (hasVerifySection) {
  205. const verifyMatch = body.match(/### How did you verify your code works\?\s*\n([\s\S]*?)(?=###|$)/);
  206. const verifyContent = verifyMatch ? verifyMatch[1].trim() : '';
  207. if (!verifyContent) {
  208. issues.push('"How did you verify your code works?" section is empty. Please explain how you tested.');
  209. }
  210. }
  211. // Check: checklist boxes are checked
  212. if (hasChecklistSection) {
  213. const checklistMatch = body.match(/### Checklist\s*\n([\s\S]*?)(?=###|$)/);
  214. const checklistContent = checklistMatch ? checklistMatch[1] : '';
  215. const unchecked = (checklistContent.match(/- \[ \]/g) || []).length;
  216. const checked = (checklistContent.match(/- \[x\]/gi) || []).length;
  217. if (checked < 2) {
  218. issues.push('Not all checklist items are checked. Please confirm you have tested locally and have not included unrelated changes.');
  219. }
  220. }
  221. // Helper functions
  222. async function addLabel(label) {
  223. await github.rest.issues.addLabels({
  224. owner: context.repo.owner,
  225. repo: context.repo.repo,
  226. issue_number: pr.number,
  227. labels: [label]
  228. });
  229. }
  230. async function removeLabel(label) {
  231. try {
  232. await github.rest.issues.removeLabel({
  233. owner: context.repo.owner,
  234. repo: context.repo.repo,
  235. issue_number: pr.number,
  236. name: label
  237. });
  238. } catch (e) {}
  239. }
  240. const hasComplianceLabel = pr.labels.some(l => l.name === 'needs:compliance');
  241. if (issues.length > 0) {
  242. // Non-compliant
  243. if (!hasComplianceLabel) {
  244. await addLabel('needs:compliance');
  245. }
  246. const marker = '<!-- issue-compliance -->';
  247. const { data: comments } = await github.rest.issues.listComments({
  248. owner: context.repo.owner,
  249. repo: context.repo.repo,
  250. issue_number: pr.number
  251. });
  252. const existing = comments.find(c => c.body.includes(marker));
  253. const body_text = `${marker}
  254. This PR doesn't fully meet our [contributing guidelines](../blob/dev/CONTRIBUTING.md) and [PR template](../blob/dev/.github/pull_request_template.md).
  255. **What needs to be fixed:**
  256. ${issues.map(i => `- ${i}`).join('\n')}
  257. Please edit this PR description to address the above within **2 hours**, or it will be automatically closed.
  258. If you believe this was flagged incorrectly, please let a maintainer know.`;
  259. if (existing) {
  260. await github.rest.issues.updateComment({
  261. owner: context.repo.owner,
  262. repo: context.repo.repo,
  263. comment_id: existing.id,
  264. body: body_text
  265. });
  266. } else {
  267. await github.rest.issues.createComment({
  268. owner: context.repo.owner,
  269. repo: context.repo.repo,
  270. issue_number: pr.number,
  271. body: body_text
  272. });
  273. }
  274. console.log(`PR #${pr.number} is non-compliant: ${issues.join(', ')}`);
  275. } else if (hasComplianceLabel) {
  276. // Was non-compliant, now fixed
  277. await removeLabel('needs:compliance');
  278. const { data: comments } = await github.rest.issues.listComments({
  279. owner: context.repo.owner,
  280. repo: context.repo.repo,
  281. issue_number: pr.number
  282. });
  283. const marker = '<!-- issue-compliance -->';
  284. const existing = comments.find(c => c.body.includes(marker));
  285. if (existing) {
  286. await github.rest.issues.deleteComment({
  287. owner: context.repo.owner,
  288. repo: context.repo.repo,
  289. comment_id: existing.id
  290. });
  291. }
  292. await github.rest.issues.createComment({
  293. owner: context.repo.owner,
  294. repo: context.repo.repo,
  295. issue_number: pr.number,
  296. body: 'Thanks for updating your PR! It now meets our contributing guidelines. :+1:'
  297. });
  298. console.log(`PR #${pr.number} is now compliant, label removed`);
  299. } else {
  300. console.log(`PR #${pr.number} is compliant`);
  301. }