Skip to content

Commit 28b449d

Browse files
committed
Improve version handling in combineSarifFiles
1 parent 1721ce7 commit 28b449d

File tree

5 files changed

+66
-56
lines changed

5 files changed

+66
-56
lines changed

lib/analyze-action.js

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

lib/init-action-post.js

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

lib/upload-lib.js

Lines changed: 12 additions & 11 deletions
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: 12 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/sarif/index.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,33 @@ export function combineSarifFiles(
4646
logger: Logger,
4747
): sarif.Log {
4848
logger.info(`Loading SARIF file(s)`);
49-
const combinedSarif: sarif.Log = {
50-
version: "2.1.0",
51-
runs: [],
52-
};
49+
const runs: sarif.Run[] = [];
50+
let version: sarif.Log.version | undefined = undefined;
5351

5452
for (const sarifFile of sarifFiles) {
5553
logger.debug(`Loading SARIF file: ${sarifFile}`);
56-
const sarifObject = readSarifFile(sarifFile);
57-
// Check SARIF version
58-
if (combinedSarif.version === null) {
59-
combinedSarif.version = sarifObject.version;
60-
} else if (combinedSarif.version !== sarifObject.version) {
54+
const sarifLog = readSarifFile(sarifFile);
55+
// If this is the first SARIF file we are reading, store the version from it so that we
56+
// can put it in the combined SARIF. If not, then check that the versions match and
57+
// throw an exception if they do not.
58+
if (version === undefined) {
59+
version = sarifLog.version;
60+
} else if (version !== sarifLog.version) {
6161
throw new InvalidSarifUploadError(
62-
`Different SARIF versions encountered: ${combinedSarif.version} and ${sarifObject.version}`,
62+
`Different SARIF versions encountered: ${version} and ${sarifLog.version}`,
6363
);
6464
}
6565

66-
combinedSarif.runs.push(...sarifObject.runs);
66+
runs.push(...sarifLog.runs);
67+
}
68+
69+
// We can't guarantee that the SARIF files we load will have version properties. As a fallback,
70+
// we set it to the expected version if we didn't find any other.
71+
if (version === undefined) {
72+
version = "2.1.0";
6773
}
6874

69-
return combinedSarif;
75+
return { version, runs };
7076
}
7177

7278
/**

0 commit comments

Comments
 (0)