Skip to content

Commit 6d060bb

Browse files
committed
Return Partial<Log> from readSarifFile
Our previous definition had `tools` as a mandatory field, so this also makes some changes to deal with the case where that may be `undefined` by treating it as equivalent to `[]`.
1 parent 28b449d commit 6d060bb

File tree

7 files changed

+88
-36
lines changed

7 files changed

+88
-36
lines changed

lib/analyze-action.js

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

src/fingerprints.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,10 @@ export function resolveUriToFile(
256256
// Compute fingerprints for results in the given sarif file
257257
// and return an updated sarif file contents.
258258
export async function addFingerprints(
259-
sarifLog: sarif.Log,
259+
sarifLog: Partial<sarif.Log>,
260260
sourceRoot: string,
261261
logger: Logger,
262-
): Promise<sarif.Log> {
262+
): Promise<Partial<sarif.Log>> {
263263
logger.info(
264264
`Adding fingerprints to SARIF file. See ${DocUrl.TRACK_CODE_SCANNING_ALERTS_ACROSS_RUNS} for more information.`,
265265
);

src/sarif/index.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class InvalidSarifUploadError extends Error {}
2121
*
2222
* Returns an array of unique string tool names.
2323
*/
24-
export function getToolNames(sarifFile: sarif.Log): string[] {
24+
export function getToolNames(sarifFile: Partial<sarif.Log>): string[] {
2525
const toolNames = {};
2626

2727
for (const run of sarifFile.runs || []) {
@@ -35,7 +35,15 @@ export function getToolNames(sarifFile: sarif.Log): string[] {
3535
return Object.keys(toolNames);
3636
}
3737

38-
export function readSarifFile(sarifFilePath: string): sarif.Log {
38+
/**
39+
* Reads the file pointed at by `sarifFilePath` and parses it as JSON. This function does
40+
* not validate that the JSON represents a valid SARIF file. I.e. this function will only
41+
* throw if the file cannot be read or does not contain valid JSON.
42+
*
43+
* @param sarifFilePath The file to read.
44+
* @returns The resulting JSON value, cast to a SARIF `Log`.
45+
*/
46+
export function readSarifFile(sarifFilePath: string): Partial<sarif.Log> {
3947
return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as sarif.Log;
4048
}
4149

@@ -63,7 +71,7 @@ export function combineSarifFiles(
6371
);
6472
}
6573

66-
runs.push(...sarifLog.runs);
74+
runs.push(...(sarifLog?.runs || []));
6775
}
6876

6977
// We can't guarantee that the SARIF files we load will have version properties. As a fallback,
@@ -79,8 +87,10 @@ export function combineSarifFiles(
7987
* Checks whether all the runs in the given SARIF files were produced by CodeQL.
8088
* @param sarifLogs The list of SARIF objects to check.
8189
*/
82-
export function areAllRunsProducedByCodeQL(sarifLogs: sarif.Log[]): boolean {
83-
return sarifLogs.every((sarifLog: sarif.Log) => {
90+
export function areAllRunsProducedByCodeQL(
91+
sarifLogs: Array<Partial<sarif.Log>>,
92+
): boolean {
93+
return sarifLogs.every((sarifLog: Partial<sarif.Log>) => {
8494
return sarifLog.runs?.every((run) => run.tool?.driver?.name === "CodeQL");
8595
});
8696
}
@@ -101,10 +111,16 @@ function createRunKey(run: sarif.Run): RunKey {
101111
* criteria used by Code Scanning to determine analysis categories).
102112
* @param sarifLogs The list of SARIF objects to check.
103113
*/
104-
export function areAllRunsUnique(sarifLogs: sarif.Log[]): boolean {
114+
export function areAllRunsUnique(
115+
sarifLogs: Array<Partial<sarif.Log>>,
116+
): boolean {
105117
const keys = new Set<string>();
106118

107119
for (const sarifLog of sarifLogs) {
120+
if (sarifLog.runs === undefined) {
121+
continue;
122+
}
123+
108124
for (const run of sarifLog.runs) {
109125
const key = JSON.stringify(createRunKey(run));
110126

0 commit comments

Comments
 (0)