-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathbatchGetStewardship.ts
More file actions
59 lines (49 loc) · 1.73 KB
/
Copy pathbatchGetStewardship.ts
File metadata and controls
59 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import type { Request, Response } from 'express'
import { z } from 'zod'
import { getPackagesByStewardshipPurls } from '@crowd/data-access-layer'
import { getPackagesQx } from '@/db/packagesDb'
import { ok } from '@/utils/api'
import { validateOrThrow } from '@/utils/validation'
import type { StewardshipSummary } from './types'
const MAX_PURLS = 100
const bodySchema = z.object({
purls: z
.array(
z
.string()
.trim()
.min(1)
.refine((v) => v.startsWith('pkg:'), { message: 'each purl must start with pkg:' }),
)
.min(1)
.max(MAX_PURLS, `Maximum ${MAX_PURLS} purls per request`),
})
export async function batchGetStewardship(req: Request, res: Response): Promise<void> {
const { purls: rawPurls } = validateOrThrow(bodySchema, req.body)
const normalizedPurls = rawPurls.map((p) => p.replace(/@/g, '%40'))
const qx = await getPackagesQx()
const rows = await getPackagesByStewardshipPurls(qx, normalizedPurls)
const byPurl = new Map(rows.map((r) => [r.purl, r]))
const packages: Record<string, StewardshipSummary | null> = {}
for (let i = 0; i < rawPurls.length; i++) {
const row = byPurl.get(normalizedPurls[i])
if (!row) {
packages[rawPurls[i]] = null
} else {
packages[rawPurls[i]] = {
name: row.name,
ecosystem: row.ecosystem,
lifecycle: null,
health: null,
impact:
row.criticalityScore != null ? Math.round(Number(row.criticalityScore) * 100) : null,
openVulns: null,
stewardship: (row.stewardshipStatus ?? 'unassigned') as StewardshipSummary['stewardship'],
stewards: null,
lastActivityAt: null,
lastActivityDescription: null,
}
}
}
ok(res, { packages })
}