Skip to content

Commit

Permalink
fixes 405 error when merging multiple PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
k-kinzal committed Apr 7, 2021
1 parent 1e73666 commit 2700d8b
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions pkg/api/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"bytes"
"context"
"net/http"
"text/template"
"time"

Expand All @@ -13,6 +14,9 @@ import (
"github.com/google/go-github/v28/github"
)

const mergeRetryCount = 6
const mergeRetryIntervalSec = 10

type MergeOption struct {
CommitTitleTemplate string
CommitMessageTemplate string
Expand Down Expand Up @@ -47,21 +51,26 @@ func (c *Client) Merge(ctx context.Context, pulls []*PullRequest, opt *MergeOpti
CommitTitle: commitTitle,
MergeMethod: opt.MergeMethod,
}
result, _, err := c.github.PullRequests.Merge(ctx, pull.Owner, pull.Repo, int(pull.Number), commitMessage, o)
if err != nil {
return err
}
if !result.GetMerged() {
return xerrors.New(result.GetMessage())
for i := 0; i < mergeRetryCount; i++ {
result, res, err := c.github.PullRequests.Merge(ctx, pull.Owner, pull.Repo, int(pull.Number), commitMessage, o)
if err != nil {
return err
}
if result.GetMerged() {
pull.State = state
pull.UpdatedAt = now
pull.ClosedAt = now
pull.MergedAt = now
pull.MergeCommitSha = *result.SHA
break
}
if res.StatusCode != http.StatusMethodNotAllowed || i+1 == mergeRetryCount {
return xerrors.New(result.GetMessage())
}

time.Sleep(time.Second * mergeRetryIntervalSec)
}

pull.State = state
pull.UpdatedAt = now
pull.ClosedAt = now
pull.MergedAt = now
pull.MergeCommitSha = *result.SHA

return nil
}
}(pull))
Expand Down

0 comments on commit 2700d8b

Please sign in to comment.