forked from mooyoul/tslint-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (153 loc) · 6.13 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core = require("@actions/core"); // tslint:disable-line
// Currently @actions/github cannot be loaded via import statement due to typing error
const github = require("@actions/github"); // tslint:disable-line
const common_tags_1 = require("common-tags");
const fs = require("fs");
const glob = require("glob");
const path = require("path");
const tslint_1 = require("tslint");
const CHECK_NAME = "TSLint Checks";
const findChangedFiles = async (ctx, octokit, pattern) => {
let files = glob.sync(pattern);
const pullRequest = ctx.payload.pull_request;
// tslint:disable-next-line:no-console
console.log("Is this pull request?", !!pullRequest);
if (pullRequest) {
const response = await octokit.pulls.listFiles({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
pull_number: pullRequest.number,
});
const changedFiles = response.data.map((f) => f.filename);
files = files.filter((f) => changedFiles.includes(f));
// tslint:disable-next-line:no-console
console.log("List of files in pull request:", files.length);
// tslint:disable-next-line:no-console
console.log("List of files in pull request (2):", changedFiles.length);
}
else {
const response = await octokit.pulls.listFiles({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
pull_number: (await octokit.repos.listPullRequestsAssociatedWithCommit({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
commit_sha: ctx.sha,
})).data[0].number,
});
const changedFiles = response.data.map((f) => f.filename);
files = files.filter((f) => changedFiles.includes(f));
// tslint:disable-next-line:no-console
console.log("List of files in commit:", files.length);
// tslint:disable-next-line:no-console
console.log("List of files in pull commit (2):", changedFiles.length);
}
return files;
};
const SeverityAnnotationLevelMap = new Map([
["warning", "warning"],
["error", "failure"],
]);
(async () => {
const ctx = github.context;
const configFileName = core.getInput("config") || "tslint.json";
const projectFileName = core.getInput("project");
const pattern = core.getInput("pattern");
const ghToken = core.getInput("token");
const onlyChangedFiles = core.getInput("only-changed-files") || false;
if (!projectFileName && !pattern) {
core.setFailed("tslint-actions: Please set project or pattern input");
return;
}
if (!ghToken) {
core.setFailed("tslint-actions: Please set token");
return;
}
const octokit = new github.GitHub(ghToken);
// Create check
const check = await octokit.checks.create({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
name: CHECK_NAME,
head_sha: ctx.payload.pull_request ? ctx.payload.pull_request.head.sha : ctx.sha,
status: "in_progress",
});
const options = {
fix: false,
formatter: "json",
};
// Create a new Linter instance
const result = await (async () => {
if (projectFileName && !pattern) {
const projectDir = path.dirname(path.resolve(projectFileName));
const program = tslint_1.Linter.createProgram(projectFileName, projectDir);
const linter = new tslint_1.Linter(options, program);
const files = tslint_1.Linter.getFileNames(program);
for (const file of files) {
const sourceFile = program.getSourceFile(file);
if (sourceFile) {
const fileContents = sourceFile.getFullText();
const configuration = tslint_1.Configuration.findConfiguration(configFileName, file).results;
linter.lint(file, fileContents, configuration);
}
}
return linter.getResult();
}
else {
const linter = new tslint_1.Linter(options);
let files = [];
if (onlyChangedFiles) {
files = await findChangedFiles(ctx, octokit, pattern);
}
else {
files = glob.sync(pattern);
}
for (const file of files) {
const fileContents = fs.readFileSync(file, { encoding: "utf8" });
const configuration = tslint_1.Configuration.findConfiguration(configFileName, file).results;
linter.lint(file, fileContents, configuration);
}
return linter.getResult();
}
})();
const annotations = result.failures.map((failure) => ({
path: failure.getFileName(),
start_line: failure.getStartPosition().getLineAndCharacter().line,
end_line: failure.getEndPosition().getLineAndCharacter().line,
annotation_level: SeverityAnnotationLevelMap.get(failure.getRuleSeverity()) || "notice",
message: `[${failure.getRuleName()}] ${failure.getFailure()}`,
}));
// Update check
await octokit.checks.update({
owner: ctx.repo.owner,
repo: ctx.repo.repo,
check_run_id: check.data.id,
name: CHECK_NAME,
status: "completed",
conclusion: result.errorCount > 0 ? "failure" : "success",
output: {
title: CHECK_NAME,
summary: `${result.errorCount} error(s), ${result.warningCount} warning(s) found`,
text: common_tags_1.stripIndent `
## Configuration
#### Actions Input
| Name | Value |
| ---- | ----- |
| config | \`${configFileName}\` |
| project | \`${projectFileName || "(not provided)"}\` |
| pattern | \`${pattern || "(not provided)"}\` |
#### TSLint Configuration
\`\`\`json
__CONFIG_CONTENT__
\`\`\`
</details>
`.replace("__CONFIG_CONTENT__", JSON.stringify(tslint_1.Configuration.readConfigurationFile(configFileName), null, 2)),
annotations,
},
});
})().catch((e) => {
console.error(e.stack); // tslint:disable-line
core.setFailed(e.message);
});