From 2700d8b9170478141645085f4a1f4eda6a75b67b Mon Sep 17 00:00:00 2001 From: cw-ozaki Date: Thu, 8 Apr 2021 00:24:45 +0900 Subject: [PATCH] fixes 405 error when merging multiple PRs --- pkg/api/merge.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/pkg/api/merge.go b/pkg/api/merge.go index 82ed824..b35114e 100644 --- a/pkg/api/merge.go +++ b/pkg/api/merge.go @@ -3,6 +3,7 @@ package api import ( "bytes" "context" + "net/http" "text/template" "time" @@ -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 @@ -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))