Merge branch 'main' into octokit

This commit is contained in:
Chris Gavin 2020-06-26 10:29:51 +01:00
commit ef507971e7
No known key found for this signature in database
GPG key ID: 07F950B80C27E4DA
33 changed files with 276 additions and 47 deletions

View file

@ -2,6 +2,9 @@ import test from 'ava';
import * as analysisPaths from './analysis-paths';
import * as configUtils from './config-utils';
import {silenceDebugOutput} from './testing-utils';
silenceDebugOutput(test);
test("emptyPaths", async t => {
let config = new configUtils.Config();

View file

@ -3,8 +3,11 @@ import * as fs from 'fs';
import * as path from 'path';
import * as configUtils from './config-utils';
import {silenceDebugOutput} from './testing-utils';
import * as util from './util';
silenceDebugOutput(test);
function setInput(name: string, value: string | undefined) {
// Transformation copied from
// https://github.com/actions/toolkit/blob/05e39f551d33e1688f61b209ab5cdd335198f1b8/packages/core/src/core.ts#L69

View file

@ -4,8 +4,11 @@ import * as path from "path";
import * as configUtils from "./config-utils";
import * as externalQueries from "./external-queries";
import {silenceDebugOutput} from './testing-utils';
import * as util from "./util";
silenceDebugOutput(test);
test("checkoutExternalQueries", async t => {
let config = new configUtils.Config();
config.externalQueries = [

View file

@ -4,6 +4,9 @@ import * as fs from 'fs';
import * as path from 'path';
import * as fingerprints from './fingerprints';
import {silenceDebugOutput} from './testing-utils';
silenceDebugOutput(test);
function testHash(t: ava.Assertions, input: string, expectedHashes: string[]) {
let index = 0;

View file

@ -4,8 +4,11 @@ import nock from 'nock';
import * as path from 'path';
import * as setupTools from './setup-tools';
import {silenceDebugOutput} from './testing-utils';
import * as util from './util';
silenceDebugOutput(test);
test('download codeql bundle cache', async t => {
await util.withTmpDir(async tmpDir => {

View file

@ -139,30 +139,37 @@ function concatTracerConfigs(configs: { [lang: string]: TracerConfig }): TracerC
return { env, spec };
}
async function run() {
let languages: string[];
try {
if (util.should_abort('init', false) || !await util.reportActionStarting('init')) {
return;
}
// The config file MUST be parsed in the init action
const config = await configUtils.loadConfig();
core.startGroup('Load language configuration');
const languages = await util.getLanguages();
const config = await configUtils.loadConfig();
languages = await util.getLanguages();
// If the languages parameter was not given and no languages were
// detected then fail here as this is a workflow configuration error.
if (languages.length === 0) {
core.setFailed("Did not detect any languages to analyze. Please update input in workflow.");
return;
throw new Error("Did not detect any languages to analyze. Please update input in workflow.");
}
analysisPaths.includeAndExcludeAnalysisPaths(config, languages);
core.endGroup();
analysisPaths.includeAndExcludeAnalysisPaths(config, languages);
} catch (e) {
core.setFailed(e.message);
await util.reportActionAborted('init', e.message);
return;
}
try {
const sourceRoot = path.resolve();

56
src/testing-utils.ts Normal file
View file

@ -0,0 +1,56 @@
import {TestInterface} from 'ava';
type TestContext = {stdoutWrite: any, stderrWrite: any, testOutput: string};
function wrapOutput(context: TestContext) {
// Function signature taken from Socket.write.
// Note there are two overloads:
// write(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
// write(str: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean;
return (chunk: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean => {
// Work out which method overload we are in
if (cb === undefined && typeof encoding === 'function') {
cb = encoding;
encoding = undefined;
}
// Record the output
if (typeof chunk === 'string') {
context.testOutput += chunk;
} else {
context.testOutput += new TextDecoder(encoding || 'utf-8').decode(chunk);
}
// Satisfy contract by calling callback when done
if (cb !== undefined && typeof cb === 'function') {
cb();
}
return true;
};
}
export function silenceDebugOutput(test: TestInterface<any>) {
const typedTest = test as TestInterface<TestContext>;
typedTest.beforeEach(t => {
t.context.testOutput = "";
const processStdoutWrite = process.stdout.write.bind(process.stdout);
t.context.stdoutWrite = processStdoutWrite;
process.stdout.write = wrapOutput(t.context) as any;
const processStderrWrite = process.stderr.write.bind(process.stderr);
t.context.stderrWrite = processStderrWrite;
process.stderr.write = wrapOutput(t.context) as any;
});
typedTest.afterEach.always(t => {
process.stdout.write = t.context.stdoutWrite;
process.stderr.write = t.context.stderrWrite;
if (!t.passed) {
process.stdout.write(t.context.testOutput);
}
});
}

View file

@ -1,7 +1,10 @@
import test from 'ava';
import {silenceDebugOutput} from './testing-utils';
import * as uploadLib from './upload-lib';
silenceDebugOutput(test);
test('validateSarifFileSchema - valid', t => {
const inputFile = __dirname + '/../src/testdata/valid-sarif.sarif';
t.true(uploadLib.validateSarifFileSchema(inputFile));

View file

@ -2,8 +2,11 @@ import test from 'ava';
import * as fs from 'fs';
import * as os from "os";
import {silenceDebugOutput} from './testing-utils';
import * as util from './util';
silenceDebugOutput(test);
test('getToolNames', t => {
const input = fs.readFileSync(__dirname + '/../src/testdata/tool-names.sarif', 'utf8');
const toolNames = util.getToolNames(input);

View file

@ -131,15 +131,27 @@ export async function getLanguages(): Promise<string[]> {
* Gets the SHA of the commit that is currently checked out.
*/
export async function getCommitOid(): Promise<string> {
let commitOid = '';
await exec.exec('git', ['rev-parse', 'HEAD'], {
silent: true,
listeners: {
stdout: (data) => { commitOid += data.toString(); },
stderr: (data) => { process.stderr.write(data); }
}
});
return commitOid.trim();
// Try to use git to get the current commit SHA. If that fails then
// log but otherwise silently fall back to using the SHA from the environment.
// The only time these two values will differ is during analysis of a PR when
// the workflow has changed the current commit to the head commit instead of
// the merge commit, which must mean that git is available.
// Even if this does go wrong, it's not a huge problem for the alerts to
// reported on the merge commit.
try {
let commitOid = '';
await exec.exec('git', ['rev-parse', 'HEAD'], {
silent: true,
listeners: {
stdout: (data) => { commitOid += data.toString(); },
stderr: (data) => { process.stderr.write(data); }
}
});
return commitOid.trim();
} catch (e) {
core.info("Failed to call git to get current commit. Continuing with data from environment: " + e);
return getRequiredEnvParam('GITHUB_SHA');
}
}
/**
@ -269,7 +281,7 @@ async function createStatusReport(
if (exception) {
statusReport.exception = exception;
}
if (status === 'success' || status === 'failure') {
if (status === 'success' || status === 'failure' || status === 'aborted') {
statusReport.completed_at = new Date().toISOString();
}
let matrix: string | undefined = core.getInput('matrix');
@ -349,6 +361,16 @@ export async function reportActionSucceeded(action: string) {
await sendStatusReport(await createStatusReport(action, 'success'));
}
/**
* Report that an action has been aborted.
*
* Note that the started_at date is always that of the `init` action, since
* this is likely to give a more useful duration when inspecting events.
*/
export async function reportActionAborted(action: string, cause?: string) {
await sendStatusReport(await createStatusReport(action, 'aborted', cause));
}
/**
* Get the array of all the tool names contained in the given sarif contents.
*
@ -373,7 +395,11 @@ export function getToolNames(sarifContents: string): string[] {
// Mostly intended for use within tests.
export async function withTmpDir<T>(body: (tmpDir: string) => Promise<T>): Promise<T> {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codeql-action-'));
const result = await body(tmpDir);
const realSubdir = path.join(tmpDir, 'real');
fs.mkdirSync(realSubdir);
const symlinkSubdir = path.join(tmpDir, 'symlink');
fs.symlinkSync(realSubdir, symlinkSubdir, 'dir');
const result = await body(symlinkSubdir);
fs.rmdirSync(tmpDir, { recursive: true });
return result;
}