Merge branch 'main' into lgtm_filters

This commit is contained in:
Robert Brignull
2020-07-10 15:01:22 +01:00
205 changed files with 99252 additions and 6163 deletions

View File

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

View File

@@ -1,11 +1,12 @@
import * as core from "@actions/core";
import * as octokit from "@octokit/rest";
import * as github from "@actions/github";
import consoleLogLevel from "console-log-level";
const githubAPIURL = process.env["GITHUB_API_URL"] || "https://api.github.com";
export const client = new octokit.Octokit({
auth: core.getInput("token"),
baseUrl: githubAPIURL,
userAgent: "CodeQL Action",
log: consoleLogLevel({ level: "debug" })
});
export const getApiClient = function() {
return new github.GitHub(
core.getInput('token'),
{
userAgent: "CodeQL Action",
log: consoleLogLevel({ level: "debug" })
});
};

View File

@@ -4,10 +4,10 @@ import nock from 'nock';
import * as path from 'path';
import * as codeql from './codeql';
import {silenceDebugOutput} from './testing-utils';
import {setupTests} from './testing-utils';
import * as util from './util';
silenceDebugOutput(test);
setupTests(test);
test('download codeql bundle cache', async t => {

View File

@@ -1,4 +1,4 @@
import * as octokit from '@octokit/rest';
import * as github from "@actions/github";
import test from 'ava';
import * as fs from 'fs';
import * as path from 'path';
@@ -6,10 +6,10 @@ import sinon from 'sinon';
import * as api from './api-client';
import * as configUtils from './config-utils';
import {silenceDebugOutput} from './testing-utils';
import {setupTests} from './testing-utils';
import * as util from './util';
silenceDebugOutput(test);
setupTests(test);
function setInput(name: string, value: string | undefined) {
// Transformation copied from
@@ -22,6 +22,19 @@ function setInput(name: string, value: string | undefined) {
}
}
type GetContentsResponse = { content?: string; } | {}[];
function mockGetContents(content: GetContentsResponse): sinon.SinonStub<any, any> {
// Passing an auth token is required, so we just use a dummy value
let client = new github.GitHub('123');
const response = {
data: content
};
const spyGetContents = sinon.stub(client.repos, "getContents").resolves(response as any);
sinon.stub(api, "getApiClient").value(() => client);
return spyGetContents;
}
test("load empty config", async t => {
return await util.withTmpDir(async tmpDir => {
process.env['RUNNER_TEMP'] = tmpDir;
@@ -161,24 +174,13 @@ test("API client used when reading remote config", async t => {
paths:
- c/d`;
const dummyResponse = {
data: {
content: Buffer.from(inputFileContents).toString("base64"),
}
content: Buffer.from(inputFileContents).toString("base64"),
};
let ok = new octokit.Octokit({
userAgent: "CodeQL Action",
});
const repos = ok.repos;
const spyGetContents = sinon.stub(repos, "getContents").resolves(Promise.resolve(dummyResponse));
ok.repos = repos;
sinon.stub(api, "client").value(ok);
const spyGetContents = mockGetContents(dummyResponse);
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
await configUtils.loadConfig();
t.assert(spyGetContents.called);
sinon.restore();
});
});
@@ -187,17 +189,8 @@ test("Remote config handles the case where a directory is provided", async t =>
process.env['RUNNER_TEMP'] = tmpDir;
process.env['GITHUB_WORKSPACE'] = tmpDir;
const dummyResponse = {
data: [], // directories are returned as arrays
};
let ok = new octokit.Octokit({
userAgent: "CodeQL Action",
});
const repos = ok.repos;
sinon.stub(repos, "getContents").resolves(Promise.resolve(dummyResponse));
ok.repos = repos;
sinon.stub(api, "client").value(ok);
const dummyResponse = []; // directories are returned as arrays
mockGetContents(dummyResponse);
const repoReference = 'octo-org/codeql-config/config.yaml@main';
setInput('config-file', repoReference);
@@ -207,8 +200,6 @@ test("Remote config handles the case where a directory is provided", async t =>
} catch (err) {
t.deepEqual(err, new Error(configUtils.getConfigFileDirectoryGivenMessage(repoReference)));
}
sinon.restore();
});
});
@@ -218,18 +209,9 @@ test("Invalid format of remote config handled correctly", async t => {
process.env['GITHUB_WORKSPACE'] = tmpDir;
const dummyResponse = {
data: {
// note no "content" property here
}
// note no "content" property here
};
let ok = new octokit.Octokit({
userAgent: "CodeQL Action",
});
const repos = ok.repos;
sinon.stub(repos, "getContents").resolves(Promise.resolve(dummyResponse));
ok.repos = repos;
sinon.stub(api, "client").value(ok);
mockGetContents(dummyResponse);
const repoReference = 'octo-org/codeql-config/config.yaml@main';
setInput('config-file', repoReference);
@@ -239,8 +221,6 @@ test("Invalid format of remote config handled correctly", async t => {
} catch (err) {
t.deepEqual(err, new Error(configUtils.getConfigFileFormatInvalidMessage(repoReference)));
}
sinon.restore();
});
});

View File

@@ -356,7 +356,7 @@ async function getRemoteConfig(configFile: string): Promise<any> {
throw new Error(getConfigFileRepoFormatInvalidMessage(configFile));
}
const response = await api.client.repos.getContents({
const response = await api.getApiClient().repos.getContents({
owner: pieces.groups.owner,
repo: pieces.groups.repo,
path: pieces.groups.path,

View File

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

View File

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

View File

@@ -1,4 +1,5 @@
import {TestInterface} from 'ava';
import sinon from 'sinon';
type TestContext = {stdoutWrite: any, stderrWrite: any, testOutput: string};
@@ -30,7 +31,7 @@ function wrapOutput(context: TestContext) {
};
}
export function silenceDebugOutput(test: TestInterface<any>) {
export function setupTests(test: TestInterface<any>) {
const typedTest = test as TestInterface<TestContext>;
typedTest.beforeEach(t => {
@@ -53,4 +54,8 @@ export function silenceDebugOutput(test: TestInterface<any>) {
process.stdout.write(t.context.testOutput);
}
});
typedTest.afterEach.always(() => {
sinon.restore();
});
}

View File

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

View File

@@ -53,7 +53,7 @@ async function uploadPayload(payload): Promise<boolean> {
const backoffPeriods = [1, 5, 15];
for (let attempt = 0; attempt <= backoffPeriods.length; attempt++) {
const response = await api.client.request("PUT /repos/:owner/:repo/code-scanning/analysis", ({
const response = await api.getApiClient().request("PUT /repos/:owner/:repo/code-scanning/analysis", ({
owner: owner,
repo: repo,
data: payload,

View File

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

View File

@@ -65,7 +65,7 @@ async function getLanguagesInRepo(): Promise<string[]> {
let repo = repo_nwo[1];
core.debug(`GitHub repo ${owner} ${repo}`);
const response = await api.client.request("GET /repos/:owner/:repo/languages", ({
const response = await api.getApiClient().request("GET /repos/:owner/:repo/languages", ({
owner,
repo
}));
@@ -163,14 +163,15 @@ async function getWorkflowPath(): Promise<string> {
const repo = repo_nwo[1];
const run_id = Number(getRequiredEnvParam('GITHUB_RUN_ID'));
const runsResponse = await api.client.request('GET /repos/:owner/:repo/actions/runs/:run_id', {
const apiClient = api.getApiClient();
const runsResponse = await apiClient.request('GET /repos/:owner/:repo/actions/runs/:run_id', {
owner,
repo,
run_id
});
const workflowUrl = runsResponse.data.workflow_url;
const workflowResponse = await api.client.request('GET ' + workflowUrl);
const workflowResponse = await apiClient.request('GET ' + workflowUrl);
return workflowResponse.data.path;
}
@@ -307,7 +308,7 @@ async function sendStatusReport(statusReport: StatusReport): Promise<number> {
const nwo = getRequiredEnvParam("GITHUB_REPOSITORY");
const [owner, repo] = nwo.split("/");
const statusResponse = await api.client.request('PUT /repos/:owner/:repo/code-scanning/analysis/status', {
const statusResponse = await api.getApiClient().request('PUT /repos/:owner/:repo/code-scanning/analysis/status', {
owner: owner,
repo: repo,
data: statusReportJSON,