-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathreviewerChecklist.js
103 lines (90 loc) · 4.47 KB
/
reviewerChecklist.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
const core = require('@actions/core');
const github = require('@actions/github');
const https = require('https');
const GitHubUtils = require('../../../libs/GithubUtils');
const pathToReviewerChecklist = 'https://raw.githubusercontent.com/Expensify/App/main/contributingGuides/REVIEWER_CHECKLIST.md';
const reviewerChecklistStartsWith = '## Reviewer Checklist';
const issue = github.context.payload.issue ? github.context.payload.issue.number : github.context.payload.pull_request.number;
const combinedComments = [];
/**
* @returns {Promise}
*/
function getNumberOfItemsFromReviewerChecklist() {
console.log('Getting the number of items in the reviewer checklist...');
return new Promise((resolve, reject) => {
https.get(pathToReviewerChecklist, (res) => {
let fileContents = '';
res.on('data', (chunk) => {
fileContents += chunk;
});
res.on('end', () => {
const numberOfChecklistItems = (fileContents.match(/- \[ \]/g) || []).length;
console.log(`There are ${numberOfChecklistItems} items in the reviewer checklist.`);
resolve(numberOfChecklistItems);
});
})
.on('error', (err) => {
console.error(err);
reject(err);
});
});
}
/**
* @param {Number} numberOfChecklistItems
*/
function checkIssueForCompletedChecklist(numberOfChecklistItems) {
GitHubUtils.getAllReviewComments(issue)
.then((reviewComments) => {
console.log(`Pulled ${reviewComments.length} review comments, now adding them to the list...`);
combinedComments.push(...reviewComments);
})
.then(() => GitHubUtils.getAllComments(issue))
.then((comments) => {
console.log(`Pulled ${comments.length} comments, now adding them to the list...`);
combinedComments.push(...comments);
})
.then(() => {
console.log(`Looking through all ${combinedComments.length} comments for the reviewer checklist...`);
let foundReviewerChecklist = false;
let numberOfFinishedChecklistItems = 0;
let numberOfUnfinishedChecklistItems = 0;
// Once we've gathered all the data, loop through each comment and look to see if it contains the reviewer checklist
for (let i = 0; i < combinedComments.length; i++) {
// Skip all other comments if we already found the reviewer checklist
if (foundReviewerChecklist) {
return;
}
const whitespace = /([\n\r])/gm;
const comment = combinedComments[i].replace(whitespace, '');
console.log(`Comment ${i} starts with: ${comment.slice(0, 20)}...`);
// Found the reviewer checklist, so count how many completed checklist items there are
if (comment.startsWith(reviewerChecklistStartsWith)) {
console.log('Found the reviewer checklist!');
foundReviewerChecklist = true;
numberOfFinishedChecklistItems = (comment.match(/- \[x\]/gi) || []).length;
numberOfUnfinishedChecklistItems = (comment.match(/- \[ \]/g) || []).length;
}
}
if (!foundReviewerChecklist) {
core.setFailed('No PR Reviewer Checklist was found');
return;
}
const maxCompletedItems = numberOfChecklistItems + 2;
const minCompletedItems = numberOfChecklistItems - 2;
console.log(`You completed ${numberOfFinishedChecklistItems} out of ${numberOfChecklistItems} checklist items with ${numberOfUnfinishedChecklistItems} unfinished items`);
if (numberOfFinishedChecklistItems >= minCompletedItems
&& numberOfFinishedChecklistItems <= maxCompletedItems
&& numberOfUnfinishedChecklistItems === 0) {
console.log('PR Reviewer checklist is complete 🎉');
return;
}
console.log(`Make sure you are using the most up to date checklist found here: ${pathToReviewerChecklist}`);
core.setFailed('PR Reviewer Checklist is not completely filled out. Please check every box to verify you\'ve thought about the item.');
});
}
getNumberOfItemsFromReviewerChecklist()
.then(checkIssueForCompletedChecklist)
.catch((err) => {
console.error(err);
core.setFailed(err);
});