Generalize getCategoryInputOrThrow to arbitrary inputs

This commit is contained in:
Henry Mercer 2022-11-23 12:43:32 +00:00
parent daf4614f68
commit bff0be7364
6 changed files with 141 additions and 97 deletions

74
lib/workflow.js generated
View file

@ -19,7 +19,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tryGetCategoryInput = exports.getAnalyzeSteps = exports.getWorkflowRunID = exports.getWorkflowPath = exports.getWorkflow = exports.formatWorkflowCause = exports.formatWorkflowErrors = exports.validateWorkflow = exports.getWorkflowErrors = exports.WorkflowErrors = exports.patternIsSuperset = void 0;
exports.getCategoryInputOrThrow = exports.getStepsCallingAction = exports.getWorkflowRunID = exports.getWorkflowPath = exports.getWorkflow = exports.formatWorkflowCause = exports.formatWorkflowErrors = exports.validateWorkflow = exports.getWorkflowErrors = exports.WorkflowErrors = exports.patternIsSuperset = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const core = __importStar(require("@actions/core"));
@ -246,14 +246,49 @@ function getWorkflowRunID() {
return workflowRunID;
}
exports.getWorkflowRunID = getWorkflowRunID;
function getAnalyzeSteps(job) {
function getStepsCallingAction(job, actionName) {
const steps = job.steps;
if (!Array.isArray(steps)) {
throw new Error("Could not get analyze steps since job.steps was not an array.");
throw new Error(`Could not get steps calling ${actionName} since job.steps was not an array.`);
}
return steps.filter((step) => { var _a; return (_a = step.uses) === null || _a === void 0 ? void 0 : _a.includes("github/codeql-action/analyze"); });
return steps.filter((step) => { var _a; return (_a = step.uses) === null || _a === void 0 ? void 0 : _a.includes(actionName); });
}
exports.getStepsCallingAction = getStepsCallingAction;
/**
* Makes a best effort attempt to retrieve the value of a particular input with which
* an Action in the workflow would be invoked.
*
* @returns the value of the input, or undefined if no such input is passed to the Action
* @throws an error if the value of the input could not be determined, or we could not
* determine that no such input is passed to the Action.
*/
function getInputOrThrow(workflow, actionName, inputName, matrixVars) {
if (!workflow.jobs) {
throw new Error(`Could not get ${inputName} input to ${actionName} since the workflow has no jobs.`);
}
const inputs = Object.values(workflow.jobs)
.map((job) => getStepsCallingAction(job, actionName).map((step) => { var _a; return (_a = step.with) === null || _a === void 0 ? void 0 : _a[inputName]; }))
.flat()
.filter((input) => input !== undefined)
.map((input) => input);
if (inputs.length === 0) {
return undefined;
}
if (!inputs.every((input) => input === inputs[0])) {
throw new Error(`Could not get ${inputName} input to ${actionName} since there were multiple steps calling ` +
`${actionName} with different values for ${inputName}.`);
}
// Make a basic attempt to substitute matrix variables
// First normalize by removing whitespace
let input = inputs[0].replace(/\${{\s+/, "${{").replace(/\s+}}/, "}}");
for (const [key, value] of Object.entries(matrixVars)) {
input = input.replace(`\${{matrix.${key}}}`, value);
}
if (input.includes("${{")) {
throw new Error(`Could not get ${inputName} input to ${actionName} since it contained an unrecognized dynamic value.`);
}
return input;
}
exports.getAnalyzeSteps = getAnalyzeSteps;
/**
* Makes a best effort attempt to retrieve the category input for the particular job,
* given a set of matrix variables.
@ -261,31 +296,8 @@ exports.getAnalyzeSteps = getAnalyzeSteps;
* @returns the category input, or undefined if the category input is not defined
* @throws an error if the category input could not be determined
*/
function tryGetCategoryInput(workflow, matrixVars) {
if (!workflow.jobs) {
throw new Error("Could not get category input since workflow.jobs was undefined.");
}
const categories = Object.values(workflow.jobs)
.map((job) => getAnalyzeSteps(job).map((step) => { var _a; return (_a = step.with) === null || _a === void 0 ? void 0 : _a.category; }))
.flat()
.filter((category) => category !== undefined)
.map((category) => category);
if (categories.length === 0) {
return undefined;
}
if (!categories.every((category) => category === categories[0])) {
throw new Error("Could not get category input since multiple categories were specified by the analysis step.");
}
// Make a basic attempt to substitute matrix variables
// First normalize by removing whitespace
let category = categories[0].replace(/\${{\s+/, "${{").replace(/\s+}}/, "}}");
for (const [key, value] of Object.entries(matrixVars)) {
category = category.replace(`\${{matrix.${key}}}`, value);
}
if (category.includes("${{")) {
throw new Error("Could not get category input since it contained an unrecognized dynamic value.");
}
return category;
function getCategoryInputOrThrow(workflow, matrixVars) {
return getInputOrThrow(workflow, "github/codeql-action/analyze", "category", matrixVars);
}
exports.tryGetCategoryInput = tryGetCategoryInput;
exports.getCategoryInputOrThrow = getCategoryInputOrThrow;
//# sourceMappingURL=workflow.js.map