forked from DavidAnson/markdownlint-cli2-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdownlint-cli2-action.js
77 lines (71 loc) · 2.21 KB
/
markdownlint-cli2-action.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
// @ts-check
"use strict";
const core = require("@actions/core");
const { "main": markdownlintCli2 } = require("markdownlint-cli2");
const logMessage = core.info;
const outputFormatter = (options) => {
const { results } = options;
for (const lintError of results) {
const {
errorContext,
errorDetail,
errorRange,
fileName,
lineNumber,
ruleDescription,
ruleInformation,
ruleNames
} = lintError;
const line = `:${lineNumber}`;
const column = errorRange ? `:${errorRange[0]}` : "";
const name = ruleNames.join("/");
const detail = errorDetail ? `[${errorDetail}]` : "";
const context = errorContext ? `[Context: "${errorContext}"]` : "";
const information = ruleInformation ? `${ruleInformation}` : "";
const repository = process.env.GITHUB_REPOSITORY;
const headRef = process.env.GITHUB_HEAD_REF;
const fileUrl = `https://github.com/${repository}/blob/${headRef}/${fileName}#L${lineNumber}`;
const ignoreRule = `Please correct the violation or ignore the rule <!-- markdownlint-disable-next-line ${ruleNames[0]} -->`;
const message =
`${fileName}${line}${column} ${fileUrl} ${name} ${ruleDescription} ${detail}${context} ${information} ${ignoreRule}`;
const annotation = {
"title": ruleDescription,
"file": fileName,
"startLine": lineNumber,
"endLine": lineNumber
};
if (errorRange) {
const [
errorColumn,
errorLength
] = errorRange;
annotation.startColumn = errorColumn;
annotation.endColumn = errorColumn + errorLength - 1;
}
core.error(message, annotation);
}
};
const separator = core.getInput("separator") || "\n";
const argv =
core.getInput("globs").
split(separator).
filter(String);
const config = core.getInput("config");
if (config) {
argv.push("--config", config);
}
const fix = Boolean(core.getInput("fix"));
if (fix) {
argv.push("--fix");
}
const parameters = {
argv,
logMessage,
"optionsOverride": {
"outputFormatters": [ [ outputFormatter ] ]
}
};
markdownlintCli2(parameters).then(
(code) => code && core.setFailed(`Failed with exit code: ${code}`),
(error) => core.setFailed(`Failed due to error: ${error}`)
);