{"version":3,"sources":["../src/index.ts","../src/util.ts"],"sourcesContent":["import { dirname, join } from \"node:path\";\nimport { cwd } from \"node:process\";\n\nimport type { IClone, IOptions } from \"@jscpd/core\";\nimport type { IReporter } from \"@jscpd/finder\";\nimport type { GitLabReportFinding } from \"./types\";\n\nimport {\n  ensureDirSync,\n  pathExistsSync,\n  readJsonSync,\n  writeFileSync,\n} from \"fs-extra\";\nimport { green } from \"colors/safe\";\nimport { getPath, describeLocation, countLines, hashFinding } from \"./util\";\n\nexport default class GitLabReporter implements IReporter {\n  constructor(private options: IOptions) {}\n\n  public report(clones: IClone[]): void {\n    if (this.options.output) {\n      const {\n        CI_PROJECT_DIR = cwd(),\n        CODE_QUALITY_APPEND = false,\n        CODE_QUALITY_REPORT = join(this.options.output, \"gl-codequality.json\"),\n      } = process.env;\n\n      const issues: GitLabReportFinding[] = clones.map(\n        ({ duplicationA, duplicationB }) => {\n          const pathA = getPath(CI_PROJECT_DIR, duplicationA.sourceId, this.options);\n          const locationA = describeLocation(pathA, duplicationA);\n          const pathB = getPath(CI_PROJECT_DIR, duplicationB.sourceId, this.options);\n          const locationB = describeLocation(pathB, duplicationB);\n\n          return {\n            check_name: \"jscpd/duplication\",\n            categories: [\"Duplication\"],\n            severity: \"minor\",\n            description: `${countLines(duplicationA)} lines of code duplicated at ${locationB}`,\n            fingerprint: hashFinding([locationA, locationB]),\n            location: {\n              path: pathA,\n              lines: {\n                begin: duplicationA.start.line,\n              },\n            },\n          };\n        },\n      );\n\n      const reportPath = CODE_QUALITY_REPORT;\n      ensureDirSync(dirname(reportPath));\n      if (CODE_QUALITY_APPEND === \"true\" && pathExistsSync(reportPath)) {\n        const existingIssues = readJsonSync(reportPath);\n        issues.push(existingIssues);\n      }\n      writeFileSync(reportPath, JSON.stringify(issues, null, 2));\n\n      console.log(green(`GitLab Code Quality report saved to ${reportPath}`));\n    }\n  }\n}\n","import crypto from \"node:crypto\";\nimport { relative } from \"node:path\";\n\nimport type { IOptions } from \"@jscpd/core\";\nimport type { Duplication } from \"./types\";\n\nexport function getPath(root: string, path: string, options: IOptions): string {\n  return options.absolute ? path : relative(root, path);\n}\n\nexport function describeLocation(\n  path: string,\n  { start, end }: Duplication,\n): string {\n  return `${path} [${start.line}:${start.column} - ${end.line}:${end.column}]`;\n}\n\nexport function countLines({ start, end }: Duplication): number {\n  return end.line - start.line;\n}\n\nexport function hashFinding(duplications: string[]): string {\n  const jsonStr = JSON.stringify(duplications);\n  return crypto.createHash(\"sha256\").update(jsonStr).digest(\"hex\");\n}\n"],"mappings":";AAAA,SAAS,SAAS,YAAY;AAC9B,SAAS,WAAW;AAMpB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,aAAa;;;ACbtB,OAAO,YAAY;AACnB,SAAS,gBAAgB;AAKlB,SAAS,QAAQ,MAAc,MAAc,SAA2B;AAC7E,SAAO,QAAQ,WAAW,OAAO,SAAS,MAAM,IAAI;AACtD;AAEO,SAAS,iBACd,MACA,EAAE,OAAO,IAAI,GACL;AACR,SAAO,GAAG,IAAI,KAAK,MAAM,IAAI,IAAI,MAAM,MAAM,MAAM,IAAI,IAAI,IAAI,IAAI,MAAM;AAC3E;AAEO,SAAS,WAAW,EAAE,OAAO,IAAI,GAAwB;AAC9D,SAAO,IAAI,OAAO,MAAM;AAC1B;AAEO,SAAS,YAAY,cAAgC;AAC1D,QAAM,UAAU,KAAK,UAAU,YAAY;AAC3C,SAAO,OAAO,WAAW,QAAQ,EAAE,OAAO,OAAO,EAAE,OAAO,KAAK;AACjE;;;ADRA,IAAqB,iBAArB,MAAyD;AAAA,EACvD,YAAoB,SAAmB;AAAnB;AAAA,EAAoB;AAAA,EAEjC,OAAO,QAAwB;AACpC,QAAI,KAAK,QAAQ,QAAQ;AACvB,YAAM;AAAA,QACJ,iBAAiB,IAAI;AAAA,QACrB,sBAAsB;AAAA,QACtB,sBAAsB,KAAK,KAAK,QAAQ,QAAQ,qBAAqB;AAAA,MACvE,I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