Send the version and mode with the user agent

This commit changes the way the action determines if running in action
or runner mode. There is now an environment variable that is set at the
beginning of the process and elsewhere in the process, we can check to
see if the variable is set.
This commit is contained in:
Andrew Eisenberg 2021-05-20 15:20:32 -07:00
parent fad7cc482d
commit 47588796b4
48 changed files with 361 additions and 224 deletions

View file

@ -690,3 +690,11 @@ on: ["push"]
)
);
});
test("mode", (t) => {
actionsutil.setMode(actionsutil.Mode.actions);
t.deepEqual(actionsutil.getMode(), actionsutil.Mode.actions);
actionsutil.setMode(actionsutil.Mode.runner);
t.deepEqual(actionsutil.getMode(), actionsutil.Mode.runner);
});

View file

@ -10,6 +10,11 @@ import * as api from "./api-client";
import * as sharedEnv from "./shared-environment";
import { GITHUB_DOTCOM_URL, isLocalRun } from "./util";
export enum Mode {
actions = "Action",
runner = "Runner",
}
/**
* Wrapper around core.getInput for inputs that always have a value.
* Also see getOptionalInput.
@ -727,3 +732,30 @@ export function getRelativeScriptPath(): string {
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
return path.relative(actionsDirectory, __filename);
}
const CODEQL_RUN_MODE_ENV_VAR = "CODEQL_RUN_MODE";
export function setMode(mode: Mode) {
// avoid accessing actions core when in runner mode
if (mode === Mode.actions) {
core.exportVariable(CODEQL_RUN_MODE_ENV_VAR, mode);
} else {
process.env[CODEQL_RUN_MODE_ENV_VAR] = mode;
}
}
export function getMode(): Mode {
// Make sure we fail fast if the env var is missing. This should
// only happen if there is a bug in our code and we neglected
// to set the mode early in the process.
const mode = getRequiredEnvParam(CODEQL_RUN_MODE_ENV_VAR);
if (mode !== Mode.actions && mode !== Mode.runner) {
throw new Error(`Unknown mode: ${mode}.`);
}
return mode;
}
export function isActions(): boolean {
return getMode() === Mode.actions;
}

View file

@ -49,6 +49,8 @@ async function run() {
const startedAt = new Date();
let stats: AnalysisStatusReport | undefined = undefined;
let config: Config | undefined = undefined;
actionsUtil.setMode(actionsUtil.Mode.actions);
try {
actionsUtil.prepareLocalRunEnvironment();
if (

View file

@ -2,15 +2,20 @@ import * as githubUtils from "@actions/github/lib/utils";
import test, { ExecutionContext } from "ava";
import sinon from "sinon";
import { Mode, setMode } from "./actions-util";
import { getApiClient } from "./api-client";
import { setupTests } from "./testing-utils";
// eslint-disable-next-line import/no-commonjs
const pkg = require("../package.json");
setupTests(test);
let githubStub: sinon.SinonStub;
test.beforeEach(() => {
githubStub = sinon.stub(githubUtils, "GitHub");
setMode(Mode.actions);
});
test("Get the client API", async (t) => {
@ -25,7 +30,7 @@ test("Get the client API", async (t) => {
{
auth: "token xyz",
baseUrl: "http://hucairz/api/v3",
userAgent: "CodeQL Action",
userAgent: `CodeQL Action/${pkg.version}`,
}
);
});
@ -42,7 +47,7 @@ test("Get the client API external", async (t) => {
{
auth: "token abc",
baseUrl: "http://hucairz/api/v3",
userAgent: "CodeQL Action",
userAgent: `CodeQL Action/${pkg.version}`,
}
);
});
@ -58,7 +63,7 @@ test("Get the client API external not present", async (t) => {
{
auth: "token xyz",
baseUrl: "http://hucairz/api/v3",
userAgent: "CodeQL Action",
userAgent: `CodeQL Action/${pkg.version}`,
}
);
});
@ -74,7 +79,7 @@ test("Get the client API with github url", async (t) => {
{
auth: "token xyz",
baseUrl: "https://api.github.com",
userAgent: "CodeQL Action",
userAgent: `CodeQL Action/${pkg.version}`,
}
);
});

View file

@ -3,9 +3,12 @@ import * as path from "path";
import * as githubUtils from "@actions/github/lib/utils";
import consoleLogLevel from "console-log-level";
import { getRequiredEnvParam, getRequiredInput } from "./actions-util";
import { getMode, getRequiredEnvParam, getRequiredInput } from "./actions-util";
import { isLocalRun } from "./util";
// eslint-disable-next-line import/no-commonjs
const pkg = require("../package.json");
export enum DisallowedAPIVersionReason {
ACTION_TOO_OLD,
ACTION_TOO_NEW,
@ -37,7 +40,7 @@ export const getApiClient = function (
return new githubUtils.GitHub(
githubUtils.getOctokitOptions(auth, {
baseUrl: getApiUrl(apiDetails.url),
userAgent: "CodeQL Action",
userAgent: `CodeQL ${getMode()}/${pkg.version}`,
log: consoleLogLevel({ level: "debug" }),
})
);

View file

@ -1,12 +1,20 @@
import * as core from "@actions/core";
import * as actionsUtil from "./actions-util";
import {
createStatusReportBase,
getTemporaryDirectory,
Mode,
prepareLocalRunEnvironment,
sendStatusReport,
setMode,
StatusReportBase,
} from "./actions-util";
import { determineAutobuildLanguage, runAutobuild } from "./autobuild";
import * as config_utils from "./config-utils";
import { Language } from "./languages";
import { getActionsLogger } from "./logging";
interface AutobuildStatusReport extends actionsUtil.StatusReportBase {
interface AutobuildStatusReport extends StatusReportBase {
// Comma-separated set of languages being auto-built
autobuild_languages: string;
// Language that failed autobuilding (or undefined if all languages succeeded).
@ -19,11 +27,13 @@ async function sendCompletedStatusReport(
failingLanguage?: string,
cause?: Error
) {
setMode(Mode.actions);
const status =
failingLanguage !== undefined || cause !== undefined
? "failure"
: "success";
const statusReportBase = await actionsUtil.createStatusReportBase(
const statusReportBase = await createStatusReportBase(
"autobuild",
status,
startedAt,
@ -35,7 +45,7 @@ async function sendCompletedStatusReport(
autobuild_languages: allLanguages.join(","),
autobuild_failure: failingLanguage,
};
await actionsUtil.sendStatusReport(statusReport);
await sendStatusReport(statusReport);
}
async function run() {
@ -43,21 +53,17 @@ async function run() {
const startedAt = new Date();
let language: Language | undefined = undefined;
try {
actionsUtil.prepareLocalRunEnvironment();
prepareLocalRunEnvironment();
if (
!(await actionsUtil.sendStatusReport(
await actionsUtil.createStatusReportBase(
"autobuild",
"starting",
startedAt
)
!(await sendStatusReport(
await createStatusReportBase("autobuild", "starting", startedAt)
))
) {
return;
}
const config = await config_utils.getConfig(
actionsUtil.getTemporaryDirectory(),
getTemporaryDirectory(),
logger
);
if (config === undefined) {

View file

@ -4,6 +4,7 @@ import * as toolcache from "@actions/tool-cache";
import test from "ava";
import nock from "nock";
import { Mode, setMode } from "./actions-util";
import * as codeql from "./codeql";
import * as defaults from "./defaults.json";
import { getRunnerLogger } from "./logging";
@ -22,6 +23,10 @@ const sampleGHAEApiDetails = {
url: "https://example.githubenterprise.com",
};
test.beforeEach(() => {
setMode(Mode.actions);
});
test("download codeql bundle cache", async (t) => {
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
@ -43,7 +48,6 @@ test("download codeql bundle cache", async (t) => {
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -73,7 +77,6 @@ test("download codeql bundle cache explicitly requested with pinned different ve
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -92,7 +95,6 @@ test("download codeql bundle cache explicitly requested with pinned different ve
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -117,7 +119,6 @@ test("don't download codeql bundle cache with pinned different version cached",
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -129,7 +130,6 @@ test("don't download codeql bundle cache with pinned different version cached",
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -156,7 +156,6 @@ test("download codeql bundle cache with different version cached (not pinned)",
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -183,7 +182,6 @@ test("download codeql bundle cache with different version cached (not pinned)",
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -210,7 +208,6 @@ test('download codeql bundle cache with pinned different version cached if "late
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -238,7 +235,6 @@ test('download codeql bundle cache with pinned different version cached if "late
sampleApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.DOTCOM,
getRunnerLogger(true)
);
@ -293,7 +289,6 @@ test("download codeql bundle from github ae endpoint", async (t) => {
sampleGHAEApiDetails,
tmpDir,
tmpDir,
"runner",
util.GitHubVariant.GHAE,
getRunnerLogger(true)
);
@ -375,3 +370,23 @@ test("getExtraOptions throws for bad content", (t) => {
)
);
});
test.only("getCodeQLActionRepository", (t) => {
const logger = getRunnerLogger(true);
setMode(Mode.actions);
const repoActions = codeql.getCodeQLActionRepository(logger);
t.deepEqual(repoActions, "github/codeql-action");
setMode(Mode.runner);
// isRunningLocalAction() === true
delete process.env["GITHUB_ACTION_REPOSITORY"];
process.env["RUNNER_TEMP"] = path.dirname(__dirname);
const repoLocalRunner = codeql.getCodeQLActionRepository(logger);
t.deepEqual(repoLocalRunner, "github/codeql-action");
process.env["GITHUB_ACTION_REPOSITORY"] = "xxx/yyy";
const repoEnv = codeql.getCodeQLActionRepository(logger);
t.deepEqual(repoEnv, "xxx/yyy");
});

View file

@ -11,7 +11,11 @@ import { default as queryString } from "query-string";
import * as semver from "semver";
import { v4 as uuidV4 } from "uuid";
import { isRunningLocalAction, getRelativeScriptPath } from "./actions-util";
import {
isRunningLocalAction,
getRelativeScriptPath,
isActions,
} from "./actions-util";
import * as api from "./api-client";
import * as defaults from "./defaults.json"; // Referenced from codeql-action-sync-tool!
import { errorMatchers } from "./error-matcher";
@ -144,8 +148,8 @@ function getCodeQLBundleName(): string {
return `codeql-bundle-${platform}.tar.gz`;
}
function getCodeQLActionRepository(mode: util.Mode, logger: Logger): string {
if (mode !== "actions") {
export function getCodeQLActionRepository(logger: Logger): string {
if (isActions()) {
return CODEQL_DEFAULT_ACTION_REPOSITORY;
} else {
return getActionsCodeQLActionRepository(logger);
@ -177,11 +181,10 @@ function getActionsCodeQLActionRepository(logger: Logger): string {
async function getCodeQLBundleDownloadURL(
apiDetails: api.GitHubApiDetails,
mode: util.Mode,
variant: util.GitHubVariant,
logger: Logger
): Promise<string> {
const codeQLActionRepository = getCodeQLActionRepository(mode, logger);
const codeQLActionRepository = getCodeQLActionRepository(logger);
const potentialDownloadSources = [
// This GitHub instance, and this Action.
[apiDetails.url, codeQLActionRepository],
@ -292,7 +295,6 @@ export async function setupCodeQL(
apiDetails: api.GitHubApiDetails,
tempDir: string,
toolCacheDir: string,
mode: util.Mode,
variant: util.GitHubVariant,
logger: Logger
): Promise<{ codeql: CodeQL; toolsVersion: string }> {
@ -313,7 +315,6 @@ export async function setupCodeQL(
let codeqlFolder = toolcache.find(
"CodeQL",
codeqlURLSemVer,
mode,
toolCacheDir,
logger
);
@ -324,7 +325,6 @@ export async function setupCodeQL(
if (!codeqlFolder && !codeqlURL && !forceLatest) {
const codeqlVersions = toolcache.findAllVersions(
"CodeQL",
mode,
toolCacheDir,
logger
);
@ -332,7 +332,6 @@ export async function setupCodeQL(
const tmpCodeqlFolder = toolcache.find(
"CodeQL",
codeqlVersions[0],
mode,
toolCacheDir,
logger
);
@ -351,7 +350,6 @@ export async function setupCodeQL(
if (!codeqlURL) {
codeqlURL = await getCodeQLBundleDownloadURL(
apiDetails,
mode,
variant,
logger
);
@ -386,7 +384,6 @@ export async function setupCodeQL(
const codeqlExtracted = await toolcache.extractTar(
codeqlPath,
mode,
tempDir,
logger
);
@ -394,7 +391,6 @@ export async function setupCodeQL(
codeqlExtracted,
"CodeQL",
codeqlURLSemVer,
mode,
toolCacheDir,
logger
);

View file

@ -1,6 +1,19 @@
import * as core from "@actions/core";
import * as actionsUtil from "./actions-util";
import {
createStatusReportBase,
getOptionalInput,
getRequiredEnvParam,
getRequiredInput,
getTemporaryDirectory,
getToolCacheDirectory,
Mode,
prepareLocalRunEnvironment,
sendStatusReport,
setMode,
StatusReportBase,
validateWorkflow,
} from "./actions-util";
import { CodeQL } from "./codeql";
import * as configUtils from "./config-utils";
import {
@ -15,7 +28,7 @@ import { getActionsLogger } from "./logging";
import { parseRepositoryNwo } from "./repository";
import { checkGitHubVersionInRange, getGitHubVersion } from "./util";
interface InitSuccessStatusReport extends actionsUtil.StatusReportBase {
interface InitSuccessStatusReport extends StatusReportBase {
// Comma-separated list of languages that analysis was run for
// This may be from the workflow file or may be calculated from repository contents
languages: string;
@ -40,14 +53,14 @@ async function sendSuccessStatusReport(
config: configUtils.Config,
toolsVersion: string
) {
const statusReportBase = await actionsUtil.createStatusReportBase(
const statusReportBase = await createStatusReportBase(
"init",
"success",
startedAt
);
const languages = config.languages.join(",");
const workflowLanguages = actionsUtil.getOptionalInput("languages");
const workflowLanguages = getOptionalInput("languages");
const paths = (config.originalUserInput.paths || []).join(",");
const pathsIgnore = (config.originalUserInput["paths-ignore"] || []).join(
","
@ -59,7 +72,7 @@ async function sendSuccessStatusReport(
: "";
const queries: string[] = [];
let queriesInput = actionsUtil.getOptionalInput("queries")?.trim();
let queriesInput = getOptionalInput("queries")?.trim();
if (queriesInput === undefined || queriesInput.startsWith("+")) {
queries.push(
...(config.originalUserInput.queries || []).map((q) => q.uses)
@ -80,37 +93,39 @@ async function sendSuccessStatusReport(
paths_ignore: pathsIgnore,
disable_default_queries: disableDefaultQueries,
queries: queries.join(","),
tools_input: actionsUtil.getOptionalInput("tools") || "",
tools_input: getOptionalInput("tools") || "",
tools_resolved_version: toolsVersion,
};
await actionsUtil.sendStatusReport(statusReport);
await sendStatusReport(statusReport);
}
async function run() {
const startedAt = new Date();
const logger = getActionsLogger();
setMode(Mode.actions);
let config: configUtils.Config;
let codeql: CodeQL;
let toolsVersion: string;
const apiDetails = {
auth: actionsUtil.getRequiredInput("token"),
externalRepoAuth: actionsUtil.getOptionalInput("external-repository-token"),
url: actionsUtil.getRequiredEnvParam("GITHUB_SERVER_URL"),
auth: getRequiredInput("token"),
externalRepoAuth: getOptionalInput("external-repository-token"),
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
};
const gitHubVersion = await getGitHubVersion(apiDetails);
checkGitHubVersionInRange(gitHubVersion, "actions", logger);
checkGitHubVersionInRange(gitHubVersion, logger, Mode.actions);
try {
actionsUtil.prepareLocalRunEnvironment();
prepareLocalRunEnvironment();
const workflowErrors = await actionsUtil.validateWorkflow();
const workflowErrors = await validateWorkflow();
if (
!(await actionsUtil.sendStatusReport(
await actionsUtil.createStatusReportBase(
!(await sendStatusReport(
await createStatusReportBase(
"init",
"starting",
startedAt,
@ -122,11 +137,10 @@ async function run() {
}
const initCodeQLResult = await initCodeQL(
actionsUtil.getOptionalInput("tools"),
getOptionalInput("tools"),
apiDetails,
actionsUtil.getTemporaryDirectory(),
actionsUtil.getToolCacheDirectory(),
"actions",
getTemporaryDirectory(),
getToolCacheDirectory(),
gitHubVersion.type,
logger
);
@ -134,15 +148,15 @@ async function run() {
toolsVersion = initCodeQLResult.toolsVersion;
config = await initConfig(
actionsUtil.getOptionalInput("languages"),
actionsUtil.getOptionalInput("queries"),
actionsUtil.getOptionalInput("config-file"),
actionsUtil.getOptionalInput("db-location"),
parseRepositoryNwo(actionsUtil.getRequiredEnvParam("GITHUB_REPOSITORY")),
actionsUtil.getTemporaryDirectory(),
actionsUtil.getRequiredEnvParam("RUNNER_TOOL_CACHE"),
getOptionalInput("languages"),
getOptionalInput("queries"),
getOptionalInput("config-file"),
getOptionalInput("db-location"),
parseRepositoryNwo(getRequiredEnvParam("GITHUB_REPOSITORY")),
getTemporaryDirectory(),
getRequiredEnvParam("RUNNER_TOOL_CACHE"),
codeql,
actionsUtil.getRequiredEnvParam("GITHUB_WORKSPACE"),
getRequiredEnvParam("GITHUB_WORKSPACE"),
gitHubVersion,
apiDetails,
logger
@ -150,7 +164,7 @@ async function run() {
if (
config.languages.includes(Language.python) &&
actionsUtil.getRequiredInput("setup-python-dependencies") === "true"
getRequiredInput("setup-python-dependencies") === "true"
) {
try {
await installPythonDeps(codeql, logger);
@ -163,13 +177,8 @@ async function run() {
} catch (e) {
core.setFailed(e.message);
console.log(e);
await actionsUtil.sendStatusReport(
await actionsUtil.createStatusReportBase(
"init",
"aborted",
startedAt,
e.message
)
await sendStatusReport(
await createStatusReportBase("init", "aborted", startedAt, e.message)
);
return;
}
@ -209,8 +218,8 @@ async function run() {
} catch (error) {
core.setFailed(error.message);
console.log(error);
await actionsUtil.sendStatusReport(
await actionsUtil.createStatusReportBase(
await sendStatusReport(
await createStatusReportBase(
"init",
"failure",
startedAt,

View file

@ -18,7 +18,6 @@ export async function initCodeQL(
apiDetails: GitHubApiDetails,
tempDir: string,
toolCacheDir: string,
mode: util.Mode,
variant: util.GitHubVariant,
logger: Logger
): Promise<{ codeql: CodeQL; toolsVersion: string }> {
@ -28,7 +27,6 @@ export async function initCodeQL(
apiDetails,
tempDir,
toolCacheDir,
mode,
variant,
logger
);

View file

@ -4,6 +4,7 @@ import * as path from "path";
import { Command } from "commander";
import { Mode, setMode } from "./actions-util";
import { runAnalyze } from "./analyze";
import { determineAutobuildLanguage, runAutobuild } from "./autobuild";
import { CodeQL, getCodeQL } from "./codeql";
@ -149,7 +150,9 @@ program
"(Advanced, windows-only) Inject a windows tracer of this process into a parent process <number> levels up."
)
.action(async (cmd: InitArgs) => {
setMode(Mode.runner);
const logger = getRunnerLogger(cmd.debug);
try {
const tempDir = getTempDir(cmd.tempDir);
const toolsDir = getToolsDir(cmd.toolsDir);
@ -172,7 +175,7 @@ program
};
const gitHubVersion = await getGitHubVersion(apiDetails);
checkGitHubVersionInRange(gitHubVersion, "runner", logger);
checkGitHubVersionInRange(gitHubVersion, logger, Mode.runner);
let codeql: CodeQL;
if (cmd.codeqlPath !== undefined) {
@ -184,7 +187,6 @@ program
apiDetails,
tempDir,
toolsDir,
"runner",
gitHubVersion.type,
logger
)
@ -288,6 +290,8 @@ program
)
.option("--debug", "Print more verbose output", false)
.action(async (cmd: AutobuildArgs) => {
setMode(Mode.runner);
const logger = getRunnerLogger(cmd.debug);
try {
const config = await getConfig(getTempDir(cmd.tempDir), logger);
@ -390,6 +394,7 @@ program
)
.option("--debug", "Print more verbose output", false)
.action(async (cmd: AnalyzeArgs) => {
setMode(Mode.runner);
const logger = getRunnerLogger(cmd.debug);
try {
const config = await getConfig(getTempDir(cmd.tempDir), logger);
@ -493,6 +498,7 @@ program
)
.option("--debug", "Print more verbose output", false)
.action(async (cmd: UploadArgs) => {
setMode(Mode.runner);
const logger = getRunnerLogger(cmd.debug);
const auth = await getGitHubAuth(
logger,

View file

@ -8,8 +8,8 @@ import * as actionsToolcache from "@actions/tool-cache";
import * as safeWhich from "@chrisgavin/safe-which";
import * as semver from "semver";
import { isActions } from "./actions-util";
import { Logger } from "./logging";
import { Mode } from "./util";
/*
* This file acts as an interface to the functionality of the actions toolcache.
@ -33,11 +33,10 @@ import { Mode } from "./util";
*/
export async function extractTar(
file: string,
mode: Mode,
tempDir: string,
logger: Logger
): Promise<string> {
if (mode === "actions") {
if (isActions()) {
return await actionsToolcache.extractTar(file);
} else {
// Initial implementation copied from node_modules/@actions/tool-cache/lib/tool-cache.js
@ -104,11 +103,10 @@ export async function cacheDir(
sourceDir: string,
tool: string,
version: string,
mode: Mode,
toolCacheDir: string,
logger: Logger
): Promise<string> {
if (mode === "actions") {
if (isActions()) {
return await actionsToolcache.cacheDir(sourceDir, tool, version);
} else {
// Initial implementation copied from node_modules/@actions/tool-cache/lib/tool-cache.js
@ -148,11 +146,10 @@ export async function cacheDir(
export function find(
toolName: string,
versionSpec: string,
mode: Mode,
toolCacheDir: string,
logger: Logger
): string {
if (mode === "actions") {
if (isActions()) {
return actionsToolcache.find(toolName, versionSpec);
} else {
// Initial implementation copied from node_modules/@actions/tool-cache/lib/tool-cache.js
@ -166,12 +163,7 @@ export function find(
const arch = os.arch();
// attempt to resolve an explicit version
if (!isExplicitVersion(versionSpec, logger)) {
const localVersions = findAllVersions(
toolName,
mode,
toolCacheDir,
logger
);
const localVersions = findAllVersions(toolName, toolCacheDir, logger);
const match = evaluateVersions(localVersions, versionSpec, logger);
versionSpec = match;
}
@ -198,17 +190,15 @@ export function find(
* Also see findAllVersions function from node_modules/@actions/tool-cache/lib/tool-cache.d.ts
*
* @param toolName name of the tool
* @param mode should run the actions or runner implementation
* @param toolCacheDir path to the tool cache directory
* @param logger logger to use
*/
export function findAllVersions(
toolName: string,
mode: Mode,
toolCacheDir: string,
logger: Logger
): string[] {
if (mode === "actions") {
if (isActions()) {
return actionsToolcache.findAllVersions(toolName);
} else {
// Initial implementation copied from node_modules/@actions/tool-cache/lib/tool-cache.js

View file

@ -3,6 +3,7 @@ import * as path from "path";
import test from "ava";
import { Mode, setMode } from "./actions-util";
import { getRunnerLogger } from "./logging";
import { setupTests } from "./testing-utils";
import * as uploadLib from "./upload-lib";
@ -10,6 +11,10 @@ import { GitHubVersion, GitHubVariant, withTmpDir } from "./util";
setupTests(test);
test.beforeEach(() => {
setMode(Mode.actions);
});
test("validateSarifFileSchema - valid", (t) => {
const inputFile = `${__dirname}/../src/testdata/valid-sarif.sarif`;
t.notThrows(() =>
@ -47,8 +52,7 @@ test("validate correct payload used per version", async (t) => {
"/opt/src",
undefined,
["CodeQL", "eslint"],
version,
"actions"
version
);
// Not triggered by a pull request
t.falsy(payload.base_ref);
@ -70,8 +74,7 @@ test("validate correct payload used per version", async (t) => {
"/opt/src",
undefined,
["CodeQL", "eslint"],
version,
"actions"
version
);
t.deepEqual(payload.base_ref, "refs/heads/master");
t.deepEqual(payload.base_sha, "f95f852bd8fca8fcc58a9a2d6c842781e32a215e");
@ -88,8 +91,7 @@ test("validate correct payload used per version", async (t) => {
"/opt/src",
undefined,
["CodeQL", "eslint"],
version,
"actions"
version
);
// These older versions won't expect these values
t.falsy(payload.base_ref);

View file

@ -87,7 +87,6 @@ async function uploadPayload(
payload: any,
repositoryNwo: RepositoryNwo,
apiDetails: api.GitHubApiDetails,
mode: util.Mode,
logger: Logger
) {
logger.info("Uploading results");
@ -100,10 +99,9 @@ async function uploadPayload(
const client = api.getApiClient(apiDetails);
const reqURL =
mode === "actions"
? "PUT /repos/:owner/:repo/code-scanning/analysis"
: "POST /repos/:owner/:repo/code-scanning/sarifs";
const reqURL = actionsUtil.isActions()
? "PUT /repos/:owner/:repo/code-scanning/analysis"
: "POST /repos/:owner/:repo/code-scanning/sarifs";
const response = await client.request(reqURL, {
owner: repositoryNwo.owner,
repo: repositoryNwo.repo,
@ -163,7 +161,6 @@ export async function uploadFromActions(
actionsUtil.getRequiredInput("matrix"),
gitHubVersion,
apiDetails,
"actions",
logger
);
}
@ -195,7 +192,6 @@ export async function uploadFromRunner(
undefined,
gitHubVersion,
apiDetails,
"runner",
logger
);
}
@ -277,10 +273,9 @@ export function buildPayload(
checkoutURI: string,
environment: string | undefined,
toolNames: string[],
gitHubVersion: util.GitHubVersion,
mode: util.Mode
gitHubVersion: util.GitHubVersion
) {
if (mode === "actions") {
if (actionsUtil.isActions()) {
const payloadObj = {
commit_oid: commitOid,
ref,
@ -339,13 +334,12 @@ async function uploadFiles(
environment: string | undefined,
gitHubVersion: util.GitHubVersion,
apiDetails: api.GitHubApiDetails,
mode: util.Mode,
logger: Logger
): Promise<UploadStatusReport> {
logger.startGroup("Uploading results");
logger.info(`Processing sarif files: ${JSON.stringify(sarifFiles)}`);
if (mode === "actions") {
if (actionsUtil.isActions()) {
// This check only works on actions as env vars don't persist between calls to the runner
const sentinelEnvVar = "CODEQL_UPLOAD_SARIF";
if (process.env[sentinelEnvVar]) {
@ -389,8 +383,7 @@ async function uploadFiles(
checkoutURI,
environment,
toolNames,
gitHubVersion,
mode
gitHubVersion
);
// Log some useful debug info about the info
@ -402,7 +395,7 @@ async function uploadFiles(
logger.debug(`Number of results in upload: ${numResultInSarif}`);
// Make the upload
await uploadPayload(payload, repositoryNwo, apiDetails, mode, logger);
await uploadPayload(payload, repositoryNwo, apiDetails, logger);
logger.endGroup();

View file

@ -26,6 +26,7 @@ async function sendSuccessStatusReport(
}
async function run() {
actionsUtil.setMode(actionsUtil.Mode.actions);
const startedAt = new Date();
if (
!(await actionsUtil.sendStatusReport(

View file

@ -6,17 +6,13 @@ import { Readable } from "stream";
import * as core from "@actions/core";
import * as semver from "semver";
import { isActions, Mode } from "./actions-util";
import { getApiClient, GitHubApiDetails } from "./api-client";
import * as apiCompatibility from "./api-compatibility.json";
import { Config } from "./config-utils";
import { Language } from "./languages";
import { Logger } from "./logging";
/**
* Are we running on actions, or not.
*/
export type Mode = "actions" | "runner";
/**
* The URL for github.com.
*/
@ -225,6 +221,7 @@ export function parseGitHubUrl(inputUrl: string): string {
const GITHUB_ENTERPRISE_VERSION_HEADER = "x-github-enterprise-version";
const CODEQL_ACTION_WARNED_ABOUT_VERSION_ENV_VAR =
"CODEQL_ACTION_WARNED_ABOUT_VERSION";
let hasBeenWarnedAboutVersion = false;
export enum GitHubVariant {
@ -266,8 +263,8 @@ export async function getGitHubVersion(
export function checkGitHubVersionInRange(
version: GitHubVersion,
mode: Mode,
logger: Logger
logger: Logger,
toolName: Mode
) {
if (hasBeenWarnedAboutVersion || version.type !== GitHubVariant.GHES) {
return;
@ -279,8 +276,6 @@ export function checkGitHubVersionInRange(
apiCompatibility.maximumVersion
);
const toolName = mode === "actions" ? "Action" : "Runner";
if (
disallowedAPIVersionReason === DisallowedAPIVersionReason.ACTION_TOO_OLD
) {
@ -296,7 +291,7 @@ export function checkGitHubVersionInRange(
);
}
hasBeenWarnedAboutVersion = true;
if (mode === "actions") {
if (isActions()) {
core.exportVariable(CODEQL_ACTION_WARNED_ABOUT_VERSION_ENV_VAR, true);
}
}