-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub.js
More file actions
475 lines (449 loc) · 19.2 KB
/
github.js
File metadata and controls
475 lines (449 loc) · 19.2 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
'use strict'
require('loadenv')()
const Boom = require('dat-middleware').Boom
const Code = require('code')
const Lab = require('lab')
const path = require('path')
const sinon = require('sinon')
require('sinon-as-promised')(require('bluebird'))
const Github = require('models/apis/github')
const lab = exports.lab = Lab.script()
const moduleName = path.relative(process.cwd(), __filename)
const beforeEach = lab.beforeEach
const describe = lab.describe
const expect = Code.expect
const it = lab.it
describe('github: ' + moduleName, function () {
describe('isOrgMember', function () {
it('should return 404 if getting orgs returned 404', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Orgs error')
err.code = 404
sinon.stub(github, 'getUserAuthorizedOrgs').yieldsAsync(err)
github.isOrgMember('CodeNow', function (err) {
expect(err).to.exist()
expect(err.output.statusCode).to.equal(404)
expect(err.output.payload.message).to.equal('user is not a member of org')
done()
})
})
it('should return 502 in case other error', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Orgs error')
sinon.stub(github, 'getUserAuthorizedOrgs').yieldsAsync(err)
github.isOrgMember('CodeNow', function (err) {
expect(err).to.exist()
expect(err.output.statusCode).to.equal(502)
expect(err.output.payload.message).to.equal('failed to get user orgs')
done()
})
})
it('should return 404 if not orgs were found (null)', function (done) {
var github = new Github({token: 'some-token'})
sinon.stub(github, 'getUserAuthorizedOrgs').yieldsAsync(null, null)
github.isOrgMember('CodeNow', function (err) {
expect(err).to.exist()
expect(err.output.statusCode).to.equal(404)
expect(err.output.payload.message).to.equal('user is not a member of org')
done()
})
})
it('should return 404 if not orgs were found ([])', function (done) {
var github = new Github({token: 'some-token'})
sinon.stub(github, 'getUserAuthorizedOrgs').yieldsAsync(null, [])
github.isOrgMember('CodeNow', function (err) {
expect(err).to.exist()
expect(err.output.statusCode).to.equal(404)
expect(err.output.payload.message).to.equal('user is not a member of org')
done()
})
})
it('should return 404 if orgs was not in the list', function (done) {
var github = new Github({token: 'some-token'})
var orgs = [{login: 'Runnable'}]
sinon.stub(github, 'getUserAuthorizedOrgs').yieldsAsync(null, orgs)
github.isOrgMember('CodeNow', function (err) {
expect(err).to.exist()
expect(err.output.statusCode).to.equal(404)
expect(err.output.payload.message).to.equal('user is not a member of org')
done()
})
})
it('should return true if org was found', function (done) {
var github = new Github({token: 'some-token'})
var orgs = [{login: 'CodeNow'}]
sinon.stub(github, 'getUserAuthorizedOrgs').yieldsAsync(null, orgs)
github.isOrgMember('CodeNow', function (err, isMember) {
expect(err).to.not.exist()
expect(isMember).to.be.true()
done()
})
})
})
describe('_listRepoHooks', function () {
it('should return 404 if repo was not found', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Not found')
err.code = 404
sinon.stub(github.repos, 'getHooks').yieldsAsync(err)
github._listRepoHooks('codenow/api', function (boomErr) {
expect(boomErr).to.exist()
expect(boomErr.output.statusCode).to.equal(404)
expect(boomErr.output.payload.message).to.equal('Github repo codenow/api not found.')
expect(boomErr.data.err.code).to.equal(err.code)
expect(boomErr.data.err.message).to.equal(err.message)
var query = github.repos.getHooks.getCall(0).args[0]
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
done()
})
})
it('should return 502 if some error happened', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Some error')
sinon.stub(github.repos, 'getHooks').yieldsAsync(err)
github._listRepoHooks('codenow/api', function (boomErr) {
expect(boomErr).to.exist()
expect(boomErr.output.statusCode).to.equal(502)
expect(boomErr.output.payload.message).to.equal('Failed to get github repo hooks for codenow/api')
expect(boomErr.data.err.message).to.equal(err.message)
var query = github.repos.getHooks.getCall(0).args[0]
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
done()
})
})
it('should return 404 if repo was renamed', function (done) {
var github = new Github({token: 'some-token'})
var redirectResp = {
message: 'Moved Permanently',
url: 'https://api.github.com/repositories/20696842/hooks?per_page=100' +
'&access_token=aeb93139d50473aa5f812bf9731d4f04fb842864',
documentation_url: 'https://developer.github.com/v3/#http-redirects',
meta: {
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4988',
'x-ratelimit-reset': '1444181546',
'x-oauth-scopes': 'read:org, read:repo_hook, repo, user:email',
location: 'https://api.github.com/repositories/20696842/hooks' +
'?per_page=100&access_token=aeb93139d50473aa5f812bf9731d4f04fb842864',
status: '301 Moved Permanently'
}
}
sinon.stub(github.repos, 'getHooks').yieldsAsync(null, redirectResp)
github._listRepoHooks('codenow/api', function (boomErr) {
expect(boomErr).to.exist()
expect(boomErr.output.statusCode).to.equal(404)
expect(boomErr.output.payload.message).to.equal('Github repo codenow/api not found, because it moved')
var query = github.repos.getHooks.getCall(0).args[0]
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
done()
})
})
it('should work if no errors occured', function (done) {
var github = new Github({token: 'some-token'})
sinon.stub(github.repos, 'getHooks').yieldsAsync(null, [{id: 1}])
github._listRepoHooks('codenow/api', function (err, hooks) {
expect(err).to.not.exist()
expect(hooks.length).to.equal(1)
expect(hooks[0].id).to.equal(1)
done()
})
})
})
describe('_deleteRepoHook', function () {
it('should return 404 if repo wasnot found', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Not found')
err.code = 404
sinon.stub(github.repos, 'deleteHook').yieldsAsync(err)
github._deleteRepoHook(1, 'codenow/api', function (boomErr) {
expect(boomErr).to.exist()
expect(boomErr.output.statusCode).to.equal(404)
expect(boomErr.output.payload.message).to.equal('Github repo hook 1 not found.')
expect(boomErr.data.err.code).to.equal(err.code)
expect(boomErr.data.err.message).to.equal(err.message)
var query = github.repos.deleteHook.getCall(0).args[0]
expect(query.id).to.equal(1)
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
done()
})
})
it('should return 502 if some error happened', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Some error')
sinon.stub(github.repos, 'deleteHook').yieldsAsync(err)
github._deleteRepoHook(1, 'codenow/api', function (boomErr) {
expect(boomErr).to.exist()
expect(boomErr.output.statusCode).to.equal(502)
expect(boomErr.output.payload.message).to.equal('Failed to delete github repo hook with id 1')
expect(boomErr.data.err.message).to.equal(err.message)
var query = github.repos.deleteHook.getCall(0).args[0]
expect(query.id).to.equal(1)
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
done()
})
})
it('should work if no errors occured', function (done) {
var github = new Github({token: 'some-token'})
sinon.stub(github.repos, 'deleteHook').yieldsAsync(null, {})
github._deleteRepoHook(1, 'codenow/api', function (err) {
expect(err).to.not.exist()
done()
})
})
})
describe('_createRepoHook', function () {
it('should return 404 if repo wasnot found', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Not found')
err.code = 404
sinon.stub(github.repos, 'createHook').yieldsAsync(err)
github._createRepoHook('codenow/api', function (boomErr) {
expect(boomErr).to.exist()
expect(boomErr.output.statusCode).to.equal(404)
expect(boomErr.output.payload.message).to.equal('Github repo codenow/api not found.')
expect(boomErr.data.err.code).to.equal(err.code)
expect(boomErr.data.err.message).to.equal(err.message)
var query = github.repos.createHook.getCall(0).args[0]
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
expect(query.name).to.equal(process.env.GITHUB_HOOK_NAME)
var hookUrl = process.env.GITHUB_WEBHOOK_URL
expect(query.config.url).to.equal(hookUrl)
expect(query.config.content_type).to.equal('json')
expect(query.events[0]).to.equal('*')
done()
})
})
it('should return 502 if some error happened', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Some error')
sinon.stub(github.repos, 'createHook').yieldsAsync(err)
github._createRepoHook('codenow/api', function (boomErr) {
expect(boomErr).to.exist()
expect(boomErr.output.statusCode).to.equal(502)
expect(boomErr.output.payload.message).to.equal('Failed to create github repo hook for codenow/api')
expect(boomErr.data.err.message).to.equal(err.message)
var query = github.repos.createHook.getCall(0).args[0]
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
expect(query.name).to.equal(process.env.GITHUB_HOOK_NAME)
var hookUrl = process.env.GITHUB_WEBHOOK_URL
expect(query.config.url).to.equal(hookUrl)
expect(query.config.content_type).to.equal('json')
expect(query.events[0]).to.equal('*')
done()
})
})
it('should return 409 if hook already exist', function (done) {
var github = new Github({token: 'some-token'})
var err = new Error('Validation Failed')
err.code = 422
err.message = JSON.stringify({
'message': 'Validation Failed',
'errors': [{
resource: 'Hook',
code: 'custom',
message: 'Hook already exists on this repository'
}],
'documentation_url': 'https://developer.github.com/v3/repos/hooks/#create-a-hook'
})
sinon.stub(github.repos, 'createHook').yieldsAsync(err)
github._createRepoHook('codenow/api', function (boomErr) {
expect(boomErr).to.exist()
expect(boomErr.output.statusCode).to.equal(409)
expect(boomErr.output.payload.message).to.equal('Github repo codenow/api already has a hook.')
expect(boomErr.data.err.message).to.equal(err.message)
var query = github.repos.createHook.getCall(0).args[0]
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
expect(query.name).to.equal(process.env.GITHUB_HOOK_NAME)
var hookUrl = process.env.GITHUB_WEBHOOK_URL
expect(query.config.url).to.equal(hookUrl)
expect(query.config.content_type).to.equal('json')
expect(query.events[0]).to.equal('*')
done()
})
})
it('should work if no errors occured', function (done) {
var github = new Github({token: 'some-token'})
sinon.stub(github.repos, 'createHook').yieldsAsync(null, {
_id: 1
})
github._createRepoHook('codenow/api', function (err, hook) {
expect(err).to.not.exist()
expect(hook._id).to.equal(1)
var query = github.repos.createHook.getCall(0).args[0]
expect(query.user).to.equal('codenow')
expect(query.repo).to.equal('api')
expect(query.name).to.equal(process.env.GITHUB_HOOK_NAME)
var hookUrl = process.env.GITHUB_WEBHOOK_URL
expect(query.config.url).to.equal(hookUrl)
expect(query.config.content_type).to.equal('json')
expect(query.events[0]).to.equal('*')
done()
})
})
})
describe('createRepoHookIfNotAlready', function () {
it('should fail if listing hooks failed', function (done) {
var github = new Github({token: 'some-token'})
var err = Boom.notFound('Repo not found')
sinon.stub(github, '_listRepoHooks').yieldsAsync(err)
sinon.spy(github, '_createRepoHook')
github.createRepoHookIfNotAlready('codenowapi', function (err) {
expect(err).to.exist()
expect(err.output.statusCode).to.equal(404)
expect(err.output.payload.message).to.equal('Repo not found')
expect(github._listRepoHooks.callCount).to.equal(1)
expect(github._createRepoHook.callCount).to.equal(0)
done()
})
})
it('should fail if hook creation failed', function (done) {
var github = new Github({token: 'some-token'})
var err = Boom.notFound('Repo not found')
sinon.stub(github, '_listRepoHooks').yieldsAsync(null, [])
sinon.stub(github, '_createRepoHook').yieldsAsync(err)
github.createRepoHookIfNotAlready('codenowapi', function (err) {
expect(err).to.exist()
expect(err.output.statusCode).to.equal(404)
expect(err.output.payload.message).to.equal('Repo not found')
expect(github._listRepoHooks.callCount).to.equal(1)
expect(github._createRepoHook.callCount).to.equal(1)
done()
})
})
it('should not fail if hook creation failed with 409', function (done) {
var github = new Github({token: 'some-token'})
var err = Boom.conflict('Hook exist')
sinon.stub(github, '_listRepoHooks').yieldsAsync(null, [])
sinon.stub(github, '_createRepoHook').yieldsAsync(err)
github.createRepoHookIfNotAlready('codenowapi', function (err) {
expect(err).to.not.exist()
expect(github._listRepoHooks.callCount).to.equal(1)
expect(github._createRepoHook.callCount).to.equal(1)
done()
})
})
it('should not fail if hook was found', function (done) {
var github = new Github({token: 'some-token'})
var hooks = [
{
config: {
url: process.env.GITHUB_WEBHOOK_URL
},
active: true,
events: ['*']
}
]
sinon.stub(github, '_listRepoHooks').yieldsAsync(null, hooks)
sinon.spy(github, '_createRepoHook')
github.createRepoHookIfNotAlready('codenowapi', function (err) {
expect(err).to.not.exist()
expect(github._listRepoHooks.callCount).to.equal(1)
expect(github._createRepoHook.callCount).to.equal(0)
done()
})
})
})
describe('createHooksAndKeys', () => {
var github
beforeEach((done) => {
github = new Github({token: 'some-token'})
sinon.stub(github, 'createRepoHookIfNotAlready')
sinon.stub(github, 'addDeployKeyIfNotAlreadyAsync')
done()
})
it('should return keys', (done) => {
const testRepoName = 'runnable/robot'
const testKeys = {
fire: '0',
ice: '1'
}
github.createRepoHookIfNotAlready.yieldsAsync()
github.addDeployKeyIfNotAlreadyAsync.resolves(testKeys)
github.createHooksAndKeys(testRepoName)
.then((keys) => {
sinon.assert.calledOnce(github.createRepoHookIfNotAlready)
sinon.assert.calledWith(github.createRepoHookIfNotAlready, testRepoName)
sinon.assert.calledOnce(github.addDeployKeyIfNotAlreadyAsync)
sinon.assert.calledWith(github.addDeployKeyIfNotAlreadyAsync, testRepoName)
expect(keys).to.equal(testKeys)
done()
})
})
}) // end createHooksAndKeys
describe('_getAllOrgs', () => {
let github
beforeEach((done) => {
github = new Github({token: 'some-token'})
sinon.stub(github.user,'getOrgs').yields(null, [])
done()
})
it('should return no results', (done) => {
let opts = {}
github.user.getOrgs.yields(null, []);
github._getAllOrgs(opts, function (err, allOrgs) {
sinon.assert.calledOnce(github.user.getOrgs)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 100, page: 1}, sinon.match.func)
expect(allOrgs.length).to.equal(0)
done()
})
})
it('should return 99 results', (done) => {
let opts = {}
github.user.getOrgs.yields(null, new Array(99))
github._getAllOrgs(opts, (err, allOrgs) => {
sinon.assert.calledOnce(github.user.getOrgs)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 100, page: 1}, sinon.match.func)
expect(allOrgs.length).to.equal(99)
done()
})
})
it('should return 100 results', (done) => {
let opts = {}
github.user.getOrgs.onFirstCall().yields(null, new Array(100))
github.user.getOrgs.onSecondCall().yields(null, [])
github._getAllOrgs(opts, (err, allOrgs) => {
sinon.assert.calledTwice(github.user.getOrgs)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 100, page: 1}, sinon.match.func)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 100, page: 2}, sinon.match.func)
expect(allOrgs.length).to.equal(100)
done()
})
})
it('should return 201 results', (done) => {
let opts = {}
github.user.getOrgs.onFirstCall().yields(null, new Array(100))
github.user.getOrgs.onSecondCall().yields(null, new Array(100))
github.user.getOrgs.onThirdCall().yields(null, new Array(1))
github._getAllOrgs(opts, (err, allOrgs) => {
sinon.assert.calledThrice(github.user.getOrgs)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 100, page: 1}, sinon.match.func)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 100, page: 2}, sinon.match.func)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 100, page: 3}, sinon.match.func)
expect(allOrgs.length).to.equal(201)
done()
})
})
it('should return 40 results, over 2 pages, with three calls', (done) => {
let opts = {per_page: 20}
github.user.getOrgs.onFirstCall().yields(null, new Array(20))
github.user.getOrgs.onSecondCall().yields(null, new Array(20))
github.user.getOrgs.onThirdCall().yields(null, [])
github._getAllOrgs(opts, (err, allOrgs) => {
sinon.assert.calledThrice(github.user.getOrgs)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 20, page: 1}, sinon.match.func)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 20, page: 2}, sinon.match.func)
sinon.assert.calledWith(github.user.getOrgs, {per_page: 20, page: 3}, sinon.match.func)
expect(allOrgs.length).to.equal(40)
done()
})
})
})
})