Add getOptionalInput and getRequiredInput

This commit is contained in:
Robert Brignull 2020-09-15 18:42:23 +01:00
parent d88fa5cef6
commit c1cee53da5
18 changed files with 111 additions and 61 deletions

View file

@ -5,6 +5,29 @@ import * as api from "./api-client";
import * as sharedEnv from "./shared-environment";
import { isLocalRun, GITHUB_DOTCOM_URL } from "./util";
/**
* Wrapper around core.getInput for inputs that always have a value.
* Also see getOptionalInput.
*
* This allows us to get stronger type checking of required/optional inputs
* and make behaviour more consistent between actions and the runner.
*/
export function getRequiredInput(name: string): string {
return core.getInput(name, { required: true });
}
/**
* Wrapper around core.getInput that converts empty inputs to undefined.
* Also see getRequiredInput.
*
* This allows us to get stronger type checking of required/optional inputs
* and make behaviour more consistent between actions and the runner.
*/
export function getOptionalInput(name: string): string | undefined {
const value = core.getInput(name);
return value.length > 0 ? value : undefined;
}
/**
* Get an environment parameter, but throw an error if it is not set.
*/

View file

@ -68,16 +68,16 @@ async function run() {
await actionsUtil.getAnalysisKey(),
actionsUtil.getRequiredEnvParam("GITHUB_WORKFLOW"),
actionsUtil.getWorkflowRunID(),
core.getInput("checkout_path"),
core.getInput("matrix"),
core.getInput("token"),
actionsUtil.getRequiredInput("checkout_path"),
actionsUtil.getRequiredInput("matrix"),
actionsUtil.getRequiredInput("token"),
actionsUtil.getRequiredEnvParam("GITHUB_SERVER_URL"),
core.getInput("upload") === "true",
actionsUtil.getRequiredInput("upload") === "true",
"actions",
core.getInput("output"),
util.getMemoryFlag(core.getInput("ram")),
util.getAddSnippetsFlag(core.getInput("add-snippets")),
util.getThreadsFlag(core.getInput("threads"), logger),
actionsUtil.getRequiredInput("output"),
util.getMemoryFlag(actionsUtil.getOptionalInput("ram")),
util.getAddSnippetsFlag(actionsUtil.getRequiredInput("add-snippets")),
util.getThreadsFlag(actionsUtil.getOptionalInput("threads"), logger),
config,
logger
);

View file

