| 8 | `${event.repository.full_name}:${eventName}:${event.comment.id}` |
| 9 | |
| 10 | export async function relayComment(options) { |
| 11 | const { eventName, event, githubToken, webhookUrl, webhookSecret } = options |
| 12 | const fetch = options.fetch ?? globalThis.fetch |
| 13 | const matches = options.isCommand ?? isCommand |
| 14 | const keyFor = options.deliveryKey ?? deliveryKey |
| 15 | if (eventName !== "issue_comment" || |
| 16 | event?.action !== "created" || typeof event.comment?.body !== "string" || |
| 17 | // Never let the bot's own comments re-trigger it. |
| 18 | String(event.comment.user?.login).toLowerCase() === botLogin || |
| 19 | !matches(event.comment.body)) return { status: "ignored" } |
| 20 | |
| 21 | const repo = event.repository?.full_name |
| 22 | const number = event.issue?.number |
| 23 | const comment = event.comment |
| 24 | const author = comment.user?.login |
| 25 | if (typeof repo !== "string" || !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/u.test(repo) || |
| 26 | typeof author !== "string" || !/^[A-Za-z0-9-]+(?:\[bot\])?$/u.test(author) || |
| 27 | !Number.isSafeInteger(number) || number <= 0 || |
| 28 | !Number.isSafeInteger(comment.id) || comment.id <= 0 || |
| 29 | typeof comment.html_url !== "string") throw new Error("Malformed comment event") |
| 30 | if (!githubToken || !webhookUrl || typeof webhookSecret !== "string" || !webhookSecret) { |
| 31 | throw new Error("Missing relay configuration") |
| 32 | } |
| 33 | const endpoint = new URL(webhookUrl) |
| 34 | if (endpoint.protocol !== "https:" || endpoint.username || endpoint.password) { |
| 35 | throw new Error("Webhook must be an HTTPS URL without credentials") |
| 36 | } |
| 37 | const githubHeaders = { |
| 38 | Authorization: `Bearer ${githubToken}`, |
| 39 | Accept: "application/vnd.github+json", |
| 40 | "X-GitHub-Api-Version": "2022-11-28", |
| 41 | "Content-Type": "application/json" |
| 42 | } |
| 43 | const request = (url, init) => fetch(url, { |
| 44 | ...init, |
| 45 | redirect: "error", |
| 46 | signal: AbortSignal.timeout(30_000) |
| 47 | }) |
| 48 | const githubBase = `https://api.github.com/repos/${repo}` |
| 49 | const permission = await request(`${githubBase}/collaborators/${encodeURIComponent(author)}/permission`, { |
| 50 | headers: githubHeaders |
| 51 | }) |
| 52 | if (!permission.ok) throw new Error(`Permission lookup failed: HTTP ${permission.status}`) |
| 53 | const { role_name: role } = await permission.json() |
| 54 | // The live role is authoritative. author_association only narrows write |
| 55 | // access to outside collaborators, and reflects affiliation when the comment |
| 56 | // was created; no permission fallback or custom-role inference. |
| 57 | const outsideWriter = role === "write" && comment.author_association === "COLLABORATOR" |
| 58 | if (role !== "admin" && role !== "maintain" && !outsideWriter) return { status: "rejected" } |
| 59 | |
| 60 | const payload = { |
| 61 | action: "created", |
| 62 | repo, |
| 63 | number, |
| 64 | isPullRequest: Boolean(event.issue?.pull_request), |
| 65 | commentId: comment.id, |
| 66 | commentUrl: comment.html_url, |
| 67 | body: comment.body, |