-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
reviewCheck.js
109 lines (89 loc) · 2.93 KB
/
reviewCheck.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
/**
* @fileoverview Checks if the at least one user has approved the review
* @author Gyandeep Singh
*/
"use strict";
const { getPullrequestBySha, labels } = require("./common");
/**
* Gets all the reviews for the PR number
* @param {Object} context context given by the probot
* @param {int} prId pull request number
* @returns {Promise.<Array>} Resolves with all reviews collection
*/
async function getAllReviewsByPR(context, prId) {
const { data: allReviews } = await context.github.pullRequests.getReviews(context.repo({
number: prId
}));
return allReviews;
}
/**
* Sorts all the reviews by creating buckets based on the user
* @param {Array<Object>} allReviews collection of reviews
* @returns {Map} Map of reviews to users
*/
function sortReviewsByUser(allReviews) {
return allReviews.reduceRight((coll, review) => {
if (coll.has(review.user.login)) {
coll.get(review.user.login).push(review);
} else {
coll.set(review.user.login, []);
}
return coll;
}, new Map());
}
/**
* Sorts all the reviews based on submitted date time
* @param {Array<Object>} allReviews collection of sorted reviews
* @returns {Array<Object>} A list of sorted reviews
*/
function sortReviewsByDtTm(allReviews) {
return allReviews.sort(
(r1, r2) => new Date(r1.submitted_at).getTime() > new Date(r2.submitted_at).getTime()
);
}
/**
* Check to see if atleast one review has been approved.
* if their are no reviews then it will say false
* @param {Map} allReviewsMap Map of reviews to users
* @returns {boolean} true id atleast one is approved
*/
function isAtleastOneApproved(allReviewsMap) {
const relevantReviewCollection = [];
allReviewsMap.forEach(reviews => {
// find the first APPROVED or CHANGES_REQUESTED for each user
const reviewObj = reviews.find(
review =>
review.state === labels.reviewApproved ||
review.state === labels.reviewChangeRequested
);
if (reviewObj) {
relevantReviewCollection.push(reviewObj);
}
});
const sortedRelevantReviews = sortReviewsByDtTm(relevantReviewCollection);
if (
sortedRelevantReviews.length > 0 &&
sortedRelevantReviews[0].state === labels.reviewApproved
) {
return true;
}
return false;
}
/**
* Check if the review on the PR is good or not
* @param {Object} context context given by the probot
* @param {string} sha git sha value
* @returns {Promise.<boolean>} True if its good
*/
async function isPrReviewSuccess(context, sha) {
const prObject = await getPullrequestBySha(context, sha);
if (prObject) {
// makesure at least one review is approved
const allReviews = await getAllReviewsByPR(context, prObject.number);
return isAtleastOneApproved(sortReviewsByUser(allReviews));
}
return false;
}
module.exports = {
isPrReviewSuccess
};