Skip to content

Commit 2ef97e8

Browse files
author
Kevin Tang
authored
Merge pull request #1 from kevo1ution/optimize.paginateMilestones
Fixing pagination in teams
2 parents 511b54d + aa9becf commit 2ef97e8

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

lib/plugins/teams.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ module.exports = class Teams extends Diffable {
2222
add (attrs) {
2323
// There is not a way to resolve a team slug to an id without fetching all
2424
// teams for an organization.
25-
return this.allTeams.then(teams => {
25+
const options = this.github.teams.list.endpoint.merge({ per_page: 100, org: this.repo.owner })
26+
this.github.paginate(options).then(teams => {
2627
const existing = teams.find(team => this.comparator(team, attrs))
2728

2829
return this.github.teams.addOrUpdateRepo(this.toParams(existing, attrs))
@@ -43,29 +44,4 @@ module.exports = class Teams extends Diffable {
4344
permission: attrs.permission
4445
}
4546
}
46-
47-
// Lazy getter to fetch all teams for the organization
48-
get allTeams () {
49-
const getter = this.github.teams.list({ org: this.repo.owner })
50-
.then(this.paginate.bind(this))
51-
.then(responses => {
52-
return responses.reduce((teams, res) => {
53-
return teams.concat(res.data)
54-
}, [])
55-
})
56-
Object.defineProperty(this, 'allTeams', getter)
57-
return getter
58-
}
59-
60-
// Paginator will keep fetching the next page until there are no more.
61-
paginate (res, records = []) {
62-
records = records.concat(res)
63-
if (res.meta && this.github.hasNextPage(res)) {
64-
return this.github.getNextPage(res).then(next => {
65-
return this.paginate(next, records)
66-
})
67-
} else {
68-
return Promise.resolve(records)
69-
}
70-
}
7147
}

test/lib/plugins/teams.test.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('Teams', () => {
99

1010
beforeEach(() => {
1111
github = {
12+
paginate: jest.fn().mockImplementation(() => Promise.resolve()),
1213
repos: {
1314
listTeams: jest.fn().mockImplementation(() => Promise.resolve({
1415
data: [
@@ -20,18 +21,21 @@ describe('Teams', () => {
2021
},
2122
teams: {
2223
addOrUpdateRepo: jest.fn().mockImplementation(() => Promise.resolve()),
23-
list: jest.fn().mockImplementation(() => Promise.resolve({
24-
data: [
25-
{ id: 4, slug: 'added' }
26-
]
27-
})),
24+
list: {
25+
endpoint: {
26+
merge: jest.fn().mockImplementation(() => {})
27+
}
28+
},
2829
removeRepo: jest.fn().mockImplementation(() => Promise.resolve())
2930
}
3031
}
3132
})
3233

3334
describe('sync', () => {
3435
it('syncs teams', () => {
36+
github.paginate.mockReturnValueOnce(Promise.resolve([
37+
{ id: 4, slug: 'added' }
38+
]))
3539
const plugin = configure([
3640
{ name: 'unchanged', permission: 'push' },
3741
{ name: 'updated-permission', permission: 'admin' },

0 commit comments

Comments
 (0)