jscpd-gitlab-reporter
Version:
Reporter for jscpd. Generate a report in CodeClimate format for use in GitLab Code Quality Reports.
102 lines (99 loc) • 4.15 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => GitLabReporter
});
module.exports = __toCommonJS(src_exports);
var import_node_path2 = require("path");
var import_node_process = require("process");
var import_fs_extra = require("fs-extra");
var import_safe = require("colors/safe");
// src/util.ts
var import_node_crypto = __toESM(require("crypto"));
var import_node_path = require("path");
function getPath(root, path, options) {
return options.absolute ? path : (0, import_node_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 import_node_crypto.default.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 = (0, import_node_process.cwd)(),
CODE_QUALITY_APPEND = false,
CODE_QUALITY_REPORT = (0, import_node_path2.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;
(0, import_fs_extra.ensureDirSync)((0, import_node_path2.dirname)(reportPath));
if (CODE_QUALITY_APPEND === "true" && (0, import_fs_extra.pathExistsSync)(reportPath)) {
const existingIssues = (0, import_fs_extra.readJsonSync)(reportPath);
issues.push(existingIssues);
}
(0, import_fs_extra.writeFileSync)(reportPath, JSON.stringify(issues, null, 2));
console.log((0, import_safe.green)(`GitLab Code Quality report saved to ${reportPath}`));
}
}
};
//# sourceMappingURL=index.js.map