-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.js
108 lines (86 loc) · 3.34 KB
/
project.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
'use strict';
const request = require('request');
const q = require('q');
const apiRequest = (path, callback) => {
request({
url: path.includes('http') ? path : `https://api.github.com${path}`,
headers: {
'Authorization': `token ${process.env.GITHUB_API_KEY}`,
'User-Agent': 'request',
'Accept': 'application/vnd.github.mercy-preview+json',
}
}, (err, response, body) => callback(JSON.parse(body)));
};
const determinePullRequestStatus = reviews => {
let reviewsByUser = {};
reviews
// We only care about approvals and failures
.filter(review => ['APPROVED', 'CHANGES_REQUESTED'].indexOf(review.state) !== -1)
.map(review => reviewsByUser[review.user.login] = review.state);
let approved = true;
let numberOfApprovals = 0;
for (var key in reviewsByUser) {
const isApproved = reviewsByUser[key] === 'APPROVED';
numberOfApprovals += (isApproved ? 1 : 0);
if (!isApproved) {
approved = false;
}
}
return approved ? numberOfApprovals : -1;
}
const pullRequestData = (project, {number, title, html_url, state, head, user}) => {
const deferred = q.defer();
const prUrl = `/repos/${process.env.GITHUB_ORG}/${project}/pulls/${number}`;
const getReviewData = reviewers => apiRequest(`${prUrl}/reviews`, data => {
deferred.resolve({
number,
repo: head.repo,
reviewers,
state,
status: determinePullRequestStatus(data),
title,
url: html_url,
user,
});
});
// Get requested reviewers
apiRequest(`${prUrl}/requested_reviewers`, getReviewData);
return deferred.promise;
};
const pullRequestInfoForProject = project => {
const deferred = q.defer();
apiRequest(`/repos/${process.env.GITHUB_ORG}/${project}/pulls`, pulls => {
q.all(pulls.map(pull => pullRequestData(project, pull)))
.then(results => deferred.resolve(results));
});
return deferred.promise;
};
const getOpenPullRequests = projects => {
const deferred = q.defer();
q.all(projects.map(project => pullRequestInfoForProject(project)))
.then(pullRequestsByProject => {
let pullRequests = [];
pullRequestsByProject.forEach(projectPullRequests => {
pullRequests = pullRequests.concat(projectPullRequests);
});
const filteredPullRequests = pullRequests
// We only care about open pull requests with assigned reviewers
.filter(
pullRequest => pullRequest.state === 'open' &&
(pullRequest.reviewers.users.length > 0 || pullRequest.reviewers.teams.length > 0)
)
.sort((a, b) => a.status > b.status ? -1 : 1);
deferred.resolve(filteredPullRequests);
});
return deferred.promise;
};
const getProjectsForTopicAndOrg = (topic, org) => {
const deferred = q.defer();
apiRequest(
`/search/repositories?q=topic:${topic}+user:${org}`,
({items}) => getOpenPullRequests(items.map(repo => repo.name))
.then(deferred.resolve)
);
return deferred.promise;
};
module.exports = { pullRequestInfoForProject, getProjectsForTopicAndOrg };