-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgithubdatafetch.js
More file actions
252 lines (191 loc) · 7.92 KB
/
githubdatafetch.js
File metadata and controls
252 lines (191 loc) · 7.92 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
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
var Promise = require("bluebird");
var retry = require('bluebird-retry');
var poll = require("bluebird-poll");
var github = require("../github");
var pullrequests = require("../pullrequests");
var branches = require("../branches");
var trees = require("../trees");
var blobs = require("../blobs");
var builder = require("../builder");
var git = require("../git");
var Queue = require('data-structures').Queue;
var Branchqueue = new Queue();
var Pullrequestqueue = new Queue();
var Commentqueue = new Queue();
github.on("pullrequests",function(prs){
return Promise.resolve(prs).map(function(pr){
return pullrequests.isChanged(pr).then(function(changed){
if (!changed) {
return pr;
} else {
return pullrequests.isShaChanged(pr).then(function(changed){
if (!changed) {
return pullrequests.save(pr);
} else {
Pullrequestqueue.enqueue(pr);
return Promise.resolve(true);
}
});
}
});
}).catch(function(err){ console.log(err);});
});
github.on("branches",function(brs){
return Promise.resolve(brs).map(function(pr){
return branches.isChanged(pr).then(function(changed){
if (!changed) {
return pr;
} else {
Branchqueue.enqueue(pr);
return Promise.resolve(true);
}
});
}).catch(function(err){ console.log(err);});
});
poll(function(){
if (Branchqueue.size){
var br = Branchqueue.dequeue();
var start = new Date();
console.log("Start updating branch -> " + br.name);
return UpdateBranch(br)
.then(function(){
console.log("time to get branch: " + (+(new Date())-start));
console.log("branch done -> " + br.name);
return branches.save(br);
}).catch(function(err){console.log(err)});
} else if (Pullrequestqueue.size){
var pr = Pullrequestqueue.dequeue();
var start = new Date();
console.log("Start updating pullrequest-> " + pr.id + ":" + pr.title);
return UpdatePullRequest(pr)
.then(function() { return sendComment(pr);})
.then(function(){
console.log("time to get PR: " + (+(new Date())-start));
console.log("PR done -> " + pr.id + ":" + pr.title);
return pullrequests.save(pr);
}).catch(function(err){console.log(err)});
} else {
return Promise.resolve(true);
}
}, 1000);
function sendComment(pr) {
Commentqueue.enqueue(pr);
return Promise.resolve(true);
}
poll(function(){
if (Commentqueue.size){
var pr = Commentqueue.dequeue();
return github.getCommentsFromIssue(pr.id)
.then(function(comments){
var hascomment = false;
comments.forEach(function(comment){
if (comment.user === "threejsworker"){
hascomment = true;
}
});
if (!hascomment){
return github.sendCommentToIssue(pr.id,"The examples of this pullrequest are now built and visible in threejsworker. To view them, go to the following link: \n\n http://threejsworker.com/viewpullrequest.html#" + pr.id);
} else {
return true;
}
}).catch(function(){ return true;});
} else {
return Promise.resolve(true);
}
}, 1000);
function UpdateBranch(branch){
return trees.contains(branch.sha)
.then(function(exists){
if (exists) {
return trees.get(branch.sha);
} else {
var start = new Date();
return github.getTree("mrdoob/three.js", branch.sha)
.then(saveTree)
.then(function(tree) {
console.log("time to get tree from github: " + (+(new Date())-start));
return retry(function(){
return getBlobs("mrdoob/three.js", branch.name, tree);
}, { max_tries: 10, interval: 2000 });
})
.then(function(tree) { return buildTree(tree)})
.then(saveTree);
}
});
//.then(function(data){console.log(data);})
}
function UpdatePullRequest(pullrequest){
return trees.contains(pullrequest.sha)
.then(function(exists){
if (exists) {
return trees.get(pullrequest.sha);
} else {
var start = new Date();
return github.getTree(pullrequest.repositoryName, pullrequest.sha)
.then(saveTree)
.then(function(tree) {
console.log("time to get tree from github: " + (+(new Date())-start));
return retry(function(){
return getBlobs(pullrequest.repositoryName, "pull/" + pullrequest.id + "/head", tree);
}, { max_tries: 10, interval: 2000 });
})
.then(function(tree) { return buildTree(tree)})
.then(saveTree);
}
});
//.then(function(data){console.log(data);})
}
function saveTree(tree) {
return trees.save(tree);
}
function getBlobs(repositoryName,ref, tree) {
var start = new Date();
var failureCounter = [];
var paths = [];
for (var path in tree.paths){
paths.push(path);
}
return git.fetchRepository("mrdoob/three.js",ref,tree).then(function(repo){
return Promise.resolve(paths).map(function(path){
var sha = tree.paths[path];
var tempPath = path;
return blobs.contains(sha)
.then(function(stat){
if (stat){
return;
}
return git.getBlobContent(repo,sha).then(function(source){
return blobs.save(sha,source);
}).catch(function(err){
console.log("repo data failed -> " + path );
return github.getBlob(repositoryName,tree.sha,tempPath)
.then(function(source){
if (sha !== git.getBlobSha(source)){
throw "failed";
}
return source;
}).then(function(source){
return blobs.save(sha,source);
}).catch(function(err){
failureCounter++;
return true;
});
});
});
}, {"concurrency": 10})
.then(function(a){
if (failureCounter.length > 0) {
console.log(failureCounter.length + " failures left: \n " + failureCounter.join("\n "));
throw "failed";
}
return a;
});
}).then(function() {console.log("time to check all files: " + (+(new Date())-start));return tree});
}
function buildTree(tree) {
var start = new Date();
return builder.buildTree(tree).then(function(tree) {
console.log("time to build files: " + (+(new Date())-start));
return tree;
});
}