Remove old baseline LoC injection

This commit is contained in:
Edoardo Pirovano 2021-08-26 13:46:22 +01:00
Родитель a44b61d961
Коммит 2e71e02553
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 047556B5D93FFE28
9 изменённых файлов: 15 добавлений и 179 удалений

12
lib/analyze.js сгенерированный
Просмотреть файл

@ -233,24 +233,14 @@ exports.runCleanup = runCleanup;
async function injectLinesOfCode(sarifFile, language, locPromise) {
var _a;
const lineCounts = await locPromise;
const idPrefix = count_loc_1.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) {
// Old style: Baseline is inserted when rule ID has suffix /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(
// the rule id can be in either of two places
(r) => { var _a; return r.ruleId === ruleId || ((_a = r.rule) === null || _a === void 0 ? void 0 : _a.id) === ruleId; });
// only add the baseline value if the rule already exists
if (rule) {
rule.baseline = lineCounts[language];
}
// New style: Baseline is inserted when matching rule has tag lines-of-code
for (const metric of run.properties.metricResults) {
// Baseline is inserted when matching rule has tag lines-of-code
if (metric.rule && metric.rule.toolComponent) {
const matchingRule = run.tool.extensions[metric.rule.toolComponent.index].rules[metric.rule.index];
if ((_a = matchingRule.properties.tags) === null || _a === void 0 ? void 0 : _a.includes("lines-of-code")) {

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

50
lib/analyze.test.js сгенерированный
Просмотреть файл

@ -30,7 +30,6 @@ const semver_1 = require("semver");
const sinon = __importStar(require("sinon"));
const analyze_1 = require("./analyze");
const codeql_1 = require("./codeql");
const count_loc_1 = require("./count-loc");
const count = __importStar(require("./count-loc"));
const languages_1 = require("./languages");
const logging_1 = require("./logging");
@ -76,31 +75,7 @@ ava_1.default("status report fields and search path setting", async (t) => {
databaseInterpretResults: async (_db, _queriesRun, sarifFile) => {
fs.writeFileSync(sarifFile, JSON.stringify({
runs: [
// variant 1 uses ruleId
{
properties: {
metricResults: [
{
ruleId: `${count_loc_1.getIdPrefix(language)}/summary/lines-of-code`,
value: 123,
},
],
},
},
// variant 2 uses rule.id
{
properties: {
metricResults: [
{
rule: {
id: `${count_loc_1.getIdPrefix(language)}/summary/lines-of-code`,
},
value: 123,
},
],
},
},
// variant 3 references a rule with the lines-of-code tag
// references a rule with the lines-of-code tag, so baseline should be injected
{
tool: {
extensions: [
@ -199,29 +174,12 @@ ava_1.default("status report fields and search path setting", async (t) => {
function verifyLineCounts(tmpDir) {
// eslint-disable-next-line github/array-foreach
Object.keys(languages_1.Language).forEach((lang, i) => {
verifyLineCountForFile(lang, path.join(tmpDir, `${lang}.sarif`), i + 1);
verifyLineCountForFile(path.join(tmpDir, `${lang}.sarif`), i + 1);
});
}
function verifyLineCountForFile(lang, filePath, lineCount) {
const idPrefix = count_loc_1.getIdPrefix(lang);
function verifyLineCountForFile(filePath, lineCount) {
const sarif = JSON.parse(fs.readFileSync(filePath, "utf8"));
t.deepEqual(sarif.runs[0].properties.metricResults, [
{
ruleId: `${idPrefix}/summary/lines-of-code`,
value: 123,
baseline: lineCount,
},
]);
t.deepEqual(sarif.runs[1].properties.metricResults, [
{
rule: {
id: `${idPrefix}/summary/lines-of-code`,
},
value: 123,
baseline: lineCount,
},
]);
t.deepEqual(sarif.runs[2].properties.metricResults, [
{
rule: {
index: 0,
@ -234,7 +192,7 @@ ava_1.default("status report fields and search path setting", async (t) => {
},
]);
// when the rule doesn't exist, it should not be added
t.deepEqual(sarif.runs[3].properties.metricResults, []);
t.deepEqual(sarif.runs[1].properties.metricResults, []);
}
function verifyQuerySuites(tmpDir) {
const qlsContent = [

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

24
lib/count-loc.js сгенерированный
Просмотреть файл

@ -1,9 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.countLoc = exports.getIdPrefix = void 0;
exports.countLoc = void 0;
const github_linguist_1 = require("github-linguist");
const languages_1 = require("./languages");
const util_1 = require("./util");
// Map from linguist language names to language prefixes used in the action and codeql
const linguistToMetrics = {
c: languages_1.Language.cpp,
@ -23,27 +22,6 @@ const nameToLinguist = Object.entries(linguistToMetrics).reduce((obj, [key, name
obj[name].push(key);
return obj;
}, {});
function getIdPrefix(language) {
switch (language) {
case languages_1.Language.cpp:
return "cpp";
case languages_1.Language.csharp:
return "cs";
case languages_1.Language.go:
return "go";
case languages_1.Language.java:
return "java";
case languages_1.Language.javascript:
return "js";
case languages_1.Language.python:
return "py";
case languages_1.Language.ruby:
return "rb";
default:
util_1.assertNever(language);
}
}
exports.getIdPrefix = getIdPrefix;
/**
* Count the lines of code of the specified language using the include
* and exclude glob paths.

Просмотреть файл

@ -1 +1 @@
{"version":3,"file":"count-loc.js","sourceRoot":"","sources":["../src/count-loc.ts"],"names":[],"mappings":";;;AAAA,qDAAyC;AAEzC,2CAAuC;AAEvC,iCAAqC;AAKrC,sFAAsF;AACtF,MAAM,iBAAiB,GAA6B;IAClD,CAAC,EAAE,oBAAQ,CAAC,GAAG;IACf,KAAK,EAAE,oBAAQ,CAAC,GAAG;IACnB,IAAI,EAAE,oBAAQ,CAAC,MAAM;IACrB,EAAE,EAAE,oBAAQ,CAAC,EAAE;IACf,IAAI,EAAE,oBAAQ,CAAC,IAAI;IACnB,UAAU,EAAE,oBAAQ,CAAC,UAAU;IAC/B,MAAM,EAAE,oBAAQ,CAAC,MAAM;IACvB,IAAI,EAAE,oBAAQ,CAAC,IAAI;IACnB,UAAU,EAAE,oBAAQ,CAAC,UAAU;CAChC,CAAC;AAEF,MAAM,cAAc,GAAG,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAC7D,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,IAAI,CAAC,EAAE,EAAE;IACnB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;QACd,GAAG,CAAC,IAAI,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AAE;QAC5B,MAAM,eAAe,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAC;QACpD,IAAI,eAAe,IAAI,WAAW,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE;YAC5D,GAAG,CAAC,eAAe,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC;SAC3D;QACD,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAA8B,CAC/B,CAAC;IAEF,IAAI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,MAAM,EAAE;QAClC,MAAM,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAC;QACrC,KAAK,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE;YAC1D,MAAM,CAAC,KAAK,CAAC,KAAK,QAAQ,KAAK,KAAK,EAAE,CAAC,CAAC;SACzC;KACF;SAAM;QACL,MAAM,CAAC,IAAI,CACT,2EAA2E;YACzE,gEAAgE;YAChE,4EAA4E;YAC5E,4EAA4E;YAC5E,2EAA2E;YAC3E,6EAA6E;YAC7E,2CAA2C,CAC9C,CAAC;KACH;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AA9CD,4BA8CC"}

Просмотреть файл

@ -9,7 +9,6 @@ import * as sinon from "sinon";
import { runQueries } from "./analyze";
import { setCodeQL } from "./codeql";
import { Config } from "./config-utils";
import { getIdPrefix } from "./count-loc";
import * as count from "./count-loc";
import { Language } from "./languages";
import { getRunnerLogger } from "./logging";
@ -68,33 +67,7 @@ test("status report fields and search path setting", async (t) => {
sarifFile,
JSON.stringify({
runs: [
// variant 1 uses ruleId
{
properties: {
metricResults: [
{
ruleId: `${getIdPrefix(
language
)}/summary/lines-of-code`,
value: 123,
},
],
},
},
// variant 2 uses rule.id
{
properties: {
metricResults: [
{
rule: {
id: `${getIdPrefix(language)}/summary/lines-of-code`,
},
value: 123,
},
],
},
},
// variant 3 references a rule with the lines-of-code tag
// references a rule with the lines-of-code tag, so baseline should be injected
{
tool: {
extensions: [
@ -231,38 +204,13 @@ test("status report fields and search path setting", async (t) => {
function verifyLineCounts(tmpDir: string) {
// eslint-disable-next-line github/array-foreach
Object.keys(Language).forEach((lang, i) => {
verifyLineCountForFile(
lang as Language,
path.join(tmpDir, `${lang}.sarif`),
i + 1
);
verifyLineCountForFile(path.join(tmpDir, `${lang}.sarif`), i + 1);
});
}
function verifyLineCountForFile(
lang: Language,
filePath: string,
lineCount: number
) {
const idPrefix = getIdPrefix(lang);
function verifyLineCountForFile(filePath: string, lineCount: number) {
const sarif = JSON.parse(fs.readFileSync(filePath, "utf8"));
t.deepEqual(sarif.runs[0].properties.metricResults, [
{
ruleId: `${idPrefix}/summary/lines-of-code`,
value: 123,
baseline: lineCount,
},
]);
t.deepEqual(sarif.runs[1].properties.metricResults, [
{
rule: {
id: `${idPrefix}/summary/lines-of-code`,
},
value: 123,
baseline: lineCount,
},
]);
t.deepEqual(sarif.runs[2].properties.metricResults, [
{
rule: {
index: 0,
@ -275,7 +223,7 @@ test("status report fields and search path setting", async (t) => {
},
]);
// when the rule doesn't exist, it should not be added
t.deepEqual(sarif.runs[3].properties.metricResults, []);
t.deepEqual(sarif.runs[1].properties.metricResults, []);
}
function verifyQuerySuites(tmpDir: string) {

Просмотреть файл

@ -7,7 +7,7 @@ import * as yaml from "js-yaml";
import * as analysisPaths from "./analysis-paths";
import { getCodeQL } from "./codeql";
import * as configUtils from "./config-utils";
import { countLoc, getIdPrefix } from "./count-loc";
import { countLoc } from "./count-loc";
import { isScannedLanguage, Language } from "./languages";
import { Logger } from "./logging";
import * as sharedEnv from "./shared-environment";
@ -415,27 +415,15 @@ async function injectLinesOfCode(
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) {
// Old style: Baseline is inserted when rule ID has suffix /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(
// the rule id can be in either of two places
(r) => r.ruleId === ruleId || r.rule?.id === ruleId
);
// only add the baseline value if the rule already exists
if (rule) {
rule.baseline = lineCounts[language];
}
// New style: Baseline is inserted when matching rule has tag lines-of-code
for (const metric of run.properties.metricResults) {
// Baseline is inserted when matching rule has tag lines-of-code
if (metric.rule && metric.rule.toolComponent) {
const matchingRule =
run.tool.extensions[metric.rule.toolComponent.index].rules[

Просмотреть файл

@ -2,10 +2,6 @@ import { LocDir } from "github-linguist";
import { Language } from "./languages";
import { Logger } from "./logging";
import { assertNever } from "./util";
// Language IDs used by codeql when specifying its metrics.
export type IdPrefix = "cpp" | "cs" | "go" | "java" | "js" | "py" | "rb";
// Map from linguist language names to language prefixes used in the action and codeql
const linguistToMetrics: Record<string, Language> = {
@ -31,28 +27,6 @@ const nameToLinguist = Object.entries(linguistToMetrics).reduce(
{} as Record<Language, string[]>
);
export function getIdPrefix(language: Language): IdPrefix {
switch (language) {
case Language.cpp:
return "cpp";
case Language.csharp:
return "cs";
case Language.go:
return "go";
case Language.java:
return "java";
case Language.javascript:
return "js";
case Language.python:
return "py";
case Language.ruby:
return "rb";
default:
assertNever(language);
}
}
/**
* Count the lines of code of the specified language using the include
* and exclude glob paths.