2022-05-12 18:05:14 +02:00
|
|
|
import * as fs from 'fs'
|
|
|
|
|
import YAML from 'yaml'
|
2022-06-01 06:36:02 +02:00
|
|
|
import { ConfigurationOptions, ConfigurationOptionsSchema } from './schemas'
|
2022-05-26 15:54:59 -07:00
|
|
|
import path from 'path'
|
|
|
|
|
|
2022-05-31 16:50:39 +02:00
|
|
|
export type Severity = "critical" | "high" | "moderate" | "low"
|
2022-05-12 18:05:14 +02:00
|
|
|
|
2022-05-31 16:50:39 +02:00
|
|
|
export const SEVERITIES = ["critical", "high", "moderate", "low"] as const
|
|
|
|
|
export const CONFIG_FILEPATH = "./.github/dep-review.yml"
|
2022-05-12 18:05:14 +02:00
|
|
|
|
2022-05-26 15:54:59 -07:00
|
|
|
export function readConfigFile(filePath: string = CONFIG_FILEPATH): ConfigurationOptions {
|
|
|
|
|
// By default we want to fail on all severities and allow all licenses.
|
2022-06-01 05:31:33 +02:00
|
|
|
const defaultOptions: ConfigurationOptions = {
|
2022-06-01 06:36:02 +02:00
|
|
|
fail_on_severity: 'low',
|
|
|
|
|
allow_licenses: []
|
2022-05-26 15:54:59 -07:00
|
|
|
}
|
2022-05-12 18:05:14 +02:00
|
|
|
|
2022-06-01 05:31:33 +02:00
|
|
|
let data
|
|
|
|
|
|
2022-05-26 15:54:59 -07:00
|
|
|
try {
|
2022-06-01 05:31:33 +02:00
|
|
|
data = fs.readFileSync(path.resolve(filePath), "utf-8");
|
2022-05-26 15:54:59 -07:00
|
|
|
} catch (error: any) {
|
|
|
|
|
if (error.code && error.code === 'ENOENT') {
|
|
|
|
|
return defaultOptions
|
|
|
|
|
} else {
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-12 18:05:14 +02:00
|
|
|
|
2022-06-01 06:36:02 +02:00
|
|
|
// This is a copy of line 34, not sure why this is failing!
|
|
|
|
|
ConfigurationOptionsSchema.parse({ fail_on_severity: 'critical', allow_licenses: ['BSD', 'GPL 2'] })
|
2022-05-26 15:54:59 -07:00
|
|
|
|
2022-06-01 06:36:02 +02:00
|
|
|
const values = YAML.parse(data)
|
|
|
|
|
const parsed = ConfigurationOptionsSchema.parse(values)
|
2022-05-12 18:05:14 +02:00
|
|
|
|
2022-06-01 06:36:02 +02:00
|
|
|
return parsed;
|
2022-05-31 16:50:39 +02:00
|
|
|
}
|