Avoid using a global for getOrInitCodeQL

This commit is contained in:
Michael B. Gale
2026-02-24 20:09:10 +00:00
parent 37f3bfc967
commit 6d90f4c71e
3 changed files with 24 additions and 16 deletions
+7 -7
View File
@@ -111370,19 +111370,18 @@ function filterAlertsByDiffRange(logger, sarif) {
}
// src/upload-sarif.ts
var cachedCodeQL2;
async function getOrInitCodeQL(logger, gitHubVersion, features, config) {
if (cachedCodeQL2 !== void 0) return cachedCodeQL2;
async function getOrInitCodeQL(actionState, logger, gitHubVersion, features, config) {
if (actionState.cachedCodeQL !== void 0) return actionState.cachedCodeQL;
if (config !== void 0) {
cachedCodeQL2 = await getCodeQL(config.codeQLCmd);
actionState.cachedCodeQL = await getCodeQL(config.codeQLCmd);
} else {
cachedCodeQL2 = await minimalInitCodeQL(
actionState.cachedCodeQL = await minimalInitCodeQL(
logger,
gitHubVersion,
features
);
}
return cachedCodeQL2;
return actionState.cachedCodeQL;
}
async function postProcessAndUploadSarif(logger, tempPath, features, getCodeQL2, uploadKind, checkoutPath, sarifPath, category, postProcessedOutputPath) {
const sarifGroups = await getGroupedSarifFilePaths(
@@ -111442,6 +111441,7 @@ async function sendSuccessStatusReport(startedAt, uploadStats, logger) {
}
async function run(startedAt) {
const logger = getActionsLogger();
const state = { cachedCodeQL: void 0 };
try {
initializeEnvironment(getActionVersion());
const gitHubVersion = await getGitHubVersion();
@@ -111477,7 +111477,7 @@ async function run(startedAt) {
logger,
tempDir,
features,
() => getOrInitCodeQL(logger, gitHubVersion, features, config),
() => getOrInitCodeQL(state, logger, gitHubVersion, features, config),
"always",
checkoutPath,
sarifPath,
+7 -2
View File
@@ -17,7 +17,11 @@ import {
isThirdPartyAnalysis,
} from "./status-report";
import * as upload_lib from "./upload-lib";
import { getOrInitCodeQL, postProcessAndUploadSarif } from "./upload-sarif";
import {
getOrInitCodeQL,
postProcessAndUploadSarif,
UploadSarifState,
} from "./upload-sarif";
import {
ConfigurationError,
checkActionVersion,
@@ -59,6 +63,7 @@ async function run(startedAt: Date) {
// possible, and only use safe functions outside.
const logger = getActionsLogger();
const state: UploadSarifState = { cachedCodeQL: undefined };
try {
initializeEnvironment(actionsUtil.getActionVersion());
@@ -107,7 +112,7 @@ async function run(startedAt: Date) {
logger,
tempDir,
features,
() => getOrInitCodeQL(logger, gitHubVersion, features, config),
() => getOrInitCodeQL(state, logger, gitHubVersion, features, config),
"always",
checkoutPath,
sarifPath,
+10 -7
View File
@@ -8,38 +8,41 @@ import { Logger } from "./logging";
import * as upload_lib from "./upload-lib";
import { GitHubVersion, unsafeEntriesInvariant } from "./util";
export interface UploadSarifState {
/** The cached `CodeQL` instance, if any. */
cachedCodeQL: CodeQL | undefined;
}
// Maps analysis kinds to SARIF IDs.
export type UploadSarifResults = Partial<
Record<analyses.AnalysisKind, upload_lib.UploadResult>
>;
/** The cached `CodeQL` instance, if any. */
let cachedCodeQL: CodeQL | undefined;
/** Get or initialise a `CodeQL` instance for use by the `upload-sarif` action. */
export async function getOrInitCodeQL(
actionState: UploadSarifState,
logger: Logger,
gitHubVersion: GitHubVersion,
features: FeatureEnablement,
config: Config | undefined,
): Promise<CodeQL> {
// Return the cached instance, if we have one.
if (cachedCodeQL !== undefined) return cachedCodeQL;
if (actionState.cachedCodeQL !== undefined) return actionState.cachedCodeQL;
// If we have been able to load a `Config` from an earlier CodeQL Action step in the job,
// then use the CodeQL executable that we have used previously. Otherwise, initialise the
// CLI specifically for `upload-sarif`. Either way, we cache the instance.
if (config !== undefined) {
cachedCodeQL = await codeql.getCodeQL(config.codeQLCmd);
actionState.cachedCodeQL = await codeql.getCodeQL(config.codeQLCmd);
} else {
cachedCodeQL = await upload_lib.minimalInitCodeQL(
actionState.cachedCodeQL = await upload_lib.minimalInitCodeQL(
logger,
gitHubVersion,
features,
);
}
return cachedCodeQL;
return actionState.cachedCodeQL;
}
/**