Skip to content

Commit 73340da

Browse files
Add RateLimit() function to gh client
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent fafe98e commit 73340da

File tree

5 files changed

+54
-29
lines changed

5 files changed

+54
-29
lines changed

runner/common/util.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type GithubEntityOperations interface {
1818
ListEntityRunners(ctx context.Context, opts *github.ListRunnersOptions) (*github.Runners, *github.Response, error)
1919
ListEntityRunnerApplicationDownloads(ctx context.Context) ([]*github.RunnerApplicationDownload, *github.Response, error)
2020
RemoveEntityRunner(ctx context.Context, runnerID int64) error
21+
RateLimit(ctx context.Context) (*github.RateLimits, error)
2122
CreateEntityRegistrationToken(ctx context.Context) (*github.RegistrationToken, *github.Response, error)
2223
GetEntityJITConfig(ctx context.Context, instance string, pool params.Pool, labels []string) (jitConfigMap map[string]string, runner *github.Runner, err error)
2324

runner/pool/stub_client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ func (s *stubGithubClient) GetEntity() params.GithubEntity {
6464
func (s *stubGithubClient) GithubBaseURL() *url.URL {
6565
return nil
6666
}
67+
68+
func (s *stubGithubClient) RateLimit(_ context.Context) (*github.RateLimits, error) {
69+
return nil, s.err
70+
}

test/integration/organizations_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ func getGhOrgWebhook(url, ghToken, orgName string) (*github.Hook, error) {
9999
}
100100

101101
for _, hook := range ghOrgHooks {
102-
hookURL, ok := hook.Config["url"].(string)
103-
if ok && hookURL == url {
102+
hookURL := hook.Config.GetURL()
103+
if hookURL == url {
104104
return hook, nil
105105
}
106106
}

test/integration/repositories_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ func getGhRepoWebhook(url, ghToken, orgName, repoName string) (*github.Hook, err
109109
}
110110

111111
for _, hook := range ghRepoHooks {
112-
hookURL, ok := hook.Config["url"].(string)
113-
if ok && hookURL == url {
112+
hookURL := hook.Config.GetURL()
113+
if hookURL == url {
114114
return hook, nil
115115
}
116116
}

util/github/client.go

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type githubClient struct {
3737
org *github.OrganizationsService
3838
repo *github.RepositoriesService
3939
enterprise *github.EnterpriseService
40+
rateLimit *github.RateLimitService
4041

4142
entity params.GithubEntity
4243
cli *github.Client
@@ -226,6 +227,38 @@ func (g *githubClient) ListEntityRunnerApplicationDownloads(ctx context.Context)
226227
return ret, response, err
227228
}
228229

230+
func parseError(response *github.Response, err error) error {
231+
switch response.StatusCode {
232+
case http.StatusNotFound:
233+
return runnerErrors.ErrNotFound
234+
case http.StatusUnauthorized:
235+
return runnerErrors.ErrUnauthorized
236+
case http.StatusUnprocessableEntity:
237+
return runnerErrors.ErrBadRequest
238+
default:
239+
if response.StatusCode >= 100 && response.StatusCode < 300 {
240+
return nil
241+
}
242+
if err != nil {
243+
errResp := &github.ErrorResponse{}
244+
if errors.As(err, &errResp) && errResp.Response != nil {
245+
switch errResp.Response.StatusCode {
246+
case http.StatusNotFound:
247+
return runnerErrors.ErrNotFound
248+
case http.StatusUnauthorized:
249+
return runnerErrors.ErrUnauthorized
250+
case http.StatusUnprocessableEntity:
251+
return runnerErrors.ErrBadRequest
252+
default:
253+
return err
254+
}
255+
}
256+
return err
257+
}
258+
return errors.New("unknown error")
259+
}
260+
}
261+
229262
func (g *githubClient) RemoveEntityRunner(ctx context.Context, runnerID int64) error {
230263
var response *github.Response
231264
var err error
@@ -254,30 +287,8 @@ func (g *githubClient) RemoveEntityRunner(ctx context.Context, runnerID int64) e
254287
return errors.New("invalid entity type")
255288
}
256289

257-
switch response.StatusCode {
258-
case http.StatusNotFound:
259-
return runnerErrors.NewNotFoundError("runner %d not found", runnerID)
260-
case http.StatusUnauthorized:
261-
return runnerErrors.ErrUnauthorized
262-
case http.StatusUnprocessableEntity:
263-
return runnerErrors.NewBadRequestError("cannot remove runner %d in its current state", runnerID)
264-
default:
265-
if err != nil {
266-
errResp := &github.ErrorResponse{}
267-
if errors.As(err, &errResp) && errResp.Response != nil {
268-
switch errResp.Response.StatusCode {
269-
case http.StatusNotFound:
270-
return runnerErrors.NewNotFoundError("runner %d not found", runnerID)
271-
case http.StatusUnauthorized:
272-
return runnerErrors.ErrUnauthorized
273-
case http.StatusUnprocessableEntity:
274-
return runnerErrors.NewBadRequestError("cannot remove runner %d in its current state", runnerID)
275-
default:
276-
return errors.Wrap(err, "removing runner")
277-
}
278-
}
279-
return errors.Wrap(err, "removing runner")
280-
}
290+
if err := parseError(response, err); err != nil {
291+
return errors.Wrapf(err, "removing runner %d", runnerID)
281292
}
282293

283294
return nil
@@ -411,7 +422,7 @@ func (g *githubClient) GetEntityJITConfig(ctx context.Context, instance string,
411422
Labels: labels,
412423
// nolint:golangci-lint,godox
413424
// TODO(gabriel-samfira): Should we make this configurable?
414-
WorkFolder: github.String("_work"),
425+
WorkFolder: github.Ptr("_work"),
415426
}
416427

417428
metrics.GithubOperationCount.WithLabelValues(
@@ -463,6 +474,14 @@ func (g *githubClient) GetEntityJITConfig(ctx context.Context, instance string,
463474
return jitConfig, ret.Runner, nil
464475
}
465476

477+
func (g *githubClient) RateLimit(ctx context.Context) (*github.RateLimits, error) {
478+
limits, resp, err := g.rateLimit.Get(ctx)
479+
if err := parseError(resp, err); err != nil {
480+
return nil, fmt.Errorf("getting rate limit: %w", err)
481+
}
482+
return limits, nil
483+
}
484+
466485
func (g *githubClient) GetEntity() params.GithubEntity {
467486
return g.entity
468487
}
@@ -494,6 +513,7 @@ func Client(ctx context.Context, entity params.GithubEntity) (common.GithubClien
494513
org: ghClient.Organizations,
495514
repo: ghClient.Repositories,
496515
enterprise: ghClient.Enterprise,
516+
rateLimit: ghClient.RateLimit,
497517
cli: ghClient,
498518
entity: entity,
499519
}

0 commit comments

Comments
 (0)