Remove local environment running

This is a functionality that never worked perfectly and hasn't been
used for a while.

This allows developers to run the action on their local machine, but
the run was always flaky and never 100% mirrored what was happening on
the actions runner.
This commit is contained in:
Andrew Eisenberg
2021-06-01 15:04:15 -07:00
parent 3708898bf2
commit 2c2ebdc5c5
19 changed files with 21 additions and 262 deletions

View File

@@ -62,18 +62,6 @@ test("getRef() returns head PR ref if GITHUB_REF no longer checked out", async (
callback.restore();
});
test("getAnalysisKey() when a local run", async (t) => {
process.env.CODEQL_LOCAL_RUN = "true";
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
process.env.GITHUB_JOB = "";
initializeEnvironment(Mode.actions, "1.2.3");
const actualAnalysisKey = await actionsutil.getAnalysisKey();
t.deepEqual(actualAnalysisKey, "LOCAL-RUN:UNKNOWN-JOB");
});
test("computeAutomationID()", async (t) => {
let actualAutomationID = actionsutil.computeAutomationID(
".github/workflows/codeql-analysis.yml:analyze",
@@ -125,52 +113,6 @@ test("computeAutomationID()", async (t) => {
);
});
test("prepareEnvironment() when a local run", (t) => {
process.env.CODEQL_LOCAL_RUN = "false";
process.env.GITHUB_JOB = "YYY";
process.env.CODEQL_ACTION_ANALYSIS_KEY = "TEST";
initializeEnvironment(Mode.actions, "1.2.3");
// unchanged
t.deepEqual(process.env.GITHUB_JOB, "YYY");
t.deepEqual(process.env.CODEQL_ACTION_ANALYSIS_KEY, "TEST");
process.env.CODEQL_LOCAL_RUN = "true";
initializeEnvironment(Mode.actions, "1.2.3");
// unchanged
t.deepEqual(process.env.GITHUB_JOB, "YYY");
t.deepEqual(process.env.CODEQL_ACTION_ANALYSIS_KEY, "TEST");
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
initializeEnvironment(Mode.actions, "1.2.3");
// updated
t.deepEqual(process.env.GITHUB_JOB, "YYY");
t.deepEqual(process.env.CODEQL_ACTION_ANALYSIS_KEY, "LOCAL-RUN:YYY");
process.env.GITHUB_JOB = "";
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
initializeEnvironment(Mode.actions, "1.2.3");
// updated
t.deepEqual(process.env.GITHUB_JOB, "UNKNOWN-JOB");
t.deepEqual(process.env.CODEQL_ACTION_ANALYSIS_KEY, "LOCAL-RUN:UNKNOWN-JOB");
process.env.GITHUB_JOB = "";
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
initializeEnvironment(Mode.runner, "1.2.3");
// unchanged. local runs not allowed for runner
t.deepEqual(process.env.GITHUB_JOB, "");
t.deepEqual(process.env.CODEQL_ACTION_ANALYSIS_KEY, "");
});
test("getWorkflowErrors() when on is empty", (t) => {
const errors = actionsutil.getWorkflowErrors({ on: {} });

View File

@@ -8,7 +8,7 @@ import * as yaml from "js-yaml";
import * as api from "./api-client";
import * as sharedEnv from "./shared-environment";
import { getRequiredEnvParam, GITHUB_DOTCOM_URL, isLocalRun } from "./util";
import { getRequiredEnvParam, GITHUB_DOTCOM_URL } from "./util";
/**
* The utils in this module are meant to be run inside of the action only.
@@ -45,26 +45,6 @@ export function getToolCacheDirectory(): string {
: getRequiredEnvParam("RUNNER_TOOL_CACHE");
}
/**
* Ensures all required environment variables are set in the context of a local run.
*/
export function prepareLocalRunEnvironment() {
if (!isLocalRun()) {
return;
}
core.debug("Action is running locally.");
if (!process.env.GITHUB_JOB) {
core.exportVariable("GITHUB_JOB", "UNKNOWN-JOB");
}
if (!process.env.CODEQL_ACTION_ANALYSIS_KEY) {
core.exportVariable(
"CODEQL_ACTION_ANALYSIS_KEY",
`LOCAL-RUN:${process.env.GITHUB_JOB}`
);
}
}
/**
* Gets the SHA of the commit that is currently checked out.
*/
@@ -352,10 +332,6 @@ export async function getWorkflow(): Promise<Workflow> {
* Get the path of the currently executing workflow.
*/
async function getWorkflowPath(): Promise<string> {
if (isLocalRun()) {
return getRequiredEnvParam("WORKFLOW_PATH");
}
const repo_nwo = getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
const owner = repo_nwo[0];
const repo = repo_nwo[1];
@@ -623,11 +599,6 @@ const INCOMPATIBLE_MSG =
export async function sendStatusReport<S extends StatusReportBase>(
statusReport: S
): Promise<boolean> {
if (isLocalRun()) {
core.debug("Not sending status report because this is a local run");
return true;
}
const statusReportJSON = JSON.stringify(statusReport);
core.debug(`Sending status report: ${statusReportJSON}`);

View File

@@ -4,7 +4,7 @@ import * as githubUtils from "@actions/github/lib/utils";
import consoleLogLevel from "console-log-level";
import { getRequiredInput } from "./actions-util";
import { getMode, getRequiredEnvParam, isLocalRun } from "./util";
import { getMode, getRequiredEnvParam } from "./util";
// eslint-disable-next-line import/no-commonjs
const pkg = require("../package.json");
@@ -29,12 +29,8 @@ export interface GitHubApiExternalRepoDetails {
export const getApiClient = function (
apiDetails: GitHubApiCombinedDetails,
{ allowLocalRun = false, allowExternal = false } = {}
{ allowExternal = false } = {}
) {
if (isLocalRun() && !allowLocalRun) {
throw new Error("Invalid API call in local run");
}
const auth =
(allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
return new githubUtils.GitHub(
@@ -63,11 +59,11 @@ function getApiUrl(githubUrl: string): string {
// Temporary function to aid in the transition to running on and off of github actions.
// Once all code has been converted this function should be removed or made canonical
// and called only from the action entrypoints.
export function getActionsApiClient(allowLocalRun = false) {
export function getActionsApiClient() {
const apiDetails = {
auth: getRequiredInput("token"),
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
};
return getApiClient(apiDetails, { allowLocalRun });
return getApiClient(apiDetails);
}

View File

@@ -619,12 +619,10 @@ async function getLanguagesInRepo(
logger: Logger
): Promise<Language[]> {
logger.debug(`GitHub repo ${repository.owner} ${repository.repo}`);
const response = await api
.getApiClient(apiDetails, { allowLocalRun: true })
.repos.listLanguages({
owner: repository.owner,
repo: repository.repo,
});
const response = await api.getApiClient(apiDetails).repos.listLanguages({
owner: repository.owner,
repo: repository.repo,
});
logger.debug(`Languages API response: ${JSON.stringify(response)}`);
@@ -1076,7 +1074,7 @@ async function getRemoteConfig(
}
const response = await api
.getApiClient(apiDetails, { allowLocalRun: true, allowExternal: true })
.getApiClient(apiDetails, { allowExternal: true })
.repos.getContent({
owner: pieces.groups.owner,
repo: pieces.groups.repo,

View File

@@ -9,7 +9,6 @@ import sinon from "sinon";
import * as api from "./api-client";
import { getRunnerLogger, Logger } from "./logging";
import { setupTests } from "./testing-utils";
import { initializeEnvironment, Mode } from "./util";
import * as util from "./util";
setupTests(test);
@@ -77,28 +76,6 @@ test("getThreadsFlag() throws if the threads input is not an integer", (t) => {
t.throws(() => util.getThreadsFlag("hello!", getRunnerLogger(true)));
});
test("isLocalRun() runs correctly", (t) => {
initializeEnvironment(Mode.actions, "1.2.3");
process.env.CODEQL_LOCAL_RUN = "";
t.assert(!util.isLocalRun());
process.env.CODEQL_LOCAL_RUN = "false";
t.assert(!util.isLocalRun());
process.env.CODEQL_LOCAL_RUN = "0";
t.assert(!util.isLocalRun());
process.env.CODEQL_LOCAL_RUN = "true";
t.assert(util.isLocalRun());
process.env.CODEQL_LOCAL_RUN = "hucairz";
t.assert(util.isLocalRun());
initializeEnvironment(Mode.runner, "1.2.3");
t.assert(!util.isLocalRun());
});
test("getExtraOptionsEnvParam() succeeds on valid JSON with invalid options (for now)", (t) => {
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;

View File

@@ -6,7 +6,6 @@ import { Readable } from "stream";
import * as core from "@actions/core";
import * as semver from "semver";
import { prepareLocalRunEnvironment } from "./actions-util";
import { getApiClient, GitHubApiDetails } from "./api-client";
import * as apiCompatibility from "./api-compatibility.json";
import { Config } from "./config-utils";
@@ -36,16 +35,6 @@ export function getExtraOptionsEnvParam(): object {
}
}
export function isLocalRun(): boolean {
return (
!!process.env.CODEQL_LOCAL_RUN &&
process.env.CODEQL_LOCAL_RUN !== "false" &&
process.env.CODEQL_LOCAL_RUN !== "0" &&
// local runs only allowed for actions
isActions()
);
}
/**
* Get the array of all the tool names contained in the given sarif contents.
*
@@ -444,7 +433,6 @@ export function initializeEnvironment(mode: Mode, version: string) {
core.exportVariable(EnvVar.FEATURE_MULTI_LANGUAGE, "true");
core.exportVariable(EnvVar.FEATURE_SANDWICH, "true");
prepareLocalRunEnvironment();
} else {
process.env[EnvVar.RUN_MODE] = mode;
process.env[EnvVar.VERSION] = version;