Skip to content

Commit

Permalink
Merge pull request #1051 from cloudflare/issue-699
Browse files Browse the repository at this point in the history
Make GitHub reporter sleep when ratelimited
  • Loading branch information
prymitive authored Aug 1, 2024
2 parents 160a26c + d399b51 commit ed96a7e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
### Fixed

- Fixed false positive warnings from [alerts/comparison](checks/alerts/comparison.md) when using `absent_over_time()`.
- GitHub reporter will now wait before making more requests if it's rate limited - #699.

## v0.62.2

Expand Down
16 changes: 10 additions & 6 deletions internal/reporter/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (gr GithubReporter) Summary(ctx context.Context, _ any, s Summary, errs []e
}

func (gr GithubReporter) List(ctx context.Context, _ any) ([]ExistingComment, error) {
reqCtx, cancel := context.WithTimeout(ctx, gr.timeout)
reqCtx, cancel := gr.reqContext(ctx)
defer cancel()

slog.Debug("Getting the list of pull request comments", slog.Int("pr", gr.prNum))
Expand Down Expand Up @@ -162,7 +162,7 @@ func (gr GithubReporter) Create(ctx context.Context, _ any, p PendingComment) er

slog.Debug("Creating a review comment", slog.String("body", comment.GetBody()), slog.String("commit", comment.GetCommitID()))

reqCtx, cancel := context.WithTimeout(ctx, gr.timeout)
reqCtx, cancel := gr.reqContext(ctx)
defer cancel()

_, _, err := gr.client.PullRequests.CreateComment(reqCtx, gr.owner, gr.repo, gr.prNum, comment)
Expand Down Expand Up @@ -192,7 +192,7 @@ func (gr GithubReporter) IsEqual(existing ExistingComment, pending PendingCommen
}

func (gr GithubReporter) findExistingReview(ctx context.Context) (*github.PullRequestReview, error) {
reqCtx, cancel := context.WithTimeout(ctx, gr.timeout)
reqCtx, cancel := gr.reqContext(ctx)
defer cancel()

reviews, _, err := gr.client.PullRequests.ListReviews(reqCtx, gr.owner, gr.repo, gr.prNum, nil)
Expand All @@ -212,7 +212,7 @@ func (gr GithubReporter) findExistingReview(ctx context.Context) (*github.PullRe
func (gr GithubReporter) updateReview(ctx context.Context, review *github.PullRequestReview, summary Summary) error {
slog.Info("Updating pull request review", slog.String("repo", fmt.Sprintf("%s/%s", gr.owner, gr.repo)))

reqCtx, cancel := context.WithTimeout(ctx, gr.timeout)
reqCtx, cancel := gr.reqContext(ctx)
defer cancel()

_, _, err := gr.client.PullRequests.UpdateReview(
Expand All @@ -229,7 +229,7 @@ func (gr GithubReporter) updateReview(ctx context.Context, review *github.PullRe
func (gr GithubReporter) createReview(ctx context.Context, summary Summary) error {
slog.Info("Creating pull request review", slog.String("repo", fmt.Sprintf("%s/%s", gr.owner, gr.repo)), slog.String("commit", gr.headCommit))

reqCtx, cancel := context.WithTimeout(ctx, gr.timeout)
reqCtx, cancel := gr.reqContext(ctx)
defer cancel()

_, resp, err := gr.client.PullRequests.CreateReview(
Expand Down Expand Up @@ -335,9 +335,13 @@ func (gr GithubReporter) generalComment(ctx context.Context, body string) error

slog.Debug("Creating PR comment", slog.String("body", comment.GetBody()))

reqCtx, cancel := context.WithTimeout(ctx, gr.timeout)
reqCtx, cancel := gr.reqContext(ctx)
defer cancel()

_, _, err := gr.client.Issues.CreateComment(reqCtx, gr.owner, gr.repo, gr.prNum, &comment)
return err
}

func (gr GithubReporter) reqContext(ctx context.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.WithValue(ctx, github.SleepUntilPrimaryRateLimitResetWhenRateLimited, true), gr.timeout)
}

0 comments on commit ed96a7e

Please sign in to comment.