-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.js
310 lines (271 loc) · 8.76 KB
/
lib.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"use strict";
var request = require("request");
var moment = require("moment");
var Promise = require('bluebird');
var _ = require("lodash");
var linkParser = require('parse-link-header');
var mongoose = require('mongoose');
var _get = Promise.promisify(request.get, {multiArgs: true});
var get = require("./cache")(_get);
var PullRequest = require("./models/PullRequest").PullRequest;
var Comment = require("./models/Comment");
var User = require("./models/User");
var connectionTemplate = _.template("mongodb://${url}/${db}");
Promise.promisifyAll(mongoose);
module.exports = class ContribCat {
constructor(config) {
this.config = config;
this.getUserTemplate = _.template("${apiUrl}/users/${username}");
this.getPullsTemplate = _.template("${apiUrl}/repos/${org}/${repo}/pulls?page=${page}&per_page=${size}&state=all&base=${head}&sort=updated&direction=desc");
this.cutOffDate = moment().startOf("day").subtract(this.config.syncDays, "days");
mongoose.connect(connectionTemplate(this.config.store), { keepAlive: 120 });
}
load() {
if (this.config.caching) {
get.load();
}
var results = this.getPullRequestsForRepos(this.config)
.then(this.getCommentsOnCodeForPullRequestsBatch.bind(this))
.then(this.getCommentsOnIssueForPullRequestsBatch.bind(this));
if (this.config.caching) {
results.then(get.dump);
}
return results;
}
sync() {
return this.createUsers()
.then(this.fetchUserDetails.bind(this))
.then(this.saveUsers.bind(this))
.then(this.getUserStatistics.bind(this))
.then(this.runPluginsForSync.bind(this))
.then(this.saveUsers.bind(this))
.then(this.saveComments.bind(this));
}
_fetchPullRequests(url, repo) {
return get(url, repo).spread((response, body) => {
body = _.cloneDeep(body);
var links = linkParser(response.headers.link);
var items = body.filter((item) => {
return moment(item.updated_at).isAfter(this.cutOffDate);
});
return Promise.map(items, (item) => {
item.base.repo.full_name = item.base.repo.full_name.toLowerCase();
item.user.login = item.user.login.toLowerCase();
return PullRequest.findOneAndUpdate({"url": item.url}, item, {"upsert": true}).execAsync().reflect();
}).then(() => {
if (links && links.next && items.length === body.length) {
return this._fetchPullRequests(links.next.url, repo);
}
});
});
}
_fetchCommentsForPullRequest(url, pr_url, repo) {
return get(url, repo).spread((response, body) => {
var links = linkParser(response.headers.link);
body = _.cloneDeep(body);
body.forEach((comment) => {
comment.user.login = comment.user.login.toLowerCase();
if (!comment.pull_request_url) {
comment.pull_request_url = pr_url;
}
});
return Promise.map(body, (item) => {
return Comment.createAsync(item).reflect();
}).then(() => {
if (links && links.next) {
return this._fetchCommentsForPullRequest(links.next.url, pr_url, repo);
}
});
});
}
getPullRequestsForRepos() {
var query = {"$or": []};
return Promise.all(this.config.repos.map((target) => {
var parts = target.split(":");
var head = parts[1];
var repo = parts[0];
var url = this.getPullsTemplate({
"apiUrl": this.config.apiUrl,
"org": repo.split("/")[0],
"repo": repo.split("/")[1],
"head": head || this.config.defaultBranch,
"page": 1,
"size": this.config.pageSize
});
query.$or.push({"base.repo.full_name": repo.toLowerCase()});
return this._fetchPullRequests(url, repo);
})).then(() => {
query.updated_at = {$gt: this.cutOffDate.toDate()};
return PullRequest.find(query).lean().execAsync();
});
}
getCommentsOnCodeForPullRequests(prs) {
return Promise.map(prs, (pr) => {
return this._fetchCommentsForPullRequest(pr.review_comments_url, pr.url, pr.base.repo.full_name);
}).then(() => {
return prs;
});
}
getCommentsOnCodeForPullRequestsBatch(prs) {
var chunkedArray = _.chunk(prs, 10);
var first = chunkedArray.shift();
var finish = chunkedArray.reduce((defPrevious, current, currentIndex) => {
return defPrevious.then(() => {
console.log("Processing Pull Requests Comments batch", currentIndex + 1, "of", chunkedArray.length);
return this.getCommentsOnCodeForPullRequests(current);
});
}, this.getCommentsOnCodeForPullRequests(first));
return finish.then(() => {
return prs;
});
}
getCommentsOnIssueForPullRequests(prs) {
return Promise.map(prs, (pr) => {
return this._fetchCommentsForPullRequest(pr.comments_url, pr.url, pr.base.repo.full_name)
}).then(() => {
return prs;
});
}
getCommentsOnIssueForPullRequestsBatch(prs) {
var chunkedArray = _.chunk(prs, 10);
var first = chunkedArray.shift();
var finish = chunkedArray.reduce((defPrevious, current, currentIndex) => {
return defPrevious.then(() => {
console.log("Processing Issue Comments batch", currentIndex + 1, "of", chunkedArray.length);
return this.getCommentsOnIssueForPullRequests(current);
});
}, this.getCommentsOnIssueForPullRequests(first));
return finish.then(() => {
return prs;
});
}
createUsers() {
return User.find().lean().execAsync().then((users) => {
users = _.keyBy(users, 'name');
return PullRequest.findAsync({ updated_at: {$gt: this.cutOffDate.toDate()}}).map((pr) => {
var author = pr.user.login;
if (!users[author]) {
users[author] = {
"repos": [],
"name": author.toLowerCase(),
"gravatar": pr.user.avatar_url
};
}
let authorRepo = _.find(users[author].repos, { 'name': pr.base.repo.full_name});
if (!authorRepo) {
authorRepo = {
"name": pr.base.repo.full_name,
"prs": [],
"for": [],
"against": []
};
users[author].repos.push(authorRepo);
}
if (_.findIndex(authorRepo.prs, function(o) {return pr._id.equals(o);}) === -1) {
authorRepo.prs.push(pr);
}
return Comment.find({"pull_request_url": pr.url }).lean().execAsync().map((comment) => {
var commenter = comment.user.login;
if (!users[commenter]) {
users[commenter] = {
"repos": [],
"name": commenter.toLowerCase(),
"gravatar": comment.user.avatar_url
};
}
let commenterRepo = _.find(users[commenter].repos, { 'name': pr.base.repo.full_name});
if (!commenterRepo) {
commenterRepo = {
"name": pr.base.repo.full_name,
"prs": [],
"for": [],
"against": []
};
users[commenter].repos.push(commenterRepo);
}
if (comment.user.login !== author) {
if (_.findIndex(authorRepo.against, function(o) {return comment._id.equals(o);}) === -1) {
authorRepo.against.push(comment);
}
if (_.findIndex(commenterRepo.for, function(o) {return comment._id.equals(o);}) === -1) {
commenterRepo.for.push(comment);
}
}
});
}).then(() => {
return users;
});
});
}
getUserStatistics() {
var sinceQuery = {
$gt: moment().startOf("day").subtract(this.config.reportDays, "days").toDate()
};
return User.find()
.populate({
path: 'repos.prs',
match: { "created_at": sinceQuery }})
.populate({
path: 'repos.for repos.against',
match: { "updated_at": sinceQuery },
select: 'path body html_url user.login filtered'})
.lean()
.execAsync().then((users) => {
return {
startDate: moment().startOf("day").subtract(this.config.reportDays, "days"),
reportDays: this.config.reportDays,
users: users
};
});
}
fetchUserDetails(users) {
return Promise.map(Object.keys(users), (username) => {
var user = users[username];
return get(this.getUserTemplate({"username": username, "apiUrl": this.config.apiUrl}), "user").spread((response, body) => {
user.details = body;
return user;
});
});
}
runPluginsForSync(results) {
return this.runPlugins(results).then(result => {
return result.users;
});
}
saveUsers(users) {
return Promise.map(users, (user) => {
return User.findOneAndUpdate({"name": user.name}, user, {"upsert": true, "new": true}).execAsync().reflect();
}).then(() => {
return users;
});
}
saveComments(users) {
return Promise.map(users, (user) => {
return Promise.map(user.repos, (repo) => {
return Promise.map(repo.for, (comment) => {
return Comment.findOneAndUpdate({"_id": comment._id}, comment, {"new": true}).execAsync().reflect();
}).then(() => {
return Promise.map(repo.against, (comment) => {
return Comment.findOneAndUpdate({"_id": comment._id}, comment, {"new": true}).execAsync().reflect();
});
});
});
}).then(() => {
return users;
});
}
runPlugins(result) {
return Promise.each(this.config.plugins, (plugin) => {
return plugin(result);
}).then(() => {
return result;
});
}
runReporters(result) {
return Promise.each(this.config.reporters, (reporter) => {
return reporter(result);
}).then(() => {
return result;
});
}
};