From a8dcc6b77458afad2fe007dff69fb0cf04d2544f Mon Sep 17 00:00:00 2001 From: Federico Builes Date: Thu, 26 May 2022 15:54:59 -0700 Subject: [PATCH] Adding basic config file parsing and some test scaffolding. --- __tests__/config.test.ts | 22 ++++++++++++ __tests__/fixtures/config-allow-sample.yml | 4 +++ __tests__/main.test.ts | 5 --- src/config.ts | 41 ++++++++++++++++------ 4 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 __tests__/config.test.ts create mode 100644 __tests__/fixtures/config-allow-sample.yml delete mode 100644 __tests__/main.test.ts diff --git a/__tests__/config.test.ts b/__tests__/config.test.ts new file mode 100644 index 0000000..5193e69 --- /dev/null +++ b/__tests__/config.test.ts @@ -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']) +}) \ No newline at end of file diff --git a/__tests__/fixtures/config-allow-sample.yml b/__tests__/fixtures/config-allow-sample.yml new file mode 100644 index 0000000..b3a6f00 --- /dev/null +++ b/__tests__/fixtures/config-allow-sample.yml @@ -0,0 +1,4 @@ +fail_on_severity: critical +allow_licenses: + - "BSD" + - "GPL 2" \ No newline at end of file diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts deleted file mode 100644 index 4d984ef..0000000 --- a/__tests__/main.test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {expect, test} from '@jest/globals' - -test('tests things', async () => { - expect(true).toEqual(true) -}) diff --git a/src/config.ts b/src/config.ts index 9e8fe52..b0c8c75 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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, + deny_licenses: Array +} -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"]) -}) \ No newline at end of file + return parsed; +} \ No newline at end of file