Skip to content

Commit c62e44a

Browse files
authored
refactor: github_api/search (#537)
* refactor: github_api/search * refactor: github_api/search * ran lint
1 parent 12f311a commit c62e44a

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

__fixtures__/unit/helper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ module.exports = {
217217
get: () => {
218218
return { data: (options.deepValidation) ? options.deepValidation : {} }
219219
}
220+
},
221+
search: {
222+
issuesAndPullRequests: jest.fn().mockReturnValue({ data: { items: options.issuesAndPullRequests || [] } })
220223
}
221224
},
222225
probotContext: {

__tests__/unit/github/api.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ describe('listFiles', () => {
2222
}
2323
})
2424

25+
describe('searchIssuesAndPR', () => {
26+
test('return correct data if no error', async () => {
27+
const issuesAndPR = [
28+
'issues 1',
29+
'PR 2'
30+
]
31+
32+
const res = await GithubAPI.searchIssuesAndPR(Helper.mockContext({ issuesAndPullRequests: issuesAndPR }))
33+
expect(res).toEqual(issuesAndPR)
34+
})
35+
36+
test('that error are re-thrown', async () => {
37+
const context = Helper.mockContext()
38+
context.octokit.search.issuesAndPullRequests = jest.fn().mockRejectedValue({ status: 402 })
39+
40+
try {
41+
await GithubAPI.searchIssuesAndPR(context)
42+
// Fail test if above expression doesn't throw anything.
43+
expect(true).toBe(false)
44+
} catch (e) {
45+
expect(e.status).toBe(402)
46+
}
47+
})
48+
})
49+
2550
describe('getContent', () => {
2651
test('return correct data if no error', async () => {
2752
const content = 'This is the content'

lib/actions/merge.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ class Merge extends Action {
8080
let relatedPullRequests = []
8181
if (context.eventName === 'status') {
8282
const commitSHA = context.payload.sha
83-
const results = await context.octokit.search.issuesAndPullRequests({
83+
const results = await this.githubAPI.searchIssuesAndPR(context, {
8484
q: `repo:${context.repo().owner}/${context.repo().repo} is:open ${commitSHA}`.trim(),
8585
sort: 'updated',
8686
order: 'desc',
8787
per_page: 20
8888
})
89-
relatedPullRequests = results.data.items.filter(item => item.pull_request)
89+
relatedPullRequests = results.filter(item => item.pull_request)
9090
} else if (context.eventName === 'check_suite') {
9191
relatedPullRequests = this.getPayload(context).pull_requests
9292
} else {

lib/github/api.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,20 @@ class GithubAPI {
312312
return checkCommonError(err, context, callFn)
313313
}
314314
}
315+
316+
static async searchIssuesAndPR (context, callParam) {
317+
const callFn = 'search.issuesAndPullRequests'
318+
319+
debugLog(context, callFn)
320+
321+
try {
322+
const res = await context.octokit.search.issuesAndPullRequests(callParam)
323+
324+
return res.data.items
325+
} catch (err) {
326+
return checkCommonError(err, context, callFn)
327+
}
328+
}
315329
}
316330

317331
module.exports = GithubAPI

lib/validators/stale.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,13 @@ class Stale extends Validator {
7777
ignoreQuery += 'no:project '
7878
}
7979
const labelQuery = labelMatchQuery.concat(labelIgnoreQuery).join(' ')
80-
const results = await context.octokit.search.issuesAndPullRequests({
80+
const items = await this.githubAPI.searchIssuesAndPR(context, {
8181
q: `repo:${context.repo().owner}/${context.repo().repo} is:open updated:<${timestamp}${typeQuery} ${labelQuery} ${ignoreQuery}`.trim(),
8282
sort: 'updated',
8383
order: 'desc',
8484
per_page: MAX_ISSUES
8585
})
8686

87-
const items = results.data.items
88-
8987
const scheduleResult = {
9088
issues: items.filter(item => !item.pull_request),
9189
pulls: items.filter(item => item.pull_request)

0 commit comments

Comments
 (0)