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 | 26 | // The rationale is part of the key: it is the auditor-facing string in every |
| 27 | 27 | // export, so the doc's copy drifting from the SQL is as much a lie as a |
| 28 | 28 | // wrong posture. |
| 29 | const key = (resource, status, framework, control, posture, rationale) => | |
| 29 | const key = ({ resource, status, framework, control, posture, rationale }) => | |
| 30 | 30 | `${resource}|${status ?? NULL_STATUS}|${framework}|${control}|${posture}|${rationale}`; |
| 31 | 31 | |
| 32 | 32 | // --- 1. Source of truth: apply migrations, read control_mappings. --- |
| @@ -42,7 +42,7 @@ function rowsFromMigrations() { | ||
| 42 | 42 | .prepare("SELECT resource, status, framework, control_id, posture, rationale FROM control_mappings") |
| 43 | 43 | .all(); |
| 44 | 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 | 48 | // --- 2. Human-readable copy: parse the doc's reference table. --- |
| @@ -66,7 +66,7 @@ function rowsFromDoc() { | ||
| 66 | 66 | const framework = FRAMEWORK_LABELS[frameworkLabel]; |
| 67 | 67 | if (!framework || !POSTURES.has(posture) || !BACKTICKED.test(resource ?? "")) continue; |
| 68 | 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 | 71 | return set; |
| 72 | 72 | } |
src/dashboard.ts +49 −43
| @@ -114,79 +114,85 @@ const STYLE = ` | ||
| 114 | 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 | 118 | const counts = { positive: 0, negative: 0, informational: 0 }; |
| 119 | 119 | const repos = new Set<string>(); |
| 120 | for (const r of data.rows) { | |
| 120 | for (const r of rows) { | |
| 121 | 121 | counts[r.posture as keyof typeof counts] = (counts[r.posture as keyof typeof counts] ?? 0) + 1; |
| 122 | 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 | |
| 126 | // posture filter is applied — otherwise clicking "Gaps" would zero the other | |
| 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> | |
| 127 | function evidenceRowHtml(r: EvidenceRow): string { | |
| 128 | return `<tr> | |
| 146 | 129 | <td>${esc(r.framework)}</td> |
| 147 | 130 | <td>${esc(r.control_id)}</td> |
| 148 | 131 | <td class="posture ${esc(r.posture)}">${esc(r.posture)}</td> |
| 149 | 132 | <td>${esc(r.repo ?? r.subject ?? "—")}</td> |
| 150 | 133 | <td>${esc(r.resource)}</td> |
| 151 | 134 | <td>${esc(r.status)}</td> |
| 152 | </tr>`, | |
| 153 | ) | |
| 154 | .join(""); | |
| 135 | </tr>`; | |
| 136 | } | |
| 155 | 137 | |
| 156 | const exportRows = data.exports | |
| 157 | .map((e) => { | |
| 158 | const done = e.status === "done"; | |
| 159 | const cell = done | |
| 160 | ? `<a href="/exports/${esc(e.id)}/download">Download ${esc(e.format.toUpperCase())}</a>` | |
| 161 | : `<span class="muted" data-export-id="${esc(e.id)}">${esc(e.status)}…</span>`; | |
| 162 | return `<tr> | |
| 138 | function exportRowHtml(e: ExportListRow): string { | |
| 139 | const done = e.status === "done"; | |
| 140 | const cell = done | |
| 141 | ? `<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>`; | |
| 143 | return `<tr> | |
| 163 | 144 | <td>${esc(e.created_at)}</td> |
| 164 | 145 | <td>${esc(e.framework)}</td> |
| 165 | 146 | <td>${esc(e.format.toUpperCase())}</td> |
| 166 | 147 | <td class="export-status">${cell}</td> |
| 167 | 148 | </tr>`; |
| 168 | }) | |
| 169 | .join(""); | |
| 149 | } | |
| 170 | 150 | |
| 171 | const excludeForm = data.excludableRepos.length | |
| 151 | function excludeFormHtml(excludableRepos: string[]): string { | |
| 152 | return excludableRepos.length | |
| 172 | 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 | 155 | <button type="submit">Exclude</button> |
| 175 | 156 | </form></div>` |
| 176 | 157 | : `<p class="muted">No repositories left to exclude.</p>`; |
| 158 | } | |
| 177 | 159 | |
| 178 | const exclusionRows = data.excludedRepos | |
| 179 | .map( | |
| 180 | (repo) => `<tr> | |
| 160 | function exclusionRowHtml(repo: string): string { | |
| 161 | return `<tr> | |
| 181 | 162 | <td>${esc(repo)}</td> |
| 182 | 163 | <td><form method="post" action="/exclusions"> |
| 183 | 164 | <input type="hidden" name="repo" value="${esc(repo)}"> |
| 184 | 165 | <input type="hidden" name="action" value="remove"> |
| 185 | 166 | <button class="secondary" type="submit">Include again</button> |
| 186 | 167 | </form></td> |
| 187 | </tr>`, | |
| 188 | ) | |
| 189 | .join(""); | |
| 168 | </tr>`; | |
| 169 | } | |
| 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 | 197 | return `<!doctype html> |
| 192 | 198 | <html lang="en"> |
src/index.ts +37 −55
| @@ -43,65 +43,47 @@ const CONTENT_TYPE: Record<ExportFormat, string> = { | ||
| 43 | 43 | const SNAPSHOT_RETENTION_DAYS = 396; // ~13 months: an annual audit period + buffer |
| 44 | 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 | 74 | export default { |
| 47 | 75 | async fetch(request: Request, env: Env): Promise<Response> { |
| 48 | 76 | const url = new URL(request.url); |
| 49 | 77 | |
| 50 | if (request.method === "POST" && url.pathname === "/webhooks/github") { | |
| 51 | return handleWebhook(request, env); | |
| 52 | } | |
| 53 | if (request.method === "POST" && url.pathname === "/webhooks/marketplace") { | |
| 54 | return handleMarketplaceWebhook(request, env); | |
| 55 | } | |
| 56 | if (request.method === "GET" && url.pathname === "/login") { | |
| 57 | return handleLogin(request, env); | |
| 58 | } | |
| 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); | |
| 78 | const route = ROUTES.get(`${request.method} ${url.pathname}`); | |
| 79 | if (route) return route(request, env); | |
| 80 | ||
| 81 | if (request.method === "GET") { | |
| 82 | for (const { pattern, handler } of EXPORT_JOB_ROUTES) { | |
| 83 | const match = url.pathname.match(pattern); | |
| 84 | const [, jobId, downloadSuffix] = match ?? []; | |
| 85 | if (jobId) return handler(request, env, jobId, downloadSuffix === "/download"); | |
| 86 | } | |
| 105 | 87 | } |
| 106 | 88 | |
| 107 | 89 | return new Response("Not found", { status: 404 }); |
src/webhook.ts +82 −87
| @@ -33,96 +33,91 @@ interface ExtractedFact { | ||
| 33 | 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 | 48 | // Minimal resource/status extraction per event type. Control-ID mapping |
| 37 | 49 | // (resource+status -> SOC 2 / ISO 27001 control) is a separate, later step. |
| 38 | export function extractFact(eventType: string, payload: Record<string, unknown>): ExtractedFact { | |
| 39 | const action = typeof payload.action === "string" ? payload.action : undefined; | |
| 40 | ||
| 41 | switch (eventType) { | |
| 42 | // The event is scoped to one rule, which may target any branch. Only a | |
| 43 | // rule whose pattern is exactly the default branch changes the repo's | |
| 44 | // protection state (normalized to the enabled/disabled vocabulary the | |
| 45 | // poller shares); any other rule is recorded as an unmapped trail event, | |
| 46 | // with the hourly poll authoritative for current state. | |
| 47 | case "branch_protection_rule": { | |
| 48 | const rule = payload.rule as Record<string, unknown> | undefined; | |
| 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) }; | |
| 50 | const EXTRACTORS: Record<string, Extractor> = { | |
| 51 | // The event is scoped to one rule, which may target any branch. Only a | |
| 52 | // rule whose pattern is exactly the default branch changes the repo's | |
| 53 | // protection state (normalized to the enabled/disabled vocabulary the | |
| 54 | // poller shares); any other rule is recorded as an unmapped trail event, | |
| 55 | // with the hourly poll authoritative for current state. | |
| 56 | branch_protection_rule: (payload, action) => { | |
| 57 | const pattern = stringField(record(payload.rule), "name"); | |
| 58 | const defaultBranch = stringField(record(payload.repository), "default_branch") ?? undefined; | |
| 59 | if (pattern !== null && pattern === defaultBranch) { | |
| 60 | return { resource: "branch_protection", status: action === "deleted" ? "disabled" : "enabled", subject: null }; | |
| 71 | 61 | } |
| 72 | // Unlike the two alert payloads above, the secret-scanning webhook alert | |
| 73 | // carries no `state` field — only `resolution`, which is set iff the | |
| 74 | // alert is resolved. Derive open/resolved from that (falling back to | |
| 75 | // `state` should GitHub ever add it). | |
| 76 | case "secret_scanning_alert": { | |
| 77 | const alert = payload.alert as Record<string, unknown> | undefined; | |
| 78 | const state = typeof alert?.state === "string" ? alert.state : undefined; | |
| 79 | return { | |
| 80 | resource: eventType, | |
| 81 | status: state ?? (alert?.resolution ? "resolved" : "open"), | |
| 82 | subject: alertNumber(alert), | |
| 83 | }; | |
| 84 | } | |
| 85 | // Repository collaborators. Org-level membership arrives on the | |
| 86 | // `organization` event below, not here. | |
| 87 | case "member": { | |
| 88 | const member = payload.member as Record<string, unknown> | undefined; | |
| 89 | return { | |
| 90 | resource: "member_access", | |
| 91 | status: action ?? "unknown", | |
| 92 | subject: typeof member?.login === "string" ? member.login : null, | |
| 93 | }; | |
| 94 | } | |
| 95 | case "team": { | |
| 96 | const team = payload.team as Record<string, unknown> | undefined; | |
| 97 | return { | |
| 98 | resource: "team", | |
| 99 | status: action ?? "unknown", | |
| 100 | subject: typeof team?.slug === "string" ? team.slug : null, | |
| 101 | }; | |
| 102 | } | |
| 103 | case "organization": { | |
| 104 | const membership = payload.membership as Record<string, unknown> | undefined; | |
| 105 | const user = membership?.user as Record<string, unknown> | undefined; | |
| 106 | // member_invited identifies the invitee via `invitation` (login for | |
| 107 | // existing users, email otherwise) rather than `membership`. | |
| 108 | const invitation = payload.invitation as Record<string, unknown> | undefined; | |
| 109 | const subject = | |
| 110 | typeof user?.login === "string" | |
| 111 | ? user.login | |
| 112 | : typeof invitation?.login === "string" | |
| 113 | ? invitation.login | |
| 114 | : typeof invitation?.email === "string" | |
| 115 | ? invitation.email | |
| 116 | : null; | |
| 117 | return { resource: "org_membership", status: action ?? "unknown", subject }; | |
| 118 | } | |
| 119 | case "repository": | |
| 120 | return { resource: "repository", status: action ?? "unknown", subject: null }; | |
| 121 | case "push": | |
| 122 | return { resource: "push", status: "received", subject: null }; | |
| 123 | default: | |
| 124 | return { resource: eventType, status: action ?? "received", subject: null }; | |
| 125 | } | |
| 62 | return { resource: "branch_protection_rule_event", status: action ?? "unknown", subject: pattern }; | |
| 63 | }, | |
| 64 | // Ruleset events are ruleset-scoped: one ruleset being created or deleted | |
| 65 | // says nothing about whether *other* active rulesets still cover the | |
| 66 | // default branch, so this is trail-only; current repository_ruleset state | |
| 67 | // comes from the poller's /rules/branches/{default-branch} check. | |
| 68 | repository_ruleset: (payload, action) => { | |
| 69 | const ruleset = record(payload.repository_ruleset); | |
| 70 | const id = typeof ruleset?.id === "number" ? String(ruleset.id) : null; | |
| 71 | return { resource: "repository_ruleset_event", status: action ?? "unknown", subject: id }; | |
| 72 | }, | |
| 73 | dependabot_alert: (payload, action) => stateAlert("dependabot_alert", payload, action), | |
| 74 | code_scanning_alert: (payload, action) => stateAlert("code_scanning_alert", payload, action), | |
| 75 | // Unlike the two alert payloads above, the secret-scanning webhook alert | |
| 76 | // carries no `state` field — only `resolution`, which is set iff the | |
| 77 | // alert is resolved. Derive open/resolved from that (falling back to | |
| 78 | // `state` should GitHub ever add it). | |
| 79 | secret_scanning_alert: (payload) => { | |
| 80 | const alert = record(payload.alert); | |
| 81 | return { | |
| 82 | resource: "secret_scanning_alert", | |
| 83 | status: stringField(alert, "state") ?? (alert?.resolution ? "resolved" : "open"), | |
| 84 | subject: alertNumber(alert), | |
| 85 | }; | |
| 86 | }, | |
| 87 | // Repository collaborators. Org-level membership arrives on the | |
| 88 | // `organization` event below, not here. | |
| 89 | member: (payload, action) => ({ | |
| 90 | resource: "member_access", | |
| 91 | status: action ?? "unknown", | |
| 92 | subject: stringField(record(payload.member), "login"), | |
| 93 | }), | |
| 94 | team: (payload, action) => ({ | |
| 95 | resource: "team", | |
| 96 | status: action ?? "unknown", | |
| 97 | subject: stringField(record(payload.team), "slug"), | |
| 98 | }), | |
| 99 | // member_invited identifies the invitee via `invitation` (login for | |
| 100 | // existing users, email otherwise) rather than `membership`. | |
| 101 | organization: (payload, action) => { | |
| 102 | const user = record(record(payload.membership)?.user); | |
| 103 | const invitation = record(payload.invitation); | |
| 104 | const subject = | |
| 105 | stringField(user, "login") ?? stringField(invitation, "login") ?? stringField(invitation, "email"); | |
| 106 | return { resource: "org_membership", status: action ?? "unknown", subject }; | |
| 107 | }, | |
| 108 | repository: (_payload, action) => ({ resource: "repository", status: action ?? "unknown", subject: null }), | |
| 109 | push: () => ({ resource: "push", status: "received", subject: null }), | |
| 110 | }; | |
| 111 | ||
| 112 | function stateAlert(resource: string, payload: Payload, action: string | undefined): ExtractedFact { | |
| 113 | const alert = record(payload.alert); | |
| 114 | return { resource, status: stringField(alert, "state") ?? action ?? "unknown", subject: alertNumber(alert) }; | |
| 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 | 123 | function alertNumber(alert: Record<string, unknown> | undefined): string | null { |