forked from rosedu/challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
executable file
·366 lines (300 loc) · 11 KB
/
core.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
var MACRO = require('./model/macro.js');
var mongoose = require('mongoose');
var https = require('https');
var fs = require('fs');
var url = require('url');
var md5 = require('MD5');
var Users = mongoose.model('Users');
var Notifications = mongoose.model('Notifications');
var Challenges = mongoose.model('Challenges');
var nextUserId = 0;
global.usersById = {};
var usersByGhId = {};
exports.send_mail = function (destination, type, body, subject) {
var nodemailer = require("nodemailer");
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: global.config.mail_user,
pass: global.config.mail_pass
}
});
fs.readFile(__dirname + '/public/emails/' + type + '.html', 'utf8', function (err, html) {
var mailOpt = {};
if (type == 'welcome') {
mailOpt['from'] = 'ROSEdu Challenge <[email protected]>';
mailOpt['to'] = destination,
mailOpt['subject'] = 'Welcome to Challenge by ROSEdu',
mailOpt['text'] = '',
mailOpt['html'] = html;
} else if (type == 'feedback') {
mailOpt['from'] = 'ROSEdu Challenge <[email protected]>';
mailOpt['to'] = '[email protected]',
mailOpt['subject'] = 'Feedback: ' + body.email,
mailOpt['text'] = body.msg
} else if (type == 'challenge') {
mailOpt['from'] = 'ROSEdu Challenge <[email protected]>';
mailOpt['to'] = destination,
mailOpt['subject'] = subject,
mailOpt['text'] = body
}
// send mail with defined transport object
smtpTransport.sendMail(mailOpt, function(err, response){
if (err) console.log(err);
else console.log("* Email sent to " + destination);
smtpTransport.close();
});
});
}
function addUser (source, sourceUser) {
var user;
if (arguments.length === 1) { // password-based
user = sourceUser = source;
user.id = ++nextUserId;
return usersById[nextUserId] = user;
} else { // non-password-based
user = usersById[++nextUserId] = {id: nextUserId};
user[source] = sourceUser;
}
return user;
}
exports.login = function(sess, accessToken, accessTokenExtra, ghUser) {
sess.oauth = accessToken;
if (typeof usersByGhId[ghUser.id] === 'undefined') {
usersByGhId[ghUser.id] = addUser('github', ghUser);
Users
.findOne({ 'user_id': usersByGhId[ghUser.id].github.id },
'user_name', function (err, user) {
if (err) return handleError(err);
if (user != null) {
// update last_seen
var conditions = {'user_name': user.user_name};
var update = {$set: {
'last_seen': Date.now(),
'user_fullname': usersByGhId[ghUser.id].github.name,
'user_email': usersByGhId[ghUser.id].github.email,
'followers_no': usersByGhId[ghUser.id].github.followers,
'following_no': usersByGhId[ghUser.id].github.following,
'location': usersByGhId[ghUser.id].github.location,
}};
Users.update(conditions, update, function (err, num) {
console.log("* User " + user.user_name + " logged in.");
});
} else {
// Send welcome notification
new Notifications({
'src': null,
'dest': usersByGhId[ghUser.id].github.login,
'type': "welcome",
'link': "/faq"
}).save(function(err, todo, count) {
if (err) console.log("[ERR] Notification not sent.");
});
// Import data from github
return new Users ({
'user_id': usersByGhId[ghUser.id].github.id,
'user_name': usersByGhId[ghUser.id].github.login,
'user_fullname': usersByGhId[ghUser.id].github.name,
'user_email': usersByGhId[ghUser.id].github.email,
'avatar_url': usersByGhId[ghUser.id].github.avatar_url,
'location': usersByGhId[ghUser.id].github.location,
'join_github': usersByGhId[ghUser.id].github.created_at,
'followers_no': usersByGhId[ghUser.id].github.followers,
'following_no': usersByGhId[ghUser.id].github.following
}).save (function (err, user, count) {
console.log("* User " + user.user_name + " added.");
// send welcome email
module.exports.send_mail(user.user_email, 'welcome');
});
}
});
return usersByGhId[ghUser.id];
} else {
return usersByGhId[ghUser.id];
}
}
exports.get_time_from = function (then) {
var now = Date.now();
// interval between time now and db date
var msec = now - new Date(then).getTime();
var hh = Math.floor(msec / 1000 / 60 / 60);
if (hh > 24) { // older that 24 hours
// return actual date
return "on " + then.toString().substring(4, 15);
} else if (hh > 1) { // older than 1 hour
return hh + " hours ago";
} else {
msec -= hh * 1000 * 60 * 60;
var mm = Math.floor(msec / 1000 / 60);
if (mm > 1) { // older than 1 mnute
return mm + " minutes ago";
} else {
return "one minute ago";
}
}
}
function create_patch_request(ch, pull) {
var patch_url = pull.url;
var patch_url_host = url.parse(patch_url, true).host;
var patch_url_path = url.parse(patch_url, true).pathname;
var patch_options = {
host: patch_url_host,
path: patch_url_path,
method: "GET",
headers: { "User-Agent": "github-connect" }
};
var patch_request = https.request(patch_options, function(response) {
var pull_body = '';
response.on("data", function(chunk) {
pull_body+=chunk.toString("utf8");
});
response.on("end", function() {
var pull_info = JSON.parse(pull_body);
// Check if merge date exists
var merge_date;
if (!pull.merged_at) merge_date = null;
else merge_date = new Date(pull.merged_at);
// Generate unique PR id
var oid = md5(pull.html_url).substring(0,12)
var update = {$set: {
'pulls.$.title': pull.title,
'pulls.$.created': new Date(pull.created_at),
'pulls.$.merged': merge_date,
'pulls.$.lines_inserted': pull_info.additions,
'pulls.$.lines_removed': pull_info.deletions,
'pulls.$.files_changed': pull_info.changed_files
}}
Challenges.update({'pulls.url': pull.html_url}, update, function (err, count) {
// No updates were made, object did not exist, let's push it
if (count == 0) {
var newpr = {
'_id': new mongoose.Types.ObjectId(oid),
'repo': pull.base.repo.full_name,
'auth': pull.user.login,
'hide': false,
'url': pull.html_url,
'title': pull.title,
'created': new Date(pull.created_at),
'merged': merge_date,
'score': 0,
'rating': 0,
'lines_inserted': pull_info.additions,
'lines_removed': pull_info.deletions,
'files_changed': pull_info.changed_files
}
// Update score if formulae is valid
if (exports.eval_formulae(ch.formulae))
newpr['score'] = exports.eval_formulae(ch.formulae, newpr)
Challenges.update({'link': ch.link}, {$push: {pulls: newpr}}).exec()
}
})
});
});
patch_request.end();
}
/*
Replace individual constants in formulae with actual values
and supply a result.
If pull argument is missing, test arithmetic expression using a default value
and try to evaluate expression. Returns null if failed.
IL - Inserted lines
RL - Removed lines
FC - Files changed
R - Rating
*/
exports.eval_formulae = function(formulae, pull) {
// Default test value, if parameter is missing
pull = pull || {
'lines_inserted': 0,
'lines_removed': 0,
'files_changed': 0,
'rating': 0
}
// Replace all occurances
formulae = formulae.replace(/IL/g, pull.lines_inserted)
formulae = formulae.replace(/RL/g, pull.lines_removed)
formulae = formulae.replace(/FC/g, pull.files_changed)
formulae = formulae.replace(/R/g, pull.rating)
try {
score = eval(formulae)
} catch(err) {
score = null
}
return score
}
/*
Refresh all repos from all challeneges that are active ('live').
*/
exports.refresh_challenges = function() {
Challenges.find({'status': 'live'}).exec(gotChallenges);
function gotChallenges(err, all) {
// For each challenge in pool
for (var c in all) {
var ch = all[c];
// Update last refresh date
var update = {$set: {'refresh': Date.now()}};
Challenges.update({'link': ch.link}, update).exec();
//New request for each repo of challenge
for (var r=0; r<ch.repos.length; r++) {
var options = {
host: "api.github.com",
path: "/repos/" + ch.repos[r] + "/pulls?state=all",
method: "GET",
headers: { "User-Agent": "github-connect" }
};
var request = https.request(options, function(response){
var body = '';
response.on("data", function(chunk){
body+=chunk.toString("utf8");
});
response.on("end", function(){
var pulls = JSON.parse(body);
// Log errors
if (pulls.message)
console.log("[ERR] " + pulls.message + " - " + options.path
+ " (" + pulls.documentation_url + ")");
for (var p in pulls) {
// Accept only pulls created after challenge start date, before end
// date and only from registered users
if (new Date(pulls[p].created_at).getTime() > ch.start.getTime() &&
new Date(pulls[p].created_at).getTime() < ch.end.getTime() &&
ch.users.indexOf(pulls[p].user.login) > -1) {
create_patch_request(ch, pulls[p]);
}
}
});
});
request.end();
}
}
}
}
/*
Adds dummy user with given id and username to db.
This is used during development when multiple users are needed for testing
purposes.
*/
exports.add_user = function(userid, username, callback) {
var u = {'id': userid, 'login': username}
// Add some content for user
var repo = {
'name': username + '\'s cool repo',
'description': 'A very nice description should be added here.',
'html_url': 'http://www.github.com',
'fork': true,
'forks_count': 3,
'watchers_count': 5,
'closed_pulls': 3,
}
var update = {
'user_id': u.id,
'user_name': u.login,
'user_fullname': 'Development user',
'user_email': '[email protected]',
'avatar_url': 'https://avatars.githubusercontent.com/u/0',
'location': 'Somewhere',
'repos': [repo]
}
Users.update({'user_id': u.id}, update, {'upsert': true}).exec(callback)
}