@ -1,9 +1,8 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import consoleLogLevel from "console-log-level";
import * as path from "path";
import { getRequiredEnvParam } from "./actions-util";
import { getRequiredEnvParam, getRequiredInput } from "./actions-util";
import { isLocalRun } from "./util";
export const getApiClient = function (
@ -41,7 +40,7 @@ function getApiUrl(githubUrl: string): string {
// and called only from the action entrypoints.
export function getActionsApiClient(allowLocalRun = false) {
return getApiClient(
core.getInput("token"),
getRequiredInput("token"),
getRequiredEnvParam("GITHUB_SERVER_URL"),
allowLocalRun
);

View file

@ -34,7 +34,7 @@ async function sendSuccessStatusReport(
);
const languages = config.languages.join(",");
const workflowLanguages = core.getInput("languages", { required: false });
const workflowLanguages = actionsUtil.getOptionalInput("languages");
const paths = (config.originalUserInput.paths || []).join(",");
const pathsIgnore = (config.originalUserInput["paths-ignore"] || []).join(
","
@ -51,7 +51,7 @@ async function sendSuccessStatusReport(
const statusReport: InitSuccessStatusReport = {
...statusReportBase,
languages,
workflow_languages: workflowLanguages,
workflow_languages: workflowLanguages || "",
paths,
paths_ignore: pathsIgnore,
disable_default_queries: disableDefaultQueries,
@ -79,8 +79,8 @@ async function run() {
}
codeql = await initCodeQL(
core.getInput("tools"),
core.getInput("token"),
actionsUtil.getOptionalInput("tools"),
actionsUtil.getRequiredInput("token"),
actionsUtil.getRequiredEnvParam("GITHUB_SERVER_URL"),
actionsUtil.getRequiredEnvParam("RUNNER_TEMP"),
actionsUtil.getRequiredEnvParam("RUNNER_TOOL_CACHE"),
@ -88,15 +88,15 @@ async function run() {
logger
);
config = await initConfig(
core.getInput("languages"),
core.getInput("queries"),
core.getInput("config-file"),
actionsUtil.getOptionalInput("languages"),
actionsUtil.getOptionalInput("queries"),
actionsUtil.getOptionalInput("config-file"),
parseRepositoryNwo(actionsUtil.getRequiredEnvParam("GITHUB_REPOSITORY")),
actionsUtil.getRequiredEnvParam("RUNNER_TEMP"),
actionsUtil.getRequiredEnvParam("RUNNER_TOOL_CACHE"),
codeql,
actionsUtil.getRequiredEnvParam("GITHUB_WORKSPACE"),
core.getInput("token"),
actionsUtil.getRequiredInput("token"),
actionsUtil.getRequiredEnvParam("GITHUB_SERVER_URL"),
logger
);

View file

@ -42,16 +42,16 @@ async function run() {
try {
const uploadStats = await upload_lib.upload(
core.getInput("sarif_file"),
actionsUtil.getRequiredInput("sarif_file"),
parseRepositoryNwo(actionsUtil.getRequiredEnvParam("GITHUB_REPOSITORY")),
await actionsUtil.getCommitOid(),
actionsUtil.getRef(),
await actionsUtil.getAnalysisKey(),
actionsUtil.getRequiredEnvParam("GITHUB_WORKFLOW"),
actionsUtil.getWorkflowRunID(),
core.getInput("checkout_path"),
core.getInput("matrix"),
core.getInput("token"),
actionsUtil.getRequiredInput("checkout_path"),
actionsUtil.getRequiredInput("matrix"),
actionsUtil.getRequiredInput("token"),
actionsUtil.getRequiredEnvParam("GITHUB_SERVER_URL"),
"actions",
getActionsLogger()

View file

@ -20,12 +20,13 @@ test("getToolNames", (t) => {
test("getMemoryFlag() should return the correct --ram flag", (t) => {
const totalMem = Math.floor(os.totalmem() / (1024 * 1024));
const tests = {
"": `--ram=${totalMem - 256}`,
"512": "--ram=512",
};
const tests = [
[undefined, `--ram=${totalMem - 256}`],
["", `--ram=${totalMem - 256}`],
["512", "--ram=512"],
];
for (const [input, expectedFlag] of Object.entries(tests)) {
for (const [input, expectedFlag] of tests) {
const flag = util.getMemoryFlag(input);
t.deepEqual(flag, expectedFlag);
}
@ -50,14 +51,16 @@ test("getAddSnippetsFlag() should return the correct flag", (t) => {
test("getThreadsFlag() should return the correct --threads flag", (t) => {
const numCpus = os.cpus().length;
const tests = {
"0": "--threads=0",
"1": "--threads=1",
[`${numCpus + 1}`]: `--threads=${numCpus}`,
[`${-numCpus - 1}`]: `--threads=${-numCpus}`,
};
const tests = [
["0", "--threads=0"],
["1", "--threads=1"],
[undefined, `--threads=${numCpus}`],
["", `--threads=${numCpus}`],
[`${numCpus + 1}`, `--threads=${numCpus}`],
[`${-numCpus - 1}`, `--threads=${-numCpus}`],
];
for (const [input, expectedFlag] of Object.entries(tests)) {
for (const [input, expectedFlag] of tests) {
const flag = util.getThreadsFlag(input, getRunnerLogger(true));
t.deepEqual(flag, expectedFlag);
}