Skip to content

Commit a468dd6

Browse files
Fabian Hollerfho
Fabian Holler
authored andcommitted
autoupdater: fix: updating branch failed because wrong ExpectedHeadSHA
Sometimes updating a PR with the base branch failed with the error message: "evaluating if PR is uptodate with base branch failed: context deadline exceeded" where the individual update operation tries failed with - "retryable error: PUT https://api.github.com/repos/[..]/update-branch: 422 expected head sha didn’t match current head ref." When a PR branch was not up to date with the base branch, and the GitHub Pull Request API endpoint did not return "behind", BranchIsBehindBase() was called. The function queries the GitHub Compare Commit endpoint to figure out if the branch needs to be updated. The returned HEAD commit of BranchIsBehindBase() was then used as ExpectedHeadSHA parameter for the UpdateBranch operation. The HEAD SHA returned by BranchIsBehindBase() was the SHA of the first commit in the branch instead of HEAD. This caused that UpdateBranch() was retried with a wrong ExpectedHeadSHA and failed until the retry timeout was exceeded. Do not return from BranchIsBehindBase() the branch HEAD SHA anymore, we would have to handle pagination to get the HEAD SHA. Use the HEAD SHA that was retrieved by the GitHub Pull-Request endpoint instead in PRIsUptodate().
1 parent b54ba85 commit a468dd6

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

internal/githubclt/client.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,13 @@ type Client struct {
6262

6363
// BranchIsBehindBase returns true if branch is based on the most recent commit of baseBranch.
6464
// If it is based on older commit, false is returned.
65-
func (clt *Client) BranchIsBehindBase(ctx context.Context, owner, repo, baseBranch, branch string) (behind bool, branchHEADSHA string, err error) {
65+
func (clt *Client) BranchIsBehindBase(ctx context.Context, owner, repo, baseBranch, branch string) (behind bool, err error) {
6666
cmp, _, err := clt.restClt.Repositories.CompareCommits(ctx, owner, repo, baseBranch, branch, &github.ListOptions{PerPage: 1})
6767
if err != nil {
68-
return false, "", clt.wrapRetryableErrors(err)
69-
}
70-
71-
if len(cmp.Commits) == 0 {
72-
return false, "", errors.New("compare response contains 0 commits")
73-
}
74-
75-
branchHEADSHA = cmp.Commits[len(cmp.Commits)-1].GetSHA()
76-
if branchHEADSHA == "" {
77-
return false, "", errors.New("sha field of last commit is empty")
68+
return false, clt.wrapRetryableErrors(err)
7869
}
7970

80-
return cmp.GetBehindBy() > 0, branchHEADSHA, nil
71+
return cmp.GetBehindBy() > 0, nil
8172
}
8273

8374
const (
@@ -155,12 +146,12 @@ func (clt *Client) PRIsUptodate(ctx context.Context, owner, repo string, pullReq
155146
return false, "", errors.New("got pull request object with empty base ref field")
156147
}
157148

158-
isBehind, branchHEADSHA, err := clt.BranchIsBehindBase(ctx, owner, repo, baseBranch, prBranch)
149+
isBehind, err := clt.BranchIsBehindBase(ctx, owner, repo, baseBranch, prBranch)
159150
if err != nil {
160-
return false, branchHEADSHA, fmt.Errorf("evaluating if branch is behind base failed: %w", err)
151+
return false, "", fmt.Errorf("evaluating if branch is behind base failed: %w", err)
161152
}
162153

163-
return !isBehind, branchHEADSHA, nil
154+
return !isBehind, prHeadSHA, nil
164155
}
165156

166157
// CreateIssueComment creates a comment in a issue or pull request

0 commit comments

Comments
 (0)