-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (70 loc) · 1.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { ESLint } = require('eslint');
const Crypto = require('node:crypto');
const Path = require('node:path');
/**
* @param {any[]} data
*/
function fingerprint(data) {
const hash = Crypto.createHash('md5');
for (const part of data) {
// Skip empty values
if (!part) continue;
hash.update(part.toString());
}
return hash.digest('hex');
}
/**
* @param {string} path
* @param {ESLint.LintResultData} context
* @returns {string}
*/
function getRelativePath(path, context) {
const root = process.env.CI_PROJECT_DIR ?? context.cwd;
return Path.relative(root, path);
}
/**
* @param {ESLint.LintResult[]} results
* @param {ESLint.LintResultData} context
* @returns {GitLabCodeQuality.Issue[]}
*/
function convert(results, context) {
/** @type {GitLabCodeQuality.Issue[]} */
const issues = [];
for (const result of results) {
for (const message of result.messages) {
// Skip severity "off"
if (message.severity === 0) continue;
issues.push({
description: message.message,
check_name: message.ruleId ?? 'unknown',
fingerprint: fingerprint([result.filePath, message.line, message.ruleId, message.message]),
severity: message.fatal ? 'critical' : message.severity === 1 ? 'minor' : 'major',
location: {
path: getRelativePath(result.filePath, context),
lines: {
begin: message.line,
},
},
});
}
}
return issues;
}
/**
* @param {ESLint.LintResult[]} results
* @param {ESLint.LintResultData} context
* @returns {Promise<string>}
*/
module.exports = async function (results, context) {
// Create console formatter
const instance = new ESLint();
const consoleFormatter = await instance.loadFormatter('stylish');
// Output console
const consoleOutput = await consoleFormatter.format(results, context);
process.stdout.write(consoleOutput);
// Generate GitLab Code Quality JSON
const issues = convert(results, context);
// Format as JSON and return
const json = JSON.stringify(issues, null, '\t');
return json;
};