@@ -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