Skip to content

Commit

Permalink
update error_wrapper to take matcher array
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Fyson committed Sep 1, 2020
1 parent cd22abc commit 7b7e0e1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lib/codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/codeql.js.map

Large diffs are not rendered by default.

46 changes: 29 additions & 17 deletions lib/exec_wrapper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/exec_wrapper.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions src/codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,14 @@ function getCodeQLForCmd(cmd: string): CodeQL {
]);
},
finalizeDatabase: async function(databasePath: string) {
await exec_wrapper(cmd, [
'database',
'finalize',
...getExtraOptionsFromEnv(['database', 'finalize']),
databasePath
]);
await exec_wrapper(
cmd, [
'database',
'finalize',
...getExtraOptionsFromEnv(['database', 'finalize']),
databasePath
],
[[0, new RegExp("(No source code was seen during the build\\.|No JavaScript or TypeScript code found\\.)"), 'foo bar']]);
},
resolveQueries: async function(queries: string[], extraSearchPath: string | undefined) {
const codeqlArgs = [
Expand Down
48 changes: 30 additions & 18 deletions src/exec_wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import * as exec from '@actions/exec';
import * as im from '@actions/exec/lib/interfaces';

export async function exec_wrapper(commandLine: string, args?: string[], options?: im.ExecOptions): Promise<number> {

const originalListener = options?.listeners;
/**
* 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
*/
export async function exec_wrapper(commandLine: string, args?: string[],
matchers?: [[number, RegExp, string]],
options?: im.ExecOptions): Promise<number> {

let stdout = '';
let stderr = '';

// custom listeners to store stdout and stderr, while also replicating the behaviour of the passed listeners
const originalListener = options?.listeners;
let listeners = {
stdout: (data: Buffer) => {
stdout += data.toString();
Expand All @@ -30,32 +44,30 @@ export async function exec_wrapper(commandLine: string, args?: string[], options
}
};

let returnCode: number;
// we capture the original return code and error so that (if no match is found) we can duplicate the behaviour
let originalReturnCode: number;
let originalError: Error|undefined;
try {
returnCode = await exec.exec(
originalReturnCode = await exec.exec(
commandLine,
args,
{
listeners: listeners,
...options
});
} catch (e) {
returnCode = 1;
}
if (returnCode === 0) {
throw new Error('The exit code was ' + returnCode + '?!');
originalError = e;
originalReturnCode = 1; // TODO linter insists, but presumably there's a better way to do _all_ this...
}

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`);
if (matchers) {
for (const [customCode, regex, message] of matchers) {
if (customCode === originalReturnCode || regex.test(stderr) || regex.test(stdout) ) {
throw new Error(message);
}
}
}

return returnCode;
if (originalError) throw originalError;
return originalReturnCode;
}

0 comments on commit 7b7e0e1

Please sign in to comment.