Skip to content

Commit

Permalink
Showing 9 changed files with 15 additions and 179 deletions.
12 changes: 1 addition & 11 deletions lib/analyze.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/analyze.js.map

Large diffs are not rendered by default.

50 changes: 4 additions & 46 deletions lib/analyze.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/analyze.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 1 addition & 23 deletions lib/count-loc.js
2 changes: 1 addition & 1 deletion lib/count-loc.js.map
60 changes: 4 additions & 56 deletions src/analyze.test.ts
@@ -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) {
16 changes: 2 additions & 14 deletions src/analyze.ts
@@ -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[
26 changes: 0 additions & 26 deletions src/count-loc.ts
@@ -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.

0 comments on commit 2e71e02

Please sign in to comment.