'use strict'; var node_crypto = require('node:crypto'); var node_path = require('node:path'); const getRelativePath = (path, context) => { return path ? node_path.relative(context.cwd, path) : 'unknown path'; }; const getRuleUrl = (rule, linterResult) => { if (!rule || !linterResult.ruleMetadata) { return undefined; } const metadata = linterResult.ruleMetadata[rule]; return metadata ? metadata.url : undefined; }; const generateFingerprint = (data, hashes) => { const hash = node_crypto.createHash('md5'); data.forEach((part) => { if (part) { hash.update(part.toString()); } }); // Hash collisions should not happen, but if they do, a random hash will be generated. const hashCopy = hash.copy(); let digest = hash.digest('hex'); if (hashes.has(digest)) { hashCopy.update(Math.random().toString()); digest = hashCopy.digest('hex'); } hashes.add(digest); return digest; }; const determineSeverity = (severity) => { switch (severity) { case 'error': return 'major'; case 'warning': return 'minor'; default: return 'info'; } }; const gitlabCodeQualityFormatter = (results, returnValue) => { const hashes = new Set(); const issues = results.flatMap((result) => result.warnings.map((message) => { var _a, _b, _c; return ({ type: 'issue', check_name: message.rule, description: message.text, content: { body: `Error found in ${message.rule}.${getRuleUrl(message.rule, returnValue) ? ` See ${getRuleUrl(message.rule, returnValue)} for more details.` : ''}`, }, categories: ['Style'], location: { path: getRelativePath(result.source, returnValue), lines: { begin: message.line, end: (_a = message.endLine) !== null && _a !== void 0 ? _a : message.line, }, positions: { begin: { line: message.line, column: message.column, }, end: { line: (_b = message.endLine) !== null && _b !== void 0 ? _b : message.line, column: (_c = message.endColumn) !== null && _c !== void 0 ? _c : message.column, }, }, }, severity: determineSeverity(message.severity), fingerprint: generateFingerprint([result.source, message.rule, message.text, `${message.line}`, `${message.column}`], hashes), }); })); return JSON.stringify(issues); }; module.exports = gitlabCodeQualityFormatter;