Skip to content

Commit

Permalink
switch to semver instead of hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kalyvitis committed Jun 18, 2020
1 parent 4c67491 commit 3f2a60b
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/setup-tools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core';
import * as toolcache from '@actions/tool-cache';
import * as crypto from 'crypto';
import * as semver from 'semver';
import * as path from 'path';

export class CodeQLSetup {
Expand Down Expand Up @@ -30,18 +30,17 @@ export class CodeQLSetup {
}

export async function setupCodeQL(): Promise<CodeQLSetup> {
const hash = crypto.createHash('sha256');
const codeqlURL = core.getInput('tools', { required: true });
const codeqlURLHash = hash.update(codeqlURL).digest('hex');

try {
let codeqlFolder = toolcache.find('CodeQL', codeqlURLHash);
const codeqlURL = core.getInput('tools', { required: true });
const codeqlURLVersion = getCodeQLURLVersion(codeqlURL);

let codeqlFolder = toolcache.find('CodeQL', codeqlURLVersion);
if (codeqlFolder) {
core.debug(`CodeQL found in cache ${codeqlFolder}`);
} else {
const codeqlPath = await toolcache.downloadTool(codeqlURL);
const codeqlExtracted = await toolcache.extractTar(codeqlPath);
codeqlFolder = await toolcache.cacheDir(codeqlExtracted, 'CodeQL', codeqlURLHash);
codeqlFolder = await toolcache.cacheDir(codeqlExtracted, 'CodeQL', codeqlURLVersion);
}
return new CodeQLSetup(path.join(codeqlFolder, 'codeql'));

Expand All @@ -50,3 +49,25 @@ export async function setupCodeQL(): Promise<CodeQLSetup> {
throw new Error("Unable to download and extract CodeQL CLI");
}
}

export function getCodeQLURLVersion(url: string): string {

const match = url.match(/codeql-bundle-([\d+(\.\d+)]+)/);
if (match === null || match.length < 2) {
throw new Error(`Malformed tools url: ${url}. Version could not be inferred`);
}

let version = match[1];

if (!semver.valid(version)) {
core.debug(`Bundle version ${version} is not in SemVer format. Will treat it as pre-release 0.0.0-${version}.`);
version = '0.0.0-' + version;
}

const s = semver.clean(version);
if (!s) {
throw new Error(`Malformed tools url ${url}. Version should be in SemVer format but have ${version} instead`);
}

return s;
}

0 comments on commit 3f2a60b

Please sign in to comment.