external-pr-scope.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. // Shared logic for letting a NON-MEMBER pull request stay open and be reviewed, scoped to
  3. // the contributor's own already-listed plugin repo. No maintained allowlist, no individuals.
  4. //
  5. // Trust model: we do NOT verify the submitter's identity. We trust the SOURCE REPO. A PR is
  6. // in scope only if it ADDS marketplace.json entries whose source.url is a repo that ALREADY
  7. // backs a live entry in this marketplace (derived from the base marketplace.json), pinned to
  8. // a commit in that repo. Because the repo is org-controlled and the SHA pins to a real commit
  9. // there, the shipped code is the org's code regardless of who opened the PR. Merge still
  10. // requires CI + a maintainer approval.
  11. //
  12. // Used by:
  13. // - close-external-prs.yml (skip the auto-close when in scope)
  14. // - external-pr-scope-guard.yml (required status check: fail a non-member PR that is out of scope)
  15. //
  16. // Security: evaluate() reads base + head marketplace.json as DATA via the API and parses them;
  17. // it never checks out or executes head code.
  18. const MARKETPLACE = '.claude-plugin/marketplace.json';
  19. function normalizeRepo(u) {
  20. return String(u || '').trim().toLowerCase()
  21. .replace(/^git\+/, '')
  22. .replace(/^https?:\/\//, '')
  23. .replace(/\.git$/, '')
  24. .replace(/\/+$/, '');
  25. }
  26. function pluginsByName(json) {
  27. const map = {};
  28. for (const p of (json && json.plugins) || []) { if (p && p.name) map[p.name] = p; }
  29. return map;
  30. }
  31. // Repos that already back a live entry, derived from the base marketplace.json.
  32. function liveReposOf(base) {
  33. const s = new Set();
  34. for (const name of Object.keys(base)) {
  35. const u = base[name] && base[name].source && base[name].source.url;
  36. if (!u) continue;
  37. const r = normalizeRepo(u);
  38. if (r.split('/').length >= 3) s.add(r); // host/org/repo
  39. }
  40. return s;
  41. }
  42. // Pure decision over an already-computed diff. Returns { ok, problems, added, removed, modified }.
  43. function analyze({ changedFiles, base, head }) {
  44. const problems = [];
  45. const off = changedFiles.filter(n => n !== MARKETPLACE);
  46. if (off.length) problems.push(`changes files other than ${MARKETPLACE}: ${off.join(', ')}`);
  47. const liveRepos = liveReposOf(base);
  48. const baseNames = new Set(Object.keys(base));
  49. const headNames = new Set(Object.keys(head));
  50. const removed = [...baseNames].filter(n => !headNames.has(n));
  51. const added = [...headNames].filter(n => !baseNames.has(n));
  52. const modified = [...headNames].filter(
  53. n => baseNames.has(n) && JSON.stringify(base[n]) !== JSON.stringify(head[n])
  54. );
  55. if (removed.length) problems.push(`removes existing entr${removed.length > 1 ? 'ies' : 'y'}: ${removed.join(', ')}`);
  56. if (modified.length) problems.push(`modifies existing entr${modified.length > 1 ? 'ies' : 'y'}: ${modified.join(', ')}`);
  57. if (!off.length && !added.length && !removed.length && !modified.length) {
  58. problems.push('makes no in-scope change (expected additions to marketplace.json)');
  59. }
  60. for (const name of added) {
  61. const u = head[name] && head[name].source && head[name].source.url;
  62. if (!u) { problems.push(`added "${name}" has no source.url to validate`); continue; }
  63. const r = normalizeRepo(u);
  64. if (r.split('/').length < 3) { problems.push(`added "${name}" source.url ${u} is not a valid repo URL`); continue; }
  65. if (!liveRepos.has(r)) {
  66. problems.push(`added "${name}" points at ${u}, a repo with no existing live plugin in this marketplace`);
  67. }
  68. }
  69. return { ok: problems.length === 0, problems, added, removed, modified, liveRepoCount: liveRepos.size };
  70. }
  71. async function readPlugins(github, owner, repo, ref) {
  72. try {
  73. const { data } = await github.rest.repos.getContent({ owner, repo, ref, path: MARKETPLACE });
  74. return pluginsByName(JSON.parse(Buffer.from(data.content, 'base64').toString('utf8')));
  75. } catch (e) {
  76. return null;
  77. }
  78. }
  79. // API wrapper used by both workflows. Fetches the diff + base/head marketplace.json, delegates to analyze().
  80. async function evaluate({ github, context }) {
  81. const pr = context.payload.pull_request;
  82. const owner = context.repo.owner, repo = context.repo.repo;
  83. const files = await github.paginate(github.rest.pulls.listFiles, {
  84. owner, repo, pull_number: pr.number, per_page: 100,
  85. });
  86. const changedFiles = files.map(f => f.filename);
  87. const base = await readPlugins(github, owner, repo, pr.base.sha);
  88. const head = await readPlugins(github, pr.head.repo.owner.login, pr.head.repo.name, pr.head.sha);
  89. if (base === null || head === null) {
  90. return { ok: false, problems: ['could not read marketplace.json at base and/or head'], added: [], removed: [], modified: [] };
  91. }
  92. return analyze({ changedFiles, base, head });
  93. }
  94. module.exports = { normalizeRepo, liveReposOf, analyze, readPlugins, evaluate, MARKETPLACE };