Skip to content

Commit 5e3e6b8

Browse files
committed
Add ActionsEnvVars enum
1 parent 3cf0a52 commit 5e3e6b8

4 files changed

Lines changed: 57 additions & 27 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 14 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/actions-util.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ import {
2121
*/
2222
declare const __CODEQL_ACTION_VERSION__: string;
2323

24+
/**
25+
* Enumerates known GitHub Actions environment variables that we expect
26+
* to be set in a GitHub Actions environment.
27+
*/
28+
export enum ActionsEnvVars {
29+
GITHUB_ACTION_REPOSITORY = "GITHUB_ACTION_REPOSITORY",
30+
GITHUB_API_URL = "GITHUB_API_URL",
31+
GITHUB_EVENT_NAME = "GITHUB_EVENT_NAME",
32+
GITHUB_EVENT_PATH = "GITHUB_EVENT_PATH",
33+
GITHUB_JOB = "GITHUB_JOB",
34+
GITHUB_REF = "GITHUB_REF",
35+
GITHUB_REPOSITORY = "GITHUB_REPOSITORY",
36+
GITHUB_RUN_ATTEMPT = "GITHUB_RUN_ATTEMPT",
37+
GITHUB_RUN_ID = "GITHUB_RUN_ID",
38+
GITHUB_SERVER_URL = "GITHUB_SERVER_URL",
39+
GITHUB_SHA = "GITHUB_SHA",
40+
GITHUB_WORKFLOW = "GITHUB_WORKFLOW",
41+
RUNNER_NAME = "RUNNER_NAME",
42+
RUNNER_OS = "RUNNER_OS",
43+
RUNNER_TEMP = "RUNNER_TEMP",
44+
}
45+
2446
/**
2547
* Abstracts over GitHub Actions functions so that we do not have to stub
2648
* global functions in tests.
@@ -65,7 +87,7 @@ export function getTemporaryDirectory(): string {
6587
const value = process.env["CODEQL_ACTION_TEMP"];
6688
return value !== undefined && value !== ""
6789
? value
68-
: getRequiredEnvParam("RUNNER_TEMP");
90+
: getRequiredEnvParam(ActionsEnvVars.RUNNER_TEMP);
6991
}
7092

7193
const PR_DIFF_RANGE_JSON_FILENAME = "pr-diff-range.json";
@@ -84,7 +106,7 @@ export function getActionVersion(): string {
84106
* This will be "dynamic" for default setup workflow runs.
85107
*/
86108
export function getWorkflowEventName() {
87-
return getRequiredEnvParam("GITHUB_EVENT_NAME");
109+
return getRequiredEnvParam(ActionsEnvVars.GITHUB_EVENT_NAME);
88110
}
89111

90112
/**
@@ -104,14 +126,14 @@ export function isRunningLocalAction(): boolean {
104126
* This can be used to get the Action's name or tell if we're running a local Action.
105127
*/
106128
function getRelativeScriptPath(): string {
107-
const runnerTemp = getRequiredEnvParam("RUNNER_TEMP");
129+
const runnerTemp = getRequiredEnvParam(ActionsEnvVars.RUNNER_TEMP);
108130
const actionsDirectory = path.join(path.dirname(runnerTemp), "_actions");
109131
return path.relative(actionsDirectory, __filename);
110132
}
111133

112134
/** Returns the contents of `GITHUB_EVENT_PATH` as a JSON object. */
113135
export function getWorkflowEvent(): any {
114-
const eventJsonFile = getRequiredEnvParam("GITHUB_EVENT_PATH");
136+
const eventJsonFile = getRequiredEnvParam(ActionsEnvVars.GITHUB_EVENT_PATH);
115137
try {
116138
return JSON.parse(fs.readFileSync(eventJsonFile, "utf-8"));
117139
} catch (e) {
@@ -181,16 +203,16 @@ export function getUploadValue(input: string | undefined): UploadKind {
181203
* Get the workflow run ID.
182204
*/
183205
export function getWorkflowRunID(): number {
184-
const workflowRunIdString = getRequiredEnvParam("GITHUB_RUN_ID");
206+
const workflowRunIdString = getRequiredEnvParam(ActionsEnvVars.GITHUB_RUN_ID);
185207
const workflowRunID = parseInt(workflowRunIdString, 10);
186208
if (Number.isNaN(workflowRunID)) {
187209
throw new Error(
188-
`GITHUB_RUN_ID must define a non NaN workflow run ID. Current value is ${workflowRunIdString}`,
210+
`${ActionsEnvVars.GITHUB_RUN_ID} must define a non NaN workflow run ID. Current value is ${workflowRunIdString}`,
189211
);
190212
}
191213
if (workflowRunID < 0) {
192214
throw new Error(
193-
`GITHUB_RUN_ID must be a non-negative integer. Current value is ${workflowRunIdString}`,
215+
`${ActionsEnvVars.GITHUB_RUN_ID} must be a non-negative integer. Current value is ${workflowRunIdString}`,
194216
);
195217
}
196218
return workflowRunID;
@@ -200,16 +222,18 @@ export function getWorkflowRunID(): number {
200222
* Get the workflow run attempt number.
201223
*/
202224
export function getWorkflowRunAttempt(): number {
203-
const workflowRunAttemptString = getRequiredEnvParam("GITHUB_RUN_ATTEMPT");
225+
const workflowRunAttemptString = getRequiredEnvParam(
226+
ActionsEnvVars.GITHUB_RUN_ATTEMPT,
227+
);
204228
const workflowRunAttempt = parseInt(workflowRunAttemptString, 10);
205229
if (Number.isNaN(workflowRunAttempt)) {
206230
throw new Error(
207-
`GITHUB_RUN_ATTEMPT must define a non NaN workflow run attempt. Current value is ${workflowRunAttemptString}`,
231+
`${ActionsEnvVars.GITHUB_RUN_ATTEMPT} must define a non NaN workflow run attempt. Current value is ${workflowRunAttemptString}`,
208232
);
209233
}
210234
if (workflowRunAttempt <= 0) {
211235
throw new Error(
212-
`GITHUB_RUN_ATTEMPT must be a positive integer. Current value is ${workflowRunAttemptString}`,
236+
`${ActionsEnvVars.GITHUB_RUN_ATTEMPT} must be a positive integer. Current value is ${workflowRunAttemptString}`,
213237
);
214238
}
215239
return workflowRunAttempt;

‎src/api-client.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as core from "@actions/core";
22
import * as githubUtils from "@actions/github/lib/utils";
33
import * as retry from "@octokit/plugin-retry";
44

5-
import { getActionVersion, getRequiredInput } from "./actions-util";
5+
import {
6+
ActionsEnvVars,
7+
getActionVersion,
8+
getRequiredInput,
9+
} from "./actions-util";
610
import { EnvVar } from "./environment";
711
import { Logger } from "./logging";
812
import { getRepositoryNwo, RepositoryNwo } from "./repository";
@@ -70,8 +74,8 @@ function createApiClientWithDetails(
7074
export function getApiDetails(): GitHubApiDetails {
7175
return {
7276
auth: getRequiredInput("token"),
73-
url: getRequiredEnvParam("GITHUB_SERVER_URL"),
74-
apiURL: getRequiredEnvParam("GITHUB_API_URL"),
77+
url: getRequiredEnvParam(ActionsEnvVars.GITHUB_SERVER_URL),
78+
apiURL: getRequiredEnvParam(ActionsEnvVars.GITHUB_API_URL),
7579
};
7680
}
7781

‎src/testing-utils.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import test, {
1010
import nock from "nock";
1111
import * as sinon from "sinon";
1212

13-
import { ActionsEnv, getActionVersion } from "./actions-util";
13+
import { ActionsEnv, ActionsEnvVars, getActionVersion } from "./actions-util";
1414
import { AnalysisKind } from "./analyses";
1515
import * as apiClient from "./api-client";
1616
import { GitHubApiDetails } from "./api-client";
@@ -200,7 +200,7 @@ export const DEFAULT_ACTIONS_VARS = {
200200
GITHUB_WORKFLOW: "test-workflow",
201201
RUNNER_NAME: "my-runner",
202202
RUNNER_OS: "Linux",
203-
} as const satisfies Record<string, string>;
203+
} as const satisfies Partial<Record<ActionsEnvVars, string>>;
204204

205205
/** Partial mappings from GitHub Actions environment variables to values. */
206206
export type ActionVarOverrides = Partial<

0 commit comments

Comments
 (0)