@@ -10,9 +10,9 @@ import (
1010 "strings"
1111 "time"
1212
13- "github.com/eapache/go-resiliency/retrier"
14- "github.com/google/go-github/github"
1513 "golang.org/x/oauth2"
14+
15+ "github.com/google/go-github/github"
1616)
1717
1818// Command represents a command to run.
@@ -71,7 +71,7 @@ func (o Output) String() string {
7171}
7272
7373// Push pushes the commit to Github and opens a pull request
74- func Push (ctx context.Context , input Input ) (Output , error ) {
74+ func Push (ctx context.Context , input Input , githubLimiter * time. Ticker ) (Output , error ) {
7575 // Get the commit SHA from the last commit
7676 cmd := Command {Path : "git" , Args : []string {"log" , "-1" , "--pretty=format:%H" }}
7777 gitLog := exec .CommandContext (ctx , cmd .Path , cmd .Args ... )
@@ -90,54 +90,36 @@ func Push(ctx context.Context, input Input) (Output, error) {
9090 return Output {Success : false }, errors .New (string (output ))
9191 }
9292
93- // Open a pull request, if one doesn't exist already
93+ // Create Github Client
9494 ts := oauth2 .StaticTokenSource (
9595 & oauth2.Token {AccessToken : os .Getenv ("GITHUB_API_TOKEN" )},
9696 )
9797 tc := oauth2 .NewClient (ctx , ts )
9898 client := github .NewClient (tc )
99+
100+ // Open a pull request, if one doesn't exist already
99101 head := fmt .Sprintf ("%s:%s" , input .RepoOwner , input .BranchName )
100102 base := "master"
101103
102- // Configure a retrier - exponential backoff upon hitting Github API rate limit errors
103- r := retrier .New (retrier .ExponentialBackoff (10 , time .Second ), retrier.WhitelistClassifier {
104- & github.RateLimitError {},
105- & github.AbuseRateLimitError {},
106- })
107- r .SetJitter (0.5 )
108-
109- var pr * github.PullRequest
110- err = r .Run (func () error {
111- var prErr error
112- pr , prErr = findOrCreatePR (ctx , client , input .RepoOwner , input .RepoName , & github.NewPullRequest {
113- Title : & input .PRMessage ,
114- Head : & head ,
115- Base : & base ,
116- })
117- return prErr
118- })
104+ pr , err := findOrCreatePR (ctx , client , input .RepoOwner , input .RepoName , & github.NewPullRequest {
105+ Title : & input .PRMessage ,
106+ Head : & head ,
107+ Base : & base ,
108+ }, githubLimiter )
119109 if err != nil {
120110 return Output {Success : false }, err
121111 }
122112
123113 if pr .Assignee == nil || pr .Assignee .Login == nil || * pr .Assignee .Login != input .PRAssignee {
124- err = r .Run (func () error {
125- var addErr error
126- _ , _ , addErr = client .Issues .AddAssignees (ctx , input .RepoOwner , input .RepoName ,
127- * pr .Number , []string {input .PRAssignee })
128- return addErr
129- })
114+ <- githubLimiter .C
115+ _ , _ , err := client .Issues .AddAssignees (ctx , input .RepoOwner , input .RepoName , * pr .Number , []string {input .PRAssignee })
130116 if err != nil {
131117 return Output {Success : false }, err
132118 }
133119 }
134120
135- var cs * github.CombinedStatus
136- err = r .Run (func () error {
137- var getErr error
138- cs , _ , getErr = client .Repositories .GetCombinedStatus (ctx , input .RepoOwner , input .RepoName , * pr .Head .SHA , nil )
139- return getErr
140- })
121+ <- githubLimiter .C
122+ cs , _ , err := client .Repositories .GetCombinedStatus (ctx , input .RepoOwner , input .RepoName , * pr .Head .SHA , nil )
141123 if err != nil {
142124 return Output {Success : false }, err
143125 }
@@ -171,10 +153,12 @@ func Push(ctx context.Context, input Input) (Output, error) {
171153 }, nil
172154}
173155
174- func findOrCreatePR (ctx context.Context , client * github.Client , owner string , name string , pull * github.NewPullRequest ) (* github.PullRequest , error ) {
156+ func findOrCreatePR (ctx context.Context , client * github.Client , owner string , name string , pull * github.NewPullRequest , githubLimiter * time. Ticker ) (* github.PullRequest , error ) {
175157 var pr * github.PullRequest
158+ <- githubLimiter .C
176159 newPR , _ , err := client .PullRequests .Create (ctx , owner , name , pull )
177160 if err != nil && strings .Contains (err .Error (), "pull request already exists" ) {
161+ <- githubLimiter .C
178162 existingPRs , _ , err := client .PullRequests .List (ctx , owner , name , & github.PullRequestListOptions {
179163 Head : * pull .Head ,
180164 Base : * pull .Base ,
0 commit comments