|
| 1 | +import { QueryExecutor } from '@crowd/data-access-layer/src/queryExecutor' |
| 2 | +import { getServiceChildLogger } from '@crowd/logging' |
| 3 | + |
| 4 | +import { getSecurityContactsConfig } from '../config' |
| 5 | + |
| 6 | +import { buildBaseDeps, processRepo } from './processBatch' |
| 7 | +import { RepoPackage, RepoTarget } from './types' |
| 8 | +import { markRepoAttempted } from './writeContacts' |
| 9 | + |
| 10 | +const log = getServiceChildLogger('security-contacts-ondemand') |
| 11 | + |
| 12 | +type Config = ReturnType<typeof getSecurityContactsConfig> |
| 13 | + |
| 14 | +export interface IngestSingleResult { |
| 15 | + found: boolean |
| 16 | + repoId?: string |
| 17 | +} |
| 18 | + |
| 19 | +interface SingleRepoRow { |
| 20 | + repoId: string |
| 21 | + url: string |
| 22 | + homepage: string | null |
| 23 | + archived: boolean | null |
| 24 | + packages: RepoPackage[] | null |
| 25 | +} |
| 26 | + |
| 27 | +// Mirrors the best-repo LATERAL selection in getPackageDetailByPurl |
| 28 | +// (services/libs/data-access-layer/src/osspckgs/api.ts) so the repo we ingest is the |
| 29 | +// one the read side surfaces. No `is_critical` filter — non-critical purls are exactly |
| 30 | +// what this on-demand path exists for. `packages` aggregates every package linked to |
| 31 | +// the repo (not just the requested purl) so extractors see every ecosystem, matching |
| 32 | +// the shape the batch sweep builds. |
| 33 | +// |
| 34 | +// No host filter: processRepo already degrades gracefully for non-github repos (the |
| 35 | +// github-specific extractors no-op, security.txt/registry-manifest extractors still run) — |
| 36 | +// filtering here would leave non-github repos permanently NULL, re-triggering this |
| 37 | +// on-demand path on every single request. |
| 38 | +async function findBestRepoForPurl(qx: QueryExecutor, purl: string): Promise<SingleRepoRow | null> { |
| 39 | + return qx.selectOneOrNone( |
| 40 | + ` |
| 41 | + SELECT r.id::text AS "repoId", |
| 42 | + r.url, |
| 43 | + r.homepage, |
| 44 | + r.archived, |
| 45 | + ( |
| 46 | + SELECT json_agg(json_build_object('purl', p2.purl, 'ecosystem', p2.ecosystem)) |
| 47 | + FROM package_repos pr2 |
| 48 | + JOIN packages p2 ON p2.id = pr2.package_id |
| 49 | + WHERE pr2.repo_id = r.id |
| 50 | + ) AS packages |
| 51 | + FROM packages p |
| 52 | + JOIN LATERAL ( |
| 53 | + SELECT pr.repo_id |
| 54 | + FROM package_repos pr |
| 55 | + WHERE pr.package_id = p.id |
| 56 | + ORDER BY pr.confidence DESC, (pr.source = 'declared') DESC |
| 57 | + LIMIT 1 |
| 58 | + ) best ON true |
| 59 | + JOIN repos r ON r.id = best.repo_id |
| 60 | + WHERE p.purl = $(purl) |
| 61 | + `, |
| 62 | + { purl }, |
| 63 | + ) |
| 64 | +} |
| 65 | + |
| 66 | +function toTarget(row: SingleRepoRow): RepoTarget { |
| 67 | + return { |
| 68 | + repoId: row.repoId, |
| 69 | + url: row.url, |
| 70 | + homepage: row.homepage, |
| 71 | + archived: row.archived, |
| 72 | + packages: row.packages ?? [], |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * On-demand ingest for a single purl, bypassing the daily critical-only sweep. |
| 78 | + * Used when the akrites API hits a repo that has never been evaluated |
| 79 | + * (repos.contacts_last_refreshed IS NULL). |
| 80 | + */ |
| 81 | +export async function ingestSecurityContactsForPurl( |
| 82 | + qx: QueryExecutor, |
| 83 | + config: Config, |
| 84 | + purl: string, |
| 85 | +): Promise<IngestSingleResult> { |
| 86 | + const row = await findBestRepoForPurl(qx, purl) |
| 87 | + if (!row) { |
| 88 | + log.info({ purl }, 'On-demand security contacts: no linked repo found — skipping') |
| 89 | + return { found: false } |
| 90 | + } |
| 91 | + |
| 92 | + const target = toTarget(row) |
| 93 | + const baseDeps = buildBaseDeps(config) |
| 94 | + try { |
| 95 | + await processRepo(target, baseDeps, qx) |
| 96 | + } catch (err) { |
| 97 | + log.error({ repoId: target.repoId, errMsg: (err as Error).message }, 'Repo processing failed') |
| 98 | + await markRepoAttempted(qx, target.repoId).catch(() => undefined) |
| 99 | + } |
| 100 | + |
| 101 | + return { found: true, repoId: target.repoId } |
| 102 | +} |
0 commit comments