jscpd-gitlab-reporter
Version:
Reporter for jscpd. Generate a report in CodeClimate format for use in GitLab Code Quality Reports.
76 lines (74 loc) • 2.47 kB
JavaScript
// src/index.ts
import { dirname, join } from "node:path";
import { cwd } from "node:process";
import {
ensureDirSync,
pathExistsSync,
readJsonSync,
writeFileSync
} from "fs-extra";
import { green } from "colors/safe";
// src/util.ts
import crypto from "node:crypto";
import { relative } from "node:path";
function getPath(root, path, options) {
return options.absolute ? path : relative(root, path);
}
function describeLocation(path, { start, end }) {
return `${path} [${start.line}:${start.column} - ${end.line}:${end.column}]`;
}
function countLines({ start, end }) {
return end.line - start.line;
}
function hashFinding(duplications) {
const jsonStr = JSON.stringify(duplications);
return crypto.createHash("sha256").update(jsonStr).digest("hex");
}
// src/index.ts
var GitLabReporter = class {
constructor(options) {
this.options = options;
}
report(clones) {
if (this.options.output) {
const {
CI_PROJECT_DIR = cwd(),
CODE_QUALITY_APPEND = false,
CODE_QUALITY_REPORT = join(this.options.output, "gl-codequality.json")
} = process.env;
const issues = clones.map(
({ duplicationA, duplicationB }) => {
const pathA = getPath(CI_PROJECT_DIR, duplicationA.sourceId, this.options);
const locationA = describeLocation(pathA, duplicationA);
const pathB = getPath(CI_PROJECT_DIR, duplicationB.sourceId, this.options);
const locationB = describeLocation(pathB, duplicationB);
return {
check_name: "jscpd/duplication",
categories: ["Duplication"],
severity: "minor",
description: `${countLines(duplicationA)} lines of code duplicated at ${locationB}`,
fingerprint: hashFinding([locationA, locationB]),
location: {
path: pathA,
lines: {
begin: duplicationA.start.line
}
}
};
}
);
const reportPath = CODE_QUALITY_REPORT;
ensureDirSync(dirname(reportPath));
if (CODE_QUALITY_APPEND === "true" && pathExistsSync(reportPath)) {
const existingIssues = readJsonSync(reportPath);
issues.push(existingIssues);
}
writeFileSync(reportPath, JSON.stringify(issues, null, 2));
console.log(green(`GitLab Code Quality report saved to ${reportPath}`));
}
}
};
export {
GitLabReporter as default
};
//# sourceMappingURL=index.mjs.map