Add environment variables to signal feature and version to the CLI

This PR ensures environment variables are set before any invocation of
the CLI.  Here is a list of vars that are set:

https://github.com/github/codeql-coreql-team/issues/1124#issuecomment-852463521

This ensures the CLI knows the features and versions of the containing
actions/runner.

Additionally:

- Fix the user agent so that it more closely aligns with user agent
  spec
- Refactor environment variable initialization so that it all happens in
  one place and call.
- Move Mode, getRequiredEnvParam, setMode, getMode out of actions-util
  and into util. actions-util is meant for utils only called by the
  action, not the runner.

The `prepareLocalRunEnvironment()` method is most likely deprecated and
should be removed. I originally added it because I had a way of working
where I would run the action from my local machine to test out changes,
but this was always a little flaky. So, I no longer use this way of
working. I will probably remove it soon.
This commit is contained in:
Andrew Eisenberg
2021-06-01 14:49:07 -07:00
parent 539d968ad7
commit 3708898bf2
48 changed files with 387 additions and 250 deletions

View File

@@ -4,6 +4,7 @@ import sinon from "sinon";
import * as actionsutil from "./actions-util";
import { setupTests } from "./testing-utils";
import { getMode, initializeEnvironment, Mode } from "./util";
function errorCodes(
actual: actionsutil.CodedError[],
@@ -66,7 +67,7 @@ test("getAnalysisKey() when a local run", async (t) => {
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
process.env.GITHUB_JOB = "";
actionsutil.prepareLocalRunEnvironment();
initializeEnvironment(Mode.actions, "1.2.3");
const actualAnalysisKey = await actionsutil.getAnalysisKey();
@@ -129,7 +130,7 @@ test("prepareEnvironment() when a local run", (t) => {
process.env.GITHUB_JOB = "YYY";
process.env.CODEQL_ACTION_ANALYSIS_KEY = "TEST";
actionsutil.prepareLocalRunEnvironment();
initializeEnvironment(Mode.actions, "1.2.3");
// unchanged
t.deepEqual(process.env.GITHUB_JOB, "YYY");
@@ -137,7 +138,7 @@ test("prepareEnvironment() when a local run", (t) => {
process.env.CODEQL_LOCAL_RUN = "true";
actionsutil.prepareLocalRunEnvironment();
initializeEnvironment(Mode.actions, "1.2.3");
// unchanged
t.deepEqual(process.env.GITHUB_JOB, "YYY");
@@ -145,7 +146,7 @@ test("prepareEnvironment() when a local run", (t) => {
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
actionsutil.prepareLocalRunEnvironment();
initializeEnvironment(Mode.actions, "1.2.3");
// updated
t.deepEqual(process.env.GITHUB_JOB, "YYY");
@@ -154,11 +155,20 @@ test("prepareEnvironment() when a local run", (t) => {
process.env.GITHUB_JOB = "";
process.env.CODEQL_ACTION_ANALYSIS_KEY = "";
actionsutil.prepareLocalRunEnvironment();
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) => {
@@ -691,10 +701,12 @@ on: ["push"]
);
});
test("mode", (t) => {
actionsutil.setMode(actionsutil.Mode.actions);
t.deepEqual(actionsutil.getMode(), actionsutil.Mode.actions);
test("initializeEnvironment", (t) => {
initializeEnvironment(Mode.actions, "1.2.3");
t.deepEqual(getMode(), Mode.actions);
t.deepEqual(process.env.CODEQL_ACTION_VERSION, "1.2.3");
actionsutil.setMode(actionsutil.Mode.runner);
t.deepEqual(actionsutil.getMode(), actionsutil.Mode.runner);
initializeEnvironment(Mode.runner, "4.5.6");
t.deepEqual(getMode(), Mode.runner);
t.deepEqual(process.env.CODEQL_ACTION_VERSION, "4.5.6");
});