@@ -37,6 +37,7 @@ type githubClient struct {
37
37
org * github.OrganizationsService
38
38
repo * github.RepositoriesService
39
39
enterprise * github.EnterpriseService
40
+ rateLimit * github.RateLimitService
40
41
41
42
entity params.GithubEntity
42
43
cli * github.Client
@@ -226,6 +227,38 @@ func (g *githubClient) ListEntityRunnerApplicationDownloads(ctx context.Context)
226
227
return ret , response , err
227
228
}
228
229
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
+
229
262
func (g * githubClient ) RemoveEntityRunner (ctx context.Context , runnerID int64 ) error {
230
263
var response * github.Response
231
264
var err error
@@ -254,30 +287,8 @@ func (g *githubClient) RemoveEntityRunner(ctx context.Context, runnerID int64) e
254
287
return errors .New ("invalid entity type" )
255
288
}
256
289
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 )
281
292
}
282
293
283
294
return nil
@@ -411,7 +422,7 @@ func (g *githubClient) GetEntityJITConfig(ctx context.Context, instance string,
411
422
Labels : labels ,
412
423
// nolint:golangci-lint,godox
413
424
// TODO(gabriel-samfira): Should we make this configurable?
414
- WorkFolder : github .String ("_work" ),
425
+ WorkFolder : github .Ptr ("_work" ),
415
426
}
416
427
417
428
metrics .GithubOperationCount .WithLabelValues (
@@ -463,6 +474,14 @@ func (g *githubClient) GetEntityJITConfig(ctx context.Context, instance string,
463
474
return jitConfig , ret .Runner , nil
464
475
}
465
476
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
+
466
485
func (g * githubClient ) GetEntity () params.GithubEntity {
467
486
return g .entity
468
487
}
@@ -494,6 +513,7 @@ func Client(ctx context.Context, entity params.GithubEntity) (common.GithubClien
494
513
org : ghClient .Organizations ,
495
514
repo : ghClient .Repositories ,
496
515
enterprise : ghClient .Enterprise ,
516
+ rateLimit : ghClient .RateLimit ,
497
517
cli : ghClient ,
498
518
entity : entity ,
499
519
}
0 commit comments