Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allowing a cached version of the CodeQL bundle sometimes
To save time downloading the CodeQL bundle we're pre-downloading it into the
hosted Actions runner, but because the release schedule is different there may
be some version drift. This change allows a different version of the bundle
to be used than the default if a version isn't explicitly specified, there's
only one version downloaded, and it's been marked as a 'pinned-version' -
otherwise it reverts to the prior behavior.
Chris Raynor committed Sep 22, 2020
1 parent 367ad73 commit 41464b1
Showing 7 changed files with 276 additions and 2 deletions.
20 changes: 20 additions & 0 deletions 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.

57 changes: 57 additions & 0 deletions lib/codeql.test.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.test.js.map

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

172 changes: 172 additions & 0 deletions src/codeql.test.ts
@@ -7,6 +7,7 @@ import * as codeql from "./codeql";
import { getRunnerLogger } from "./logging";
import { setupTests } from "./testing-utils";
import * as util from "./util";
import * as defaults from "./defaults.json";

setupTests(test);

@@ -43,6 +44,177 @@ test("download codeql bundle cache", async (t) => {
});
});

test("download codeql bundle cache explicitly requested with pinned different version cached", async (t) => {
await util.withTmpDir(async (tmpDir) => {
nock("https://example.com")
.get(`/download/codeql-bundle-20200601/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle-pinned.tar.gz`)
);

await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200601/codeql-bundle.tar.gz",
"token",
"https://github.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);

t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));

nock("https://example.com")
.get(`/download/codeql-bundle-20200610/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);

await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200610/codeql-bundle.tar.gz",
"token",
"https://github.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);

t.assert(toolcache.find("CodeQL", "0.0.0-20200610"));
});
});

test("don't download codeql bundle cache with pinned different version cached", async (t) => {
await util.withTmpDir(async (tmpDir) => {
nock("https://example.com")
.get(`/download/codeql-bundle-20200601/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle-pinned.tar.gz`)
);

await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200601/codeql-bundle.tar.gz",
"token",
"https://github.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);

t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));

await codeql.setupCodeQL(
undefined,
"token",
"https://github.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);

const cachedVersions = toolcache.findAllVersions("CodeQL");

t.is(cachedVersions.length, 1);
});
});

test("download codeql bundle cache with different version cached (not pinned)", async (t) => {
await util.withTmpDir(async (tmpDir) => {
nock("https://example.com")
.get(`/download/codeql-bundle-20200601/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);

await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200601/codeql-bundle.tar.gz",
"token",
"https://github.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);

t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));

nock("https://github.com")
.get(
`/github/codeql-action/releases/download/${defaults.bundleVersion}/codeql-bundle.tar.gz`
)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);

await codeql.setupCodeQL(
undefined,
"token",
"https://github.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);

const cachedVersions = toolcache.findAllVersions("CodeQL");

t.is(cachedVersions.length, 2);
});
});

test('download codeql bundle cache with pinned different version cached if "latests" tools specied', async (t) => {
await util.withTmpDir(async (tmpDir) => {
nock("https://example.com")
.get(`/download/codeql-bundle-20200601/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle-pinned.tar.gz`)
);

await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200601/codeql-bundle.tar.gz",
"token",
"https://github.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);

t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));

nock("https://github.com")
.get(
`/github/codeql-action/releases/download/${defaults.bundleVersion}/codeql-bundle.tar.gz`
)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);

await codeql.setupCodeQL(
"latest",
"token",
"https://github.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);

const cachedVersions = toolcache.findAllVersions("CodeQL");

t.is(cachedVersions.length, 2);
});
});

test("parse codeql bundle url version", (t) => {
const tests = {
"20200601": "0.0.0-20200601",
25 changes: 25 additions & 0 deletions src/codeql.ts
@@ -235,12 +235,37 @@ export async function setupCodeQL(
process.env["RUNNER_TOOL_CACHE"] = toolsDir;

try {
// We use the special value of 'latest' to prioritize the version in the
// defaults over any pinned cached version.
const forceLatest = codeqlURL === "latest";
if (forceLatest) {
codeqlURL = undefined;
}

const codeqlURLVersion = getCodeQLURLVersion(
codeqlURL || `/${CODEQL_BUNDLE_VERSION}/`,
logger
);

// If we find the specified version, we always use that.
let codeqlFolder = toolcache.find("CodeQL", codeqlURLVersion);

// If we don't find the requested version, in some cases we may allow a
// different version to save download time if the version hasn't been
// specified explicitly (in which case we always honor it).
if (!codeqlFolder && !codeqlURL && !forceLatest) {
const codeqlVersions = toolcache.findAllVersions("CodeQL");
if (codeqlVersions.length === 1) {
const tmpCodeqlFolder = toolcache.find("CodeQL", codeqlVersions[0]);
if (fs.existsSync(path.join(tmpCodeqlFolder, "pinned-version"))) {
logger.debug(
`CodeQL in cache overriding the default ${CODEQL_BUNDLE_VERSION}`
);
codeqlFolder = tmpCodeqlFolder;
}
}
}

if (codeqlFolder) {
logger.debug(`CodeQL found in cache ${codeqlFolder}`);
} else {
Binary file added src/testdata/codeql-bundle-pinned.tar.gz
Binary file not shown.

0 comments on commit 41464b1

Please sign in to comment.