From 3ff2c60a86962c28ff33a8072735c9e1d29057c9 Mon Sep 17 00:00:00 2001 From: Norwin Date: Fri, 14 Jan 2022 01:30:39 +0800 Subject: [PATCH] GetPullRequestDiff: add PullRequestDiffOptions param (#542) this is for the upstream change in https://github.com/go-gitea/gitea/pull/17158 Co-authored-by: Norwin Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/542 Reviewed-by: Lunny Xiao Reviewed-by: 6543 <6543@obermui.de> Co-authored-by: Norwin Co-committed-by: Norwin --- docs/migrate-v0.14-to-v0.15.md | 4 ++-- docs/migrate-v0.15-to-v0.16.md | 21 +++++++++++++++++++ gitea/pull.go | 37 ++++++++++++++++++++++++++-------- gitea/pull_test.go | 4 +++- 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 docs/migrate-v0.15-to-v0.16.md diff --git a/docs/migrate-v0.14-to-v0.15.md b/docs/migrate-v0.14-to-v0.15.md index 3c59e04..953e987 100644 --- a/docs/migrate-v0.14-to-v0.15.md +++ b/docs/migrate-v0.14-to-v0.15.md @@ -1,7 +1,7 @@ # Migration Guide: v0.14 to v0.15 -v0.15.0 introduces a number of api changes, through which it should not be difficult to migrate. -Just follow this guid and if you still encounter problems, ask for help on discord or feel free to create an issue. +v0.15.0 introduces a number of API changes, which should be simple to migrate. +Just follow this guide and if you still encounter problems, ask for help on Discord or feel free to create an issue. diff --git a/docs/migrate-v0.15-to-v0.16.md b/docs/migrate-v0.15-to-v0.16.md new file mode 100644 index 0000000..7b4648b --- /dev/null +++ b/docs/migrate-v0.15-to-v0.16.md @@ -0,0 +1,21 @@ +# Migration Guide: v0.15 to v0.16 + +v0.16.0 introduces a number of API changes, which should be simple to migrate. +Just follow this guide and if you still encounter problems, ask for help on Discord or feel free to create an issue. + + + +- [Upstream API changes](#upstream-api-changes) +- [GetPullRequestDiff: add PullRequestDiffOption parameter (#542)](#getpullrequestdiff) + + + +## Upstream API changes + +As we aim to track API changes in Gitea 1.16 with this SDK release, you may find this [summary listing of changes](https://gitea.com/gitea/go-sdk/issues/558) helpful. + +## GetPullRequestDiff + Added new parameter `opts PullRequestDiffOption`. Gitea 1.16 will default to omit binary file changes in diffs; if you still need that information, set `opts.Binary = true`. + Related PRs: + - [go-sdk#542](https://gitea.com/gitea/go-sdk/pulls/542) + - [gitea#17158](https://github.com/go-gitea/gitea/pull/17158) diff --git a/gitea/pull.go b/gitea/pull.go index 6afebc0..2f41465 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -264,9 +264,29 @@ func (c *Client) IsPullRequestMerged(owner, repo string, index int64) (bool, *Re return status == 204, resp, nil } +// PullRequestDiffOptions options for GET /repos///pulls/.[diff|patch] +type PullRequestDiffOptions struct { + // Include binary file changes when requesting a .diff + Binary bool +} + +// QueryEncode converts the options to a query string +func (o PullRequestDiffOptions) QueryEncode() string { + query := make(url.Values) + query.Add("binary", fmt.Sprintf("%v", o.Binary)) + return query.Encode() +} + +type pullRequestDiffType string + +const ( + pullRequestDiffTypeDiff pullRequestDiffType = "diff" + pullRequestDiffTypePatch pullRequestDiffType = "patch" +) + // getPullRequestDiffOrPatch gets the patch or diff file as bytes for a PR -func (c *Client) getPullRequestDiffOrPatch(owner, repo, kind string, index int64) ([]byte, *Response, error) { - if err := escapeValidatePathSegments(&owner, &repo, &kind); err != nil { +func (c *Client) getPullRequestDiffOrPatch(owner, repo string, kind pullRequestDiffType, index int64, opts PullRequestDiffOptions) ([]byte, *Response, error) { + if err := escapeValidatePathSegments(&owner, &repo); err != nil { return nil, nil, err } if err := c.checkServerVersionGreaterThanOrEqual(version1_13_0); err != nil { @@ -277,19 +297,20 @@ func (c *Client) getPullRequestDiffOrPatch(owner, repo, kind string, index int64 if r.Private { return nil, nil, err } - return c.getWebResponse("GET", fmt.Sprintf("/%s/%s/pulls/%d.%s", owner, repo, index, kind), nil) + url := fmt.Sprintf("/%s/%s/pulls/%d.%s?%s", owner, repo, index, kind, opts.QueryEncode()) + return c.getWebResponse("GET", url, 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 git patchset of a PR func (c *Client) GetPullRequestPatch(owner, repo string, index int64) ([]byte, *Response, error) { - return c.getPullRequestDiffOrPatch(owner, repo, "patch", index) + return c.getPullRequestDiffOrPatch(owner, repo, pullRequestDiffTypePatch, index, PullRequestDiffOptions{}) } -// GetPullRequestDiff gets the .diff file as bytes for a PR -func (c *Client) GetPullRequestDiff(owner, repo string, index int64) ([]byte, *Response, error) { - return c.getPullRequestDiffOrPatch(owner, repo, "diff", index) +// GetPullRequestDiff gets the diff of a PR. For Gitea >= 1.16, you must set includeBinary to get an applicable diff +func (c *Client) GetPullRequestDiff(owner, repo string, index int64, opts PullRequestDiffOptions) ([]byte, *Response, error) { + return c.getPullRequestDiffOrPatch(owner, repo, pullRequestDiffTypeDiff, index, opts) } // ListPullRequestCommitsOptions options for listing pull requests diff --git a/gitea/pull_test.go b/gitea/pull_test.go index 7b24b29..5b9dcb5 100644 --- a/gitea/pull_test.go +++ b/gitea/pull_test.go @@ -56,7 +56,9 @@ func TestPull(t *testing.T) { assert.NoError(t, err) assert.Len(t, pulls, 3) - diff, _, err := c.GetPullRequestDiff(c.username, repoName, pullUpdateFile.Index) + diff, _, err := c.GetPullRequestDiff(c.username, repoName, pullUpdateFile.Index, PullRequestDiffOptions{ + Binary: true, + }) assert.NoError(t, err) assert.True(t, len(diff) > 1100 && len(diff) < 1300) patch, _, err := c.GetPullRequestPatch(c.username, repoName, pullUpdateFile.Index)