Skip to content

Commit 374f1ee

Browse files
Merge pull request #41 from ConductorOne/luisinasantos/fix-rate-limit-error
add rate limit error to be handled by baton-sdk
2 parents 954f4a2 + a4b518e commit 374f1ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+39
-12790
lines changed

go.mod

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ require (
99
github.com/ennyjfrick/ruleguard-logfatal v0.0.2
1010
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
1111
github.com/quasilyte/go-ruleguard/dsl v0.3.22
12-
github.com/spf13/viper v1.19.0
1312
gitlab.com/gitlab-org/api/client-go v0.118.0
1413
go.uber.org/zap v1.27.0
14+
google.golang.org/grpc v1.71.0
1515
)
1616

1717
require (
@@ -43,7 +43,6 @@ require (
4343
github.com/conductorone/dpop v0.2.3 // indirect
4444
github.com/conductorone/dpop/integrations/dpop_grpc v0.2.3 // indirect
4545
github.com/conductorone/dpop/integrations/dpop_oauth2 v0.2.3 // indirect
46-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
4746
github.com/deckarep/golang-set/v2 v2.7.0 // indirect
4847
github.com/dolthub/maphash v0.1.0 // indirect
4948
github.com/doug-martin/goqu/v9 v9.19.0 // indirect
@@ -73,7 +72,6 @@ require (
7372
github.com/mitchellh/mapstructure v1.5.0 // indirect
7473
github.com/ncruces/go-strftime v0.1.9 // indirect
7574
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
76-
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
7775
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
7876
github.com/pquerna/cachecontrol v0.2.0 // indirect
7977
github.com/pquerna/xjwt v0.3.0 // indirect
@@ -89,7 +87,8 @@ require (
8987
github.com/spf13/cast v1.7.1 // indirect
9088
github.com/spf13/cobra v1.8.1 // indirect
9189
github.com/spf13/pflag v1.0.6 // indirect
92-
github.com/stretchr/testify v1.10.0 // indirect
90+
github.com/spf13/viper v1.19.0 // indirect
91+
github.com/stretchr/objx v0.5.2 // indirect
9392
github.com/subosito/gotenv v1.6.0 // indirect
9493
github.com/tklauser/go-sysconf v0.3.14 // indirect
9594
github.com/tklauser/numcpus v0.9.0 // indirect
@@ -119,7 +118,6 @@ require (
119118
golang.org/x/time v0.9.0 // indirect
120119
google.golang.org/genproto/googleapis/api v0.0.0-20250218202821-56aae31c358a // indirect
121120
google.golang.org/genproto/googleapis/rpc v0.0.0-20250303144028-a0af3efb3deb // indirect
122-
google.golang.org/grpc v1.71.0 // indirect
123121
google.golang.org/protobuf v1.36.5 // indirect
124122
gopkg.in/ini.v1 v1.67.0 // indirect
125123
gopkg.in/yaml.v2 v2.4.0 // indirect

pkg/connector/gitlab/client.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package gitlab
22

33
import (
44
"context"
5+
"errors"
56
"net/http"
67
"strconv"
78

89
"github.com/conductorone/baton-sdk/pkg/uhttp"
910
gitlabSDK "gitlab.com/gitlab-org/api/client-go"
11+
"google.golang.org/grpc/codes"
12+
"google.golang.org/grpc/status"
1013
)
1114

1215
type Client struct {
@@ -69,3 +72,21 @@ func (o *Client) GetAllUsers(ctx context.Context, nextPageToken string) ([]gitla
6972

7073
return users, resp, nil
7174
}
75+
76+
// wrapError takes the error from the request and validates the code. It wraps the error with the expected code for the SDK to handle it.
77+
//
78+
// TODO: Include the other codes expected by the SDK for it to behave properly.
79+
func wrapError(err error, response *gitlabSDK.Response) error {
80+
if response == nil {
81+
return err
82+
}
83+
84+
// Validates if the code error was a 429 (rate limit) and wraps the error with the expected code by the baton-sdk.
85+
if response.StatusCode == http.StatusTooManyRequests {
86+
st := status.New(codes.Unavailable, response.Status)
87+
allErrs := append([]error{st.Err()}, err)
88+
err = errors.Join(allErrs...)
89+
}
90+
91+
return err
92+
}

pkg/connector/gitlab/groups_endpoints.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gitlab
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"strconv"
@@ -33,7 +34,7 @@ func (o *Client) ListGroups(ctx context.Context, nextPageStr string) ([]*gitlabS
3334
}
3435

3536
if res.StatusCode < 200 || res.StatusCode >= 300 {
36-
return nil, res, err
37+
return nil, res, wrapError(err, res)
3738
}
3839

3940
return groups, res, nil
@@ -50,7 +51,7 @@ func (o *Client) ListGroupMembers(ctx context.Context, groupId string) ([]*gitla
5051
}
5152

5253
if res.StatusCode < 200 || res.StatusCode >= 300 {
53-
return nil, res, err
54+
return nil, res, wrapError(err, res)
5455
}
5556

5657
return users, res, nil
@@ -86,7 +87,7 @@ func (o *Client) ListGroupMembersPaginate(ctx context.Context, groupId string, n
8687
}
8788

8889
if res.StatusCode < 200 || res.StatusCode >= 300 {
89-
return nil, res, err
90+
return nil, res, wrapError(err, res)
9091
}
9192

9293
return users, res, nil
@@ -105,7 +106,7 @@ func (o *Client) AddGroupMember(ctx context.Context, groupId string, userId int,
105106
}
106107

107108
if res.StatusCode < 200 || res.StatusCode >= 300 {
108-
return err
109+
return wrapError(err, res)
109110
}
110111

111112
return nil
@@ -126,9 +127,9 @@ func (o *Client) InviteGroupMember(ctx context.Context, groupId, userEmail strin
126127
if res.StatusCode < 200 || res.StatusCode >= 300 {
127128
body, readErr := io.ReadAll(res.Body)
128129
if readErr != nil {
129-
return fmt.Errorf("failed to invite user: status=%d, could not read response body: %w", res.StatusCode, readErr)
130+
return errors.Join(wrapError(err, res), fmt.Errorf("failed to invite user: status=%d, could not read response body: %w", res.StatusCode, readErr))
130131
}
131-
return fmt.Errorf("failed to invite user: status=%d body=%s", res.StatusCode, string(body))
132+
return errors.Join(wrapError(err, res), fmt.Errorf("failed to invite user: status=%d body=%s", res.StatusCode, string(body)))
132133
}
133134

134135
return nil
@@ -145,7 +146,7 @@ func (o *Client) RemoveGroupMember(ctx context.Context, groupId string, userId i
145146
}
146147

147148
if res.StatusCode < 200 || res.StatusCode >= 300 {
148-
return err
149+
return wrapError(err, res)
149150
}
150151

151152
return nil

pkg/connector/gitlab/projects_endpoints.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (o *Client) ListProjects(ctx context.Context, groupId, nextPageStr string)
3232
}
3333

3434
if res.StatusCode < 200 || res.StatusCode >= 300 {
35-
return nil, res, err
35+
return nil, res, wrapError(err, res)
3636
}
3737

3838
return projects, res, nil
@@ -49,7 +49,7 @@ func (o *Client) ListProjectMembers(ctx context.Context, projectId string) ([]*g
4949
}
5050

5151
if res.StatusCode < 200 || res.StatusCode >= 300 {
52-
return nil, res, err
52+
return nil, res, wrapError(err, res)
5353
}
5454

5555
return users, res, nil
@@ -85,7 +85,7 @@ func (o *Client) ListProjectMembersPaginate(ctx context.Context, projectId, next
8585
}
8686

8787
if res.StatusCode < 200 || res.StatusCode >= 300 {
88-
return nil, res, err
88+
return nil, res, wrapError(err, res)
8989
}
9090

9191
return users, res, nil
@@ -104,7 +104,7 @@ func (o *Client) AddProjectMember(ctx context.Context, projectId string, userId
104104
}
105105

106106
if res.StatusCode < 200 || res.StatusCode >= 300 {
107-
return nil, err
107+
return nil, wrapError(err, res)
108108
}
109109

110110
return user, nil
@@ -120,7 +120,7 @@ func (o *Client) RemoveProjectMember(ctx context.Context, projectId string, user
120120
}
121121

122122
if res.StatusCode < 200 || res.StatusCode >= 300 {
123-
return err
123+
return wrapError(err, res)
124124
}
125125

126126
return nil

vendor/github.com/conductorone/baton-sdk/pkg/test/configSchema.go

Lines changed: 0 additions & 102 deletions
This file was deleted.

vendor/github.com/conductorone/baton-sdk/pkg/test/integration.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)