-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathgetPackage.ts
More file actions
91 lines (82 loc) · 2.81 KB
/
Copy pathgetPackage.ts
File metadata and controls
91 lines (82 loc) · 2.81 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import type { Request, Response } from 'express'
import { z } from 'zod'
import { NotFoundError } from '@crowd/common'
import { getAdvisoriesByPackageId, getPackageDetailByPurl } from '@crowd/data-access-layer'
import { getPackagesQx } from '@/db/packagesDb'
import { ok } from '@/utils/api'
import { validateOrThrow } from '@/utils/validation'
import type { StewardshipStatus } from './types'
const querySchema = z.object({
purl: z
.string()
.trim()
.min(1)
.refine((v) => v.startsWith('pkg:'), { message: 'purl must start with pkg:' })
.transform((v) => v.replace(/@/g, '%40')),
})
export async function getPackage(req: Request, res: Response): Promise<void> {
const { purl } = validateOrThrow(querySchema, req.query)
const qx = await getPackagesQx()
const pkg = await getPackageDetailByPurl(qx, purl)
if (!pkg) {
throw new NotFoundError()
}
const advisories = await getAdvisoriesByPackageId(qx, pkg.id)
ok(res, {
purl: pkg.purl,
name: pkg.name,
ecosystem: pkg.ecosystem,
general: {
healthScore: null,
impact: {
impactScore:
pkg.criticalityScore != null ? Math.round(Number(pkg.criticalityScore) * 100) : null,
downloadsLastMonth:
pkg.downloadsLast30d != null ? parseInt(pkg.downloadsLast30d, 10) : null,
dependentPackages: pkg.dependentPackagesCount ?? null,
dependentRepos: pkg.dependentReposCount ?? null,
transitiveReach: pkg.transitiveReach,
},
riskSignals: {
lifecycle: null,
maintainerBusFactor: pkg.maintainerCount,
lastRelease: pkg.latestReleaseAt ? pkg.latestReleaseAt.toISOString() : null,
hasSecurityFile: pkg.hasSecurityFile,
openSSFScorecard: pkg.scorecardScore != null ? Number(pkg.scorecardScore) : null,
},
},
assessment: {},
security: {
securityContacts: null,
advisories: advisories.map((a) => ({
osvId: a.osvId,
severity: a.severity,
resolution: a.resolution,
})),
cvd: {
isPvrEnabled: null,
hasSecurityPolicyEnabled: pkg.branchProtectionEnabled,
tier0Steward: null,
criticalVulnerabilityFlag: pkg.hasCriticalVulnerability,
},
},
provenance: {
repositoryMapping: {
declaredRepo: pkg.repoUrl ?? pkg.repositoryUrl ?? pkg.declaredRepositoryUrl ?? null,
mappingConfidence:
pkg.repoMappingConfidence != null ? Number(pkg.repoMappingConfidence) : null,
lastCommitAt: pkg.repoLastCommitAt ? pkg.repoLastCommitAt.toISOString() : null,
},
supplyChainIntegrity: {
buildProvenance: null,
signedReleases: null,
},
},
stewardship: {
status: (pkg.stewardshipStatus ?? 'unassigned') as StewardshipStatus,
stewards: null,
lastActivityAt: null,
},
history: {},
})
}