Skip to content

Commit 2fce45b

Browse files
committed
Add wrapper around JSON.parse to sarif module
1 parent d7cfd19 commit 2fce45b

File tree

6 files changed

+684
-683
lines changed

6 files changed

+684
-683
lines changed

lib/analyze-action.js

Lines changed: 130 additions & 130 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: 338 additions & 338 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: 102 additions & 102 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: 103 additions & 103 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as fs from "fs";
2+
13
import { Logger } from "../logging";
24

35
export interface SarifLocation {
@@ -166,3 +168,7 @@ export function fixInvalidNotifications(
166168
}
167169
return newSarif;
168170
}
171+
172+
export function readSarifFile(sarifFilePath: string): SarifFile {
173+
return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as SarifFile;
174+
}

src/upload-lib.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as gitUtils from "./git-utils";
2121
import { initCodeQL } from "./init";
2222
import { Logger } from "./logging";
2323
import { getRepositoryNwo, RepositoryNwo } from "./repository";
24+
import type { SarifFile, SarifRun } from "./sarif";
2425
import { BasePayload, UploadPayload } from "./upload-lib/types";
2526
import * as util from "./util";
2627
import {
@@ -30,8 +31,6 @@ import {
3031
GitHubVariant,
3132
GitHubVersion,
3233
satisfiesGHESVersion,
33-
SarifFile,
34-
SarifRun,
3534
} from "./util";
3635

3736
const GENERIC_403_MSG =
@@ -50,9 +49,7 @@ function combineSarifFiles(sarifFiles: string[], logger: Logger): SarifFile {
5049

5150
for (const sarifFile of sarifFiles) {
5251
logger.debug(`Loading SARIF file: ${sarifFile}`);
53-
const sarifObject = JSON.parse(
54-
fs.readFileSync(sarifFile, "utf8"),
55-
) as SarifFile;
52+
const sarifObject = util.readSarifFile(sarifFile);
5653
// Check SARIF version
5754
if (combinedSarif.version === null) {
5855
combinedSarif.version = sarifObject.version;
@@ -195,9 +192,7 @@ async function combineSarifFilesUsingCLI(
195192
): Promise<SarifFile> {
196193
logger.info("Combining SARIF files using the CodeQL CLI");
197194

198-
const sarifObjects = sarifFiles.map((sarifFile): SarifFile => {
199-
return JSON.parse(fs.readFileSync(sarifFile, "utf8")) as SarifFile;
200-
});
195+
const sarifObjects = sarifFiles.map(util.readSarifFile);
201196

202197
const deprecationWarningMessage =
203198
gitHubVersion.type === GitHubVariant.GHES
@@ -279,7 +274,7 @@ async function combineSarifFilesUsingCLI(
279274
mergeRunsFromEqualCategory: true,
280275
});
281276

282-
return JSON.parse(fs.readFileSync(outputFile, "utf8")) as SarifFile;
277+
return util.readSarifFile(outputFile);
283278
}
284279

285280
// Populates the run.automationDetails.id field using the analysis_key and environment
@@ -531,7 +526,7 @@ function countResultsInSarif(sarif: string): number {
531526

532527
export function readSarifFile(sarifFilePath: string): SarifFile {
533528
try {
534-
return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as SarifFile;
529+
return util.readSarifFile(sarifFilePath);
535530
} catch (e) {
536531
throw new InvalidSarifUploadError(
537532
`Invalid SARIF. JSON syntax error: ${getErrorMessage(e)}`,

0 commit comments

Comments
 (0)