-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmarkdownlint-cli2-formatter-codequality.js
72 lines (62 loc) · 2.24 KB
/
markdownlint-cli2-formatter-codequality.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
// @ts-check
"use strict";
const fs = require("node:fs").promises;
const path = require("node:path");
const { createHash } = require("node:crypto");
/**
* @param {string} violation The complete textual description of the violation.
* @returns {string} The SHA256 fingerprint for the violation as a hex string.
*/
const createFingerprint = function createFingerprint(violation) {
const sha256 = createHash("sha256");
sha256.update(violation);
return sha256.digest("hex");
};
// Writes markdownlint-cli2 results to a GitLab Code Quality report JSON file.
// See: https://docs.gitlab.com/ee/ci/testing/code_quality.html#implementing-a-custom-tool
const outputFormatter = (options, params) => {
const { directory, results } = options;
const { name } = (params || {});
const issues = [];
for (const errorInfo of results) {
const { fileName, lineNumber, ruleNames, ruleDescription, errorDetail,
errorContext, errorRange } = errorInfo;
const ruleName = ruleNames.join("/");
const errorDetailText = errorDetail ? ` [${errorDetail}]` : "";
const text = `${ruleName}: ${ruleDescription}${errorDetailText}`;
const column = (errorRange && errorRange[0]) || 0;
const columnText = column ? `:${column}` : "";
const description = ruleDescription +
(errorDetail ? ` [${errorDetail}]` : "") +
(errorContext ? ` [Context: "${errorContext}"]` : "");
// Construct error text with all details to use for unique fingerprint.
// Avoids duplicate fingerprints for the same violation on multiple lines.
const errorText =
`${fileName}:${lineNumber}${columnText} ${ruleName} ${description}`;
const issue = {
"type": "issue",
"check_name": ruleName,
"description": text,
"severity": "minor",
"fingerprint": createFingerprint(errorText),
"location": {
"path": fileName,
"lines": {
"begin": lineNumber
}
}
};
issues.push(issue);
}
const content = JSON.stringify(issues, null, 2);
return fs.writeFile(
path.resolve(
// eslint-disable-next-line no-inline-comments
directory /* c8 ignore next */ || "",
name || "markdownlint-cli2-codequality.json"
),
content,
"utf8"
);
};
module.exports = outputFormatter;