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

Make GitHub reporter sleep when ratelimited #1051

Merged
merged 1 commit into from
Aug 1, 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
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)
}