Skip to content

Commit 7c7089b

Browse files
authored
refactor: github_api/repos (#538)
* refactor: github_api/repos * fix typo * ran lint
1 parent c62e44a commit 7c7089b

File tree

5 files changed

+129
-16
lines changed

5 files changed

+129
-16
lines changed

__tests__/unit/github/api.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,78 @@ describe('projectListCards', () => {
485485
}
486486
})
487487
})
488+
489+
describe('listCollaborators', () => {
490+
test('return correct data if no error', async () => {
491+
const collaborators = [
492+
{ login: 'member1' },
493+
{ login: 'member2' }
494+
]
495+
496+
const res = await GithubAPI.listCollaborators(Helper.mockContext({ collaborators }))
497+
expect(res).toEqual(['member1', 'member2'])
498+
})
499+
500+
test('that error are re-thrown', async () => {
501+
const context = Helper.mockContext()
502+
context.octokit.repos.listCollaborators = jest.fn().mockRejectedValue({ status: 402 })
503+
504+
try {
505+
await GithubAPI.listCollaborators(context)
506+
// Fail test if above expression doesn't throw anything.
507+
expect(true).toBe(false)
508+
} catch (e) {
509+
expect(e.status).toBe(402)
510+
}
511+
})
512+
})
513+
514+
describe('getAllTopics', () => {
515+
test('return correct data if no error', async () => {
516+
const topics = [
517+
'topic 1',
518+
'topic 2'
519+
]
520+
521+
const res = await GithubAPI.getAllTopics(Helper.mockContext({ repoTopics: topics }))
522+
expect(res).toEqual(topics)
523+
})
524+
525+
test('that error are re-thrown', async () => {
526+
const context = Helper.mockContext()
527+
context.octokit.repos.getAllTopics = jest.fn().mockRejectedValue({ status: 402 })
528+
529+
try {
530+
await GithubAPI.getAllTopics(context)
531+
// Fail test if above expression doesn't throw anything.
532+
expect(true).toBe(false)
533+
} catch (e) {
534+
expect(e.status).toBe(402)
535+
}
536+
})
537+
})
538+
539+
describe('compareCommits', () => {
540+
test('return correct data if no error', async () => {
541+
const diff = [
542+
'file 1',
543+
'file 2'
544+
]
545+
546+
const res = await GithubAPI.compareCommits(Helper.mockContext({ compareCommits: diff }))
547+
expect(res.files).toEqual(diff)
548+
})
549+
550+
test('that error are re-thrown', async () => {
551+
const context = Helper.mockContext()
552+
context.octokit.repos.compareCommits = jest.fn().mockRejectedValue({ status: 402 })
553+
554+
try {
555+
await GithubAPI.compareCommits(context)
556+
// Fail test if above expression doesn't throw anything.
557+
expect(true).toBe(false)
558+
} catch (e) {
559+
expect(e.status).toBe(402)
560+
}
561+
})
562+
})

lib/actions/request_review.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ const createRequestReview = async (context, number, reviewers, actionObj) => {
2121
return res
2222
}
2323

24-
const fetchCollaborators = async (context) => {
25-
return context.octokit.repos.listCollaborators(
26-
context.repo()
27-
)
28-
}
29-
3024
class RequestReview extends Action {
3125
constructor () {
3226
super('request_review')
@@ -52,8 +46,7 @@ class RequestReview extends Action {
5246
const prNumber = payload.number
5347

5448
// get Collaborators
55-
const rawCollaboratorsResult = await fetchCollaborators(context)
56-
const collaborators = rawCollaboratorsResult.data.map(user => user.login)
49+
const collaborators = await this.githubAPI.listCollaborators(context, context.repo())
5750

5851
// remove anyone in the array that is not a collaborator
5952
const collaboratorsToRequest = _.intersection(reviewerToRequest, collaborators)

lib/filters/options_processor/topics.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Options = require('./options')
22
const CacheManager = require('../../cache/cache')
3+
const GithubAPI = require('../../github/api')
34

45
// Setup the cache manager
56
const cacheManager = new CacheManager()
@@ -23,15 +24,15 @@ const repoTopics = async (context) => {
2324
}
2425
}
2526

26-
return context.octokit.repos.getAllTopics({
27+
const response = GithubAPI.getAllTopics(context, {
2728
owner: context.payload.repository.owner.login,
2829
repo: context.payload.repository.name
29-
}).then(response => {
30-
if (globalSettings.use_config_cache !== undefined && globalSettings.use_config_cache === true) {
31-
cacheManager.set(`${repo.owner}/${repo.repo}/topics`, response.data.names)
32-
}
33-
return response.data.names
3430
})
31+
32+
if (globalSettings.use_config_cache !== undefined && globalSettings.use_config_cache === true) {
33+
cacheManager.set(`${repo.owner}/${repo.repo}/topics`, response)
34+
}
35+
return response
3536
}
3637

3738
module.exports = Topics

lib/github/api.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,50 @@ class GithubAPI {
326326
return checkCommonError(err, context, callFn)
327327
}
328328
}
329+
330+
static async listCollaborators (context, callParam) {
331+
const callFn = 'repos.listCollaborators'
332+
333+
debugLog(context, callFn)
334+
335+
try {
336+
const res = await context.octokit.repos.listCollaborators(
337+
callParam
338+
)
339+
340+
return res.data.map(user => user.login)
341+
} catch (err) {
342+
return checkCommonError(err, context, callFn)
343+
}
344+
}
345+
346+
static async getAllTopics (context, callParam) {
347+
const callFn = 'repos.getAllTopics'
348+
349+
debugLog(context, callFn)
350+
351+
try {
352+
const res = await context.octokit.repos.getAllTopics(callParam)
353+
354+
return res.data.names
355+
} catch (err) {
356+
return checkCommonError(err, context, callFn)
357+
}
358+
}
359+
360+
static async compareCommits (context, callParam) {
361+
const callFn = 'repos.compareCommits'
362+
363+
debugLog(context, callFn)
364+
365+
try {
366+
const res = await context.octokit.repos.compareCommits(callParam)
367+
368+
return res.data
369+
} catch (err) {
370+
return checkCommonError(err, context, callFn)
371+
}
372+
}
329373
}
330374

331375
module.exports = GithubAPI

lib/validators/options_processor/owners.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class Owner {
1111

1212
const owners = gitPattern.parseOwnerFile(CODEOWNERS)
1313

14-
const compare = await context.octokit.repos.compareCommits(context.repo({
14+
const compare = await GithubAPI.compareCommits(context, context.repo({
1515
base: payload.base.sha,
1616
head: payload.head.sha
1717
}))
1818

19-
const paths = compare.data.files.map(file => file.filename)
19+
const paths = compare.files.map(file => file.filename)
2020

2121
let ownerList = []
2222

0 commit comments

Comments
 (0)