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

as title

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/498
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
6543 2021-03-09 05:15:42 +08:00 committed by Andrew Thornton
parent ff82113459
commit e0cf20a456
3 changed files with 35 additions and 3 deletions

View file

@ -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 {
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
}

View file

@ -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))

View file

@ -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()))
}