Add workaround to get head branch sha of pulls with deleted head branch #498

Merged
zeripath merged 6 commits from 6543/go-sdk:fix-empty-pull-head-sha into master 2021-03-08 21:15:43 +00:00
3 changed files with 35 additions and 3 deletions

@ -105,6 +105,13 @@ func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOp
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo))
link.RawQuery = opt.QueryEncode()
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs)
if c.checkServerVersionGreaterThanOrEqual(version1_14_0) != nil {
for i := range prs {
if err := fixPullHeadSha(c, prs[i]); err != nil {
return prs, resp, err
}
}
}
return prs, resp, err
}
@ -112,6 +119,11 @@ func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOp
func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, *Response, error) {
pr := new(PullRequest)
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
if c.checkServerVersionGreaterThanOrEqual(version1_14_0) != nil {
6543 marked this conversation as resolved Outdated
Outdated
Review

Move the version check into fixPullHeadSha, so it's selfdocumenting?

Move the version check into fixPullHeadSha, so it's selfdocumenting?
Outdated
Review

Scratch that, I see why you did that now.
Maybe add a comment to fixPullHeadSha instead about the affected gitea versions?

Scratch that, I see why you did that now. Maybe add a comment to `fixPullHeadSha` instead about the affected gitea versions?
Outdated
Review

@noerw done

@noerw done
if err := fixPullHeadSha(c, pr); err != nil {
return pr, resp, err
}
}
return pr, resp, err
}
@ -251,3 +263,23 @@ func (c *Client) GetPullRequestPatch(owner, repo string, index int64) ([]byte, *
func (c *Client) GetPullRequestDiff(owner, repo string, index int64) ([]byte, *Response, error) {
return c.getPullRequestDiffOrPatch(owner, repo, "diff", index)
}
// fixPullHeadSha is a workaround for https://github.com/go-gitea/gitea/issues/12675
// When no head sha is available, this is because the branch got deleted in the base repo.
// pr.Head.Ref points in this case not to the head repo branch name, but the base repo ref,
// which stays available to resolve the commit sha. This is fixed for gitea >= 1.14.0
func fixPullHeadSha(client *Client, pr *PullRequest) error {
if pr.Base != nil && pr.Base.Repository != nil && pr.Base.Repository.Owner != nil &&
pr.Head != nil && pr.Head.Ref != "" && pr.Head.Sha == "" {
owner := pr.Base.Repository.Owner.UserName
repo := pr.Base.Repository.Name
refs, _, err := client.GetRepoRefs(owner, repo, pr.Head.Ref)
if err != nil {
return err
} else if len(refs) == 0 {
return fmt.Errorf("unable to resolve PR ref '%s'", pr.Head.Ref)
}
pr.Head.Sha = refs[0].Object.SHA
}
return nil
}

@ -58,7 +58,7 @@ func TestPull(t *testing.T) {
diff, _, err := c.GetPullRequestDiff(c.username, repoName, pullUpdateFile.Index)
assert.NoError(t, err)
assert.Len(t, diff, 1281)
assert.True(t, len(diff) > 1100 && len(diff) < 1300)
patch, _, err := c.GetPullRequestPatch(c.username, repoName, pullUpdateFile.Index)
assert.NoError(t, err)
assert.True(t, len(patch) > len(diff))

@ -138,7 +138,7 @@ func TestGetArchive(t *testing.T) {
time.Sleep(time.Second / 2)
archive, _, err := c.GetArchive(repo.Owner.UserName, repo.Name, "master", ZipArchive)
assert.NoError(t, err)
assert.EqualValues(t, 1602, len(archive))
assert.True(t, len(archive) > 1500 && len(archive) < 1700)
}
func TestGetArchiveReader(t *testing.T) {
@ -153,7 +153,7 @@ func TestGetArchiveReader(t *testing.T) {
archive := bytes.NewBuffer(nil)
nBytes, err := io.Copy(archive, r)
assert.NoError(t, err)
assert.EqualValues(t, 1602, nBytes)
assert.True(t, nBytes > 1500)
assert.EqualValues(t, nBytes, len(archive.Bytes()))
}