Adding basic config file parsing and some test scaffolding.
This commit is contained in:
22
__tests__/config.test.ts
Normal file
22
__tests__/config.test.ts
Normal file
@@ -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'])
|
||||
})
|
||||
4
__tests__/fixtures/config-allow-sample.yml
Normal file
4
__tests__/fixtures/config-allow-sample.yml
Normal file
@@ -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)
|
||||
})
|
||||
@@ -2,19 +2,41 @@ import * as fs from 'fs'
|
||||
import * as core from '@actions/core'
|
||||
import YAML from 'yaml'
|
||||
import * as z from 'zod'
|
||||
import path from 'path'
|
||||
import { type } from 'os'
|
||||
|
||||
|
||||
const CONFIG_FILEPATH = "./.github/dep-review.yml"
|
||||
const SEVERITIES = ["critical", "high", "moderate", "low"] as const
|
||||
|
||||
// TODO check for file not existing
|
||||
// TODO check for file with both extensions
|
||||
// TODO parse yaml format, validate keys
|
||||
type ConfigurationOptions = {
|
||||
fail_on_severity: string,
|
||||
allow_licenses: Array<string>,
|
||||
deny_licenses: Array<string>
|
||||
}
|
||||
|
||||
var severity: string
|
||||
var allowlist, blocklist: [string]
|
||||
export function readConfigFile(filePath: string = CONFIG_FILEPATH): ConfigurationOptions {
|
||||
// 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 parsed = z.object({
|
||||
fail_on_severity: z.enum(SEVERITIES),
|
||||
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")
|
||||
.parse(values)
|
||||
|
||||
// vlaidate licenses dynamically
|
||||
core.info(parsed.fail_on_severity!)
|
||||
//core.info(values["allow_licenses"])
|
||||
//core.info(values["deny_licenses"])
|
||||
})
|
||||
return <ConfigurationOptions>parsed;
|
||||
}
|
||||
Reference in New Issue
Block a user