diff --git a/__tests__/config.test.ts b/__tests__/config.test.ts index cb84cdd..b3b67bd 100644 --- a/__tests__/config.test.ts +++ b/__tests__/config.test.ts @@ -13,6 +13,7 @@ function setInput(input: string, value: string) { function clearInputs() { const allowedOptions = [ 'FAIL-ON-SEVERITY', + 'FAIL-ON-SCOPES', 'ALLOW-LICENSES', 'DENY-LICENSES', 'BASE-REF', @@ -82,3 +83,22 @@ test('it raises an error when no refs are provided and the event is not a pull r }) ).toThrow() }) + +test('it defaults to runtime scope', async () => { + const options = readConfig() + expect(options.fail_on_scopes).toEqual(['runtime']) +}) +test('it parses custom scopes preference', async () => { + setInput('fail-on-scopes', 'runtime, development') + let options = readConfig() + expect(options.fail_on_scopes).toEqual(['runtime', 'development']) + + clearInputs() + setInput('fail-on-scopes', 'development') + options = readConfig() + expect(options.fail_on_scopes).toEqual(['development']) +}) +test('it raises an error when given invalid scope', async () => { + setInput('fail-on-scopes', 'runtime, zombies') + expect(() => readConfig()).toThrow() +}) diff --git a/src/config.ts b/src/config.ts index 800a672..439c126 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,17 +1,29 @@ import * as core from '@actions/core' import * as z from 'zod' -import {ConfigurationOptions, SEVERITIES} from './schemas' +import {ConfigurationOptions, SEVERITIES, SCOPES} from './schemas' function getOptionalInput(name: string): string | undefined { const value = core.getInput(name) return value.length > 0 ? value : undefined } +function parseList(list: string | undefined): string[] | undefined { + if (list === undefined) { + return list + } else { + return list.split(',').map(x => x.trim()) + } +} + export function readConfig(): ConfigurationOptions { const fail_on_severity = z .enum(SEVERITIES) .default('low') .parse(getOptionalInput('fail-on-severity')) + const fail_on_scopes = z + .array(z.enum(SCOPES)) + .default(['runtime']) + .parse(parseList(getOptionalInput('fail-on-scopes'))) const allow_licenses = getOptionalInput('allow-licenses') const deny_licenses = getOptionalInput('deny-licenses') @@ -24,8 +36,9 @@ export function readConfig(): ConfigurationOptions { return { fail_on_severity, - allow_licenses: allow_licenses?.split(',').map(x => x.trim()), - deny_licenses: deny_licenses?.split(',').map(x => x.trim()), + fail_on_scopes, + allow_licenses: parseList(allow_licenses), + deny_licenses: parseList(deny_licenses), base_ref, head_ref } diff --git a/src/schemas.ts b/src/schemas.ts index b91a6b7..05ffa0f 100644 --- a/src/schemas.ts +++ b/src/schemas.ts @@ -35,6 +35,7 @@ export const PullRequestSchema = z.object({ export const ConfigurationOptionsSchema = z .object({ fail_on_severity: z.enum(SEVERITIES).default('low'), + fail_on_scopes: z.array(z.enum(SCOPES)).default(['runtime']), allow_licenses: z.array(z.string()).default([]), deny_licenses: z.array(z.string()).default([]), base_ref: z.string(),