Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Seconday API Rate limit #27

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"fmt"

gh "github.com/google/go-github/v59/github"
gh "github.com/google/go-github/v67/github"
"github.com/spf13/cobra"

"github.com/cilium/team-manager/pkg/config"
Expand Down
2 changes: 1 addition & 1 deletion cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"strings"

gh "github.com/google/go-github/v59/github"
gh "github.com/google/go-github/v67/github"
"github.com/spf13/cobra"

"github.com/cilium/team-manager/pkg/config"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/cilium/team-manager
go 1.22

require (
github.com/google/go-github/v59 v59.0.0
github.com/google/go-github/v67 v67.0.0
github.com/google/renameio v1.0.1
github.com/kr/pretty v0.3.1
github.com/pmezard/go-difflib v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v59 v59.0.0 h1:7h6bgpF5as0YQLLkEiVqpgtJqjimMYhBkD4jT5aN3VA=
github.com/google/go-github/v59 v59.0.0/go.mod h1:rJU4R0rQHFVFDOkqGWxfLNo6vEk4dv40oDjhV/gH6wM=
github.com/google/go-github/v67 v67.0.0 h1:g11NDAmfaBaCO8qYdI9fsmbaRipHNWRIU/2YGvlh4rg=
github.com/google/go-github/v67 v67.0.0/go.mod h1:zH3K7BxjFndr9QSeFibx4lTKkYS3K9nDanoI1NjaOtY=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU=
Expand Down
2 changes: 1 addition & 1 deletion pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"os"

gh "github.com/google/go-github/v59/github"
gh "github.com/google/go-github/v67/github"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
Expand Down
30 changes: 28 additions & 2 deletions pkg/team/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ package team

import (
"context"
"errors"
"fmt"
"sort"
"strings"
"time"

gh "github.com/google/go-github/v59/github"
gh "github.com/google/go-github/v67/github"
"github.com/schollz/progressbar/v3"
"github.com/shurcooL/githubv4"
"github.com/shurcooL/graphql"
Expand Down Expand Up @@ -110,7 +112,7 @@ func (tm *Manager) fetchRepositories(ctx context.Context, c *config.Config) erro
if requeryOrgs {
resultRepos, err = tm.queryOrgRepos(ctx, variables)
if err != nil {
return fmt.Errorf("failed to requery teams: %w", err)
return fmt.Errorf("failed to requery org repositories: %w", err)
}
requeryOrgs = false
}
Expand Down Expand Up @@ -555,3 +557,27 @@ func (tm *Manager) fetchMemberLimitedAvailability(ctx context.Context, login str

return bool(resultMember.User.Status.IndicatesLimitedAvailability), nil
}

func (tm *Manager) gqlQuery(ctx context.Context, q interface{}, variables map[string]interface{}) error {
var (
rateLimitError *gh.RateLimitError
abuseRateLimitError *gh.AbuseRateLimitError
)
for {
err := tm.gqlGHClient.Query(ctx, q, variables)
if err != nil {
switch {
case errors.As(err, &rateLimitError):
fmt.Printf("hit rate limit, sleeping for 30 seconds...\n")
time.Sleep(30 * time.Second)
case errors.As(err, &abuseRateLimitError) || strings.Contains(err.Error(), "You have exceeded a secondary rate limit"):
fmt.Printf("hit secondary limit, sleeping for 30 seconds...\n")
time.Sleep(30 * time.Second)
default:
return err
}
continue
}
return nil
}
}
2 changes: 1 addition & 1 deletion pkg/team/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/cilium/team-manager/pkg/github"
"github.com/cilium/team-manager/pkg/slices"
"github.com/cilium/team-manager/pkg/terminal"
gh "github.com/google/go-github/v59/github"
gh "github.com/google/go-github/v67/github"
"github.com/shurcooL/githubv4"
)

Expand Down
11 changes: 6 additions & 5 deletions pkg/team/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (tm *Manager) queryOrgRepos(ctx context.Context, additionalVariables map[st
variables[k] = v
}

err := tm.gqlGHClient.Query(ctx, &q, variables)
err := tm.gqlQuery(ctx, &q, variables)

if err != nil {
return queryResultRepositories{}, err
}
Expand All @@ -52,7 +53,7 @@ func (tm *Manager) queryOrgMembers(ctx context.Context, additionalVariables map[
variables[k] = v
}

err := tm.gqlGHClient.Query(ctx, &q, variables)
err := tm.gqlQuery(ctx, &q, variables)
if err != nil {
return queryResultMembers{}, err
}
Expand All @@ -72,7 +73,7 @@ func (tm *Manager) queryTeamsRepositories(ctx context.Context, additionalVariabl
variables[k] = v
}

err := tm.gqlGHClient.Query(ctx, &q, variables)
err := tm.gqlQuery(ctx, &q, variables)
if err != nil {
return queryTeamsRepositoriesResult{}, err
}
Expand All @@ -92,7 +93,7 @@ func (tm *Manager) queryTeamsMembers(ctx context.Context, additionalVariables ma
variables[k] = v
}

err := tm.gqlGHClient.Query(ctx, &q, variables)
err := tm.gqlQuery(ctx, &q, variables)
if err != nil {
return queryTeamsMembersResult{}, err
}
Expand All @@ -110,7 +111,7 @@ func (tm *Manager) queryOrgMemberLimitedAvailability(ctx context.Context, additi
variables[k] = v
}

err := tm.gqlGHClient.Query(ctx, &q, variables)
err := tm.gqlQuery(ctx, &q, variables)
if err != nil {
return queryResultMemberLimitedAvailability{}, err
}
Expand Down
Loading