-
Notifications
You must be signed in to change notification settings - Fork 53
/
github.js
134 lines (106 loc) · 3.35 KB
/
github.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
var request = require('request');
module.exports = function(config) {
var baseUrl = "https://api.github.com/repos/" + config.repo;
// Factorize auth and headers setting
var baseRequest = request.defaults({
auth: {
user: config.username,
pass: config.password
},
// github's API requires that we use specify a userAgent
// (see: http://developer.github.com/v3/#user-agent-required)
headers: {
'User-Agent': "gh-issues-gantt/0.0.1" // should match package.json
}
});
var memo = {
issues: {
value: [],
status: 'stale' // in ["fresh", "refreshing", "stale"]
},
milestones: {
value: [],
status: 'stale'
}
};
function fetchIssues (cb, url, tmpIssues) {
if (memo.issues.status === 'stale') {
memo.issues.status = 'refreshing';
refreshIssues.call(this, cb, url, tmpIssues);
} else {
cb(null, memo.issues.value, memo.issues.status);
}
}
function fetchMilestones (cb) {
if (memo.milestones.status === 'stale') {
memo.milestones.status = 'refreshing';
refreshMilestones.call(this, cb);
} else {
cb(null, memo.milestones.value, memo.milestones.status);
}
}
function refreshIssues (cb, url, tmpIssues) {
url = url || baseUrl + "/issues?per_page=100&status=open&direction=asc";
console.log("GET " + url);
baseRequest.get({
url: url,
}, function (error, response, body) {
var issues = (tmpIssues ? tmpIssues : []).concat(JSON.parse(body));
var links = {};
//check if there is a link, stops a crash when number of issues <=100
if (response.headers.link != null) {
response.headers.link.split(', ').forEach(function(headLink){
var s = headLink.split('; ');
links[s[1]] = s[0].substr(1, s[0].length-2);
});
}
if(links['rel="next"']) {
refreshIssues(cb, links['rel="next"'], issues);
}
else {
memo.issues.value = issues;
memo.issues.status = "fresh";
cb(null, issues, memo.issues.status);
}
});
}
function refreshMilestones (cb) {
var url = baseUrl + "/milestones?per_page=100&status=open";
console.log("GET " + url);
baseRequest.get({
url: url,
}, function (error, response, body) {
memo.milestones.value = JSON.parse(body);
memo.milestones.status = "fresh";
cb(null, memo.milestones.value, memo.issues.status);
});
}
function refresh (cb) {
for (var key in memo) {
if (memo.hasOwnProperty(key)) {
if (memo[key].status = "fresh") {
memo[key].status = "stale";
}
}
}
cb();
}
return {
fetchIssues: fetchIssues,
fetchMilestones: fetchMilestones,
refresh: refresh,
update_ms_due_on: function(milestoneId, due_on, cb) {
var url = baseUrl + "/milestones/" + milestoneId;
console.log("PATCH " + url);
baseRequest.patch({
url: url,
json: {
"due_on": due_on
}
}, function (error, response, body) {
console.log(body);
cb(null, null);
});
}
};
};