Skip to content

Commit

Permalink
Merge pull request #19 from k-kinzal/fixes-merge-not-allow
Browse files Browse the repository at this point in the history
fixes 405 error when merging multiple PRs
  • Loading branch information
k-kinzal authored Apr 7, 2021
2 parents 1e73666 + 2700d8b commit 3da0e2f
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 3da0e2f

Please sign in to comment.