Files
dependency-review-action/src/schemas.ts
T

162 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-03-31 18:31:39 +02:00
import * as z from 'zod'
2022-06-01 12:09:11 +02:00
export const SEVERITIES = ['critical', 'high', 'moderate', 'low'] as const
2022-09-15 17:53:34 +00:00
export const SCOPES = ['unknown', 'runtime', 'development'] as const
2022-03-31 18:31:39 +02:00
export const SeveritySchema = z.enum(SEVERITIES).default('low')
2022-03-31 18:31:39 +02:00
export const PackageURLSchema = z.object({
type: z.string(),
namespace: z.string(),
name: z.string(),
version: z.string(),
qualifiers: z.record(z.string()).nullable(),
subpath: z.string().nullable()
})
2022-05-31 06:06:19 +02:00
export const ChangeSchema = z.object({
2022-03-31 18:31:39 +02:00
change_type: z.enum(['added', 'removed']),
manifest: z.string(),
ecosystem: z.string(),
name: z.string(),
version: z.string(),
package_url: z.string(),
license: z.string().nullable(),
source_repository_url: z.string().nullable(),
scope: z.enum(SCOPES).optional(),
2022-03-31 18:31:39 +02:00
vulnerabilities: z
.array(
z.object({
severity: SeveritySchema,
2022-03-31 18:31:39 +02:00
advisory_ghsa_id: z.string(),
advisory_summary: z.string(),
advisory_url: z.string()
})
)
.optional()
2022-06-01 05:36:46 +02:00
.default([])
2022-03-31 18:31:39 +02:00
})
export const PullRequestSchema = z.object({
number: z.number(),
2022-06-01 12:09:11 +02:00
base: z.object({sha: z.string()}),
head: z.object({sha: z.string()})
2022-03-31 18:31:39 +02:00
})
2022-06-01 12:09:11 +02:00
export const ConfigurationOptionsSchema = z
.object({
fail_on_severity: SeveritySchema,
2022-09-15 18:48:58 +00:00
fail_on_scopes: z.array(z.enum(SCOPES)).default(['runtime']),
allow_licenses: z.array(z.string()).optional(),
deny_licenses: z.array(z.string()).optional(),
2023-04-06 09:37:42 +02:00
allow_dependencies_licenses: z.array(z.string()).optional(),
2022-09-22 21:34:18 +00:00
allow_ghsas: z.array(z.string()).default([]),
deny_packages: z.array(PackageURLSchema).default([]),
deny_groups: z.array(PackageURLSchema).default([]),
license_check: z.boolean().default(true),
vulnerability_check: z.boolean().default(true),
config_file: z.string().optional(),
base_ref: z.string().optional(),
head_ref: z.string().optional(),
2023-09-06 18:04:10 +00:00
retry_on_snapshot_warnings: z.boolean().default(false),
retry_on_snapshot_warnings_timeout: z.number().default(120),
2024-03-12 21:32:27 +00:00
show_openssf_scorecard: z.boolean().optional().default(true),
warn_on_openssf_scorecard_level: z.number().default(3),
2023-08-09 15:57:03 -04:00
comment_summary_in_pr: z
2023-08-09 21:10:48 -04:00
.union([
z.preprocess(
val => (val === 'true' ? true : val === 'false' ? false : val),
z.boolean()
),
z.enum(['always', 'never', 'on-failure'])
])
.default('never'),
2023-06-12 11:26:44 +02:00
warn_only: z.boolean().default(false)
})
2023-08-09 15:57:03 -04:00
.transform(config => {
if (config.comment_summary_in_pr === true) {
config.comment_summary_in_pr = 'always'
} else if (config.comment_summary_in_pr === false) {
config.comment_summary_in_pr = 'never'
}
return config
})
.superRefine((config, context) => {
if (config.allow_licenses && config.deny_licenses) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: 'You cannot specify both allow-licenses and deny-licenses'
})
}
if (config.allow_licenses && config.allow_licenses.length < 1) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: 'You should provide at least one license in allow-licenses'
})
}
if (
config.license_check === false &&
config.vulnerability_check === false
) {
context.addIssue({
code: z.ZodIssueCode.custom,
message: "Can't disable both license-check and vulnerability-check"
})
}
2022-06-01 12:09:11 +02:00
})
2022-03-31 18:31:39 +02:00
export const ChangesSchema = z.array(ChangeSchema)
2023-03-22 21:13:20 +00:00
export const ComparisonResponseSchema = z.object({
changes: z.array(ChangeSchema),
snapshot_warnings: z.string()
})
2024-03-06 14:43:49 +00:00
export const ScorecardApiSchema = z.object({
date: z.string(),
repo: z
.object({
name: z.string(),
commit: z.string()
})
.nullish(),
scorecard: z
.object({
version: z.string(),
commit: z.string()
})
.nullish(),
checks: z
.array(
z.object({
name: z.string(),
documentation: z.object({
shortDescription: z.string(),
url: z.string()
}),
score: z.string(),
reason: z.string(),
details: z.array(z.string())
})
)
.nullish(),
score: z.number().nullish()
})
2024-03-03 05:24:07 +00:00
export const ScorecardSchema = z.object({
dependencies: z.array(
z.object({
change: ChangeSchema,
2024-03-06 14:43:49 +00:00
scorecard: ScorecardApiSchema.nullish()
2024-03-03 05:24:07 +00:00
})
)
})
2022-05-31 06:06:19 +02:00
export type Change = z.infer<typeof ChangeSchema>
2022-03-31 18:31:39 +02:00
export type Changes = z.infer<typeof ChangesSchema>
2023-03-22 21:13:20 +00:00
export type ComparisonResponse = z.infer<typeof ComparisonResponseSchema>
export type ConfigurationOptions = z.infer<typeof ConfigurationOptionsSchema>
2022-09-21 16:50:02 +02:00
export type Severity = z.infer<typeof SeveritySchema>
2023-01-09 07:59:55 +01:00
export type Scope = (typeof SCOPES)[number]
2024-03-03 05:24:07 +00:00
export type Scorecard = z.infer<typeof ScorecardSchema>
2024-03-06 14:43:49 +00:00
export type ScorecardApi = z.infer<typeof ScorecardApiSchema>