-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetGHIssuesPromised.js
More file actions
226 lines (180 loc) · 7.59 KB
/
Copy pathgetGHIssuesPromised.js
File metadata and controls
226 lines (180 loc) · 7.59 KB
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
/**
* Created by ogi on 03.07.14.
*/
var Q = require('q');
// variant 1 would be to use q-flow but it assumes that Q is called q ...
// require('q-flow'); // extends q
// variant 2 from http://stackoverflow.com/questions/17217736/while-loop-with-promises
// `condition` is a function that returns a boolean
// `body` is a function that returns a promise
// returns a promise for the completion of the loop
function promiseWhile(condition, body) {
var done = Q.defer();
function loop() {
// When the result of calling `condition` is no longer true, we are
// done.
if (!condition()) return done.resolve();
// Use `when`, in case `body` does not return a promise.
// When it completes loop again otherwise, if it fails, reject the
// done promise
Q.when(body(), loop, done.reject);
}
// Start running the loop in the next tick so that this function is
// completely async. It would be unexpected if `body` was called
// synchronously the first time.
Q.nextTick(loop);
// The promise
return done.promise;
}
var fs = require('fs');
var path = require('path');
var GitHubApi = require("github");
var github = new GitHubApi({
// required
version: "3.0.0",
// optional
// debug: true, // very helpful
protocol: "https",
//host: "github.my-GHE-enabled-company.com",
timeout: 5000
});
procedures = {
writeGHIToFile: function (aUser, aPassword, aRepoUser, aRepo, aOutputPath, callback) {
var issuesResult = [];
function processComments(res, aResultingIssue) {
if (!aResultingIssue.ogi_allComments) {
aResultingIssue.ogi_allComments = []
}
for (var i = 0; i < res.length; i++) {
aResultingIssue.ogi_allComments.push(res[i])
//console.log("#%i comment %i", aResultingIssue.number, i);
}
return Q(); //fulfilled promise
};
function processIssues(res) {
// this is just for debugging the order
// for (var i = 0; i < res.length; i++) {
// console.log("--#%i", res[i].number);
// }
//this will preserve order !!
var p = Q.all(res.map(function (aResultingIssue) {
//console.log("#%i", aResultingIssue.number);
// get Comments for the issue aResultingIssue
issuesResult.push(aResultingIssue); //we have to push here to preserver order!!
var githubCommentsOptions = {
user: aRepoUser,
repo: aRepo,
number: aResultingIssue.number,
per_page: 100 //change to 5 for testing paging
};
var p = Q.ninvoke(github.issues, "getComments", githubCommentsOptions);
p = p.then(function (res) {
// res is on one side an array with .length representing the issues array
// but on the other side it contains a meta object for info an continuation
var p1 = processComments(res, aResultingIssue);
var theResult = res;
return p1.then(function () {
return promiseWhile(function () { //condition function
return github.hasNextPage(theResult)
}, function () { //loop function
return Q.ninvoke(github, "getNextPage", theResult).then(function (res) {
theResult = res;
return processComments(res, aResultingIssue);
});
});
});
});
return p;
}));
return p
}
// prepare file for writing
if (!fs.existsSync(aOutputPath)) {
fs.mkdirSync(aOutputPath);
}
var filename = "Issues_" + aRepoUser + "_" + aRepo + "_" + new Date().toISOString().
replace(/T/, '_'). // replace T with a underscore
replace(/\..+/, ''). // delete the dot and everything after;
replace(/:/g, '_') // replace : with an underscore g-> all
+ ".json";
filename = path.join(aOutputPath, filename);
console.log("writing to %s", filename);
// this only sets the way to authenticate every single request
github.authenticate({
type: "basic",
username: aUser,
password: aPassword
});
var githubIssuesOptions = {
user: aRepoUser,
repo: aRepo,
state: "open",
//labels: "",
sort: "created",
//direction: "desc",
per_page: 100 //change to 1 or 10 for testing paging
};
var p = Q.ninvoke(github.issues, "repoIssues", githubIssuesOptions);
p = p.then(function (res) {
// res is on one side an array with .length representing the issues array
// but on the other side it contains a meta object for info an continuation
var p1 = processIssues(res);
var theResult = res;
return p1.then(function () {
return promiseWhile(function () { //condition function
return github.hasNextPage(theResult)
}, function () { //loop function
return Q.ninvoke(github, "getNextPage", theResult).then(function (res) {
theResult = res;
return processIssues(res);
});
});
});
});
p = p.then(function () {
var fd = fs.openSync(filename, "w");
//dump first array here
// the output is like a mongodb json dump one json per line
for (var i = 0; i < issuesResult.length; i++) {
fs.writeSync(fd, JSON.stringify(issuesResult[i]) + "\n");
}
fs.closeSync(fd);
issuesResult = [];
githubIssuesOptions.state = "closed";
return Q.ninvoke(github.issues, "repoIssues", githubIssuesOptions);
});
p = p.then(function (res) {
// res is on one side an array with .length representing the issues array
// but on the other side it contains a meta object for info an continuation
var p1 = processIssues(res);
var theResult = res;
return p1.then(function () {
return promiseWhile(function () { //condition function
return github.hasNextPage(theResult)
}, function () { //loop function
return Q.ninvoke(github, "getNextPage", theResult).then(function (res) {
theResult = res;
return processIssues(res);
});
});
});
});
p = p.then(function () {
var fd = fs.openSync(filename, "a");
//dump second array here
// the output is like a mongodb json dump one json per line
for (var i = 0; i < issuesResult.length; i++) {
fs.writeSync(fd, JSON.stringify(issuesResult[i]) + "\n");
}
fs.closeSync(fd);
callback(undefined, "OK");
});
p = p.catch(function (error) {
// Handle any error from all above steps
console.log("ERROR - catch:%j\n", error);
callback("ERROR", undefined);
})
.done();
}
}
module.exports = procedures.writeGHIToFile;