send action ref and tool version in status reports

This commit is contained in:
Robert 2020-11-11 18:22:12 +00:00
parent acacf9bbd5
commit 80b43ca9d3
18 changed files with 88 additions and 54 deletions

View file

@ -197,7 +197,9 @@ export interface StatusReportBase {
ref: string;
// Name of the action being executed
action_name: ActionName;
// Version if the action being executed, as a commit oid
// Version of the action being executed, as a ref
action_ref?: string;
// Version of the action being executed, as a commit oid
action_oid: string;
// Time the first action started. Normally the init action
started_at: string;
@ -256,6 +258,7 @@ export async function createStatusReportBase(
commit_oid: commitOid,
ref,
action_name: actionName,
action_ref: process.env["GITHUB_ACTION_REF"],
action_oid: "unknown", // TODO decide if it's possible to fill this in
started_at: workflowStartedAt,
action_started_at: actionStartedAt.toISOString(),

View file

@ -230,6 +230,15 @@ test('download codeql bundle cache with pinned different version cached if "late
});
test("parse codeql bundle url version", (t) => {
t.deepEqual(
codeql.getCodeQLURLVersion(
"https://github.com/.../codeql-bundle-20200601/..."
),
"20200601"
);
});
test("convert to semver", (t) => {
const tests = {
"20200601": "0.0.0-20200601",
"20200601.0": "0.0.0-20200601.0",
@ -240,11 +249,9 @@ test("parse codeql bundle url version", (t) => {
};
for (const [version, expectedVersion] of Object.entries(tests)) {
const url = `https://github.com/.../codeql-bundle-${version}/...`;
try {
const parsedVersion = codeql.getCodeQLURLVersion(
url,
const parsedVersion = codeql.convertToSemVer(
version,
getRunnerLogger(true)
);
t.deepEqual(parsedVersion, expectedVersion);

View file

@ -251,7 +251,7 @@ export async function setupCodeQL(
toolsDir: string,
mode: util.Mode,
logger: Logger
): Promise<CodeQL> {
): Promise<{ codeql: CodeQL; toolsVersion: string }> {
// Setting these two env vars makes the toolcache code safe to use outside,
// of actions but this is obviously not a great thing we're doing and it would
// be better to write our own implementation to use outside of actions.
@ -267,12 +267,12 @@ export async function setupCodeQL(
}
const codeqlURLVersion = getCodeQLURLVersion(
codeqlURL || `/${CODEQL_BUNDLE_VERSION}/`,
logger
codeqlURL || `/${CODEQL_BUNDLE_VERSION}/`
);
const codeqlURLSemVer = convertToSemVer(codeqlURLVersion, logger);
// If we find the specified version, we always use that.
let codeqlFolder = toolcache.find("CodeQL", codeqlURLVersion);
let codeqlFolder = toolcache.find("CodeQL", codeqlURLSemVer);
// 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
@ -327,7 +327,7 @@ export async function setupCodeQL(
codeqlFolder = await toolcache.cacheDir(
codeqlExtracted,
"CodeQL",
codeqlURLVersion
codeqlURLSemVer
);
}
@ -339,23 +339,24 @@ export async function setupCodeQL(
}
cachedCodeQL = getCodeQLForCmd(codeqlCmd);
return cachedCodeQL;
return { codeql: cachedCodeQL, toolsVersion: codeqlURLVersion };
} catch (e) {
logger.error(e);
throw new Error("Unable to download and extract CodeQL CLI");
}
}
export function getCodeQLURLVersion(url: string, logger: Logger): string {
export function getCodeQLURLVersion(url: string): string {
const match = url.match(/\/codeql-bundle-(.*)\//);
if (match === null || match.length < 2) {
throw new Error(
`Malformed tools url: ${url}. Version could not be inferred`
);
}
return match[1];
}
let version = match[1];
export function convertToSemVer(version: string, logger: Logger): string {
if (!semver.valid(version)) {
logger.debug(
`Bundle version ${version} is not in SemVer format. Will treat it as pre-release 0.0.0-${version}.`
@ -365,9 +366,7 @@ export function getCodeQLURLVersion(url: string, logger: Logger): string {
const s = semver.clean(version);
if (!s) {
throw new Error(
`Malformed tools url ${url}. Version should be in SemVer format but have ${version} instead`
);
throw new Error(`Bundle version ${version} is not in SemVer format.`);
}
return s;

View file

@ -28,11 +28,16 @@ interface InitSuccessStatusReport extends actionsUtil.StatusReportBase {
disable_default_queries: string;
// Comma-separated list of queries sources, from the 'queries' config field or workflow input
queries: string;
// Value given by the user as the "tools" input
tools_input: string;
// Version of the bundle used
tools_resolved_version: string;
}
async function sendSuccessStatusReport(
startedAt: Date,
config: configUtils.Config
config: configUtils.Config,
toolsVersion: string
) {
const statusReportBase = await actionsUtil.createStatusReportBase(
"init",
@ -74,6 +79,8 @@ async function sendSuccessStatusReport(
paths_ignore: pathsIgnore,
disable_default_queries: disableDefaultQueries,
queries: queries.join(","),
tools_input: actionsUtil.getOptionalInput("tools") || "",
tools_resolved_version: toolsVersion,
};
await actionsUtil.sendStatusReport(statusReport);
@ -84,6 +91,7 @@ async function run() {
const logger = getActionsLogger();
let config: configUtils.Config;
let codeql: CodeQL;
let toolsVersion: string;
try {
actionsUtil.prepareLocalRunEnvironment();
@ -96,7 +104,7 @@ async function run() {
return;
}
codeql = await initCodeQL(
const initCodeQLResult = await initCodeQL(
actionsUtil.getOptionalInput("tools"),
actionsUtil.getRequiredInput("token"),
actionsUtil.getRequiredEnvParam("GITHUB_SERVER_URL"),
@ -105,6 +113,9 @@ async function run() {
"actions",
logger
);
codeql = initCodeQLResult.codeql;
toolsVersion = initCodeQLResult.toolsVersion;
config = await initConfig(
actionsUtil.getOptionalInput("languages"),
actionsUtil.getOptionalInput("queries"),
@ -192,7 +203,7 @@ async function run() {
);
return;
}
await sendSuccessStatusReport(startedAt, config);
await sendSuccessStatusReport(startedAt, config, toolsVersion);
}
run().catch((e) => {

View file

@ -19,9 +19,9 @@ export async function initCodeQL(
toolsDir: string,
mode: util.Mode,
logger: Logger
): Promise<CodeQL> {
): Promise<{ codeql: CodeQL; toolsVersion: string }> {
logger.startGroup("Setup CodeQL tools");
const codeql = await setupCodeQL(
const { codeql, toolsVersion } = await setupCodeQL(
codeqlURL,
githubAuth,
githubUrl,
@ -32,7 +32,7 @@ export async function initCodeQL(
);
await codeql.printVersion();
logger.endGroup();
return codeql;
return { codeql, toolsVersion };
}
export async function initConfig(

View file

@ -150,15 +150,17 @@ program
if (cmd.codeqlPath !== undefined) {
codeql = getCodeQL(cmd.codeqlPath);
} else {
codeql = await initCodeQL(
undefined,
cmd.githubAuth,
parseGithubUrl(cmd.githubUrl),
tempDir,
toolsDir,
"runner",
logger
);
codeql = (
await initCodeQL(
undefined,
cmd.githubAuth,
parseGithubUrl(cmd.githubUrl),
tempDir,
toolsDir,
"runner",
logger
)
).codeql;
}
const config = await initConfig(