forked from stilliard/github-task-list-completed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (103 loc) · 3.7 KB
/
Copy pathindex.js
File metadata and controls
121 lines (103 loc) · 3.7 KB
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
const checkOutstandingTasks = require('./src/check-outstanding-tasks');
const ENABLE_ID_LOGS = true; // simple ID only logs, no private repo data logged
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
let response = await context.octokit.pulls.get(context.repo({
pull_number: context.payload.issue.number
}));
pr = response.data;
}
if (! pr) {
app.log('[error] not on a PR?');
return;
}
if (ENABLE_ID_LOGS) {
app.log(`PR #${pr.number}: Request received`);
}
let outstandingTasks = checkOutstandingTasks(pr.body);
// lookup comments on the PR
let comments = await context.octokit.issues.listComments(context.repo({
per_page: 100,
issue_number: pr.number
}));
if (ENABLE_ID_LOGS) {
app.log(`PR #${pr.number}: Main comments api lookup complete`);
}
// as well as review comments
let reviewComments = await context.octokit.pulls.listReviews(context.repo({
per_page: 100,
pull_number: pr.number
}));
if (reviewComments.data.length) {
comments.data = comments.data.concat(reviewComments.data);
}
if (ENABLE_ID_LOGS) {
app.log(`PR #${pr.number}: Review comments api lookup complete`);
}
// and diff level comments on reviews
let reviewDiffComments = await context.octokit.pulls.listReviewComments(context.repo({
per_page: 100,
pull_number: pr.number
}));
if (reviewDiffComments.data.length) {
comments.data = comments.data.concat(reviewDiffComments.data);
}
if (ENABLE_ID_LOGS) {
app.log(`PR #${pr.number}: Diff comments api lookup complete`);
}
// & check them for tasks
if (comments.data.length) {
comments.data.forEach(function (comment) {
let commentOutstandingTasks = checkOutstandingTasks(comment.body);
outstandingTasks.total += commentOutstandingTasks.total;
outstandingTasks.remaining += commentOutstandingTasks.remaining;
});
}
let check = {
name: 'task-list-completed',
head_branch: '',
head_sha: pr.head.sha,
started_at: (new Date).toISOString(),
status: 'in_progress',
output: {
title: (outstandingTasks.total - outstandingTasks.remaining) + ' / ' + outstandingTasks.total + ' tasks completed',
summary: outstandingTasks.remaining + ' task' + (outstandingTasks.remaining > 1 ? 's' : '') + ' still to be completed',
text: 'We check if any task lists need completing before you can merge this PR'
},
request: {
retries: 3,
retryAfter: 3,
},
};
// 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';
};
if (ENABLE_ID_LOGS) {
app.log(`PR #${pr.number}: Complete and sending back to github`);
}
// send check back to GitHub
return context.octokit.checks.create(context.repo(check));
});
};