@@ -21,6 +21,28 @@ import {
2121 */
2222declare 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
7193const 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 */
86108export 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 */
106128function 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. */
113135export 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 */
183205export 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 */
202224export 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 ;
0 commit comments