-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
executable file
·224 lines (206 loc) · 5.92 KB
/
index.ts
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
// tslint:disable:no-var-requires
require("ts-node/register/transpile-only");
import * as path from "path";
require("dotenv").config({
path: process.env.ENV_FILE || path.join(process.cwd(), ".env")
});
import { createAppAuth } from "@octokit/auth-app";
import { Octokit } from "@octokit/rest";
import { eslintCheck } from "./eslint";
import { tslintCheck } from "./tslint";
import { typescriptCheck } from "./typescript";
const TSLINT_CHECK_APP_ID = 42099;
export type GithubInfo = {
/**
* An Octokit instance, authenticated as a github app with checks:write permission
*/
github: Octokit;
owner: string;
repo: string;
sha?: string;
};
export interface CheckResult {
consoleOutput: string;
annotations: GithubCheckAnnotation[];
errorCount: number;
warningCount: number;
}
import yargs from "yargs";
import { getGitRepositoryDirectoryForFile, getGitSHA } from "./git-helpers";
import { GithubCheckAnnotation } from "./octokit-types";
// tslint:disable-next-line: no-unused-expression
yargs
.epilogue(
`
This tool gets TypeScript, TSLint, or ESLint diagnostics and posts results as a "check run" to the given GitHub repository.
The following environment variables, corresponding to a GitHub app with 'checks:write' permission, are used to authenticate with the GitHub API:
GITHUB_APP_PRIVATE_KEY
GITHUB_APP_INSTALLATION_ID
GITHUB_APP_CLIENT_ID
GITHUB_APP_CLIENT_SECRET
They can also be provided in a ".env" file in the current working directory.`
)
.option("repo", {
describe: 'The github repository, "owner/repo"',
type: "string"
})
.options("sha", {
describe: "The git sha to which to post check results. Defaults to HEAD",
type: "string"
})
.options("label", {
describe: "A label for this check run",
type: "string"
})
.command(
"tsc <tsconfig>",
"Check TypeScript errors",
builder =>
builder.positional("tsconfig", {
describe: "Path to the TypeScript project configuration file",
type: "string",
default: path.join(process.cwd(), "tsconfig.json")
}),
argv => {
runCheck("Typescript", () => typescriptCheck(argv.tsconfig), {
...argv,
location: argv.tsconfig
});
}
)
.command(
"tslint <tsconfig>",
"Check TSLint errors",
builder =>
builder.positional("tsconfig", {
describe: "Path to the TypeScript project configuration file",
type: "string",
default: path.join(process.cwd(), "tsconfig.json")
}),
argv =>
runCheck("TSLint", () => tslintCheck({ tsConfigFile: argv.tsconfig }), {
...argv,
location: argv.tsconfig
})
)
.command(
"eslint <directory>",
"Check ESLint errors",
builder =>
builder
.positional("directory", {
describe: "Location of files to lint",
type: "string",
demand: true
})
.option("overrideConfig", {
describe: "ESLint configuration overrides as a JSON string",
type: "string",
default: "{}"
}),
argv =>
runCheck(
"ESLint",
() =>
eslintCheck([argv.directory || "."], JSON.parse(argv.overrideConfig)),
{
...argv,
location: argv.directory || "."
}
)
).argv;
async function runCheck(
commandName: string,
check: () => Promise<CheckResult>,
options: {
location: string;
label?: string;
repo?: string;
sha?: string;
}
) {
let gh: GithubInfo | undefined;
if (options.repo) {
const [owner, repo] = options.repo.split("/");
if (!owner || !repo) {
console.error(
`Invalid --repo argument ${options.repo}. Expected "owner/repo".`
);
process.exit(1);
}
gh = {
github: await authenticate(),
owner,
repo,
sha: options.sha
};
}
const commandAndLabel = options.label
? `${commandName} - ${options.label}`
: commandName;
const baseDir = getGitRepositoryDirectoryForFile(options.location);
let ghCheck;
if (gh) {
ghCheck = await gh.github.checks.create({
owner: gh.owner,
repo: gh.repo,
head_sha: gh.sha || getGitSHA(baseDir),
name: commandAndLabel,
status: "in_progress"
});
console.log(`Created check ${ghCheck.data.id} (${ghCheck.data.url})`);
}
const linterResult = await check();
const annotations = linterResult.annotations.map(a => ({
...a,
path: path.relative(baseDir, a.path) // patch file paths to be relative to git root
}));
const summary = `${linterResult.errorCount} errors, ${linterResult.warningCount} warnings.`;
const conclusion =
linterResult.errorCount + linterResult.warningCount > 0
? "failure"
: "success";
console.log(`${commandAndLabel}: ${summary}`);
console.log(linterResult.consoleOutput);
if (gh && ghCheck) {
for (
let updateCount = 0;
updateCount === 0 || annotations.length > 0;
updateCount++
) {
const batch = annotations.splice(0, 50);
const update = await gh.github.checks.update({
check_run_id: ghCheck.data.id,
owner: gh.owner,
repo: gh.repo,
output: {
annotations: batch,
summary,
title: commandAndLabel
},
conclusion
});
console.log(
`Updated check ${update.data.id} with ${batch.length} annotations.`
);
}
}
if (conclusion === "failure") {
process.exit(1);
}
}
async function authenticate(): Promise<Octokit> {
const auth = createAppAuth({
id: TSLINT_CHECK_APP_ID,
privateKey: process.env.GITHUB_APP_PRIVATE_KEY || "",
installationId: Number(process.env.GITHUB_APP_INSTALLATION_ID),
clientId: process.env.GITHUB_APP_CLIENT_ID,
clientSecret: process.env.GITHUB_APP_CLIENT_SECRET
});
// Retrieve installation access token
const installationAuthentication = await auth({ type: "installation" });
return new Octokit({
auth: installationAuthentication.token
});
}