-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogress.js
93 lines (85 loc) · 2.28 KB
/
progress.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
var async = require('async');
var request = require('request');
function githubRequest(pathname, callback) {
request({
url: 'https://api.github.com' + pathname,
headers: {
Accept: 'application/vnd.github.v3',
'User-Agent': 'request'
},
json: true,
auth: {
user: process.env.PROGRESS_USER,
password: process.env.PROGRESS_TOKEN
}
}, function (error, response, body) {
if (body.message) {
callback(new Error(body.message), body);
} else {
callback(error, body);
}
});
}
function getCategoryProgress(challenges, user, category, callback) {
async.map(category.challenges, function (challenge, callback) {
getProgress(user, challenge, callback);
}, function (error, challenges) {
if (error) {
callback(error);
} else {
callback(error, {
category: category.category,
description: category.description,
challenges: challenges
});
}
});
}
function getCompleted(categories) {
return categories.reduce(function (sum, category) {
return sum + category.challenges.filter(function (challenge) {
return challenge.complete;
}).length;
}, 0);
}
function getOverallProgress(challenges, user, callback) {
async.map(challenges, function (category, callback) {
getCategoryProgress(challenges, user, category, callback);
}, callback);
}
function getProgress(user, challenge, callback) {
githubRequest('/repos/paircolumbus/' + challenge + '/pulls', function (error, body) {
if (error) {
callback(error);
} else {
callback(error, {
challenge: challenge,
complete: body.some(function (pull) {
return pull.user.login === user;
})
});
}
});
}
function getTotal(challenges) {
return challenges.reduce(function (sum, category) {
return sum + category.challenges.length;
}, 0);
}
function userExists(user, callback) {
githubRequest('/users/' + user, function (error, body) {
if (error) {
callback(error);
} else {
callback(error, body.message !== 'Not Found');
}
});
}
module.exports = {
getCategoryProgress: getCategoryProgress,
getCompleted: getCompleted,
getOverallProgress: getOverallProgress,
getProgress: getProgress,
getTotal: getTotal,
userExists: userExists
};