2022-09-16 14:30:57 +02:00
|
|
|
import * as fs from 'fs'
|
|
|
|
|
import path from 'path'
|
|
|
|
|
import YAML from 'yaml'
|
2022-06-13 19:55:08 +02:00
|
|
|
import * as core from '@actions/core'
|
2022-06-14 11:29:13 +02:00
|
|
|
import * as z from 'zod'
|
2022-11-07 17:08:00 +00:00
|
|
|
import {ConfigurationOptions, ConfigurationOptionsSchema} from './schemas'
|
2022-11-04 09:05:45 +00:00
|
|
|
import {isSPDXValid, octokitClient} from './utils'
|
2022-10-26 09:01:43 +00:00
|
|
|
|
2022-11-07 17:08:00 +00:00
|
|
|
type ConfigurationOptionsPartial = Partial<ConfigurationOptions>
|
2022-06-14 11:29:13 +02:00
|
|
|
|
2022-11-04 09:05:45 +00:00
|
|
|
export async function readConfig(): Promise<ConfigurationOptions> {
|
|
|
|
|
const inlineConfig = readInlineConfig()
|
|
|
|
|
|
2022-11-07 12:12:03 +00:00
|
|
|
const configFile = getOptionalInput('config-file')
|
|
|
|
|
if (configFile !== undefined) {
|
|
|
|
|
const externalConfig = await readConfigFile(configFile)
|
2022-11-08 10:52:30 +00:00
|
|
|
|
|
|
|
|
return ConfigurationOptionsSchema.parse({
|
|
|
|
|
...externalConfig,
|
|
|
|
|
...inlineConfig
|
|
|
|
|
})
|
2022-09-19 17:28:44 +02:00
|
|
|
}
|
2022-11-07 17:08:00 +00:00
|
|
|
|
|
|
|
|
return ConfigurationOptionsSchema.parse(inlineConfig)
|
2022-09-19 17:28:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-08 09:53:36 +00:00
|
|
|
function readInlineConfig(): ConfigurationOptionsPartial {
|
2022-11-07 17:08:00 +00:00
|
|
|
const fail_on_severity = getOptionalInput('fail-on-severity')
|
|
|
|
|
|
|
|
|
|
const fail_on_scopes = parseList(getOptionalInput('fail-on-scopes'))
|
2022-09-21 16:50:02 +02:00
|
|
|
|
2022-09-22 21:34:18 +00:00
|
|
|
const allow_licenses = parseList(getOptionalInput('allow-licenses'))
|
|
|
|
|
const deny_licenses = parseList(getOptionalInput('deny-licenses'))
|
2022-05-12 18:05:14 +02:00
|
|
|
|
2022-10-26 09:01:43 +00:00
|
|
|
validateLicenses('allow-licenses', allow_licenses)
|
|
|
|
|
validateLicenses('deny-licenses', deny_licenses)
|
2022-06-13 19:55:08 +02:00
|
|
|
|
2022-09-22 21:34:18 +00:00
|
|
|
const allow_ghsas = parseList(getOptionalInput('allow-ghsas'))
|
|
|
|
|
|
2022-11-07 17:08:00 +00:00
|
|
|
const license_check = getOptionalBoolean('license-check')
|
|
|
|
|
const vulnerability_check = getOptionalBoolean('vulnerability-check')
|
2022-10-28 21:59:30 +02:00
|
|
|
|
2022-07-21 15:47:05 -04:00
|
|
|
const base_ref = getOptionalInput('base-ref')
|
|
|
|
|
const head_ref = getOptionalInput('head-ref')
|
|
|
|
|
|
2022-11-07 17:08:00 +00:00
|
|
|
const data = {
|
2022-09-22 22:45:27 +00:00
|
|
|
fail_on_severity,
|
|
|
|
|
fail_on_scopes,
|
|
|
|
|
allow_licenses,
|
|
|
|
|
deny_licenses,
|
|
|
|
|
allow_ghsas,
|
2022-10-28 21:59:30 +02:00
|
|
|
license_check,
|
|
|
|
|
vulnerability_check,
|
2022-09-22 22:45:27 +00:00
|
|
|
base_ref,
|
|
|
|
|
head_ref
|
2022-06-13 19:55:08 +02:00
|
|
|
}
|
2022-11-07 17:08:00 +00:00
|
|
|
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
|
Object.entries(data).filter(([_, value]) => value !== undefined)
|
|
|
|
|
)
|
2022-05-31 16:50:39 +02:00
|
|
|
}
|
2022-09-16 14:30:57 +02:00
|
|
|
|
2022-11-08 09:53:36 +00:00
|
|
|
function getOptionalBoolean(name: string): boolean | undefined {
|
|
|
|
|
const value = core.getInput(name)
|
|
|
|
|
return value.length > 0 ? core.getBooleanInput(name) : undefined
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateLicenses(
|
|
|
|
|
key: 'allow-licenses' | 'deny-licenses',
|
|
|
|
|
licenses: string[] | undefined
|
|
|
|
|
): void {
|
|
|
|
|
if (licenses === undefined) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const invalid_licenses = licenses.filter(license => !isSPDXValid(license))
|
|
|
|
|
|
|
|
|
|
if (invalid_licenses.length > 0) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Invalid license(s) in ${key}: ${invalid_licenses.join(', ')}`
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readConfigFile(
|
2022-11-07 12:12:03 +00:00
|
|
|
filePath: string
|
2022-11-07 17:08:00 +00:00
|
|
|
): Promise<ConfigurationOptionsPartial> {
|
2022-11-07 12:12:03 +00:00
|
|
|
const format = new RegExp(
|
|
|
|
|
'(?<owner>[^/]+)/(?<repo>[^/]+)/(?<path>[^@]+)@(?<ref>.*)'
|
|
|
|
|
)
|
|
|
|
|
let data: string
|
|
|
|
|
|
|
|
|
|
const pieces = format.exec(filePath)
|
|
|
|
|
try {
|
|
|
|
|
if (pieces?.groups && pieces.length === 5) {
|
|
|
|
|
data = await getRemoteConfig({
|
|
|
|
|
owner: pieces.groups.owner,
|
|
|
|
|
repo: pieces.groups.repo,
|
|
|
|
|
path: pieces.groups.path,
|
|
|
|
|
ref: pieces.groups.ref
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
data = fs.readFileSync(path.resolve(filePath), 'utf-8')
|
|
|
|
|
}
|
|
|
|
|
return parseConfigFile(data)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
core.debug(error as string)
|
|
|
|
|
throw new Error('Unable to fetch config file')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 09:53:36 +00:00
|
|
|
function parseConfigFile(configData: string): ConfigurationOptionsPartial {
|
2022-11-04 14:51:41 +00:00
|
|
|
try {
|
|
|
|
|
const data = YAML.parse(configData)
|
|
|
|
|
for (const key of Object.keys(data)) {
|
|
|
|
|
if (key === 'allow-licenses' || key === 'deny-licenses') {
|
|
|
|
|
validateLicenses(key, data[key])
|
|
|
|
|
}
|
|
|
|
|
// get rid of the ugly dashes from the actions conventions
|
|
|
|
|
if (key.includes('-')) {
|
|
|
|
|
data[key.replace(/-/g, '_')] = data[key]
|
|
|
|
|
delete data[key]
|
|
|
|
|
}
|
2022-09-19 17:28:59 +02:00
|
|
|
}
|
2022-11-07 17:08:00 +00:00
|
|
|
return data
|
2022-11-04 14:51:41 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
throw error
|
2022-09-19 17:28:59 +02:00
|
|
|
}
|
2022-09-16 14:30:57 +02:00
|
|
|
}
|
2022-11-04 09:05:45 +00:00
|
|
|
|
2022-11-07 12:12:03 +00:00
|
|
|
async function getRemoteConfig(configOpts: {
|
|
|
|
|
[key: string]: string
|
|
|
|
|
}): Promise<string> {
|
2022-11-04 09:05:45 +00:00
|
|
|
try {
|
2022-11-08 10:52:30 +00:00
|
|
|
// https://github.com/github/codeql-action/blob/main/init/action.yml#L59
|
|
|
|
|
// external-repo-token
|
2022-11-04 09:05:45 +00:00
|
|
|
const {data} = await octokitClient(
|
2022-11-08 11:16:26 +00:00
|
|
|
'external-repo-token',
|
2022-11-04 14:51:41 +00:00
|
|
|
false
|
2022-11-04 09:05:45 +00:00
|
|
|
).rest.repos.getContent({
|
|
|
|
|
mediaType: {
|
|
|
|
|
format: 'raw'
|
|
|
|
|
},
|
2022-11-07 12:12:03 +00:00
|
|
|
owner: configOpts.owner,
|
|
|
|
|
repo: configOpts.repo,
|
|
|
|
|
path: configOpts.path,
|
|
|
|
|
ref: configOpts.ref
|
2022-11-04 09:05:45 +00:00
|
|
|
})
|
2022-11-04 14:51:41 +00:00
|
|
|
|
2022-11-04 10:08:00 +00:00
|
|
|
// When using mediaType.format = 'raw', the response.data is a string but this is not reflected
|
|
|
|
|
// in the return type of getContent. So we're casting the return value to a string.
|
|
|
|
|
return z.string().parse(data as unknown)
|
2022-11-04 09:05:45 +00:00
|
|
|
} catch (error) {
|
2022-11-04 14:51:41 +00:00
|
|
|
core.debug(error as string)
|
|
|
|
|
throw new Error('Error fetching remote config file')
|
2022-11-04 09:05:45 +00:00
|
|
|
}
|
|
|
|
|
}
|