Add workaround to get head branch sha of pulls with deleted head branch #498
|
@ -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, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls", owner, repo))
|
||||||
link.RawQuery = opt.QueryEncode()
|
link.RawQuery = opt.QueryEncode()
|
||||||
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &prs)
|
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
|
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) {
|
func (c *Client) GetPullRequest(owner, repo string, index int64) (*PullRequest, *Response, error) {
|
||||||
pr := new(PullRequest)
|
pr := new(PullRequest)
|
||||||
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, index), nil, nil, pr)
|
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
|
|||||||
|
if err := fixPullHeadSha(c, pr); err != nil {
|
||||||
|
return pr, resp, err
|
||||||
|
}
|
||||||
|
}
|
||||||
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) {
|
func (c *Client) GetPullRequestDiff(owner, repo string, index int64) ([]byte, *Response, error) {
|
||||||
return c.getPullRequestDiffOrPatch(owner, repo, "diff", index)
|
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)
|
diff, _, err := c.GetPullRequestDiff(c.username, repoName, pullUpdateFile.Index)
|
||||||
assert.NoError(t, err)
|
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)
|
patch, _, err := c.GetPullRequestPatch(c.username, repoName, pullUpdateFile.Index)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.True(t, len(patch) > len(diff))
|
assert.True(t, len(patch) > len(diff))
|
||||||
|
|
|
@ -138,7 +138,7 @@ func TestGetArchive(t *testing.T) {
|
||||||
time.Sleep(time.Second / 2)
|
time.Sleep(time.Second / 2)
|
||||||
archive, _, err := c.GetArchive(repo.Owner.UserName, repo.Name, "master", ZipArchive)
|
archive, _, err := c.GetArchive(repo.Owner.UserName, repo.Name, "master", ZipArchive)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 1602, len(archive))
|
assert.True(t, len(archive) > 1500 && len(archive) < 1700)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetArchiveReader(t *testing.T) {
|
func TestGetArchiveReader(t *testing.T) {
|
||||||
|
@ -153,7 +153,7 @@ func TestGetArchiveReader(t *testing.T) {
|
||||||
archive := bytes.NewBuffer(nil)
|
archive := bytes.NewBuffer(nil)
|
||||||
nBytes, err := io.Copy(archive, r)
|
nBytes, err := io.Copy(archive, r)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.EqualValues(t, 1602, nBytes)
|
assert.True(t, nBytes > 1500)
|
||||||
assert.EqualValues(t, nBytes, len(archive.Bytes()))
|
assert.EqualValues(t, nBytes, len(archive.Bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user
Move the version check into fixPullHeadSha, so it's selfdocumenting?
Scratch that, I see why you did that now.
Maybe add a comment to
fixPullHeadSha
instead about the affected gitea versions?@noerw done