Introduce areAllRunsProducedByCodeQL function

This commit is contained in:
Koen Vlaswinkel 2024-03-25 10:06:45 +01:00
parent 70aa50b057
commit 3bd271cec3
3 changed files with 36 additions and 28 deletions

26
lib/upload-lib.js generated
View file

@ -69,22 +69,26 @@ function combineSarifFiles(sarifFiles) {
}
return combinedSarif;
}
/**
* Checks whether all the runs in the given SARIF files were produced by CodeQL.
* @param sarifFiles The list of SARIF files to check.
*/
function areAllRunsProducedByCodeQL(sarifFiles) {
for (const sarifFile of sarifFiles) {
const sarifObject = JSON.parse(fs.readFileSync(sarifFile, "utf8"));
const allRunsCodeQL = sarifObject.runs?.every((run) => run.tool?.driver?.name === "CodeQL");
if (!allRunsCodeQL) {
return false;
}
}
return true;
}
// Takes a list of paths to sarif files and combines them together using the
// CLI `github merge-results` command when all SARIF files are produced by
// CodeQL. Otherwise, it will fall back to combining the files in the action.
// Returns the contents of the combined sarif file.
async function combineSarifFilesUsingCLI(sarifFiles, gitHubVersion, features, logger) {
// First check if all files are produced by CodeQL.
let allCodeQL = true;
for (const sarifFile of sarifFiles) {
const sarifObject = JSON.parse(fs.readFileSync(sarifFile, "utf8"));
const allRunsCodeQL = sarifObject.runs?.every((run) => run.tool?.driver?.name === "CodeQL");
if (!allRunsCodeQL) {
allCodeQL = false;
break;
}
}
if (!allCodeQL) {
if (!areAllRunsProducedByCodeQL(sarifFiles)) {
logger.warning("Not all SARIF files were produced by CodeQL. Merging files in the action.");
// If not, use the naive method of combining the files.
return combineSarifFiles(sarifFiles);

File diff suppressed because one or more lines are too long

View file

@ -61,19 +61,11 @@ function combineSarifFiles(sarifFiles: string[]): SarifFile {
return combinedSarif;
}
// Takes a list of paths to sarif files and combines them together using the
// CLI `github merge-results` command when all SARIF files are produced by
// CodeQL. Otherwise, it will fall back to combining the files in the action.
// Returns the contents of the combined sarif file.
async function combineSarifFilesUsingCLI(
sarifFiles: string[],
gitHubVersion: GitHubVersion,
features: Features,
logger: Logger,
): Promise<SarifFile> {
// First check if all files are produced by CodeQL.
let allCodeQL = true;
/**
* Checks whether all the runs in the given SARIF files were produced by CodeQL.
* @param sarifFiles The list of SARIF files to check.
*/
function areAllRunsProducedByCodeQL(sarifFiles: string[]): boolean {
for (const sarifFile of sarifFiles) {
const sarifObject = JSON.parse(
fs.readFileSync(sarifFile, "utf8"),
@ -84,12 +76,24 @@ async function combineSarifFilesUsingCLI(
);
if (!allRunsCodeQL) {
allCodeQL = false;
break;
return false;
}
}
if (!allCodeQL) {
return true;
}
// Takes a list of paths to sarif files and combines them together using the
// CLI `github merge-results` command when all SARIF files are produced by
// CodeQL. Otherwise, it will fall back to combining the files in the action.
// Returns the contents of the combined sarif file.
async function combineSarifFilesUsingCLI(
sarifFiles: string[],
gitHubVersion: GitHubVersion,
features: Features,
logger: Logger,
): Promise<SarifFile> {
if (!areAllRunsProducedByCodeQL(sarifFiles)) {
logger.warning(
"Not all SARIF files were produced by CodeQL. Merging files in the action.",
);