update error_wrapper to take matcher array

This commit is contained in:
Nick Fyson 2020-09-01 16:35:08 +01:00
parent cd22abcda8
commit 7b7e0e12b7
6 changed files with 70 additions and 44 deletions

46
lib/exec_wrapper.js generated
View file

@ -8,11 +8,23 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const exec = __importStar(require("@actions/exec"));
async function exec_wrapper(commandLine, args, options) {
/**
* Wrapper for exec.exec which checks for specific return code and/or regex matches in console output.
* Output will be streamed to the live console as well as captured for subsequent processing.
* Returns promise with return code
*
* @param commandLine command to execute (can include additional args). Must be correctly escaped.
* @param matchers defines specific codes and/or regexes that should lead to return of a custom error
* @param args optional arguments for tool. Escaping is handled by the lib.
* @param options optional exec options. See ExecOptions
* @returns Promise<number> exit code
*/
async function exec_wrapper(commandLine, args, matchers, options) {
var _a;
const originalListener = (_a = options) === null || _a === void 0 ? void 0 : _a.listeners;
let stdout = '';
let stderr = '';
// custom listeners to store stdout and stderr, while also replicating the behaviour of the passed listeners
const originalListener = (_a = options) === null || _a === void 0 ? void 0 : _a.listeners;
let listeners = {
stdout: (data) => {
var _a;
@ -37,29 +49,29 @@ async function exec_wrapper(commandLine, args, options) {
}
}
};
let returnCode;
// we capture the original return code and error so that (if no match is found) we can duplicate the behaviour
let originalReturnCode;
let originalError;
try {
returnCode = await exec.exec(commandLine, args, {
originalReturnCode = await exec.exec(commandLine, args, {
listeners: listeners,
...options
});
}
catch (e) {
returnCode = 1;
originalError = e;
originalReturnCode = 1; // TODO linter insists, but presumably there's a better way to do _all_ this...
}
if (returnCode === 0) {
throw new Error('The exit code was ' + returnCode + '?!');
if (matchers) {
for (const [customCode, regex, message] of matchers) {
if (customCode === originalReturnCode || regex.test(stderr) || regex.test(stdout)) {
throw new Error(message);
}
}
}
const regex = new RegExp("(No source code was seen during the build\\.|No JavaScript or TypeScript code found\\.)");
if (regex.test(stderr) || regex.test(stdout)) {
throw new Error(`No source code was found. This can occur if the specified build commands failed to compile or process any code.
- Confirm that there is some source code for the specified language in the project.
- For codebases written in Go, JavaScript, TypeScript, and Python, do not specify
an explicit --command.
- For other languages, the --command must specify a "clean" build which compiles
https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning`);
}
return returnCode;
if (originalError)
throw originalError;
return originalReturnCode;
}
exports.exec_wrapper = exec_wrapper;
//# sourceMappingURL=exec_wrapper.js.map