Skip to content

Commit dc4296f

Browse files
committed
Update getPrimaryAnalysis* and add test
1 parent 8cc4d25 commit dc4296f

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/config-utils.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as yaml from "js-yaml";
77
import * as sinon from "sinon";
88

99
import * as actionsUtil from "./actions-util";
10-
import { AnalysisKind } from "./analyses";
10+
import { AnalysisKind, supportedAnalysisKinds } from "./analyses";
1111
import * as api from "./api-client";
1212
import { CachingKind } from "./caching-utils";
1313
import { createStubCodeQL } from "./codeql";
@@ -1829,3 +1829,22 @@ test("hasActionsWorkflows doesn't throw if workflows folder doesn't exist", asyn
18291829
t.notThrows(() => configUtils.hasActionsWorkflows(tmpDir));
18301830
});
18311831
});
1832+
1833+
test("getPrimaryAnalysisConfig - single analysis kind", (t) => {
1834+
// If only one analysis kind is configured, we expect to get the matching configuration.
1835+
for (const analysisKind of supportedAnalysisKinds) {
1836+
const singleKind = createTestConfig({ analysisKinds: [analysisKind] });
1837+
t.is(configUtils.getPrimaryAnalysisConfig(singleKind).kind, analysisKind);
1838+
}
1839+
});
1840+
1841+
test("getPrimaryAnalysisConfig - Code Scanning + Code Quality", (t) => {
1842+
// For CS+CQ, we expect to get the Code Scanning configuration.
1843+
const codeScanningAndCodeQuality = createTestConfig({
1844+
analysisKinds: [AnalysisKind.CodeScanning, AnalysisKind.CodeQuality],
1845+
});
1846+
t.is(
1847+
configUtils.getPrimaryAnalysisConfig(codeScanningAndCodeQuality).kind,
1848+
AnalysisKind.CodeScanning,
1849+
);
1850+
});

src/config-utils.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
CodeQuality,
1616
codeQualityQueries,
1717
CodeScanning,
18+
getAnalysisConfig,
1819
} from "./analyses";
1920
import * as api from "./api-client";
2021
import { CachingKind, getCachingKind } from "./caching-utils";
@@ -1389,28 +1390,27 @@ export function isCodeQualityEnabled(config: Config): boolean {
13891390
}
13901391

13911392
/**
1392-
* Returns the primary analysis kind that the Action is initialised with. This is
1393-
* always `AnalysisKind.CodeScanning` unless `AnalysisKind.CodeScanning` is not enabled.
1393+
* Returns the primary analysis kind that the Action is initialised with. If there is only
1394+
* one analysis kind, then that is returned.
13941395
*
1395-
* @returns Returns `AnalysisKind.CodeScanning` if `AnalysisKind.CodeScanning` is enabled;
1396-
* otherwise `AnalysisKind.CodeQuality`.
1396+
* The special case is Code Scanning + Code Quality, which can be enabled at the same time.
1397+
* In that case, this function returns Code Scanning.
13971398
*/
13981399
function getPrimaryAnalysisKind(config: Config): AnalysisKind {
1400+
if (config.analysisKinds.length === 1) {
1401+
return config.analysisKinds[0];
1402+
}
1403+
13991404
return isCodeScanningEnabled(config)
14001405
? AnalysisKind.CodeScanning
14011406
: AnalysisKind.CodeQuality;
14021407
}
14031408

14041409
/**
1405-
* Returns the primary analysis configuration that the Action is initialised with. This is
1406-
* always `CodeScanning` unless `CodeScanning` is not enabled.
1407-
*
1408-
* @returns Returns `CodeScanning` if `AnalysisKind.CodeScanning` is enabled; otherwise `CodeQuality`.
1410+
* Returns the primary analysis configuration that the Action is initialised with.
14091411
*/
14101412
export function getPrimaryAnalysisConfig(config: Config): AnalysisConfig {
1411-
return getPrimaryAnalysisKind(config) === AnalysisKind.CodeScanning
1412-
? CodeScanning
1413-
: CodeQuality;
1413+
return getAnalysisConfig(getPrimaryAnalysisKind(config));
14141414
}
14151415

14161416
/** Logs the Git version as a telemetry diagnostic. */

0 commit comments

Comments
 (0)