Adding basic config file parsing and some test scaffolding.
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
import { expect, test } from '@jest/globals'
|
||||||
|
import { readConfigFile } from '../src/config'
|
||||||
|
|
||||||
|
test('reads the config file', async () => {
|
||||||
|
var options = readConfigFile("./__tests__/fixtures/config-allow-sample.yml")
|
||||||
|
expect(options.fail_on_severity).toEqual('critical')
|
||||||
|
expect(options.allow_licenses).toEqual(['BSD', 'GPL 2'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('has a default config filepath', async () => {
|
||||||
|
expect(true).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('can read files with both extensions', async () => {
|
||||||
|
expect(true).toEqual(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns a default config when the config file was not found', async () => {
|
||||||
|
var options = readConfigFile("fixtures/i-dont-exist")
|
||||||
|
expect(options.fail_on_severity).toEqual('all')
|
||||||
|
expect(options.allow_licenses).toEqual(['all'])
|
||||||
|
})
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fail_on_severity: critical
|
||||||
|
allow_licenses:
|
||||||
|
- "BSD"
|
||||||
|
- "GPL 2"
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
import {expect, test} from '@jest/globals'
|
|
||||||
|
|
||||||
test('tests things', async () => {
|
|
||||||
expect(true).toEqual(true)
|
|
||||||
})
|
|
||||||
+30
-11
@@ -2,19 +2,41 @@ import * as fs from 'fs'
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import YAML from 'yaml'
|
import YAML from 'yaml'
|
||||||
import * as z from 'zod'
|
import * as z from 'zod'
|
||||||
|
import path from 'path'
|
||||||
|
import { type } from 'os'
|
||||||
|
|
||||||
|
|
||||||
const CONFIG_FILEPATH = "./.github/dep-review.yml"
|
const CONFIG_FILEPATH = "./.github/dep-review.yml"
|
||||||
const SEVERITIES = ["critical", "high", "moderate", "low"] as const
|
const SEVERITIES = ["critical", "high", "moderate", "low"] as const
|
||||||
|
|
||||||
// TODO check for file not existing
|
type ConfigurationOptions = {
|
||||||
// TODO check for file with both extensions
|
fail_on_severity: string,
|
||||||
// TODO parse yaml format, validate keys
|
allow_licenses: Array<string>,
|
||||||
|
deny_licenses: Array<string>
|
||||||
|
}
|
||||||
|
|
||||||
var severity: string
|
export function readConfigFile(filePath: string = CONFIG_FILEPATH): ConfigurationOptions {
|
||||||
var allowlist, blocklist: [string]
|
// By default we want to fail on all severities and allow all licenses.
|
||||||
|
var defaultOptions: ConfigurationOptions = {
|
||||||
|
fail_on_severity: "all",
|
||||||
|
allow_licenses: ['all'],
|
||||||
|
deny_licenses: []
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log(path.resolve(filePath))
|
||||||
|
var data = fs.readFileSync(path.resolve(filePath), "utf-8");
|
||||||
|
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error.code && error.code === 'ENOENT') {
|
||||||
|
return defaultOptions
|
||||||
|
} else {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var data = fs.readFile(CONFIG_FILEPATH, "utf-8", (err, data) => {
|
|
||||||
const values = YAML.parse(data)
|
const values = YAML.parse(data)
|
||||||
|
|
||||||
const parsed = z.object({
|
const parsed = z.object({
|
||||||
fail_on_severity: z.enum(SEVERITIES),
|
fail_on_severity: z.enum(SEVERITIES),
|
||||||
allow_licenses: z.array(z.string()),
|
allow_licenses: z.array(z.string()),
|
||||||
@@ -24,8 +46,5 @@ var data = fs.readFile(CONFIG_FILEPATH, "utf-8", (err, data) => {
|
|||||||
.refine(obj => !(obj.allow_licenses && obj.deny_licenses), "Can't specify both allow_licenses and deny_licenses")
|
.refine(obj => !(obj.allow_licenses && obj.deny_licenses), "Can't specify both allow_licenses and deny_licenses")
|
||||||
.parse(values)
|
.parse(values)
|
||||||
|
|
||||||
// vlaidate licenses dynamically
|
return <ConfigurationOptions>parsed;
|
||||||
core.info(parsed.fail_on_severity!)
|
}
|
||||||
//core.info(values["allow_licenses"])
|
|
||||||
//core.info(values["deny_licenses"])
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user