Use the prefix id for keying into metrics rules

Fixes a bug where we were looking for incorrect keys for metrics rules.
Previously, we were using full language names in the keys. Now, we use
the short language names in the metric ids. This is done through a
simplification of the code.

Also, this change does two smaller things:

1. Prints out the baseline count to the logs
2. Adds the `assertNever` function to ensure we never miss a case in
   a switch statement. This function is borrowed from vscode-codeql.
This commit is contained in:
Andrew Eisenberg 2021-05-13 17:56:27 +00:00
parent 4c0671c518
commit e8b2a9884b
15 changed files with 168 additions and 116 deletions

View file

@ -6,7 +6,7 @@ import * as toolrunner from "@actions/exec/lib/toolrunner";
import * as analysisPaths from "./analysis-paths";
import { getCodeQL } from "./codeql";
import * as configUtils from "./config-utils";
import { IdPrefixes, countLoc } from "./count-loc";
import { countLoc, getIdPrefix } from "./count-loc";
import { isScannedLanguage, Language } from "./languages";
import { Logger } from "./logging";
import * as sharedEnv from "./shared-environment";
@ -206,6 +206,8 @@ export async function runQueries(
statusReport[`analyze_custom_queries_${language}_duration_ms`] =
new Date().getTime() - startTimeCustom;
}
printLinesOfCodeSummary(logger, language, await locPromise);
} catch (e) {
logger.info(e);
statusReport.analyze_failure_language = language;
@ -291,15 +293,16 @@ export async function runAnalyze(
async function injectLinesOfCode(
sarifFile: string,
language: string,
locPromise: Promise<Partial<Record<IdPrefixes, number>>>
language: Language,
locPromise: Promise<Partial<Record<Language, number>>>
) {
const lineCounts = await locPromise;
const idPrefix = getIdPrefix(language);
if (language in lineCounts) {
const sarif = JSON.parse(fs.readFileSync(sarifFile, "utf8"));
if (Array.isArray(sarif.runs)) {
for (const run of sarif.runs) {
const ruleId = `${language}/summary/lines-of-code`;
const ruleId = `${idPrefix}/summary/lines-of-code`;
run.properties = run.properties || {};
run.properties.metricResults = run.properties.metricResults || [];
const rule = run.properties.metricResults.find(
@ -315,3 +318,15 @@ async function injectLinesOfCode(
fs.writeFileSync(sarifFile, JSON.stringify(sarif));
}
}
function printLinesOfCodeSummary(
logger: Logger,
language: Language,
lineCounts: Partial<Record<Language, number>>
) {
if (language in lineCounts) {
logger.info(
`Counted ${lineCounts[language]} lines of code for ${language} as a baseline.`
);
}
}