Skip to content

Commit 206fc0d

Browse files
authored
Merge pull request #95 from ConductorOne/BB1134
[BB-1134] githbu: parse rate limit from response
2 parents 449ee2f + dcf077b commit 206fc0d

File tree

10 files changed

+31
-19
lines changed

10 files changed

+31
-19
lines changed

pkg/connector/helpers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ func extractRateLimitData(response *github.Response) (*v2.RateLimitDescription,
170170
ra = &timestamppb.Timestamp{Seconds: ts}
171171
}
172172

173+
status := v2.RateLimitDescription_STATUS_OK
174+
if r <= 0 {
175+
status = v2.RateLimitDescription_STATUS_OVERLIMIT
176+
}
173177
return &v2.RateLimitDescription{
178+
Status: status,
174179
Limit: l,
175180
Remaining: r,
176181
ResetAt: ra,
@@ -220,3 +225,10 @@ func isNotFoundError(resp *github.Response) bool {
220225
}
221226
return resp.StatusCode == http.StatusNotFound
222227
}
228+
229+
func isRatelimited(resp *github.Response) bool {
230+
if resp == nil {
231+
return false
232+
}
233+
return resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusTooManyRequests
234+
}

pkg/connector/invitation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (i *invitationResourceType) List(ctx context.Context, parentID *v2.Resource
9999
invitationResources = append(invitationResources, ir)
100100
}
101101
annotations.WithRateLimiting(restApiRateLimit)
102-
return invitationResources, pageToken, nil, nil
102+
return invitationResources, pageToken, annotations, nil
103103
}
104104

105105
func (i *invitationResourceType) Entitlements(_ context.Context, resource *v2.Resource, _ *pagination.Token) ([]*v2.Entitlement, string, annotations.Annotations, error) {
@@ -155,7 +155,7 @@ func (i *invitationResourceType) CreateAccount(
155155
}
156156
return &v2.CreateAccountResponse_SuccessResult{
157157
Resource: r,
158-
}, nil, nil, nil
158+
}, nil, annotations, nil
159159
}
160160

161161
func (i *invitationResourceType) Delete(ctx context.Context, resourceId *v2.ResourceId) (annotations.Annotations, error) {

pkg/connector/org.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ func (o *orgResourceType) Grants(
204204
if isNotFoundError(resp) {
205205
return nil, "", nil, uhttp.WrapErrors(codes.NotFound, fmt.Sprintf("org: %s not found", orgName))
206206
}
207-
return nil, "", nil, fmt.Errorf("github-connectorv2: failed to list org members: %w", err)
207+
errMsg := "github-connectorv2: failed to list org members"
208+
if isRatelimited(resp) {
209+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
210+
}
211+
return nil, "", nil, fmt.Errorf("%s: %w", errMsg, err)
208212
}
209213

210214
nextPage, reqAnnos, err := parseResp(resp)

pkg/connector/org_role_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestOrgRole(t *testing.T) {
5858
grants = append(grants, nextGrants...)
5959

6060
require.Nil(t, err)
61-
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
61+
test.AssertHasRatelimitAnnotations(t, grantsAnnotations)
6262
if nextToken == "" {
6363
break
6464
}
@@ -98,7 +98,7 @@ func TestOrgRole(t *testing.T) {
9898
require.Nil(t, err)
9999
require.Empty(t, resources)
100100
require.Empty(t, nextToken)
101-
test.AssertNoRatelimitAnnotations(t, annotations)
101+
test.AssertHasRatelimitAnnotations(t, annotations)
102102

103103
// Test Grants with permission error
104104
role, _ := orgRoleResource(ctx, &OrganizationRole{
@@ -112,6 +112,6 @@ func TestOrgRole(t *testing.T) {
112112
require.Empty(t, grants)
113113
// The token should contain the initial state for users
114114
require.NotEmpty(t, nextToken)
115-
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
115+
test.AssertHasRatelimitAnnotations(t, grantsAnnotations)
116116
})
117117
}

pkg/connector/org_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestOrganization(t *testing.T) {
3939

4040
grants, nextToken, grantsAnnotations, err := client.Grants(ctx, organization, &pagination.Token{})
4141
require.Nil(t, err)
42-
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
42+
test.AssertHasRatelimitAnnotations(t, grantsAnnotations)
4343
require.Equal(t, "", nextToken)
4444
require.Len(t, grants, 2)
4545

pkg/connector/repository_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestRepository(t *testing.T) {
5353
grants = append(grants, nextGrants...)
5454

5555
require.Nil(t, err)
56-
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
56+
test.AssertHasRatelimitAnnotations(t, grantsAnnotations)
5757
if nextToken == "" {
5858
break
5959
}

pkg/connector/team.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ func (o *teamResourceType) Grants(ctx context.Context, resource *v2.Resource, pT
186186
if isNotFoundError(resp) {
187187
return nil, "", nil, uhttp.WrapErrors(codes.NotFound, fmt.Sprintf("org: %d not found", org.GetID()))
188188
}
189+
if isRatelimited(resp) {
190+
return nil, "", nil, uhttp.WrapErrors(codes.Unavailable, "too many requests", err)
191+
}
189192
return nil, "", nil, fmt.Errorf("github-connectorv2: failed to fetch team members: %w", err)
190193
}
191194

pkg/connector/team_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestTeam(t *testing.T) {
4141

4242
grants, nextToken, grantsAnnotations, err := client.Grants(ctx, team, &pagination.Token{})
4343
require.Nil(t, err)
44-
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
44+
test.AssertHasRatelimitAnnotations(t, grantsAnnotations)
4545
require.Equal(t, "", nextToken)
4646
require.Len(t, grants, 1)
4747

pkg/connector/user_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestUsersList(t *testing.T) {
5858
&pagination.Token{},
5959
)
6060
require.Nil(t, err)
61-
test.AssertNoRatelimitAnnotations(t, annotations)
61+
test.AssertHasRatelimitAnnotations(t, annotations)
6262
require.Equal(t, "", nextToken)
6363
require.Len(t, users, 1)
6464
require.Equal(t, *githubUser.Login, users[0].Id.Resource)

test/testhelpers.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package test
22

33
import (
4-
"slices"
54
"testing"
65

76
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
87
"github.com/conductorone/baton-sdk/pkg/annotations"
98
)
109

11-
func AssertNoRatelimitAnnotations(
10+
func AssertHasRatelimitAnnotations(
1211
t *testing.T,
1312
actualAnnotations annotations.Annotations,
1413
) {
@@ -22,13 +21,7 @@ func AssertNoRatelimitAnnotations(
2221
if err != nil {
2322
continue
2423
}
25-
if slices.Contains(
26-
[]v2.RateLimitDescription_Status{
27-
v2.RateLimitDescription_STATUS_ERROR,
28-
v2.RateLimitDescription_STATUS_OVERLIMIT,
29-
},
30-
ratelimitDescription.Status,
31-
) {
24+
if ratelimitDescription.Status != v2.RateLimitDescription_STATUS_OVERLIMIT {
3225
t.Fatal("request was ratelimited, expected not to be ratelimited")
3326
}
3427
}

0 commit comments

Comments
 (0)