Skip to content

Commit

Permalink
Merge pull request #336 from cpanato/add-rate-limit
Browse files Browse the repository at this point in the history
add rate limit api check
  • Loading branch information
k8s-ci-robot committed Apr 9, 2024
2 parents 9e80eab + 4becd88 commit 9c4afcf
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 0 deletions.
19 changes: 19 additions & 0 deletions github/github.go
Expand Up @@ -156,6 +156,9 @@ type Client interface {
RequestPullRequestReview(
context.Context, string, string, int, []string, []string,
) (*github.PullRequest, error)
CheckRateLimit(
context.Context,
) (*github.RateLimits, *github.Response, error)
}

// NewIssueOptions is a struct of optional fields for new issues
Expand Down Expand Up @@ -568,6 +571,17 @@ func (g *githubClient) ListComments(
return comments, response, nil
}

func (g *githubClient) CheckRateLimit(
ctx context.Context,
) (*github.RateLimits, *github.Response, error) {
rt, response, err := g.RateLimit.Get(ctx)
if err != nil {
return nil, nil, fmt.Errorf("fetching rate limit: %w", err)
}

return rt, response, nil
}

// SetClient can be used to manually set the internal GitHub client
func (g *GitHub) SetClient(client Client) {
g.client = client
Expand Down Expand Up @@ -1161,6 +1175,11 @@ func (g *GitHub) ListTags(owner, repo string) ([]*github.RepositoryTag, error) {
return tags, nil
}

// RateLimit returns the rate limits for the current client.
func (g *GitHub) CheckRateLimit(ctx context.Context) (*github.RateLimits, *github.Response, error) {
return g.Client().CheckRateLimit(ctx)
}

func (g *githubClient) UpdateIssue(
ctx context.Context, owner, repo string, number int, issueRequest *github.IssueRequest,
) (*github.Issue, *github.Response, error) {
Expand Down
27 changes: 27 additions & 0 deletions github/github_test.go
Expand Up @@ -728,3 +728,30 @@ func TestUpdateReleasePageWithOptions(t *testing.T) {
}
}
}

func TestCheckRateLimit(t *testing.T) {
// Given
sut, client := newSUT()

now := gogithub.Timestamp{time.Now().UTC()} //nolint: govet

rt := &gogithub.RateLimits{
Core: &gogithub.Rate{
Limit: 5000,
Remaining: 200,
Reset: now,
},
}

client.CheckRateLimitReturns(rt, &gogithub.Response{}, nil)

// When
rt, result, err := sut.CheckRateLimit(context.Background())

// Then
require.Nil(t, err)
require.NotNil(t, result)
require.Equal(t, rt.Core.Limit, 5000)
require.Equal(t, rt.Core.Remaining, 200)
require.Equal(t, rt.Core.Reset, now)
}
84 changes: 84 additions & 0 deletions github/githubfakes/fake_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions github/record.go
Expand Up @@ -51,6 +51,7 @@ const (
gitHubAPIListMilestones gitHubAPI = "ListMilestones"
gitHubAPIListIssues gitHubAPI = "ListIssues"
gitHubAPIListComments gitHubAPI = "ListComments"
gitHubAPICheckRateLimit gitHubAPI = "CheckRateLimit"
)

type apiRecord struct {
Expand Down Expand Up @@ -345,6 +346,19 @@ func (c *githubNotesRecordClient) ListComments(
return comments, resp, nil
}

func (c *githubNotesRecordClient) CheckRateLimit(
ctx context.Context,
) (*github.RateLimits, *github.Response, error) {
rt, resp, err := c.client.CheckRateLimit(ctx)
if err != nil {
return nil, nil, err
}
if err := c.recordAPICall(gitHubAPICheckRateLimit, rt, resp); err != nil {
return nil, nil, err
}
return rt, resp, nil
}

// recordAPICall records a single GitHub API call into a JSON file by ensuring
// naming conventions
func (c *githubNotesRecordClient) recordAPICall(
Expand Down
15 changes: 15 additions & 0 deletions github/replay.go
Expand Up @@ -357,3 +357,18 @@ func (c *githubNotesReplayClient) ListComments(
}
return comments, record.response(), nil
}

func (c *githubNotesReplayClient) CheckRateLimit(
_ context.Context,
) (*github.RateLimits, *github.Response, error) {
data, err := c.readRecordedData(gitHubAPICheckRateLimit)
if err != nil {
return nil, nil, err
}
rt := &github.RateLimits{}
record := apiRecord{Result: rt}
if err := json.Unmarshal(data, &record); err != nil {
return nil, nil, err
}
return rt, record.response(), nil
}

0 comments on commit 9c4afcf

Please sign in to comment.