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

Implement rate limit handling #238

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
104 changes: 79 additions & 25 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path"
"strconv"
"strings"
"time"

"github.com/google/go-github/v28/github"
"github.com/shurcooL/githubv4"
Expand Down Expand Up @@ -186,8 +187,13 @@ func (m *GithubClient) ListModifiedFiles(prNumber int) ([]string, error) {
prNumber,
opt,
)

if err != nil {
return nil, err
if rateLimited(err) {
continue
} else {
return nil, err
}
}
for _, f := range result {
files = append(files, *f.Filename)
Expand All @@ -207,15 +213,24 @@ func (m *GithubClient) PostComment(prNumber, comment string) error {
return fmt.Errorf("failed to convert pull request number to int: %s", err)
}

_, _, err = m.V3.Issues.CreateComment(
context.TODO(),
m.Owner,
m.Repository,
pr,
&github.IssueComment{
Body: github.String(comment),
},
)
for {
_, _, err = m.V3.Issues.CreateComment(
context.TODO(),
m.Owner,
m.Repository,
pr,
&github.IssueComment{
Body: github.String(comment),
},
)
if err != nil {
if rateLimited(err) {
continue
} else {
break
}
}
}
return err
}

Expand Down Expand Up @@ -341,18 +356,30 @@ func (m *GithubClient) UpdateCommitStatus(commitRef, baseContext, statusContext,
description = fmt.Sprintf("Concourse CI build %s", status)
}

_, _, err := m.V3.Repositories.CreateStatus(
context.TODO(),
m.Owner,
m.Repository,
commitRef,
&github.RepoStatus{
State: github.String(strings.ToLower(status)),
TargetURL: github.String(targetURL),
Description: github.String(description),
Context: github.String(path.Join(baseContext, statusContext)),
},
)
var err error
for {
_, _, err = m.V3.Repositories.CreateStatus(
context.TODO(),
m.Owner,
m.Repository,
commitRef,
&github.RepoStatus{
State: github.String(strings.ToLower(status)),
TargetURL: github.String(targetURL),
Description: github.String(description),
Context: github.String(path.Join(baseContext, statusContext)),
},
)

if err != nil {
if rateLimited(err) {
continue
} else {
break
}
}

}
return err
}

Expand Down Expand Up @@ -396,9 +423,15 @@ func (m *GithubClient) DeletePreviousComments(prNumber string) error {

for _, e := range getComments.Repository.PullRequest.Comments.Edges {
if e.Node.Author.Login == getComments.Viewer.Login {
_, err := m.V3.Issues.DeleteComment(context.TODO(), m.Owner, m.Repository, e.Node.DatabaseId)
if err != nil {
return err
for {
_, err := m.V3.Issues.DeleteComment(context.TODO(), m.Owner, m.Repository, e.Node.DatabaseId)
if err != nil {
if rateLimited(err) {
continue
} else {
return err
}
}
}
}
}
Expand All @@ -413,3 +446,24 @@ func parseRepository(s string) (string, string, error) {
}
return parts[0], parts[1], nil
}

func rateLimited(err error) bool {
if err == nil {
return false
}
rerr, ok := err.(*github.RateLimitError)
if ok {
var d = rerr.Rate.Reset.Time.Sub(time.Now())
//hit rate limit, sleeping
time.Sleep(d)
return true
}
aerr, ok := err.(*github.AbuseRateLimitError)
if ok {
var d = aerr.GetRetryAfter()
//hit abuse mechanism, sleeping
time.Sleep(d)
return true
}
return false
}