Skip to content

Commit 6d90f4c

Browse files
committed
Avoid using a global for getOrInitCodeQL
1 parent 37f3bfc commit 6d90f4c

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

lib/upload-sarif-action.js

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

src/upload-sarif-action.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import {
1717
isThirdPartyAnalysis,
1818
} from "./status-report";
1919
import * as upload_lib from "./upload-lib";
20-
import { getOrInitCodeQL, postProcessAndUploadSarif } from "./upload-sarif";
20+
import {
21+
getOrInitCodeQL,
22+
postProcessAndUploadSarif,
23+
UploadSarifState,
24+
} from "./upload-sarif";
2125
import {
2226
ConfigurationError,
2327
checkActionVersion,
@@ -59,6 +63,7 @@ async function run(startedAt: Date) {
5963
// possible, and only use safe functions outside.
6064

6165
const logger = getActionsLogger();
66+
const state: UploadSarifState = { cachedCodeQL: undefined };
6267

6368
try {
6469
initializeEnvironment(actionsUtil.getActionVersion());
@@ -107,7 +112,7 @@ async function run(startedAt: Date) {
107112
logger,
108113
tempDir,
109114
features,
110-
() => getOrInitCodeQL(logger, gitHubVersion, features, config),
115+
() => getOrInitCodeQL(state, logger, gitHubVersion, features, config),
111116
"always",
112117
checkoutPath,
113118
sarifPath,

src/upload-sarif.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,41 @@ import { Logger } from "./logging";
88
import * as upload_lib from "./upload-lib";
99
import { GitHubVersion, unsafeEntriesInvariant } from "./util";
1010

11+
export interface UploadSarifState {
12+
/** The cached `CodeQL` instance, if any. */
13+
cachedCodeQL: CodeQL | undefined;
14+
}
15+
1116
// Maps analysis kinds to SARIF IDs.
1217
export type UploadSarifResults = Partial<
1318
Record<analyses.AnalysisKind, upload_lib.UploadResult>
1419
>;
1520

16-
/** The cached `CodeQL` instance, if any. */
17-
let cachedCodeQL: CodeQL | undefined;
18-
1921
/** Get or initialise a `CodeQL` instance for use by the `upload-sarif` action. */
2022
export async function getOrInitCodeQL(
23+
actionState: UploadSarifState,
2124
logger: Logger,
2225
gitHubVersion: GitHubVersion,
2326
features: FeatureEnablement,
2427
config: Config | undefined,
2528
): Promise<CodeQL> {
2629
// Return the cached instance, if we have one.
27-
if (cachedCodeQL !== undefined) return cachedCodeQL;
30+
if (actionState.cachedCodeQL !== undefined) return actionState.cachedCodeQL;
2831

2932
// If we have been able to load a `Config` from an earlier CodeQL Action step in the job,
3033
// then use the CodeQL executable that we have used previously. Otherwise, initialise the
3134
// CLI specifically for `upload-sarif`. Either way, we cache the instance.
3235
if (config !== undefined) {
33-
cachedCodeQL = await codeql.getCodeQL(config.codeQLCmd);
36+
actionState.cachedCodeQL = await codeql.getCodeQL(config.codeQLCmd);
3437
} else {
35-
cachedCodeQL = await upload_lib.minimalInitCodeQL(
38+
actionState.cachedCodeQL = await upload_lib.minimalInitCodeQL(
3639
logger,
3740
gitHubVersion,
3841
features,
3942
);
4043
}
4144

42-
return cachedCodeQL;
45+
return actionState.cachedCodeQL;
4346
}
4447

4548
/**

0 commit comments

Comments
 (0)