Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #227 from github/cbraynor/cache-pinning
Prioritizing pre-downloaded CodeQL bundle in some circumstances
Chris Raynor authored and GitHub committed Sep 23, 2020
2 parents 367ad73 + 2f4ca98 commit 481f3ce
Showing 8 changed files with 278 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/integration-testing.yml
@@ -44,6 +44,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
tools: [~, latest]
runs-on: ${{ matrix.os }}

steps:
@@ -56,6 +57,7 @@ jobs:
mv ../action/tests/multi-language-repo/{*,.github} .
- uses: ./../action/init
with:
tools: ${{ matrix.tools }}
languages: cpp,csharp,java,javascript,python
config-file: github/codeql-action/tests/multi-language-repo/.github/codeql/custom-queries.yml@${{ github.sha }}
- name: Build code
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 481f3ce

Please sign in to comment.