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

@ -34,26 +34,6 @@ Only run `npm install` if you are explicitly changing the set of dependencies in
To see the effect of your changes and to test them, push your changes in a branch and then look at the [Actions output](https://github.com/github/codeql-action/actions) for that branch. You can also exercise the code locally by running the automated tests. To see the effect of your changes and to test them, push your changes in a branch and then look at the [Actions output](https://github.com/github/codeql-action/actions) for that branch. You can also exercise the code locally by running the automated tests.
### Running the action locally
It is possible to run this action locally via [act](https://github.com/nektos/act) via the following steps:
1. Create a GitHub [Personal Access Token](https://github.com/settings/tokens) (PAT).
1. Install [act](https://github.com/nektos/act) v0.2.10 or greater.
1. Add a `.env` file in the root of the project you are running:
```bash
CODEQL_LOCAL_RUN=true
GITHUB_SERVER_URL=https://github.com
# Optional, for better logging
GITHUB_JOB=<ANY_JOB_NAME>
```
1. Run `act -j codeql -s GITHUB_TOKEN=<PAT>`
Running locally will generate the CodeQL database and run all the queries, but it will avoid uploading and reporting results to GitHub. Note that this must be done on a repository that _consumes_ this action, not this repository. The use case is to debug failures of this action on specific repositories.
### Integration tests ### Integration tests
As well as the unit tests (see _Common tasks_ above), there are integration tests, defined in `.github/workflows/integration-testing.yml`. These are run by a CI check. Depending on the change youre making, you may want to add a test to this file or extend an existing one. As well as the unit tests (see _Common tasks_ above), there are integration tests, defined in `.github/workflows/integration-testing.yml`. These are run by a CI check. Depending on the change youre making, you may want to add a test to this file or extend an existing one.

23
lib/actions-util.js generated
View file

@ -50,22 +50,6 @@ function getToolCacheDirectory() {
: util_1.getRequiredEnvParam("RUNNER_TOOL_CACHE"); : util_1.getRequiredEnvParam("RUNNER_TOOL_CACHE");
} }
exports.getToolCacheDirectory = getToolCacheDirectory; exports.getToolCacheDirectory = getToolCacheDirectory;
/**
* Ensures all required environment variables are set in the context of a local run.
*/
function prepareLocalRunEnvironment() {
if (!util_1.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}`);
}
}
exports.prepareLocalRunEnvironment = prepareLocalRunEnvironment;
/** /**
* Gets the SHA of the commit that is currently checked out. * Gets the SHA of the commit that is currently checked out.
*/ */
@ -291,9 +275,6 @@ exports.getWorkflow = getWorkflow;
* Get the path of the currently executing workflow. * Get the path of the currently executing workflow.
*/ */
async function getWorkflowPath() { async function getWorkflowPath() {
if (util_1.isLocalRun()) {
return util_1.getRequiredEnvParam("WORKFLOW_PATH");
}
const repo_nwo = util_1.getRequiredEnvParam("GITHUB_REPOSITORY").split("/"); const repo_nwo = util_1.getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
const owner = repo_nwo[0]; const owner = repo_nwo[0];
const repo = repo_nwo[1]; const repo = repo_nwo[1];
@ -478,10 +459,6 @@ const INCOMPATIBLE_MSG = "CodeQL Action version is incompatible with the code sc
* Returns whether sending the status report was successful of not. * Returns whether sending the status report was successful of not.
*/ */
async function sendStatusReport(statusReport) { async function sendStatusReport(statusReport) {
if (util_1.isLocalRun()) {
core.debug("Not sending status report because this is a local run");
return true;
}
const statusReportJSON = JSON.stringify(statusReport); const statusReportJSON = JSON.stringify(statusReport);
core.debug(`Sending status report: ${statusReportJSON}`); core.debug(`Sending status report: ${statusReportJSON}`);
const nwo = util_1.getRequiredEnvParam("GITHUB_REPOSITORY"); const nwo = util_1.getRequiredEnvParam("GITHUB_REPOSITORY");

File diff suppressed because one or more lines are too long

View file

@ -57,14 +57,6 @@ ava_1.default("getRef() returns head PR ref if GITHUB_REF no longer checked out"
t.deepEqual(actualRef, "refs/pull/1/head"); t.deepEqual(actualRef, "refs/pull/1/head");
callback.restore(); callback.restore();
}); });
ava_1.default("getAnalysisKey() when a local run", async (t) => {
process.env.CODEQL_LOCAL_RUN = "true";
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
process.env.GITHUB_JOB = "";
util_1.initializeEnvironment(util_1.Mode.actions, "1.2.3");
const actualAnalysisKey = await actionsutil.getAnalysisKey();
t.deepEqual(actualAnalysisKey, "LOCAL-RUN:UNKNOWN-JOB");
});
ava_1.default("computeAutomationID()", async (t) => { ava_1.default("computeAutomationID()", async (t) => {
let actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", '{"language": "javascript", "os": "linux"}'); let actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", '{"language": "javascript", "os": "linux"}');
t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/"); t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/language:javascript/os:linux/");
@ -81,37 +73,6 @@ ava_1.default("computeAutomationID()", async (t) => {
actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", undefined); actualAutomationID = actionsutil.computeAutomationID(".github/workflows/codeql-analysis.yml:analyze", undefined);
t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/"); t.deepEqual(actualAutomationID, ".github/workflows/codeql-analysis.yml:analyze/");
}); });
ava_1.default("prepareEnvironment() when a local run", (t) => {
process.env.CODEQL_LOCAL_RUN = "false";
process.env.GITHUB_JOB = "YYY";
process.env.CODEQL_ACTION_ANALYSIS_KEY = "TEST";
util_1.initializeEnvironment(util_1.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";
util_1.initializeEnvironment(util_1.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 = "";
util_1.initializeEnvironment(util_1.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 = "";
util_1.initializeEnvironment(util_1.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 = "";
util_1.initializeEnvironment(util_1.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, "");
});
ava_1.default("getWorkflowErrors() when on is empty", (t) => { ava_1.default("getWorkflowErrors() when on is empty", (t) => {
const errors = actionsutil.getWorkflowErrors({ on: {} }); const errors = actionsutil.getWorkflowErrors({ on: {} });
t.deepEqual(...errorCodes(errors, [])); t.deepEqual(...errorCodes(errors, []));

File diff suppressed because one or more lines are too long

9
lib/api-client.js generated
View file

@ -22,10 +22,7 @@ var DisallowedAPIVersionReason;
DisallowedAPIVersionReason[DisallowedAPIVersionReason["ACTION_TOO_OLD"] = 0] = "ACTION_TOO_OLD"; DisallowedAPIVersionReason[DisallowedAPIVersionReason["ACTION_TOO_OLD"] = 0] = "ACTION_TOO_OLD";
DisallowedAPIVersionReason[DisallowedAPIVersionReason["ACTION_TOO_NEW"] = 1] = "ACTION_TOO_NEW"; DisallowedAPIVersionReason[DisallowedAPIVersionReason["ACTION_TOO_NEW"] = 1] = "ACTION_TOO_NEW";
})(DisallowedAPIVersionReason = exports.DisallowedAPIVersionReason || (exports.DisallowedAPIVersionReason = {})); })(DisallowedAPIVersionReason = exports.DisallowedAPIVersionReason || (exports.DisallowedAPIVersionReason = {}));
exports.getApiClient = function (apiDetails, { allowLocalRun = false, allowExternal = false } = {}) { exports.getApiClient = function (apiDetails, { allowExternal = false } = {}) {
if (util_1.isLocalRun() && !allowLocalRun) {
throw new Error("Invalid API call in local run");
}
const auth = (allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth; const auth = (allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
return new githubUtils.GitHub(githubUtils.getOctokitOptions(auth, { return new githubUtils.GitHub(githubUtils.getOctokitOptions(auth, {
baseUrl: getApiUrl(apiDetails.url), baseUrl: getApiUrl(apiDetails.url),
@ -47,12 +44,12 @@ function getApiUrl(githubUrl) {
// Temporary function to aid in the transition to running on and off of github actions. // 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 // Once all code has been converted this function should be removed or made canonical
// and called only from the action entrypoints. // and called only from the action entrypoints.
function getActionsApiClient(allowLocalRun = false) { function getActionsApiClient() {
const apiDetails = { const apiDetails = {
auth: actions_util_1.getRequiredInput("token"), auth: actions_util_1.getRequiredInput("token"),
url: util_1.getRequiredEnvParam("GITHUB_SERVER_URL"), url: util_1.getRequiredEnvParam("GITHUB_SERVER_URL"),
}; };
return exports.getApiClient(apiDetails, { allowLocalRun }); return exports.getApiClient(apiDetails);
} }
exports.getActionsApiClient = getActionsApiClient; exports.getActionsApiClient = getActionsApiClient;
//# sourceMappingURL=api-client.js.map //# sourceMappingURL=api-client.js.map

View file

@ -1 +1 @@
{"version":3,"file":"api-client.js","sourceRoot":"","sources":["../src/api-client.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA6B;AAE7B,uEAAyD;AACzD,0EAAgD;AAEhD,iDAAkD;AAClD,iCAAkE;AAElE,8CAA8C;AAC9C,MAAM,GAAG,GAAG,OAAO,CAAC,iBAAiB,CAAC,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;IAE/B,uDAAuD;IACvD,0CAA0C;IAC1C,IAAI,GAAG,CAAC,QAAQ,KAAK,YAAY,IAAI,GAAG,CAAC,QAAQ,KAAK,gBAAgB,EAAE;QACtE,OAAO,wBAAwB,CAAC;KACjC;IAED,6BAA6B;IAC7B,GAAG,CAAC,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IACpD,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;AACxB,CAAC;AAED,uFAAuF;AACvF,qFAAqF;AACrF,+CAA+C;AAC/C,SAAgB,mBAAmB;IACjC,MAAM,UAAU,GAAG;QACjB,IAAI,EAAE,+BAAgB,CAAC,OAAO,CAAC;QAC/B,GAAG,EAAE,0BAAmB,CAAC,mBAAmB,CAAC;KAC9C,CAAC;IAEF,OAAO,oBAAY,CAAC,UAAU,CAAC,CAAC;AAClC,CAAC;AAPD,kDAOC"}

6
lib/config-utils.js generated
View file

@ -306,9 +306,7 @@ exports.getUnknownLanguagesError = getUnknownLanguagesError;
*/ */
async function getLanguagesInRepo(repository, apiDetails, logger) { async function getLanguagesInRepo(repository, apiDetails, logger) {
logger.debug(`GitHub repo ${repository.owner} ${repository.repo}`); logger.debug(`GitHub repo ${repository.owner} ${repository.repo}`);
const response = await api const response = await api.getApiClient(apiDetails).repos.listLanguages({
.getApiClient(apiDetails, { allowLocalRun: true })
.repos.listLanguages({
owner: repository.owner, owner: repository.owner,
repo: repository.repo, repo: repository.repo,
}); });
@ -579,7 +577,7 @@ async function getRemoteConfig(configFile, apiDetails) {
throw new Error(getConfigFileRepoFormatInvalidMessage(configFile)); throw new Error(getConfigFileRepoFormatInvalidMessage(configFile));
} }
const response = await api const response = await api
.getApiClient(apiDetails, { allowLocalRun: true, allowExternal: true }) .getApiClient(apiDetails, { allowExternal: true })
.repos.getContent({ .repos.getContent({
owner: pieces.groups.owner, owner: pieces.groups.owner,
repo: pieces.groups.repo, repo: pieces.groups.repo,

File diff suppressed because one or more lines are too long

10
lib/util.js generated
View file

@ -12,7 +12,6 @@ const os = __importStar(require("os"));
const path = __importStar(require("path")); const path = __importStar(require("path"));
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const semver = __importStar(require("semver")); const semver = __importStar(require("semver"));
const actions_util_1 = require("./actions-util");
const api_client_1 = require("./api-client"); const api_client_1 = require("./api-client");
const apiCompatibility = __importStar(require("./api-compatibility.json")); const apiCompatibility = __importStar(require("./api-compatibility.json"));
/** /**
@ -36,14 +35,6 @@ function getExtraOptionsEnvParam() {
} }
} }
exports.getExtraOptionsEnvParam = getExtraOptionsEnvParam; exports.getExtraOptionsEnvParam = getExtraOptionsEnvParam;
function isLocalRun() {
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());
}
exports.isLocalRun = isLocalRun;
/** /**
* Get the array of all the tool names contained in the given sarif contents. * Get the array of all the tool names contained in the given sarif contents.
* *
@ -371,7 +362,6 @@ function initializeEnvironment(mode, version) {
core.exportVariable(EnvVar.FEATURE_WILL_UPLOAD, "true"); core.exportVariable(EnvVar.FEATURE_WILL_UPLOAD, "true");
core.exportVariable(EnvVar.FEATURE_MULTI_LANGUAGE, "true"); core.exportVariable(EnvVar.FEATURE_MULTI_LANGUAGE, "true");
core.exportVariable(EnvVar.FEATURE_SANDWICH, "true"); core.exportVariable(EnvVar.FEATURE_SANDWICH, "true");
actions_util_1.prepareLocalRunEnvironment();
} }
else { else {
process.env[EnvVar.RUN_MODE] = mode; process.env[EnvVar.RUN_MODE] = mode;

File diff suppressed because one or more lines are too long

16
lib/util.test.js generated
View file

@ -19,7 +19,6 @@ const sinon_1 = __importDefault(require("sinon"));
const api = __importStar(require("./api-client")); const api = __importStar(require("./api-client"));
const logging_1 = require("./logging"); const logging_1 = require("./logging");
const testing_utils_1 = require("./testing-utils"); const testing_utils_1 = require("./testing-utils");
const util_1 = require("./util");
const util = __importStar(require("./util")); const util = __importStar(require("./util"));
testing_utils_1.setupTests(ava_1.default); testing_utils_1.setupTests(ava_1.default);
ava_1.default("getToolNames", (t) => { ava_1.default("getToolNames", (t) => {
@ -71,21 +70,6 @@ ava_1.default("getThreadsFlag() should return the correct --threads flag", (t) =
ava_1.default("getThreadsFlag() throws if the threads input is not an integer", (t) => { ava_1.default("getThreadsFlag() throws if the threads input is not an integer", (t) => {
t.throws(() => util.getThreadsFlag("hello!", logging_1.getRunnerLogger(true))); t.throws(() => util.getThreadsFlag("hello!", logging_1.getRunnerLogger(true)));
}); });
ava_1.default("isLocalRun() runs correctly", (t) => {
util_1.initializeEnvironment(util_1.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());
util_1.initializeEnvironment(util_1.Mode.runner, "1.2.3");
t.assert(!util.isLocalRun());
});
ava_1.default("getExtraOptionsEnvParam() succeeds on valid JSON with invalid options (for now)", (t) => { ava_1.default("getExtraOptionsEnvParam() succeeds on valid JSON with invalid options (for now)", (t) => {
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS; const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS;
const options = { foo: 42 }; const options = { foo: 42 };

File diff suppressed because one or more lines are too long

View file

@ -62,18 +62,6 @@ test("getRef() returns head PR ref if GITHUB_REF no longer checked out", async (
callback.restore(); 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) => { test("computeAutomationID()", async (t) => {
let actualAutomationID = actionsutil.computeAutomationID( let actualAutomationID = actionsutil.computeAutomationID(
".github/workflows/codeql-analysis.yml:analyze", ".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) => { test("getWorkflowErrors() when on is empty", (t) => {
const errors = actionsutil.getWorkflowErrors({ on: {} }); 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 api from "./api-client";
import * as sharedEnv from "./shared-environment"; 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. * 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"); : 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. * 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. * Get the path of the currently executing workflow.
*/ */
async function getWorkflowPath(): Promise<string> { async function getWorkflowPath(): Promise<string> {
if (isLocalRun()) {
return getRequiredEnvParam("WORKFLOW_PATH");
}
const repo_nwo = getRequiredEnvParam("GITHUB_REPOSITORY").split("/"); const repo_nwo = getRequiredEnvParam("GITHUB_REPOSITORY").split("/");
const owner = repo_nwo[0]; const owner = repo_nwo[0];
const repo = repo_nwo[1]; const repo = repo_nwo[1];
@ -623,11 +599,6 @@ const INCOMPATIBLE_MSG =
export async function sendStatusReport<S extends StatusReportBase>( export async function sendStatusReport<S extends StatusReportBase>(
statusReport: S statusReport: S
): Promise<boolean> { ): Promise<boolean> {
if (isLocalRun()) {
core.debug("Not sending status report because this is a local run");
return true;
}
const statusReportJSON = JSON.stringify(statusReport); const statusReportJSON = JSON.stringify(statusReport);
core.debug(`Sending status report: ${statusReportJSON}`); 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 consoleLogLevel from "console-log-level";
import { getRequiredInput } from "./actions-util"; import { getRequiredInput } from "./actions-util";
import { getMode, getRequiredEnvParam, isLocalRun } from "./util"; import { getMode, getRequiredEnvParam } from "./util";
// eslint-disable-next-line import/no-commonjs // eslint-disable-next-line import/no-commonjs
const pkg = require("../package.json"); const pkg = require("../package.json");
@ -29,12 +29,8 @@ export interface GitHubApiExternalRepoDetails {
export const getApiClient = function ( export const getApiClient = function (
apiDetails: GitHubApiCombinedDetails, apiDetails: GitHubApiCombinedDetails,
{ allowLocalRun = false, allowExternal = false } = {} { allowExternal = false } = {}
) { ) {
if (isLocalRun() && !allowLocalRun) {
throw new Error("Invalid API call in local run");
}
const auth = const auth =
(allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth; (allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
return new githubUtils.GitHub( 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. // 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 // Once all code has been converted this function should be removed or made canonical
// and called only from the action entrypoints. // and called only from the action entrypoints.
export function getActionsApiClient(allowLocalRun = false) { export function getActionsApiClient() {
const apiDetails = { const apiDetails = {
auth: getRequiredInput("token"), auth: getRequiredInput("token"),
url: getRequiredEnvParam("GITHUB_SERVER_URL"), url: getRequiredEnvParam("GITHUB_SERVER_URL"),
}; };
return getApiClient(apiDetails, { allowLocalRun }); return getApiClient(apiDetails);
} }

View file

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

View file

@ -9,7 +9,6 @@ import sinon from "sinon";
import * as api from "./api-client"; import * as api from "./api-client";
import { getRunnerLogger, Logger } from "./logging"; import { getRunnerLogger, Logger } from "./logging";
import { setupTests } from "./testing-utils"; import { setupTests } from "./testing-utils";
import { initializeEnvironment, Mode } from "./util";
import * as util from "./util"; import * as util from "./util";
setupTests(test); 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))); 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) => { test("getExtraOptionsEnvParam() succeeds on valid JSON with invalid options (for now)", (t) => {
const origExtraOptions = process.env.CODEQL_ACTION_EXTRA_OPTIONS; 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 core from "@actions/core";
import * as semver from "semver"; import * as semver from "semver";
import { prepareLocalRunEnvironment } from "./actions-util";
import { getApiClient, GitHubApiDetails } from "./api-client"; import { getApiClient, GitHubApiDetails } from "./api-client";
import * as apiCompatibility from "./api-compatibility.json"; import * as apiCompatibility from "./api-compatibility.json";
import { Config } from "./config-utils"; 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. * 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_MULTI_LANGUAGE, "true");
core.exportVariable(EnvVar.FEATURE_SANDWICH, "true"); core.exportVariable(EnvVar.FEATURE_SANDWICH, "true");
prepareLocalRunEnvironment();
} else { } else {
process.env[EnvVar.RUN_MODE] = mode; process.env[EnvVar.RUN_MODE] = mode;
process.env[EnvVar.VERSION] = version; process.env[EnvVar.VERSION] = version;