Skip to content

Commit 092c0c9

Browse files
authored
Merge pull request #98 from ConductorOne/INC300_IncludeAccessTokenError
[INC-300] baton-github: include the error in the returned message
2 parents f903bb1 + 2c8e33f commit 092c0c9

File tree

8 files changed

+56
-4
lines changed

8 files changed

+56
-4
lines changed

pkg/connector/api_token.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"github.com/conductorone/baton-sdk/pkg/annotations"
99
"github.com/conductorone/baton-sdk/pkg/pagination"
1010
resourceSdk "github.com/conductorone/baton-sdk/pkg/types/resource"
11+
"github.com/conductorone/baton-sdk/pkg/uhttp"
1112
"github.com/google/go-github/v69/github"
13+
"google.golang.org/grpc/codes"
1214
)
1315

1416
func apiTokenResource(ctx context.Context, token *github.PersonalAccessToken) (*v2.Resource, error) {
@@ -92,6 +94,9 @@ func (o *apiTokenResourceType) List(
9294
},
9395
})
9496
if err != nil {
97+
if isRatelimited(resp) {
98+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
99+
}
95100
return nil, "", nil, err
96101
}
97102

pkg/connector/connector.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (gh *GitHub) Validate(ctx context.Context) (annotations.Annotations, error)
180180
membership, _, err := gh.client.Organizations.GetOrgMembership(ctx, "", o)
181181
if err != nil {
182182
if filterOrgs {
183-
return nil, fmt.Errorf("access token must be an admin on the %s organization", o)
183+
return nil, fmt.Errorf("access token must be an admin on the %s organization: %w", o, err)
184184
}
185185
continue
186186
}
@@ -450,6 +450,9 @@ func getOrgs(ctx context.Context, client *github.Client, orgs []string) ([]strin
450450
for {
451451
orgs, resp, err := client.Organizations.List(ctx, "", &github.ListOptions{Page: page, PerPage: maxPageSize})
452452
if err != nil {
453+
if isRatelimited(resp) {
454+
return nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
455+
}
453456
return nil, fmt.Errorf("github-connector: failed to retrieve org: %w", err)
454457
}
455458
if resp.StatusCode == http.StatusUnauthorized {

pkg/connector/enterprise_role.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import (
1313
"github.com/conductorone/baton-sdk/pkg/types/entitlement"
1414
"github.com/conductorone/baton-sdk/pkg/types/grant"
1515
resourceSdk "github.com/conductorone/baton-sdk/pkg/types/resource"
16+
"github.com/conductorone/baton-sdk/pkg/uhttp"
1617
"github.com/google/go-github/v69/github"
18+
"google.golang.org/grpc/codes"
1719
)
1820

1921
type enterpriseRoleResourceType struct {
@@ -137,8 +139,11 @@ func (o *enterpriseRoleResourceType) Grants(
137139

138140
ret := []*v2.Grant{}
139141
for _, userLogin := range cache[resource.Id.Resource] {
140-
user, _, err := o.client.Users.Get(ctx, userLogin)
142+
user, resp, err := o.client.Users.Get(ctx, userLogin)
141143
if err != nil {
144+
if isRatelimited(resp) {
145+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
146+
}
142147
return nil, "", nil, fmt.Errorf("baton-github: error getting user %s: %w", userLogin, err)
143148
}
144149

pkg/connector/org.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ func (o *orgResourceType) List(
102102

103103
orgs, resp, err := o.client.Organizations.List(ctx, "", opts)
104104
if err != nil {
105+
if isRatelimited(resp) {
106+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
107+
}
105108
return nil, "", nil, fmt.Errorf("github-connector: failed to fetch org: %w", err)
106109
}
107110

@@ -126,6 +129,10 @@ func (o *orgResourceType) List(
126129
l.Warn("insufficient access to list org membership, skipping org", zap.String("org", org.GetLogin()))
127130
continue
128131
}
132+
133+
if isRatelimited(resp) {
134+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
135+
}
129136
return nil, "", nil, err
130137
}
131138

pkg/connector/org_role.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import (
1313
"github.com/conductorone/baton-sdk/pkg/types/entitlement"
1414
"github.com/conductorone/baton-sdk/pkg/types/grant"
1515
"github.com/conductorone/baton-sdk/pkg/types/resource"
16+
"github.com/conductorone/baton-sdk/pkg/uhttp"
1617
"github.com/google/go-github/v69/github"
1718
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
1819
"go.uber.org/zap"
20+
"google.golang.org/grpc/codes"
1921
)
2022

2123
type OrganizationRole struct {
@@ -88,6 +90,9 @@ func (o *orgRoleResourceType) List(
8890
// Return empty list with no error to indicate we skipped this resource
8991
return nil, "", nil, nil
9092
}
93+
if isRatelimited(resp) {
94+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
95+
}
9196
return nil, "", nil, fmt.Errorf("failed to list organization roles: %w", err)
9297
}
9398

@@ -222,6 +227,9 @@ func (o *orgRoleResourceType) Grants(
222227
}
223228
return nil, pageToken, nil, nil
224229
}
230+
if isRatelimited(resp) {
231+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
232+
}
225233
return nil, "", nil, fmt.Errorf("failed to list role teams: %w", err)
226234
}
227235

pkg/connector/repository.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ func (o *repositoryResourceType) Grants(
181181
if isNotFoundError(resp) {
182182
return nil, "", nil, uhttp.WrapErrors(codes.NotFound, fmt.Sprintf("repo: %s not found", resource.DisplayName))
183183
}
184+
if isRatelimited(resp) {
185+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
186+
}
184187
return nil, "", nil, fmt.Errorf("github-connector: failed to list repos: %w", err)
185188
}
186189

@@ -233,6 +236,10 @@ func (o *repositoryResourceType) Grants(
233236
if isNotFoundError(resp) {
234237
return nil, "", nil, uhttp.WrapErrors(codes.NotFound, fmt.Sprintf("repo: %s not found", resource.DisplayName))
235238
}
239+
240+
if isRatelimited(resp) {
241+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
242+
}
236243
return nil, "", nil, fmt.Errorf("github-connector: failed to list repos: %w", err)
237244
}
238245

pkg/connector/team.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ func (o *teamResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
9595

9696
teams, resp, err := o.client.Teams.ListTeams(ctx, orgName, opts)
9797
if err != nil {
98+
if isRatelimited(resp) {
99+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
100+
}
98101
return nil, "", nil, fmt.Errorf("github-connector: failed to list teams: %w", err)
99102
}
100103

@@ -104,8 +107,11 @@ func (o *teamResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
104107
}
105108

106109
for _, team := range teams {
107-
fullTeam, _, err := o.client.Teams.GetTeamByID(ctx, orgID, team.GetID()) //nolint:staticcheck // TODO: migrate to GetTeamBySlug
110+
fullTeam, resp, err := o.client.Teams.GetTeamByID(ctx, orgID, team.GetID()) //nolint:staticcheck // TODO: migrate to GetTeamBySlug
108111
if err != nil {
112+
if isRatelimited(resp) {
113+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
114+
}
109115
return nil, "", nil, err
110116
}
111117

@@ -164,8 +170,11 @@ func (o *teamResourceType) Grants(ctx context.Context, resource *v2.Resource, pT
164170
return nil, "", nil, fmt.Errorf("error fetching orgID from team profile")
165171
}
166172

167-
org, _, err := o.client.Organizations.GetByID(ctx, orgID)
173+
org, resp, err := o.client.Organizations.GetByID(ctx, orgID)
168174
if err != nil {
175+
if isRatelimited(resp) {
176+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
177+
}
169178
return nil, "", nil, err
170179
}
171180

pkg/connector/user.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import (
1212
"github.com/conductorone/baton-sdk/pkg/annotations"
1313
"github.com/conductorone/baton-sdk/pkg/pagination"
1414
"github.com/conductorone/baton-sdk/pkg/types/resource"
15+
"github.com/conductorone/baton-sdk/pkg/uhttp"
1516
"github.com/google/go-github/v69/github"
1617
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
1718
"github.com/shurcooL/githubv4"
1819
"go.uber.org/zap"
20+
"google.golang.org/grpc/codes"
1921
"google.golang.org/protobuf/types/known/timestamppb"
2022
)
2123

@@ -131,6 +133,9 @@ func (o *userResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
131133

132134
users, resp, err := o.client.Organizations.ListMembers(ctx, orgName, &opts)
133135
if err != nil {
136+
if isRatelimited(resp) {
137+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
138+
}
134139
return nil, "", nil, fmt.Errorf("github-connector: ListMembers failed: %w", err)
135140
}
136141

@@ -154,6 +159,9 @@ func (o *userResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
154159
for _, user := range users {
155160
u, res, err := o.client.Users.GetByID(ctx, user.GetID())
156161
if err != nil {
162+
if isRatelimited(res) {
163+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
164+
}
157165
// This undocumented API can return 404 for some users. If this fails it means we won't get some of their details like email
158166
if res == nil || res.StatusCode != http.StatusNotFound {
159167
return nil, "", nil, err

0 commit comments

Comments
 (0)