-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
227 lines (199 loc) · 5.57 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
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
225
226
227
/** Copyright (c) 2017 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
module.exports = robot => {
robot.on('pull_request.opened', check);
robot.on('pull_request.synchronize', check);
robot.on('issues.closed', reopenTodos);
async function reopenTodos(context) {
// re-open todos that may have been closed
const issue = context.payload.issue;
const {github} = context;
const results = await github.search.code({
q: `TODO+repo:${context.payload.repository.full_name}`,
});
const files = await Promise.all(
results.data.items.map(async item => {
let contents = '';
try {
const res = await context.github.repos.getContents(
context.repo({
path: item.path,
}),
);
contents = Buffer.from(res.data.content, 'base64').toString();
} catch (err) {
if (err.code !== 403) {
// Could be too large, ignore it
throw err;
}
}
return {
filename: item.path,
contents,
};
}),
);
for (let file of files) {
const todos = searchFile(file);
for (let todo of todos) {
if (todo.issue === issue.number) {
github.issues.update(
context.issue({
state: 'open',
}),
);
github.issues.createComment(
context.issue({
body:
'This issue was automatically re-opened because it is referenced in a TODO.',
}),
);
}
}
}
}
async function check(context) {
const {github} = context;
const pr = context.payload.pull_request;
function setStatus(status) {
const params = Object.assign(
{
sha: pr.head.sha,
context: 'probot/todos',
},
status,
);
return github.repos.createStatus(context.repo(params));
}
setStatus({
state: 'pending',
description: 'Checking for TODOs',
});
const compare = await context.github.repos.compareCommits(
context.repo({
base: pr.base.sha,
head: pr.head.sha,
}),
);
const notRemoved = compare.data.files.filter(
file => file.status !== 'removed',
);
const repoUrl = context.payload.repository.html_url;
function getBlameUrl(todo) {
return [
repoUrl,
'blame',
pr.head.sha,
`${todo.filename}#L${todo.line}`,
].join('/');
}
function getBlobUrl(todo) {
return [
repoUrl,
'blob',
pr.head.sha,
`${todo.filename}#L${todo.line}`,
].join('/');
}
const files = await Promise.all(
notRemoved.map(async file => {
let contents = '';
try {
const res = await context.github.repos.getContents(
context.repo({
path: file.filename,
ref: pr.head.sha,
}),
);
contents = Buffer.from(res.data.content, 'base64').toString();
} catch (err) {
if (err.code !== 403) {
// Could be too large, ignore it
throw err;
}
}
return {
filename: file.filename,
contents,
};
}),
);
const todos = files.reduce((acc, file) => {
return acc.concat(searchFile(file));
}, []);
const missingIssues = [];
const withIssues = [];
todos.forEach(todo => {
if (todo.issue === void 0) {
missingIssues.push(todo);
} else {
withIssues.push(todo);
}
});
// Early return if issue numbers are missing
if (missingIssues.length) {
setStatus({
state: 'failure',
description: 'TODO without open GitHub issue',
target_url: getBlameUrl(missingIssues[0]),
});
const urls = missingIssues.map(getBlobUrl);
const commentBody = ['Found TODOs without GitHub issues:', ...urls].join(
'\n',
);
context.github.issues.createComment(
context.issue({
body: commentBody,
}),
);
return;
}
const issues = withIssues.map(async todo => {
try {
const issue = await context.github.issues.get(
context.issue({
number: todo.issue,
}),
);
return {...todo, issueState: issue.data.state};
} catch (err) {
if (err.code !== 404) {
throw err;
}
return {...todo, issueState: void 0};
}
});
for (let todo of await Promise.all(issues)) {
if (todo.issueState !== 'open') {
return setStatus({
state: 'failure',
description: 'No open issue for TODO',
target_url: getBlameUrl(todo),
});
}
}
return setStatus({
state: 'success',
description: 'All TODOs have open issues',
});
}
};
const parseTodo = /TODO(?:\(#(\d+)\))?/g;
function searchFile(file) {
const lines = file.contents.split('\n');
const todos = [];
lines.forEach((line, index) => {
let todo;
while ((todo = parseTodo.exec(line)) !== null) {
todos.push({
filename: file.filename,
line: index + 1,
issue: todo[1] ? parseInt(todo[1]) : void 0,
});
}
});
return todos;
}