-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.js
221 lines (193 loc) · 7.1 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
const checkOutstandingTasks = require('./src/check-outstanding-tasks');
const ENABLE_ID_LOGS = true; // Repo name & ID only for logs, no private data logged! (Repo name only needed to help with issue reports & debugging).
module.exports = (app) => {
app.log('Yay! The app was loaded!');
// watch for pull requests & their changes
app.on([
'pull_request.opened',
'pull_request.edited',
'pull_request.synchronize',
'issue_comment', // for comments on GitHub issues
'pull_request_review', // reviews
'pull_request_review_comment', // comment lines on diffs for reviews
], async context => {
// lookup the pr
let pr = context.payload.pull_request;
// check if this is an issue rather than pull event
if (context.name == 'issue_comment' && ! pr) {
// if so we need to make sure this is for a PR only
if (! context.payload.issue.pull_request) {
return;
}
// & lookup the PR it's for to continue
try {
let response = await context.octokit.pulls.get(context.repo({
pull_number: context.payload.issue.number
}));
pr = response.data;
// cleanup
response = null;
} catch (err) {
context.log.error(`Error looking up PR, skipping. Error (${err.status}): ${err.message}`);
}
}
if (! pr) {
context.log.error(`Not on a PR? Skipping. context.name: ${context.name}`);
return;
}
// pr details
let prRepo = pr.head.repo.full_name;
let prNumber = pr.number;
let prHeadSha = pr.head.sha;
let prBody = pr.body;
let prUser = pr.user.login;
// cleanup
pr = null;
// log helper
function log(message, type = 'info') {
if (ENABLE_ID_LOGS) {
context.log[type](`PR ${prRepo}#${prNumber}: ${message}`);
}
}
log(`Request received [Context: ${context.id}]`);
// if the author is a renovate bot, ignore checks
// https://www.mend.io/free-developer-tools/renovate/
if (prUser.indexOf('renovate[bot]') !== -1) {
prBody = null;
}
let outstandingTasks = checkOutstandingTasks(prBody);
// lookup comments on the PR
let comments;
try {
comments = await context.octokit.issues.listComments(context.repo({
per_page: 100,
issue_number: prNumber
}));
// bots to ignore
let bots = [
'linear', // ref https://github.com/stilliard/github-task-list-completed/issues/33
'linear[bot]',
];
// filter out comments from the bot
comments.data = comments.data.filter(comment => {
return ! bots.includes(comment.user.login);
});
// cleanup
bots = null;
} catch (err) {
if (err.status === 403) { // if we don't have access to the repo, skip entirely
log(`No access, skipping entirely. Error (${err.status}): ${err.message}`, 'error');
return;
}
log(`Error looking up comments, skipping. Error (${err.status}): ${err.message}`, 'error');
}
log('Main comments api lookup complete');
// as well as review comments
let reviewComments;
try {
reviewComments = await context.octokit.pulls.listReviews(context.repo({
per_page: 100,
pull_number: prNumber
}));
if (reviewComments.data.length) {
comments.data = comments.data.concat(reviewComments.data);
}
// cleanup
reviewComments = null;
} catch (err) {
log(`Error looking up review comments, skipping. Error (${err.status}): ${err.message}`, 'error');
}
log('Review comments api lookup complete');
// and diff level comments on reviews
try {
let reviewDiffComments = await context.octokit.pulls.listReviewComments(context.repo({
per_page: 100,
pull_number: prNumber
}));
if (reviewDiffComments.data.length) {
comments.data = comments.data.concat(reviewDiffComments.data);
}
// cleanup
reviewDiffComments = null;
} catch (err) {
log(`Error looking up review diff comments, skipping. Error (${err.status}): ${err.message}`, 'error');
}
log('Diff comments api lookup complete');
// & check them for tasks
if (comments && comments.data && comments.data.length) {
comments.data.forEach(function (comment) {
let commentOutstandingTasks = checkOutstandingTasks(comment.body);
outstandingTasks.total += commentOutstandingTasks.total;
outstandingTasks.remaining += commentOutstandingTasks.remaining;
outstandingTasks.optionalTotal += commentOutstandingTasks.optionalTotal;
outstandingTasks.optionalRemaining += commentOutstandingTasks.optionalRemaining;
outstandingTasks.tasks = (outstandingTasks.tasks || []).concat(commentOutstandingTasks.tasks || []);
outstandingTasks.optionalTasks = (outstandingTasks.optionalTasks || []).concat(commentOutstandingTasks.optionalTasks || []);
});
}
// optional addon text
let optionalText = '';
if (outstandingTasks.optionalRemaining > 0) {
optionalText = ' (+' + outstandingTasks.optionalRemaining + ' optional)';
}
// make a markdown table of the tasks
let tasksTable = '';
if (outstandingTasks.total > 0) {
tasksTable += `
## Required Tasks
| Task | Status |
| ---- | ------ |
${outstandingTasks.tasks.map(task => `| ${task.task} | ${task.status} |`).join('\n')}
`;
}
if (outstandingTasks.optionalTotal > 0) {
tasksTable += `
## Optional Tasks
| Task | Status |
| ---- | ------ |
${outstandingTasks.optionalTasks.map(task => `| ${task.task} | ${task.status} |`).join('\n')}
`;
}
let check = {
name: 'task-list-completed',
head_branch: '',
head_sha: prHeadSha,
started_at: (new Date).toISOString(),
status: 'in_progress',
output: {
title: (outstandingTasks.total - outstandingTasks.remaining) + ' / ' + outstandingTasks.total + ' tasks completed' + optionalText,
summary: outstandingTasks.remaining + ' task' + (outstandingTasks.remaining > 1 ? 's' : '') + ' still to be completed' + optionalText,
text: tasksTable
},
request: {
// timeout the request after 3 minutes
timeout: 1000 * 60 * 3,
// retry up to 10 times on request timeouts
retries: 10,
retryAfter: 10, // wait 10 seconds
},
};
// all finished?
if (outstandingTasks.remaining === 0) {
check.status = 'completed';
check.conclusion = 'success';
check.completed_at = (new Date).toISOString();
check.output.summary = 'All tasks have been completed' + optionalText;
};
log('Complete and sending back to GitHub');
// cleanup
prBody = null;
outstandingTasks = null;
comments = null;
tasksTable = null;
optionalText = null;
// send check back to GitHub
try {
const response = await context.octokit.checks.create(context.repo(check));
log(`Check response status from GitHub ${response.status} [X-GitHub-Request-Id: ${response.headers['x-github-request-id']}]`);
} catch (err) {
log(`Error sending check back to GitHub. Error (${err.status}): ${err.message}`, 'error');
}
return;
});
};