-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (62 loc) · 2.27 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
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Application} app
*/
module.exports = app => {
// Your code here
app.log('Yay, the app was loaded!')
app.on(['pull_request', 'pull_request', 'pull_request_review', 'check_run.rerequested'], check)
async function check (context) {
const timeStart = new Date()
const { data: reviews } = await context.github.pullRequests.listReviews({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
number: context.payload.pull_request.number
})
var sortedReviews = reviews.sort(function(a, b){
return new Date(b.submitted_at) - new Date(a.submitted_at);
})
var filteredReviews = []
var reviewers = []
sortedReviews.forEach((review) => {
if (!reviewers.includes(review.user.login)) {
reviewers.push(review.user.login)
filteredReviews.push(review)
}
})
var approvedReviews = filteredReviews.filter(e => e.state == 'APPROVED').map(e => e.user.login)
var rejectedReviews = filteredReviews.filter(e => e.state == 'CHANGES_REQUESTED').map(e => e.user.login)
var checks = []
context.payload.pull_request.assignees.forEach((assignee) => {
if (approvedReviews.includes(assignee.login)) {
}
else if (rejectedReviews.includes(assignee.login)) {
checks[assignee.login] = "CHANGES_REQUESTED"
} else {
checks[assignee.login] = "PENDING"
}
})
const conclusion = Object.keys(checks).length == 0 ? 'success' : 'failure'
var title = ""
if (context.payload.pull_request.assignees.length == 0) {
title = "No assignees found for this pull request"
} else if (Object.keys(checks).length == 0) {
title = 'All assignees have approved this pull request'
} else {
title = 'All assignees need to approve this pull request'
}
return context.github.checks.create(context.repo({
name: 'AssigneeBot',
head_branch: context.payload.pull_request.head.ref,
head_sha: context.payload.pull_request.head.sha,
status: 'completed',
started_at: timeStart,
conclusion: conclusion,
completed_at: new Date(),
output: {
title: title,
summary: title
}
}))
}
}