diff --git a/src/analyses.ts b/src/analyses.ts index 53f17a70a..ae370e643 100644 --- a/src/analyses.ts +++ b/src/analyses.ts @@ -87,6 +87,23 @@ export const CodeQuality: AnalysisConfig = { sentinelPrefix: "CODEQL_UPLOAD_QUALITY_SARIF_", }; +/** + * Gets the `AnalysisConfig` corresponding to `kind`. + * @param kind The analysis kind to get the `AnalysisConfig` for. + * @returns The `AnalysisConfig` corresponding to `kind`. + */ +export function getAnalysisConfig(kind: AnalysisKind): AnalysisConfig { + // Using a switch statement here accomplishes two things: + // 1. The type checker believes us that we have a case for every `AnalysisKind`. + // 2. If we ever add another member to `AnalysisKind`, the type checker will alert us that we have to add a case. + switch (kind) { + case AnalysisKind.CodeScanning: + return CodeScanning; + case AnalysisKind.CodeQuality: + return CodeQuality; + } +} + // Since we have overlapping extensions (i.e. ".sarif" includes ".quality.sarif"), // we want to scan a folder containing SARIF files in an order that finds the more // specific extensions first. This constant defines an array in the order of analyis diff --git a/src/upload-sarif.test.ts b/src/upload-sarif.test.ts index 44f856435..b0a288ec5 100644 --- a/src/upload-sarif.test.ts +++ b/src/upload-sarif.test.ts @@ -7,8 +7,8 @@ import * as sinon from "sinon"; import { AnalysisConfig, AnalysisKind, - CodeQuality, CodeScanning, + getAnalysisConfig, } from "./analyses"; import { getRunnerLogger } from "./logging"; import { createFeatures, setupTests } from "./testing-utils"; @@ -117,9 +117,7 @@ const uploadSarifMacro = test.macro({ sinon.match.any, features, logger, - analysisKind === AnalysisKind.CodeScanning - ? CodeScanning - : CodeQuality, + getAnalysisConfig(analysisKind), ) .resolves(expectedResult[analysisKind as AnalysisKind]?.uploadResult); } @@ -146,9 +144,7 @@ const uploadSarifMacro = test.macro({ sinon.match.any, features, logger, - analysisKind === AnalysisKind.CodeScanning - ? CodeScanning - : CodeQuality, + getAnalysisConfig(analysisKind), ), ); } else { @@ -164,9 +160,7 @@ const uploadSarifMacro = test.macro({ sinon.match.any, features, logger, - analysisKind === AnalysisKind.CodeScanning - ? CodeScanning - : CodeQuality, + getAnalysisConfig(analysisKind), ), `uploadSpecifiedFiles was called for ${analysisKind}, but should not have been.`, );