From 1791775ce6c914781354671067037f454d1710c2 Mon Sep 17 00:00:00 2001 From: Federico Builes Date: Mon, 13 Jun 2022 19:55:08 +0200 Subject: [PATCH] temp commit --- __tests__/config.test.ts | 46 +++++++++++++++++++++++----------------- src/config.ts | 40 ++++++++++++++++++---------------- src/main.ts | 4 ++-- 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/__tests__/config.test.ts b/__tests__/config.test.ts index 53946f6..6557110 100644 --- a/__tests__/config.test.ts +++ b/__tests__/config.test.ts @@ -1,31 +1,39 @@ -import {expect, test} from '@jest/globals' -import {readConfigFile} from '../src/config' +import * as core from '@actions/core' +import {expect, test, jest, beforeEach} from '@jest/globals' +import {readConfig} from '../src/config' -test('reads the config file', async () => { - let options = readConfigFile('./__tests__/fixtures/config-allow-sample.yml') +beforeEach(() => { + /* reset to our defaults after every test run */ + process.env['INPUT_FAIL-ON-SEVERITY'] = 'low' + process.env['INPUT_ALLOWED-LICENSES'] = '' + process.env['INPUT_DENY-LICENSES'] = '' +}) + +test('it defaults to low severity', async () => { + let options = readConfig() + expect(options.fail_on_severity).toEqual('low') +}) + +test('it reads custom configs', async () => { + process.env['INPUT_FAIL-ON-SEVERITY'] = 'critical' + process.env['INPUT_ALLOWED-LICENSES'] = ' BSD, GPL 2 ' + + let options = readConfig() expect(options.fail_on_severity).toEqual('critical') expect(options.allow_licenses).toEqual(['BSD', 'GPL 2']) }) -test('the default config path handles .yml and .yaml', async () => { - expect(true).toEqual(true) -}) +test('it defaults to empty allow/deny lists ', async () => { + let options = readConfig() -test('returns a default config when the config file was not found', async () => { - let options = readConfigFile('fixtures/i-dont-exist') - expect(options.fail_on_severity).toEqual('low') - expect(options.allow_licenses).toEqual(undefined) -}) - -test('it reads config files with empty options', async () => { - let options = readConfigFile('./__tests__/fixtures/no-licenses-config.yml') - expect(options.fail_on_severity).toEqual('critical') expect(options.allow_licenses).toEqual(undefined) expect(options.deny_licenses).toEqual(undefined) }) test('it raises an error if both an allow and denylist are specified', async () => { - expect(() => - readConfigFile('./__tests__/fixtures/conflictive-config.yml') - ).toThrow() + process.env['INPUT_ALLOWED-LICENSES'] = 'MIT' + process.env['INPUT_DENY-LICENSES'] = 'BSD' + expect(() => readConfig()).toThrow() }) + +test('it raises an error when given an unknown severity', async () => {}) diff --git a/src/config.ts b/src/config.ts index ae32d13..22a7c87 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,29 +1,33 @@ -import * as fs from 'fs' -import YAML from 'yaml' +import * as core from '@actions/core' import {ConfigurationOptions, ConfigurationOptionsSchema} from './schemas' -import path from 'path' +import {Severity} from './schemas' export const CONFIG_FILEPATH = './.github/dependency-review.yml' -export function readConfigFile( - filePath: string = CONFIG_FILEPATH -): ConfigurationOptions { +export function readConfig(): ConfigurationOptions { // By default we want to fail on all severities and allow all licenses. - const defaultOptions: ConfigurationOptions = { - fail_on_severity: 'low' + let options = { + fail_on_severity: 'low', + allow_licenses: [] as string[], + deny_licenses: [] as string[] } - let data + let severity = core.getInput('fail-on-severity') + let allowedLicenses = core.getInput('allowed-licenses') + let denyLicenses = core.getInput('deny-licenses') - try { - data = fs.readFileSync(path.resolve(filePath), 'utf-8') - } catch (error: any) { - if (error.code && error.code === 'ENOENT') { - return defaultOptions - } else { - throw error - } + // TODO test the empty string case + if (severity.length > 0) { + options.fail_on_severity = severity as Severity } - return ConfigurationOptionsSchema.parse(YAML.parse(data)) + if (allowedLicenses.length > 0) { + options.allow_licenses = allowedLicenses.split(',').map(s => s.trim()) + } + + if (denyLicenses.length > 0) { + options.deny_licenses = denyLicenses.split(',').map(s => s.trim()) + } + + return ConfigurationOptionsSchema.parse(options) } diff --git a/src/main.ts b/src/main.ts index 9bca8f4..943b000 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,7 @@ import * as github from '@actions/github' import styles from 'ansi-styles' import {RequestError} from '@octokit/request-error' import {Change, PullRequestSchema, Severity} from './schemas' -import {readConfigFile} from '../src/config' +import {readConfig} from '../src/config' import {filterChangesBySeverity} from '../src/filter' import {getDeniedLicenseChanges} from './licenses' @@ -27,7 +27,7 @@ async function run(): Promise { headRef: pull_request.head.sha }) - let config = readConfigFile() + let config = readConfig() let minSeverity = config.fail_on_severity let failed = false