Commit 5348692a1d
Verified · cmc
Layout: unified · split
.qlty/.gitignore added +7
| @@ -0,0 +1,7 @@ | |||
| 1 | * | ||
| 2 | !configs | ||
| 3 | !configs/** | ||
| 4 | !hooks | ||
| 5 | !hooks/** | ||
| 6 | !qlty.toml | ||
| 7 | !.gitignore | ||
.qlty/qlty.toml added +44
| @@ -0,0 +1,44 @@ | |||
| 1 | config_version = "0" | ||
| 2 | |||
| 3 | exclude_patterns = [ | ||
| 4 | "**/vendor/**", | ||
| 5 | "**/node_modules/**", | ||
| 6 | "**/testdata/**", | ||
| 7 | "**/dist/**", | ||
| 8 | "**/.build/**", | ||
| 9 | "**/build/**", | ||
| 10 | "**/*.min.*", | ||
| 11 | "**/generated/**", | ||
| 12 | ".claude/**", | ||
| 13 | "**/Pods/**", | ||
| 14 | ] | ||
| 15 | |||
| 16 | test_patterns = [ | ||
| 17 | "**/test/**", | ||
| 18 | "**/tests/**", | ||
| 19 | "**/Tests/**", | ||
| 20 | "**/*_test.*", | ||
| 21 | "**/test_*.*", | ||
| 22 | "**/*.test.*", | ||
| 23 | "**/*Tests.swift", | ||
| 24 | "e2e/**", | ||
| 25 | ] | ||
| 26 | |||
| 27 | [[source]] | ||
| 28 | name = "default" | ||
| 29 | default = true | ||
| 30 | |||
| 31 | [[plugin]] | ||
| 32 | name = "osv-scanner" | ||
| 33 | |||
| 34 | [[plugin]] | ||
| 35 | name = "trufflehog" | ||
| 36 | |||
| 37 | # Early returns are guard clauses, which keep the happy path unindented. | ||
| 38 | [smells.return_statements] | ||
| 39 | enabled = false | ||
| 40 | |||
| 41 | # Total file complexity is the sum of its functions, which function_complexity | ||
| 42 | # already checks. | ||
| 43 | [smells.file_complexity] | ||
| 44 | enabled = false | ||
scripts/check-mappings.mjs +3 −3
| @@ -26,7 +26,7 @@ const NULL_STATUS = "·"; | |||
| 26 | // The rationale is part of the key: it is the auditor-facing string in every | 26 | // The rationale is part of the key: it is the auditor-facing string in every |
| 27 | // export, so the doc's copy drifting from the SQL is as much a lie as a | 27 | // export, so the doc's copy drifting from the SQL is as much a lie as a |
| 28 | // wrong posture. | 28 | // wrong posture. |
| 29 | const key = (resource, status, framework, control, posture, rationale) => | 29 | const key = ({ resource, status, framework, control, posture, rationale }) => |
| 30 | `${resource}|${status ?? NULL_STATUS}|${framework}|${control}|${posture}|${rationale}`; | 30 | `${resource}|${status ?? NULL_STATUS}|${framework}|${control}|${posture}|${rationale}`; |
| 31 | 31 | ||
| 32 | // --- 1. Source of truth: apply migrations, read control_mappings. --- | 32 | // --- 1. Source of truth: apply migrations, read control_mappings. --- |
| @@ -42,7 +42,7 @@ function rowsFromMigrations() { | |||
| 42 | .prepare("SELECT resource, status, framework, control_id, posture, rationale FROM control_mappings") | 42 | .prepare("SELECT resource, status, framework, control_id, posture, rationale FROM control_mappings") |
| 43 | .all(); | 43 | .all(); |
| 44 | db.close(); | 44 | db.close(); |
| 45 | return new Set(rows.map((r) => key(r.resource, r.status, r.framework, r.control_id, r.posture, r.rationale))); | 45 | return new Set(rows.map((r) => key({ ...r, control: r.control_id }))); |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | // --- 2. Human-readable copy: parse the doc's reference table. --- | 48 | // --- 2. Human-readable copy: parse the doc's reference table. --- |
| @@ -66,7 +66,7 @@ function rowsFromDoc() { | |||
| 66 | const framework = FRAMEWORK_LABELS[frameworkLabel]; | 66 | const framework = FRAMEWORK_LABELS[frameworkLabel]; |
| 67 | if (!framework || !POSTURES.has(posture) || !BACKTICKED.test(resource ?? "")) continue; | 67 | if (!framework || !POSTURES.has(posture) || !BACKTICKED.test(resource ?? "")) continue; |
| 68 | const status = statusCell.replaceAll("`", ""); // "·" for NULL | 68 | const status = statusCell.replaceAll("`", ""); // "·" for NULL |
| 69 | set.add(key(resource.replaceAll("`", ""), status, framework, control, posture, rationale ?? "")); | 69 | set.add(key({ resource: resource.replaceAll("`", ""), status, framework, control, posture, rationale: rationale ?? "" })); |
| 70 | } | 70 | } |
| 71 | return set; | 71 | return set; |
| 72 | } | 72 | } |
src/dashboard.ts +49 −43
| @@ -114,79 +114,85 @@ const STYLE = ` | |||
| 114 | code { background: #eef0f3; padding: 0.1rem 0.3rem; border-radius: 4px; font-size: 0.85em; } | 114 | code { background: #eef0f3; padding: 0.1rem 0.3rem; border-radius: 4px; font-size: 0.85em; } |
| 115 | `; | 115 | `; |
| 116 | 116 | ||
| 117 | export function renderDashboard(data: DashboardData): string { | 117 | function postureCounts(rows: EvidenceRow[]) { |
| 118 | const counts = { positive: 0, negative: 0, informational: 0 }; | 118 | const counts = { positive: 0, negative: 0, informational: 0 }; |
| 119 | const repos = new Set<string>(); | 119 | const repos = new Set<string>(); |
| 120 | for (const r of data.rows) { | 120 | for (const r of rows) { |
| 121 | counts[r.posture as keyof typeof counts] = (counts[r.posture as keyof typeof counts] ?? 0) + 1; | 121 | counts[r.posture as keyof typeof counts] = (counts[r.posture as keyof typeof counts] ?? 0) + 1; |
| 122 | if (r.repo) repos.add(r.repo); | 122 | if (r.repo) repos.add(r.repo); |
| 123 | } | 123 | } |
| 124 | return { counts, repos }; | ||
| 125 | } | ||
| 124 | 126 | ||
| 125 | // Cards count the whole framework view, so the numbers stay stable while a | 127 | function evidenceRowHtml(r: EvidenceRow): string { |
| 126 | // posture filter is applied — otherwise clicking "Gaps" would zero the other | 128 | return `<tr> |
| 127 | // two cards and there would be nothing left to click. | ||
| 128 | const href = (framework: Framework, posture: Posture | null) => | ||
| 129 | `/?framework=${framework}${posture ? `&posture=${posture}` : ""}`; | ||
| 130 | |||
| 131 | const frameworkTab = (value: Framework, label: string) => | ||
| 132 | `<a href="${href(value, data.posture)}" class="${data.framework === value ? "active" : ""}">${label}</a>`; | ||
| 133 | |||
| 134 | // Clicking the active card clears the filter rather than reapplying it. | ||
| 135 | const postureCard = (posture: Posture, label: string) => { | ||
| 136 | const active = data.posture === posture; | ||
| 137 | return `<a class="card${active ? " active" : ""}" href="${href(data.framework, active ? null : posture)}" | ||
| 138 | aria-pressed="${active}"><div class="n ${posture}">${counts[posture]}</div><div class="l">${label}</div></a>`; | ||
| 139 | }; | ||
| 140 | |||
| 141 | const visibleRows = data.posture ? data.rows.filter((r) => r.posture === data.posture) : data.rows; | ||
| 142 | |||
| 143 | const evidenceRows = visibleRows | ||
| 144 | .map( | ||
| 145 | (r) => `<tr> | ||
| 146 | <td>${esc(r.framework)}</td> | 129 | <td>${esc(r.framework)}</td> |
| 147 | <td>${esc(r.control_id)}</td> | 130 | <td>${esc(r.control_id)}</td> |
| 148 | <td class="posture ${esc(r.posture)}">${esc(r.posture)}</td> | 131 | <td class="posture ${esc(r.posture)}">${esc(r.posture)}</td> |
| 149 | <td>${esc(r.repo ?? r.subject ?? "—")}</td> | 132 | <td>${esc(r.repo ?? r.subject ?? "—")}</td> |
| 150 | <td>${esc(r.resource)}</td> | 133 | <td>${esc(r.resource)}</td> |
| 151 | <td>${esc(r.status)}</td> | 134 | <td>${esc(r.status)}</td> |
| 152 | </tr>`, | 135 | </tr>`; |
| 153 | ) | 136 | } |
| 154 | .join(""); | ||
| 155 | 137 | ||
| 156 | const exportRows = data.exports | 138 | function exportRowHtml(e: ExportListRow): string { |
| 157 | .map((e) => { | 139 | const done = e.status === "done"; |
| 158 | const done = e.status === "done"; | 140 | const cell = done |
| 159 | const cell = done | 141 | ? `<a href="/exports/${esc(e.id)}/download">Download ${esc(e.format.toUpperCase())}</a>` |
| 160 | ? `<a href="/exports/${esc(e.id)}/download">Download ${esc(e.format.toUpperCase())}</a>` | 142 | : `<span class="muted" data-export-id="${esc(e.id)}">${esc(e.status)}…</span>`; |
| 161 | : `<span class="muted" data-export-id="${esc(e.id)}">${esc(e.status)}…</span>`; | 143 | return `<tr> |
| 162 | return `<tr> | ||
| 163 | <td>${esc(e.created_at)}</td> | 144 | <td>${esc(e.created_at)}</td> |
| 164 | <td>${esc(e.framework)}</td> | 145 | <td>${esc(e.framework)}</td> |
| 165 | <td>${esc(e.format.toUpperCase())}</td> | 146 | <td>${esc(e.format.toUpperCase())}</td> |
| 166 | <td class="export-status">${cell}</td> | 147 | <td class="export-status">${cell}</td> |
| 167 | </tr>`; | 148 | </tr>`; |
| 168 | }) | 149 | } |
| 169 | .join(""); | ||
| 170 | 150 | ||
| 171 | const excludeForm = data.excludableRepos.length | 151 | function excludeFormHtml(excludableRepos: string[]): string { |
| 152 | return excludableRepos.length | ||
| 172 | ? `<div class="bar"><form method="post" action="/exclusions"> | 153 | ? `<div class="bar"><form method="post" action="/exclusions"> |
| 173 | <select name="repo">${data.excludableRepos.map((r) => `<option value="${esc(r)}">${esc(r)}</option>`).join("")}</select> | 154 | <select name="repo">${excludableRepos.map((r) => `<option value="${esc(r)}">${esc(r)}</option>`).join("")}</select> |
| 174 | <button type="submit">Exclude</button> | 155 | <button type="submit">Exclude</button> |
| 175 | </form></div>` | 156 | </form></div>` |
| 176 | : `<p class="muted">No repositories left to exclude.</p>`; | 157 | : `<p class="muted">No repositories left to exclude.</p>`; |
| 158 | } | ||
| 177 | 159 | ||
| 178 | const exclusionRows = data.excludedRepos | 160 | function exclusionRowHtml(repo: string): string { |
| 179 | .map( | 161 | return `<tr> |
| 180 | (repo) => `<tr> | ||
| 181 | <td>${esc(repo)}</td> | 162 | <td>${esc(repo)}</td> |
| 182 | <td><form method="post" action="/exclusions"> | 163 | <td><form method="post" action="/exclusions"> |
| 183 | <input type="hidden" name="repo" value="${esc(repo)}"> | 164 | <input type="hidden" name="repo" value="${esc(repo)}"> |
| 184 | <input type="hidden" name="action" value="remove"> | 165 | <input type="hidden" name="action" value="remove"> |
| 185 | <button class="secondary" type="submit">Include again</button> | 166 | <button class="secondary" type="submit">Include again</button> |
| 186 | </form></td> | 167 | </form></td> |
| 187 | </tr>`, | 168 | </tr>`; |
| 188 | ) | 169 | } |
| 189 | .join(""); | 170 | |
| 171 | export function renderDashboard(data: DashboardData): string { | ||
| 172 | const { counts, repos } = postureCounts(data.rows); | ||
| 173 | |||
| 174 | // Cards count the whole framework view, so the numbers stay stable while a | ||
| 175 | // posture filter is applied — otherwise clicking "Gaps" would zero the other | ||
| 176 | // two cards and there would be nothing left to click. | ||
| 177 | const href = (framework: Framework, posture: Posture | null) => | ||
| 178 | `/?framework=${framework}${posture ? `&posture=${posture}` : ""}`; | ||
| 179 | |||
| 180 | const frameworkTab = (value: Framework, label: string) => | ||
| 181 | `<a href="${href(value, data.posture)}" class="${data.framework === value ? "active" : ""}">${label}</a>`; | ||
| 182 | |||
| 183 | // Clicking the active card clears the filter rather than reapplying it. | ||
| 184 | const postureCard = (posture: Posture, label: string) => { | ||
| 185 | const active = data.posture === posture; | ||
| 186 | return `<a class="card${active ? " active" : ""}" href="${href(data.framework, active ? null : posture)}" | ||
| 187 | aria-pressed="${active}"><div class="n ${posture}">${counts[posture]}</div><div class="l">${label}</div></a>`; | ||
| 188 | }; | ||
| 189 | |||
| 190 | const visibleRows = data.posture ? data.rows.filter((r) => r.posture === data.posture) : data.rows; | ||
| 191 | |||
| 192 | const evidenceRows = visibleRows.map(evidenceRowHtml).join(""); | ||
| 193 | const exportRows = data.exports.map(exportRowHtml).join(""); | ||
| 194 | const excludeForm = excludeFormHtml(data.excludableRepos); | ||
| 195 | const exclusionRows = data.excludedRepos.map(exclusionRowHtml).join(""); | ||
| 190 | 196 | ||
| 191 | return `<!doctype html> | 197 | return `<!doctype html> |
| 192 | <html lang="en"> | 198 | <html lang="en"> |
src/index.ts +37 −55
| @@ -43,65 +43,47 @@ const CONTENT_TYPE: Record<ExportFormat, string> = { | |||
| 43 | const SNAPSHOT_RETENTION_DAYS = 396; // ~13 months: an annual audit period + buffer | 43 | const SNAPSHOT_RETENTION_DAYS = 396; // ~13 months: an annual audit period + buffer |
| 44 | const EXPORT_RETENTION_DAYS = 90; | 44 | const EXPORT_RETENTION_DAYS = 90; |
| 45 | 45 | ||
| 46 | type Route = (request: Request, env: Env) => Promise<Response> | Response; | ||
| 47 | |||
| 48 | const ROUTES = new Map<string, Route>([ | ||
| 49 | ["POST /webhooks/github", handleWebhook], | ||
| 50 | ["POST /webhooks/marketplace", handleMarketplaceWebhook], | ||
| 51 | ["GET /login", handleLogin], | ||
| 52 | ["GET /callback", handleCallback], | ||
| 53 | ["GET /logout", () => handleLogout()], | ||
| 54 | ["POST /admin/poll", handleAdminPoll], | ||
| 55 | ["POST /admin/export", handleAdminExport], | ||
| 56 | ["POST /admin/cleanup", handleAdminCleanup], | ||
| 57 | ["POST /admin/purge", handleAdminPurge], | ||
| 58 | // Dashboard (session-authed) surfaces. | ||
| 59 | ["POST /resync", handleResync], | ||
| 60 | ["POST /exports", handleCreateExport], | ||
| 61 | ["POST /switch", handleSwitchInstallation], | ||
| 62 | ["POST /exclusions", handleExclusion], | ||
| 63 | ["GET /access-review", handleAccessReview], | ||
| 64 | ["GET /", handleDashboard], | ||
| 65 | ]); | ||
| 66 | |||
| 67 | // GET /admin/export/<id>[/download] (admin token) and /exports/<id>[/download] | ||
| 68 | // (session). | ||
| 69 | const EXPORT_JOB_ROUTES = [ | ||
| 70 | { pattern: /^\/admin\/export\/([0-9a-f-]+)(\/download)?$/, handler: handleAdminExportGet }, | ||
| 71 | { pattern: /^\/exports\/([0-9a-f-]+)(\/download)?$/, handler: handleSessionExportGet }, | ||
| 72 | ]; | ||
| 73 | |||
| 46 | export default { | 74 | export default { |
| 47 | async fetch(request: Request, env: Env): Promise<Response> { | 75 | async fetch(request: Request, env: Env): Promise<Response> { |
| 48 | const url = new URL(request.url); | 76 | const url = new URL(request.url); |
| 49 | 77 | ||
| 50 | if (request.method === "POST" && url.pathname === "/webhooks/github") { | 78 | const route = ROUTES.get(`${request.method} ${url.pathname}`); |
| 51 | return handleWebhook(request, env); | 79 | if (route) return route(request, env); |
| 52 | } | 80 | |
| 53 | if (request.method === "POST" && url.pathname === "/webhooks/marketplace") { | 81 | if (request.method === "GET") { |
| 54 | return handleMarketplaceWebhook(request, env); | 82 | for (const { pattern, handler } of EXPORT_JOB_ROUTES) { |
| 55 | } | 83 | const match = url.pathname.match(pattern); |
| 56 | if (request.method === "GET" && url.pathname === "/login") { | 84 | const [, jobId, downloadSuffix] = match ?? []; |
| 57 | return handleLogin(request, env); | 85 | if (jobId) return handler(request, env, jobId, downloadSuffix === "/download"); |
| 58 | } | 86 | } |
| 59 | if (request.method === "GET" && url.pathname === "/callback") { | ||
| 60 | return handleCallback(request, env); | ||
| 61 | } | ||
| 62 | if (request.method === "GET" && url.pathname === "/logout") { | ||
| 63 | return handleLogout(); | ||
| 64 | } | ||
| 65 | if (request.method === "POST" && url.pathname === "/admin/poll") { | ||
| 66 | return handleAdminPoll(request, env); | ||
| 67 | } | ||
| 68 | if (request.method === "POST" && url.pathname === "/admin/export") { | ||
| 69 | return handleAdminExport(request, env); | ||
| 70 | } | ||
| 71 | if (request.method === "POST" && url.pathname === "/admin/cleanup") { | ||
| 72 | return handleAdminCleanup(request, env); | ||
| 73 | } | ||
| 74 | if (request.method === "POST" && url.pathname === "/admin/purge") { | ||
| 75 | return handleAdminPurge(request, env); | ||
| 76 | } | ||
| 77 | const adminExportMatch = url.pathname.match(/^\/admin\/export\/([0-9a-f-]+)(\/download)?$/); | ||
| 78 | if (request.method === "GET" && adminExportMatch) { | ||
| 79 | const [, jobId, downloadSuffix] = adminExportMatch; | ||
| 80 | if (jobId) return handleAdminExportGet(request, env, jobId, downloadSuffix === "/download"); | ||
| 81 | } | ||
| 82 | // Dashboard (session-authed) surfaces. | ||
| 83 | if (request.method === "POST" && url.pathname === "/resync") { | ||
| 84 | return handleResync(request, env); | ||
| 85 | } | ||
| 86 | if (request.method === "POST" && url.pathname === "/exports") { | ||
| 87 | return handleCreateExport(request, env); | ||
| 88 | } | ||
| 89 | if (request.method === "POST" && url.pathname === "/switch") { | ||
| 90 | return handleSwitchInstallation(request, env); | ||
| 91 | } | ||
| 92 | if (request.method === "POST" && url.pathname === "/exclusions") { | ||
| 93 | return handleExclusion(request, env); | ||
| 94 | } | ||
| 95 | const exportMatch = url.pathname.match(/^\/exports\/([0-9a-f-]+)(\/download)?$/); | ||
| 96 | if (request.method === "GET" && exportMatch) { | ||
| 97 | const [, jobId, downloadSuffix] = exportMatch; | ||
| 98 | if (jobId) return handleSessionExportGet(request, env, jobId, downloadSuffix === "/download"); | ||
| 99 | } | ||
| 100 | if (request.method === "GET" && url.pathname === "/access-review") { | ||
| 101 | return handleAccessReview(request, env); | ||
| 102 | } | ||
| 103 | if (request.method === "GET" && url.pathname === "/") { | ||
| 104 | return handleDashboard(request, env); | ||
| 105 | } | 87 | } |
| 106 | 88 | ||
| 107 | return new Response("Not found", { status: 404 }); | 89 | return new Response("Not found", { status: 404 }); |
src/webhook.ts +82 −87
| @@ -33,96 +33,91 @@ interface ExtractedFact { | |||
| 33 | subject: string | null; | 33 | subject: string | null; |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | type Payload = Record<string, unknown>; | ||
| 37 | type Extractor = (payload: Payload, action: string | undefined) => ExtractedFact; | ||
| 38 | |||
| 39 | function record(value: unknown): Payload | undefined { | ||
| 40 | return value as Payload | undefined; | ||
| 41 | } | ||
| 42 | |||
| 43 | function stringField(obj: Payload | undefined, field: string): string | null { | ||
| 44 | const value = obj?.[field]; | ||
| 45 | return typeof value === "string" ? value : null; | ||
| 46 | } | ||
| 47 | |||
| 36 | // Minimal resource/status extraction per event type. Control-ID mapping | 48 | // Minimal resource/status extraction per event type. Control-ID mapping |
| 37 | // (resource+status -> SOC 2 / ISO 27001 control) is a separate, later step. | 49 | // (resource+status -> SOC 2 / ISO 27001 control) is a separate, later step. |
| 38 | export function extractFact(eventType: string, payload: Record<string, unknown>): ExtractedFact { | 50 | const EXTRACTORS: Record<string, Extractor> = { |
| 39 | const action = typeof payload.action === "string" ? payload.action : undefined; | 51 | // The event is scoped to one rule, which may target any branch. Only a |
| 40 | 52 | // rule whose pattern is exactly the default branch changes the repo's | |
| 41 | switch (eventType) { | 53 | // protection state (normalized to the enabled/disabled vocabulary the |
| 42 | // The event is scoped to one rule, which may target any branch. Only a | 54 | // poller shares); any other rule is recorded as an unmapped trail event, |
| 43 | // rule whose pattern is exactly the default branch changes the repo's | 55 | // with the hourly poll authoritative for current state. |
| 44 | // protection state (normalized to the enabled/disabled vocabulary the | 56 | branch_protection_rule: (payload, action) => { |
| 45 | // poller shares); any other rule is recorded as an unmapped trail event, | 57 | const pattern = stringField(record(payload.rule), "name"); |
| 46 | // with the hourly poll authoritative for current state. | 58 | const defaultBranch = stringField(record(payload.repository), "default_branch") ?? undefined; |
| 47 | case "branch_protection_rule": { | 59 | if (pattern !== null && pattern === defaultBranch) { |
| 48 | const rule = payload.rule as Record<string, unknown> | undefined; | 60 | return { resource: "branch_protection", status: action === "deleted" ? "disabled" : "enabled", subject: null }; |
| 49 | const repository = payload.repository as Record<string, unknown> | undefined; | ||
| 50 | const pattern = typeof rule?.name === "string" ? rule.name : null; | ||
| 51 | const defaultBranch = typeof repository?.default_branch === "string" ? repository.default_branch : undefined; | ||
| 52 | if (pattern !== null && pattern === defaultBranch) { | ||
| 53 | return { resource: "branch_protection", status: action === "deleted" ? "disabled" : "enabled", subject: null }; | ||
| 54 | } | ||
| 55 | return { resource: "branch_protection_rule_event", status: action ?? "unknown", subject: pattern }; | ||
| 56 | } | ||
| 57 | // Ruleset events are ruleset-scoped: one ruleset being created or deleted | ||
| 58 | // says nothing about whether *other* active rulesets still cover the | ||
| 59 | // default branch, so this is trail-only; current repository_ruleset state | ||
| 60 | // comes from the poller's /rules/branches/{default-branch} check. | ||
| 61 | case "repository_ruleset": { | ||
| 62 | const ruleset = payload.repository_ruleset as Record<string, unknown> | undefined; | ||
| 63 | const id = typeof ruleset?.id === "number" ? String(ruleset.id) : null; | ||
| 64 | return { resource: "repository_ruleset_event", status: action ?? "unknown", subject: id }; | ||
| 65 | } | ||
| 66 | case "dependabot_alert": | ||
| 67 | case "code_scanning_alert": { | ||
| 68 | const alert = payload.alert as Record<string, unknown> | undefined; | ||
| 69 | const state = typeof alert?.state === "string" ? alert.state : undefined; | ||
| 70 | return { resource: eventType, status: state ?? action ?? "unknown", subject: alertNumber(alert) }; | ||
| 71 | } | 61 | } |
| 72 | // Unlike the two alert payloads above, the secret-scanning webhook alert | 62 | return { resource: "branch_protection_rule_event", status: action ?? "unknown", subject: pattern }; |
| 73 | // carries no `state` field — only `resolution`, which is set iff the | 63 | }, |
| 74 | // alert is resolved. Derive open/resolved from that (falling back to | 64 | // Ruleset events are ruleset-scoped: one ruleset being created or deleted |
| 75 | // `state` should GitHub ever add it). | 65 | // says nothing about whether *other* active rulesets still cover the |
| 76 | case "secret_scanning_alert": { | 66 | // default branch, so this is trail-only; current repository_ruleset state |
| 77 | const alert = payload.alert as Record<string, unknown> | undefined; | 67 | // comes from the poller's /rules/branches/{default-branch} check. |
| 78 | const state = typeof alert?.state === "string" ? alert.state : undefined; | 68 | repository_ruleset: (payload, action) => { |
| 79 | return { | 69 | const ruleset = record(payload.repository_ruleset); |
| 80 | resource: eventType, | 70 | const id = typeof ruleset?.id === "number" ? String(ruleset.id) : null; |
| 81 | status: state ?? (alert?.resolution ? "resolved" : "open"), | 71 | return { resource: "repository_ruleset_event", status: action ?? "unknown", subject: id }; |
| 82 | subject: alertNumber(alert), | 72 | }, |
| 83 | }; | 73 | dependabot_alert: (payload, action) => stateAlert("dependabot_alert", payload, action), |
| 84 | } | 74 | code_scanning_alert: (payload, action) => stateAlert("code_scanning_alert", payload, action), |
| 85 | // Repository collaborators. Org-level membership arrives on the | 75 | // Unlike the two alert payloads above, the secret-scanning webhook alert |
| 86 | // `organization` event below, not here. | 76 | // carries no `state` field — only `resolution`, which is set iff the |
| 87 | case "member": { | 77 | // alert is resolved. Derive open/resolved from that (falling back to |
| 88 | const member = payload.member as Record<string, unknown> | undefined; | 78 | // `state` should GitHub ever add it). |
| 89 | return { | 79 | secret_scanning_alert: (payload) => { |
| 90 | resource: "member_access", | 80 | const alert = record(payload.alert); |
| 91 | status: action ?? "unknown", | 81 | return { |
| 92 | subject: typeof member?.login === "string" ? member.login : null, | 82 | resource: "secret_scanning_alert", |
| 93 | }; | 83 | status: stringField(alert, "state") ?? (alert?.resolution ? "resolved" : "open"), |
| 94 | } | 84 | subject: alertNumber(alert), |
| 95 | case "team": { | 85 | }; |
| 96 | const team = payload.team as Record<string, unknown> | undefined; | 86 | }, |
| 97 | return { | 87 | // Repository collaborators. Org-level membership arrives on the |
| 98 | resource: "team", | 88 | // `organization` event below, not here. |
| 99 | status: action ?? "unknown", | 89 | member: (payload, action) => ({ |
| 100 | subject: typeof team?.slug === "string" ? team.slug : null, | 90 | resource: "member_access", |
| 101 | }; | 91 | status: action ?? "unknown", |
| 102 | } | 92 | subject: stringField(record(payload.member), "login"), |
| 103 | case "organization": { | 93 | }), |
| 104 | const membership = payload.membership as Record<string, unknown> | undefined; | 94 | team: (payload, action) => ({ |
| 105 | const user = membership?.user as Record<string, unknown> | undefined; | 95 | resource: "team", |
| 106 | // member_invited identifies the invitee via `invitation` (login for | 96 | status: action ?? "unknown", |
| 107 | // existing users, email otherwise) rather than `membership`. | 97 | subject: stringField(record(payload.team), "slug"), |
| 108 | const invitation = payload.invitation as Record<string, unknown> | undefined; | 98 | }), |
| 109 | const subject = | 99 | // member_invited identifies the invitee via `invitation` (login for |
| 110 | typeof user?.login === "string" | 100 | // existing users, email otherwise) rather than `membership`. |
| 111 | ? user.login | 101 | organization: (payload, action) => { |
| 112 | : typeof invitation?.login === "string" | 102 | const user = record(record(payload.membership)?.user); |
| 113 | ? invitation.login | 103 | const invitation = record(payload.invitation); |
| 114 | : typeof invitation?.email === "string" | 104 | const subject = |
| 115 | ? invitation.email | 105 | stringField(user, "login") ?? stringField(invitation, "login") ?? stringField(invitation, "email"); |
| 116 | : null; | 106 | return { resource: "org_membership", status: action ?? "unknown", subject }; |
| 117 | return { resource: "org_membership", status: action ?? "unknown", subject }; | 107 | }, |
| 118 | } | 108 | repository: (_payload, action) => ({ resource: "repository", status: action ?? "unknown", subject: null }), |
| 119 | case "repository": | 109 | push: () => ({ resource: "push", status: "received", subject: null }), |
| 120 | return { resource: "repository", status: action ?? "unknown", subject: null }; | 110 | }; |
| 121 | case "push": | 111 | |
| 122 | return { resource: "push", status: "received", subject: null }; | 112 | function stateAlert(resource: string, payload: Payload, action: string | undefined): ExtractedFact { |
| 123 | default: | 113 | const alert = record(payload.alert); |
| 124 | return { resource: eventType, status: action ?? "received", subject: null }; | 114 | return { resource, status: stringField(alert, "state") ?? action ?? "unknown", subject: alertNumber(alert) }; |
| 125 | } | 115 | } |
| 116 | |||
| 117 | export function extractFact(eventType: string, payload: Payload): ExtractedFact { | ||
| 118 | const action = typeof payload.action === "string" ? payload.action : undefined; | ||
| 119 | const extract = Object.hasOwn(EXTRACTORS, eventType) ? EXTRACTORS[eventType] : undefined; | ||
| 120 | return extract ? extract(payload, action) : { resource: eventType, status: action ?? "received", subject: null }; | ||
| 126 | } | 121 | } |
| 127 | 122 | ||
| 128 | function alertNumber(alert: Record<string, unknown> | undefined): string | null { | 123 | function alertNumber(alert: Record<string, unknown> | undefined): string | null { |