Skip to content

Commit 649202e

Browse files
authored
Merge pull request #333 from babarot/babarot/retry-latest-pull-request
Add retry to latestPullRequest for commit-to-PR index race condition
2 parents f03cd28 + 655f406 commit 649202e

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

tag.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package tagpr
22

33
import (
44
"context"
5+
"log"
56
"strings"
7+
"time"
68

79
"github.com/google/go-github/v83/github"
810
)
@@ -13,16 +15,32 @@ func (tp *tagpr) latestPullRequest(ctx context.Context) (*github.PullRequest, er
1315
if err != nil {
1416
return nil, err
1517
}
16-
pulls, resp, err := tp.gh.PullRequests.ListPullRequestsWithCommit(
17-
ctx, tp.owner, tp.repo, commitish, nil)
18-
if err != nil {
19-
showGHError(err, resp)
20-
return nil, err
21-
}
22-
if len(pulls) == 0 {
23-
return nil, nil
18+
19+
// Retry because GitHub's internal commit-to-PR index may not be updated
20+
// immediately after a merge, causing the API to return an empty list.
21+
// This is especially common with squash merges but can also happen with
22+
// regular merge commits when the workflow triggers within seconds.
23+
// See https://github.com/Songmu/tagpr/issues/330
24+
const maxRetries = 3
25+
const retryInterval = 2 * time.Second
26+
27+
for i := range maxRetries {
28+
pulls, resp, err := tp.gh.PullRequests.ListPullRequestsWithCommit(
29+
ctx, tp.owner, tp.repo, commitish, nil)
30+
if err != nil {
31+
showGHError(err, resp)
32+
return nil, err
33+
}
34+
if len(pulls) > 0 {
35+
return pulls[0], nil
36+
}
37+
if i < maxRetries-1 {
38+
log.Printf("ListPullRequestsWithCommit returned empty for %s, retrying in %s (%d/%d)",
39+
commitish, retryInterval, i+1, maxRetries)
40+
time.Sleep(retryInterval)
41+
}
2442
}
25-
return pulls[0], nil
43+
return nil, nil
2644
}
2745

2846
func (tp *tagpr) tagRelease(ctx context.Context, pr *github.PullRequest, currVer *semv, latestSemverTag string) error {

0 commit comments

Comments
 (0)