-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitbucket.js
329 lines (285 loc) · 8.62 KB
/
bitbucket.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
const request = require('request'),
timeAgo = require('./timeago.js'),
cache = require('./cache.js');
module.exports = {
refresh_token: null,
access_token: null,
hostname: 'https://api.bitbucket.org/2.0/',
old_hostname: 'https://api.bitbucket.org/1.0/',
refresh_error: "Access token expired. Use your refresh token to obtain a new access token.",
/**
* Returns the URL which the user should be taken to which initiates the authentication process
*/
getAuthenticateURL: function(){
return "https://bitbucket.org/site/oauth2/authorize?client_id=JDnfgLXR42DsAgmfzu&response_type=code";
},
/**
* Submits a request to the chosen endpoint, using the specified method.
* Passes through the access_token in the auth header
* Runs callback after request completed containing err and response body.
*/
doAuthenticatedRequest: function(endpoint, options, callback){
var obj = this,
opt = {};
opt.method = options.method || "get";
opt.body = options.body || null;
opt.version = options.version || 2;
if(opt.version == 1){
var url = obj.old_hostname+endpoint;
}else{
var url = obj.hostname+endpoint;
}
this.getAccessToken(function(){
request({
url: url,
method: opt.method,
json: true,
auth: {
'bearer': obj.access_token
},
body: opt.body
}, function(err, resp, body){
if(err) throw err;
if(body.type && body.type == "error" && body.error.message == obj.refresh_error){
obj.refreshAccessToken(function(){
obj.doAuthenticatedRequest(endpoint, opt, callback);
});
}else{
callback(err, body);
}
});
});
},
/**
* Sets the access token needed to authenticate API requests
*/
setAccessToken: function(access_token, callback){
this.access_token = access_token;
cache.config.update({type: 'access_token'}, {type: 'access_token', value: access_token}, {upsert: true}, function(err, num){
callback();
});
},
/**
* Sets the code that we need to get to get the access_token
*/
setRefreshToken: function(refresh_token){
this.refresh_token = refresh_token;
cache.config.update({type: 'refresh_token'}, {type: 'refresh_token', value: refresh_token}, {upsert: true});
},
/**
* Gets the access token by first looking in this object,
* then checking the cache
*/
getAccessToken: function(callback){
var obj = this;
if(!obj.access_token){
cache.config.findOne({type:'access_token'}, function(err, key){
if(!key || !key.value){
obj.requestAccessToken(true, callback);
}else{
obj.access_token = key.value;
callback();
}
});
}else{
callback();
}
},
/**
* Requests a new access token from BitBucket by providing the refresh_token
*/
refreshAccessToken: function(callback){
var obj = this;
cache.config.findOne({type: 'refresh_token'}, function(err, key){
if(!key) throw "No refresh token found";
request({
url:'https://devflow.danjohnson.xyz/api/v1/access_token.php',
method:'post',
form: {
grant_type: 'refresh_token',
refresh_token: key.value
}
}, function(err, response, body){
resp = JSON.parse(body);
// TODO: Store the time in which this will expire, and then grab a new one before requesting.
obj.setAccessToken(resp.access_token, callback);
});
});
},
/**
* Requests an access token from BitBucket using OAuth2.0
*/
requestAccessToken: function(code, callback){
var obj = this,
resp = {};
request({
url: 'https://devflow.danjohnson.xyz/api/v1/access_token.php',
method:'post',
form: {
grant_type: 'authorization_code',
code: code
}
}, function(err, response, body){
resp = JSON.parse(body);
obj.setRefreshToken(resp.refresh_token);
obj.setAccessToken(resp.access_token, function(){
callback();
});
});
},
/**
* Returns an object of the repositories the current user has write access to
* and stores it in the cache
*/
getRepos: function(callback){
this.doAuthenticatedRequest('repositories?role=contributor', {}, function(err, repos){
if(!repos.values) callback(err, repos);
repos.values.forEach(function(e, i){
cache.repo.update({'uuid':e.uuid}, e, {upsert: true}, function(err, num){
callback(err, repos);
});
});
});
},
/**
* Returns an object of issues relating to the given repository
* The repo must contain the username AND the repository name,
* for example: danjohnson95/devflow
* This function also modifies the response to show timestamps, and also
* to include the full repository name
*/
getIssues: function(repo, callback){
var obj = this;
obj.doAuthenticatedRequest('repositories/'+repo+'/issues', {}, function(err, issues){
if(!issues.values || !issues.values.length) return callback(err, issues);
var promises = [];
issues.repo_slug = repo;
issues.repo_id = issues.values[0].repository.uuid;
issues.values.forEach(function(e, i){
promises.push(obj.updateIssueCache(e));
});
Promise.all(promises).then(values => {
callback(null, issues);
});
});
},
updateIssueCache: function(issue){
return new Promise(function(resolve, reject){
issue.updated_html = timeAgo.html(issue.updated_on);
issue.cached_on = new Date();
issue.repo_id = issue.repository.uuid;
issue.issue_id = parseInt(issue.id);
cache.issues.update({issue_id: issue.issue_id, repo_id: issue.repo_id}, issue, {upsert: true}, function(err, num){
if(err) reject(err);
resolve(issue);
});
});
},
/**
* Returns an object of an individual issue containing both attachments and comments.
*/
getIssueDetail: function(repo_slug, repo_id, issue_id, callback){
var getAttachments = this.getIssueAttachments(repo_slug, issue_id);
var getComments = this.getIssueComments(repo_slug, issue_id);
Promise.all([getAttachments, getComments]).then(values => {
var obj = {
attachments: values[0].values,
comments: values[1].values,
cached_on: new Date(),
issue_id: parseInt(issue_id),
repo_id: repo_id,
repo_slug: repo_slug
};
cache.issue.update({repo_id: repo_id, issue_id: parseInt(issue_id)}, obj, {upsert: true}, function(err, num, issue){
if(err) throw err;
callback(null, obj);
});
});
},
/**
* Returns an object containing any attachments relating to the issue provided.
*/
getIssueAttachments: function(repo_slug, issue_id){
var obj = this;
return new Promise(function(resolve, reject){
obj.doAuthenticatedRequest('repositories/'+repo_slug+'/issues/'+issue_id+'/attachments', {}, function(err, attachments){
if(err) reject(err);
resolve(attachments);
});
});
},
/**
* Returns an object containing comments relating to the issue provided.
*/
getIssueComments: function(repo_slug, issue_id){
var obj = this;
return new Promise(function(resolve, reject){
obj.doAuthenticatedRequest('repositories/'+repo_slug+'/issues/'+issue_id+'/comments', {}, function(err, comments){
if(err) reject(err);
if(comments.values.length > 0){
comments.values.forEach(function(e, i){
comments.values[i].created_html = timeAgo.html(e.created_on);
});
}
resolve(comments);
});
});
},
postNewIssue: function(obj, callback){
// TODO: Should really do some serverside checks here.
this.doAuthenticatedRequest('repositories/'+obj.repo+'/issues', {
method: 'post',
body: {
priority: 'trivial',
kind:'bug',
title: obj.title,
state: 'new',
content: {
raw: obj.body,
markup: 'markdown'
}
}
}, function(err, newIssue){
// Now cache it.
newIssue.updated_html = timeAgo.html(newIssue.updated_on);
newIssue.repo_id = newIssue.repository.uuid;
cache.issues.insert(newIssue, function(err, issue){
callback(err, newIssue);
});
});
},
oldCommentToNew: function(comment){
return obj = {
created_html: timeAgo.html(comment.utc_created_on),
content: {
html: comment.content
},
user: {
display_name: comment.author_info.display_name,
links: {
avatar: {
href: comment.author_info.avatar
}
}
}
};
},
postNewComment: function(msg, callback){
var obj = this;
obj.doAuthenticatedRequest('repositories/'+msg.repo+'/issues/'+msg.issue+'/comments', {
method: 'post',
version: 1,
body: {
content: msg.content
}
}, function(err, comment){
comment = obj.oldCommentToNew(comment);
cache.issue.findOne({repo_slug: msg.repo, issue_id: parseInt(msg.issue)}, function(err, issue){
issue.comments.push(comment);
cache.issue.update({repo_id: issue.repo_id, issue_id: parseInt(msg.issue)}, issue, {upsert: true}, function(err, num, issue){
callback(null, comment);
});
});
});
}
}