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

87 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-09-16 14:30:57 +02:00
import * as fs from 'fs'
import path from 'path'
import YAML from 'yaml'
2022-06-13 19:55:08 +02:00
import * as core from '@actions/core'
import * as z from 'zod'
import {
ConfigurationOptions,
ConfigurationOptionsSchema,
2022-09-21 16:50:02 +02:00
SeveritySchema,
SCOPES
} from './schemas'
function getOptionalInput(name: string): string | undefined {
const value = core.getInput(name)
return value.length > 0 ? value : undefined
}
2022-05-12 18:05:14 +02:00
2022-09-15 18:48:58 +00:00
function parseList(list: string | undefined): string[] | undefined {
if (list === undefined) {
return list
} else {
return list.split(',').map(x => x.trim())
}
}
2022-06-13 19:55:08 +02:00
export function readConfig(): ConfigurationOptions {
const externalConfig = getOptionalInput('config-file')
if (externalConfig !== undefined) {
const config = readConfigFile(externalConfig)
const inlineConfig = readInlineConfig()
return Object.assign({}, inlineConfig, config)
} else {
return readInlineConfig()
}
}
export function readInlineConfig(): ConfigurationOptions {
const fail_on_severity = SeveritySchema.parse(
getOptionalInput('fail-on-severity')
)
2022-09-21 16:50:02 +02:00
2022-09-15 18:48:58 +00:00
const fail_on_scopes = z
.array(z.enum(SCOPES))
.default(['runtime'])
.parse(parseList(getOptionalInput('fail-on-scopes')))
2022-09-21 16:50:02 +02:00
const allow_licenses = getOptionalInput('allow-licenses')
const deny_licenses = getOptionalInput('deny-licenses')
2022-05-12 18:05:14 +02:00
if (allow_licenses !== undefined && deny_licenses !== undefined) {
throw new Error("Can't specify both allow_licenses and deny_licenses")
2022-06-13 19:55:08 +02:00
}
const base_ref = getOptionalInput('base-ref')
const head_ref = getOptionalInput('head-ref')
return {
fail_on_severity,
2022-09-15 18:48:58 +00:00
fail_on_scopes,
allow_licenses: parseList(allow_licenses),
deny_licenses: parseList(deny_licenses),
base_ref,
head_ref
2022-06-13 19:55:08 +02:00
}
}
2022-09-16 14:30:57 +02:00
export function readConfigFile(filePath: string): ConfigurationOptions {
2022-09-16 14:30:57 +02:00
let data
try {
data = fs.readFileSync(path.resolve(filePath), 'utf-8')
} catch (error: unknown) {
throw error
}
data = YAML.parse(data)
// get rid of the ugly dashes from the actions conventions
for (const key of Object.keys(data)) {
if (key.includes('-')) {
data[key.replace(/-/g, '_')] = data[key]
delete data[key]
}
}
const values = ConfigurationOptionsSchema.parse(data)
2022-09-16 14:30:57 +02:00
return values
}