Skip to content

Commit 8726102

Browse files
authored
refactor: github_api/pulls:lists (#540)
* refactor: github_api/pulls:lists * add missing awaits * ran lint
1 parent 42ec1f9 commit 8726102

File tree

6 files changed

+136
-15
lines changed

6 files changed

+136
-15
lines changed

__tests__/unit/github/api.test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,3 +647,83 @@ describe('getPR', () => {
647647
}
648648
})
649649
})
650+
651+
describe('listPR', () => {
652+
test('return correct data if no error', async () => {
653+
const res = await GithubAPI.listPR(Helper.mockContext())
654+
655+
expect(res.data).toEqual([])
656+
})
657+
658+
test('that error are re-thrown', async () => {
659+
const context = Helper.mockContext()
660+
context.octokit.pulls.list = jest.fn().mockRejectedValue({ status: 402 })
661+
662+
try {
663+
await GithubAPI.listPR(context)
664+
// Fail test if above expression doesn't throw anything.
665+
expect(true).toBe(false)
666+
} catch (e) {
667+
expect(e.status).toBe(402)
668+
}
669+
})
670+
})
671+
672+
describe('listReviews', () => {
673+
test('return correct data if no error', async () => {
674+
const reviews = [
675+
'review 1',
676+
'review 2'
677+
]
678+
679+
const res = await GithubAPI.listReviews(Helper.mockContext({ reviews }))
680+
expect(res).toEqual(reviews)
681+
})
682+
683+
test('that error are re-thrown', async () => {
684+
const context = Helper.mockContext()
685+
context.octokit.pulls.listReviews.endpoint.merge = jest.fn().mockRejectedValue({ status: 402 })
686+
687+
try {
688+
await GithubAPI.listReviews(context)
689+
// Fail test if above expression doesn't throw anything.
690+
expect(true).toBe(false)
691+
} catch (e) {
692+
expect(e.status).toBe(402)
693+
}
694+
})
695+
})
696+
697+
describe('listCommits', () => {
698+
test('return correct data if no error', async () => {
699+
const date = Date.now()
700+
const commits = [
701+
{
702+
commit: {
703+
author: {
704+
date
705+
},
706+
message: 'fix: this'
707+
}
708+
}
709+
]
710+
711+
const res = await GithubAPI.listCommits(Helper.mockContext({ commits }))
712+
expect(res.length).toEqual(1)
713+
expect(res[0].date).toEqual(date)
714+
expect(res[0].message).toEqual('fix: this')
715+
})
716+
717+
test('that error are NOT re-thrown', async () => {
718+
const context = Helper.mockContext()
719+
context.octokit.pulls.listCommits.endpoint.merge = jest.fn().mockRejectedValue({ status: 402 })
720+
721+
try {
722+
await GithubAPI.listCommits(context)
723+
// Fail test if above expression doesn't throw anything.
724+
expect(true).toBe(false)
725+
} catch (e) {
726+
expect(e.status).toBe(402)
727+
}
728+
})
729+
})

lib/github/api.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,54 @@ class GithubAPI {
440440
log.info(errorLog)
441441
}
442442
}
443+
444+
static async listPR (context) {
445+
const callFn = 'pulls.list'
446+
447+
debugLog(context, callFn)
448+
449+
try {
450+
return await context.octokit.pulls.list(context.repo({
451+
base: context.payload.ref
452+
}))
453+
} catch (err) {
454+
return checkCommonError(err, context, callFn)
455+
}
456+
}
457+
458+
static async listReviews (context, pullNumber) {
459+
const callFn = 'pulls.listReviews'
460+
461+
debugLog(context, callFn)
462+
463+
try {
464+
return await context.octokit.paginate(
465+
context.octokit.pulls.listReviews.endpoint.merge(
466+
context.repo({ pull_number: pullNumber })
467+
),
468+
res => res.data
469+
)
470+
} catch (err) {
471+
return checkCommonError(err, context, callFn)
472+
}
473+
}
474+
475+
static async listCommits (context, pullNumber) {
476+
const callFn = 'pulls.listCommits'
477+
478+
debugLog(context, callFn)
479+
480+
try {
481+
return await context.octokit.paginate(
482+
context.octokit.pulls.listCommits.endpoint.merge(
483+
context.repo({ pull_number: pullNumber })
484+
),
485+
res => res.data.map(o => ({ message: o.commit.message, date: o.commit.author.date }))
486+
)
487+
} catch (err) {
488+
return checkCommonError(err, context, callFn)
489+
}
490+
}
443491
}
444492

445493
module.exports = GithubAPI

lib/interceptors/interceptor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const GithubAPI = require('../github/api')
12
/**
23
* The Interceptor class defines the interface for all inheriting interceptors that
34
* mutates the probot context with additional meta data or changes existing property Values
@@ -9,6 +10,10 @@
910
* Interceptors are cached instances and should be treated as singletons and is NOT thread safe. Instance variables should be treated as constants.
1011
*/
1112
class Interceptor {
13+
constructor () {
14+
this.githubAPI = GithubAPI
15+
}
16+
1217
/**
1318
* All Interceptors should overwrite this method and mutate the context as needed.
1419
* By default returns the context unchanged.

lib/interceptors/push.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ class Push extends Interceptor {
2929

3030
const registry = { filters: new Map(), validators: new Map(), actions: new Map() }
3131

32-
const res = await context.octokit.pulls.list(context.repo({
33-
base: context.payload.ref
34-
}))
32+
const res = await this.githubAPI.listPR(context)
3533

3634
const pulls = res.data
3735
await Promise.all(pulls.map(pullRequest => {

lib/validators/approvals.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,7 @@ class Approvals extends Validator {
6666
delete validationSettings.limit
6767
}
6868

69-
const reviews = await context.octokit.paginate(
70-
context.octokit.pulls.listReviews.endpoint.merge(
71-
context.repo({ pull_number: this.getPayload(context).number })
72-
),
73-
res => res.data
74-
)
69+
const reviews = await this.githubAPI.listReviews(context, this.getPayload(context).number)
7570

7671
const {
7772
requiredReviewers,

lib/validators/commit.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ class Commit extends Validator {
4444

4545
const validatorContext = { name: 'commit' }
4646

47-
const commits = await context.octokit.paginate(
48-
context.octokit.pulls.listCommits.endpoint.merge(
49-
context.repo({ pull_number: this.getPayload(context).number })
50-
),
51-
res => res.data.map(o => ({ message: o.commit.message, date: o.commit.author.date }))
52-
)
47+
const commits = await this.githubAPI.listCommits(context, this.getPayload(context).number)
5348
let orderedCommits = _.orderBy(commits, ['date'], ['asc'])
5449

5550
if (singleCommitOnly && orderedCommits.length !== 1) {

0 commit comments

Comments
 (0)