Skip to content

Commit 140ab15

Browse files
authored
Merge pull request #91 from ConductorOne/BB1027_max_page
[BB-1027] baton-github: use max_page_size to limit the api usage.
2 parents 16bcd96 + 023ec71 commit 140ab15

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

pkg/connector/connector.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import (
2929

3030
const githubDotCom = "https://github.com"
3131

32-
var ValidAssetDomains = []string{"avatars.githubusercontent.com"}
32+
var (
33+
ValidAssetDomains = []string{"avatars.githubusercontent.com"}
34+
maxPageSize int = 100 // maximum page size github supported.
35+
)
3336

3437
var (
3538
resourceTypeOrg = &v2.ResourceType{
@@ -424,7 +427,7 @@ func getOrgs(ctx context.Context, client *github.Client, orgs []string) ([]strin
424427
orgLogins []string
425428
)
426429
for {
427-
orgs, resp, err := client.Organizations.List(ctx, "", &github.ListOptions{Page: page})
430+
orgs, resp, err := client.Organizations.List(ctx, "", &github.ListOptions{Page: page, PerPage: maxPageSize})
428431
if err != nil {
429432
return nil, fmt.Errorf("github-connector: failed to retrieve org: %w", err)
430433
}

pkg/connector/org.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (o *orgResourceType) List(
9595

9696
opts := &github.ListOptions{
9797
Page: page,
98-
PerPage: pToken.Size,
98+
PerPage: maxPageSize,
9999
}
100100

101101
orgs, resp, err := o.client.Organizations.List(ctx, "", opts)
@@ -188,7 +188,7 @@ func (o *orgResourceType) Grants(
188188
opts := github.ListMembersOptions{
189189
ListOptions: github.ListOptions{
190190
Page: page,
191-
PerPage: pToken.Size,
191+
PerPage: maxPageSize,
192192
},
193193
}
194194

pkg/connector/org_role.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ func (o *orgRoleResourceType) Grants(
163163
})
164164
case resourceTypeUser.Id:
165165
opts := &github.ListOptions{
166-
Page: page,
166+
Page: page,
167+
PerPage: maxPageSize,
167168
}
168169
users, resp, err := o.client.Organizations.ListUsersAssignedToOrgRole(ctx, orgName, roleID, opts)
169170
if err != nil {
@@ -207,7 +208,8 @@ func (o *orgRoleResourceType) Grants(
207208
}
208209
case resourceTypeTeam.Id:
209210
opts := &github.ListOptions{
210-
Page: page,
211+
Page: page,
212+
PerPage: maxPageSize,
211213
}
212214
teams, resp, err := o.client.Organizations.ListTeamsAssignedToOrgRole(ctx, orgName, roleID, opts)
213215
if err != nil {

pkg/connector/repository.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (o *repositoryResourceType) List(ctx context.Context, parentID *v2.Resource
8282
opts := &github.RepositoryListByOrgOptions{
8383
ListOptions: github.ListOptions{
8484
Page: page,
85-
PerPage: pt.Size,
85+
PerPage: maxPageSize,
8686
},
8787
}
8888

@@ -161,7 +161,10 @@ func (o *repositoryResourceType) Grants(
161161
case resourceTypeUser.Id:
162162
opts := &github.ListCollaboratorsOptions{
163163
Affiliation: "all",
164-
ListOptions: github.ListOptions{Page: page},
164+
ListOptions: github.ListOptions{
165+
Page: page,
166+
PerPage: maxPageSize,
167+
},
165168
}
166169
users, resp, err := o.client.Repositories.ListCollaborators(ctx, orgName, resource.DisplayName, opts)
167170
if err != nil {
@@ -208,7 +211,8 @@ func (o *repositoryResourceType) Grants(
208211

209212
case resourceTypeTeam.Id:
210213
opts := &github.ListOptions{
211-
Page: page,
214+
Page: page,
215+
PerPage: maxPageSize,
212216
}
213217
teams, resp, err := o.client.Repositories.ListTeams(ctx, orgName, resource.DisplayName, opts)
214218
if err != nil {

pkg/connector/team.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (o *teamResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
7878

7979
opts := &github.ListOptions{
8080
Page: page,
81-
PerPage: pt.Size,
81+
PerPage: maxPageSize,
8282
}
8383

8484
orgID, err := parseResourceToGitHub(parentID)
@@ -175,7 +175,10 @@ func (o *teamResourceType) Grants(ctx context.Context, resource *v2.Resource, pT
175175
}
176176

177177
opts := github.TeamListTeamMembersOptions{
178-
ListOptions: github.ListOptions{Page: page},
178+
ListOptions: github.ListOptions{
179+
Page: page,
180+
PerPage: maxPageSize,
181+
},
179182
}
180183

181184
users, resp, err := o.client.Teams.ListTeamMembersByID(ctx, org.GetID(), githubID, &opts)

pkg/connector/user.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ func (o *userResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
123123
var restApiRateLimit *v2.RateLimitDescription
124124

125125
opts := github.ListMembersOptions{
126-
ListOptions: github.ListOptions{Page: page, PerPage: pt.Size},
126+
ListOptions: github.ListOptions{
127+
Page: page,
128+
PerPage: maxPageSize,
129+
},
127130
}
128131

129132
users, resp, err := o.client.Organizations.ListMembers(ctx, orgName, &opts)

0 commit comments

Comments
 (0)