Skip to content

Commit bf2cacc

Browse files
committed
Initialise CodeQL CLI at most once in upload-sarif action
1 parent f6cd153 commit bf2cacc

File tree

6 files changed

+62
-44
lines changed

6 files changed

+62
-44
lines changed

lib/upload-lib.js

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

lib/upload-lib.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-sarif-action.js

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

lib/upload-sarif-action.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/upload-lib.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ async function shouldDisableCombineSarifFiles(
189189
export async function initCodeQLForUpload(
190190
gitHubVersion: GitHubVersion,
191191
features: FeatureEnablement,
192-
tempDir: string,
193192
logger: Logger,
194193
): Promise<CodeQL> {
195194
logger.info(
@@ -210,7 +209,7 @@ export async function initCodeQLForUpload(
210209
const initCodeQLResult = await initCodeQL(
211210
undefined, // There is no tools input on the upload action
212211
apiDetails,
213-
tempDir,
212+
actionsUtil.getTemporaryDirectory(),
214213
gitHubVersion.type,
215214
codeQLDefaultVersionInfo,
216215
features,
@@ -225,9 +224,9 @@ export async function initCodeQLForUpload(
225224
// CodeQL. Otherwise, it will fall back to combining the files in the action.
226225
// Returns the contents of the combined sarif file.
227226
async function combineSarifFilesUsingCLI(
227+
codeQL: CodeQL,
228228
sarifFiles: string[],
229229
gitHubVersion: GitHubVersion,
230-
features: FeatureEnablement,
231230
logger: Logger,
232231
): Promise<SarifFile> {
233232
logger.info("Combining SARIF files using the CodeQL CLI");
@@ -266,24 +265,6 @@ async function combineSarifFilesUsingCLI(
266265
return combineSarifFiles(sarifFiles, logger);
267266
}
268267

269-
// Initialize CodeQL, either by using the config file from the 'init' step,
270-
// or by initializing it here.
271-
let codeQL: CodeQL;
272-
let tempDir: string = actionsUtil.getTemporaryDirectory();
273-
274-
const config = await getConfig(tempDir, logger);
275-
if (config !== undefined) {
276-
codeQL = await getCodeQL(config.codeQLCmd);
277-
tempDir = config.tempDir;
278-
} else {
279-
codeQL = await initCodeQLForUpload(
280-
gitHubVersion,
281-
features,
282-
tempDir,
283-
logger,
284-
);
285-
}
286-
287268
if (
288269
!(await codeQL.supportsFeature(
289270
ToolsFeature.SarifMergeRunsFromEqualCategory,
@@ -310,6 +291,7 @@ async function combineSarifFilesUsingCLI(
310291
return combineSarifFiles(sarifFiles, logger);
311292
}
312293

294+
const tempDir = actionsUtil.getTemporaryDirectory();
313295
const baseTempDir = path.resolve(tempDir, "combined-sarif");
314296
fs.mkdirSync(baseTempDir, { recursive: true });
315297
const outputDirectory = fs.mkdtempSync(path.resolve(baseTempDir, "output-"));
@@ -676,6 +658,7 @@ export async function uploadFiles(
676658
);
677659

678660
return uploadSpecifiedFiles(
661+
undefined,
679662
sarifPaths,
680663
checkoutPath,
681664
category,
@@ -689,6 +672,7 @@ export async function uploadFiles(
689672
* Uploads the given array of SARIF files.
690673
*/
691674
export async function uploadSpecifiedFiles(
675+
codeQL: CodeQL | undefined,
692676
sarifPaths: string[],
693677
checkoutPath: string,
694678
category: string | undefined,
@@ -710,10 +694,23 @@ export async function uploadSpecifiedFiles(
710694
validateSarifFileSchema(parsedSarif, sarifPath, logger);
711695
}
712696

697+
// Initialize CodeQL, either by using the config file from the 'init' step,
698+
// or by initializing it here if we don't already have an instance.
699+
if (codeQL === undefined) {
700+
const tempDir: string = actionsUtil.getTemporaryDirectory();
701+
702+
const config = await getConfig(tempDir, logger);
703+
if (config !== undefined) {
704+
codeQL = await getCodeQL(config.codeQLCmd);
705+
} else {
706+
codeQL = await initCodeQLForUpload(gitHubVersion, features, logger);
707+
}
708+
}
709+
713710
sarif = await combineSarifFilesUsingCLI(
711+
codeQL,
714712
sarifPaths,
715713
gitHubVersion,
716-
features,
717714
logger,
718715
);
719716
} else {

src/upload-sarif-action.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as core from "@actions/core";
55
import * as actionsUtil from "./actions-util";
66
import { getActionVersion, getTemporaryDirectory } from "./actions-util";
77
import { getGitHubVersion } from "./api-client";
8+
import { CodeQL } from "./codeql";
89
import { Features } from "./feature-flags";
910
import { Logger, getActionsLogger } from "./logging";
1011
import { getRepositoryNwo } from "./repository";
@@ -114,7 +115,19 @@ async function run() {
114115
const securitySarifFiles = await findSecuritySarifFiles(sarifPath);
115116
const qualitySarifFiles = await findQualitySarifFiles(sarifPath);
116117

118+
// If we have more than one SARIF file for a given service, then we need to combine
119+
// them using the CLI. To avoid initialising the CLI twice in one run, we do it here.
120+
let codeql: CodeQL | undefined;
121+
if (securitySarifFiles.length > 1 || qualitySarifFiles.length > 1) {
122+
codeql = await upload_lib.initCodeQLForUpload(
123+
gitHubVersion,
124+
features,
125+
logger,
126+
);
127+
}
128+
117129
const uploadResult = await upload_lib.uploadSpecifiedFiles(
130+
codeql,
118131
securitySarifFiles,
119132
checkoutPath,
120133
category,
@@ -129,6 +142,7 @@ async function run() {
129142
// have a directory for the results here.
130143
if (qualitySarifFiles.length !== 0) {
131144
await upload_lib.uploadSpecifiedFiles(
145+
codeql,
132146
qualitySarifFiles,
133147
checkoutPath,
134148
category,

0 commit comments

Comments
 (0)