From 230fd25196865f02fe17a0894f5ea969db8a3cf6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 3 Jul 2021 20:29:50 +0800 Subject: [PATCH] Add ListPullRequestCommits (#530) ref: https://github.com/go-gitea/gitea/pull/16300 Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/530 Reviewed-by: Lunny Xiao Reviewed-by: Norwin Co-authored-by: 6543 <6543@obermui.de> Co-committed-by: 6543 <6543@obermui.de> --- gitea/pull.go | 18 ++++++++++++++++++ gitea/pull_test.go | 6 ++++++ gitea/version.go | 1 + 3 files changed, 25 insertions(+) diff --git a/gitea/pull.go b/gitea/pull.go index 7c946e8..9528762 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -285,6 +285,24 @@ func (c *Client) GetPullRequestDiff(owner, repo string, index int64) ([]byte, *R return c.getPullRequestDiffOrPatch(owner, repo, "diff", index) } +// ListPullRequestCommitsOptions options for listing pull requests +type ListPullRequestCommitsOptions struct { + ListOptions +} + +// ListPullRequestCommits list commits for a pull request +func (c *Client) ListPullRequestCommits(owner, repo string, index int64, opt ListPullRequestCommitsOptions) ([]*Commit, *Response, error) { + if err := escapeValidatePathSegments(&owner, &repo); err != nil { + return nil, nil, err + } + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls/%d/commits", owner, repo, index)) + opt.setDefaults() + commits := make([]*Commit, 0, opt.PageSize) + link.RawQuery = opt.getURLQuery().Encode() + resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &commits) + return commits, resp, err +} + // 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, diff --git a/gitea/pull_test.go b/gitea/pull_test.go index 83e325a..7b24b29 100644 --- a/gitea/pull_test.go +++ b/gitea/pull_test.go @@ -63,6 +63,12 @@ func TestPull(t *testing.T) { assert.NoError(t, err) assert.True(t, len(patch) > len(diff)) + commits, _, err := c.ListPullRequestCommits(c.username, repoName, pullUpdateFile.Index, ListPullRequestCommitsOptions{}) + assert.NoError(t, err) + if assert.Len(t, commits, 1) && assert.Len(t, commits[0].Files, 1) { + assert.EqualValues(t, "LICENSE", commits[0].Files[0].Filename) + } + // test Update pull pr, _, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index) assert.NoError(t, err) diff --git a/gitea/version.go b/gitea/version.go index ed8a2ae..449ad97 100644 --- a/gitea/version.go +++ b/gitea/version.go @@ -45,6 +45,7 @@ var ( version1_12_0, _ = version.NewVersion("1.12.0") version1_13_0, _ = version.NewVersion("1.13.0") version1_14_0, _ = version.NewVersion("1.14.0") + version1_15_0, _ = version.NewVersion("1.15.0") ) // checkServerVersionGreaterThanOrEqual is internally used to speed up things and ignore issues with prerelease