Pull : Add a Fallback for GetPullRequestDiff/Patch (#399)
add VersionCheck and fallback for old instance Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/399 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
6594bf0b20
commit
18f39029e9
3 changed files with 38 additions and 2 deletions
|
@ -74,6 +74,20 @@ func (c *Client) SetSudo(sudo string) {
|
||||||
c.sudo = sudo
|
c.sudo = sudo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) getWebResponse(method, path string, body io.Reader) ([]byte, error) {
|
||||||
|
req, err := http.NewRequest(method, c.url+path, body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resp, err := c.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
return ioutil.ReadAll(resp.Body)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*http.Response, error) {
|
func (c *Client) doRequest(method, path string, header http.Header, body io.Reader) (*http.Response, error) {
|
||||||
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
|
req, err := http.NewRequest(method, c.url+"/api/v1"+path, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -221,12 +221,27 @@ func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, err
|
||||||
return statusCode == 204, nil
|
return statusCode == 204, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getPullRequestDiffOrPatch gets the patch or diff file as bytes for a PR
|
||||||
|
func (c *Client) getPullRequestDiffOrPatch(owner, repo, kind string, index int64) ([]byte, error) {
|
||||||
|
if err := c.CheckServerVersionConstraint(">=1.13.0"); err != nil {
|
||||||
|
r, err2 := c.GetRepo(owner, repo)
|
||||||
|
if err2 != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if r.Private {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c.getWebResponse("GET", fmt.Sprintf("/%s/%s/pulls/%d.%s", owner, repo, index, kind), nil)
|
||||||
|
}
|
||||||
|
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d.%s", owner, repo, index, kind), nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
// GetPullRequestPatch gets the .patch file as bytes for a PR
|
// GetPullRequestPatch gets the .patch file as bytes for a PR
|
||||||
func (c *Client) GetPullRequestPatch(owner, repo string, index int64) ([]byte, error) {
|
func (c *Client) GetPullRequestPatch(owner, repo string, index int64) ([]byte, error) {
|
||||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d.patch", owner, repo, index), nil, nil)
|
return c.getPullRequestDiffOrPatch(owner, repo, "patch", index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPullRequestDiff gets the .diff file as bytes for a PR
|
// GetPullRequestDiff gets the .diff file as bytes for a PR
|
||||||
func (c *Client) GetPullRequestDiff(owner, repo string, index int64) ([]byte, error) {
|
func (c *Client) GetPullRequestDiff(owner, repo string, index int64) ([]byte, error) {
|
||||||
return c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls/%d.diff", owner, repo, index), nil, nil)
|
return c.getPullRequestDiffOrPatch(owner, repo, "diff", index)
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,13 @@ func TestPull(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, pulls, 3)
|
assert.Len(t, pulls, 3)
|
||||||
|
|
||||||
|
diff, err := c.GetPullRequestDiff(c.username, repoName, pullUpdateFile.Index)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Len(t, diff, 1310)
|
||||||
|
patch, err := c.GetPullRequestPatch(c.username, repoName, pullUpdateFile.Index)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.True(t, len(patch) > len(diff))
|
||||||
|
|
||||||
// test Update pull
|
// test Update pull
|
||||||
pr, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
|
pr, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
Loading…
Reference in a new issue