pr-standards.yml 14 KB

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