Skip to content
Permalink
Browse files

fix: normalize pagination for "List installations for a user" endpoint (

  • Loading branch information...
ipmsteven authored and gr2m committed Jul 26, 2019
1 parent cc8e65b commit 7b51cee4a22b6e52adcdca011f93efdffa5df998
Showing with 51 additions and 1 deletion.
  1. +4 −1 plugins/pagination/normalize-paginated-list-response.js
  2. +47 −0 test/integration/pagination-test.js
@@ -9,6 +9,7 @@
* - https://developer.github.com/v3/checks/runs/#response-3 (key: `check_runs`)
* - https://developer.github.com/v3/checks/suites/#response-1 (key: `check_suites`)
* - https://developer.github.com/v3/apps/installations/#list-repositories (key: `repositories`)
* - https://developer.github.com/v3/apps/installations/#list-installations-for-a-user (key `installations`)
*
* Octokit normalizes these responses so that paginated results are always returned following
* the same structure. One challenge is that if the list response has only one page, no Link
@@ -30,13 +31,15 @@ const deprecateNamespace = once((log, deprecation) => log.warn(deprecation))
const REGEX_IS_SEARCH_PATH = /^\/search\//
const REGEX_IS_CHECKS_PATH = /^\/repos\/[^/]+\/[^/]+\/commits\/[^/]+\/(check-runs|check-suites)/
const REGEX_IS_INSTALLATION_REPOSITORIES_PATH = /^\/installation\/repositories/
const REGEX_IS_USER_INSTALLATIONS_PATH = /^\/user\/installations/

function normalizePaginatedListResponse (octokit, url, response) {
const path = url.replace(octokit.request.endpoint.DEFAULTS.baseUrl, '')
if (
!REGEX_IS_SEARCH_PATH.test(path) &&
!REGEX_IS_CHECKS_PATH.test(path) &&
!REGEX_IS_INSTALLATION_REPOSITORIES_PATH.test(path)
!REGEX_IS_INSTALLATION_REPOSITORIES_PATH.test(path) &&
!REGEX_IS_USER_INSTALLATIONS_PATH.test(path)
) {
return
}
@@ -288,6 +288,53 @@ describe('pagination', () => {
})
})

it('.paginate() with results namespace (GET /user/installations)', () => {
nock('https://api.github.com')
.get('/user/installations')
.query({
per_page: 1
})
.reply(200, {
total_count: 2,
installations: [
{
id: '123'
}
]
}, {
Link: '<https://api.github.com/user/installations?per_page=1&page=2>; rel="next", <https://api.github.com/user/installations?per_page=1&page=2>; rel="last"'
})

.get('/user/installations')
.query({
per_page: 1,
page: 2
})
.reply(200, {
total_count: 2,
installations: [
{
id: '456'
}
]
}, {
Link: '<https://api.github.com/user/installations?per_page=1&page=1>; rel="prev", <https://api.github.com/user/installations?per_page=1&page=1>; rel="first"'
})

const octokit = new Octokit()
const options = octokit.apps.listInstallationsForAuthenticatedUser.endpoint.merge({
per_page: 1
})

return octokit.paginate(options)
.then(results => {
expect(results).to.deep.equal([
{ id: '123' },
{ id: '456' }
])
})
})

it('.paginate() with results namespace (GET /installation/repositories, single page response)', () => {
nock('https://api.github.com')
.get('/installation/repositories')

0 comments on commit 7b51cee

Please sign in to comment.
You can’t perform that action at this time.