'use strict'; var node_crypto = require('node:crypto'); const getPosition = (message) => { var _a, _b; let startLine = (_a = message.line) !== null && _a !== void 0 ? _a : 0; let startColumn = (_b = message.column) !== null && _b !== void 0 ? _b : 0; let endLine = startLine; let endColumn = startColumn; if (message.place) { if ('start' in message.place) { startLine = message.place.start.line; startColumn = message.place.start.column; } else { startLine = message.place.line; startColumn = message.place.column; } if ('end' in message.place) { endLine = message.place.end.line; endColumn = message.place.end.column; } else { endLine = message.place.line; endColumn = message.place.column; } } return { start: { line: startLine, column: startColumn }, end: { line: endLine, column: endColumn } }; }; 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 gitlabCodeQualityFormatter = (files, options) => { const hashes = new Set(); const results = Array.isArray(files) ? files : [files]; const issues = results.flatMap((result) => result.messages.map((message) => { var _a; const messagePosition = getPosition(message); const messageRule = (_a = message.ruleId) !== null && _a !== void 0 ? _a : 'Unknown Rule'; const messageUrl = message.url ? ` See ${message.url} for more details.` : ''; return { type: 'issue', check_name: messageRule, description: message.reason, content: { body: `Error found in ${messageRule}.${messageUrl}`, }, categories: ['Style'], location: { path: result.path, lines: { begin: messagePosition.start.line, end: messagePosition.end.line, }, positions: { begin: { line: messagePosition.start.line, column: messagePosition.start.column, }, end: { line: messagePosition.end.line, column: messagePosition.end.column, }, }, }, severity: message.fatal ? 'major' : 'minor', fingerprint: generateFingerprint([ result.path, messageRule, message.reason, `${messagePosition.start.line}`, `${messagePosition.start.column}`, ], hashes), }; })); return JSON.stringify(issues); }; module.exports = gitlabCodeQualityFormatter;