From 1c9c71d84ae520d9056c5309193d2122026844b3 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 17 Oct 2022 02:33:29 +0800 Subject: [PATCH] Add `ListPullRequestFiles` (#607) Was added in https://github.com/go-gitea/gitea/commit/1dfa28ffa557720d164a351783be64c9a47a0adb. Co-authored-by: qwerty287 Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/607 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Gusted Co-authored-by: qwerty287 Co-committed-by: qwerty287 --- gitea/pull.go | 31 +++++++++++++++++++++++++++++++ gitea/pull_test.go | 10 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/gitea/pull.go b/gitea/pull.go index 0a900f7..22b03cf 100644 --- a/gitea/pull.go +++ b/gitea/pull.go @@ -60,6 +60,19 @@ type PullRequest struct { Closed *time.Time `json:"closed_at"` } +// ChangedFile is a changed file in a diff +type ChangedFile struct { + Filename string `json:"filename"` + PreviousFilename string `json:"previous_filename"` + Status string `json:"status"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + Changes int `json:"changes"` + HTMLURL string `json:"html_url"` + ContentsURL string `json:"contents_url"` + RawURL string `json:"raw_url"` +} + // ListPullRequestsOptions options for listing pull requests type ListPullRequestsOptions struct { ListOptions @@ -348,3 +361,21 @@ func fixPullHeadSha(client *Client, pr *PullRequest) error { } return nil } + +// ListPullRequestFilesOptions options for listing pull request files +type ListPullRequestFilesOptions struct { + ListOptions +} + +// ListPullRequestFiles list changed files for a pull request +func (c *Client) ListPullRequestFiles(owner, repo string, index int64, opt ListPullRequestFilesOptions) ([]*ChangedFile, *Response, error) { + if err := escapeValidatePathSegments(&owner, &repo); err != nil { + return nil, nil, err + } + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/pulls/%d/files", owner, repo, index)) + opt.setDefaults() + files := make([]*ChangedFile, 0, opt.PageSize) + link.RawQuery = opt.getURLQuery().Encode() + resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &files) + return files, resp, err +} diff --git a/gitea/pull_test.go b/gitea/pull_test.go index f3b31b9..3bdcd92 100644 --- a/gitea/pull_test.go +++ b/gitea/pull_test.go @@ -71,6 +71,16 @@ func TestPull(t *testing.T) { assert.EqualValues(t, "LICENSE", commits[0].Files[0].Filename) } + files, _, err := c.ListPullRequestFiles(c.username, repoName, pullUpdateFile.Index, ListPullRequestFilesOptions{}) + assert.NoError(t, err) + assert.Len(t, files, 1) + file := files[0] + assert.EqualValues(t, "LICENSE", file.Filename) + assert.EqualValues(t, "changed", file.Status) + assert.EqualValues(t, 3, file.Additions) + assert.EqualValues(t, 9, file.Deletions) + assert.EqualValues(t, 12, file.Changes) + // test Update pull pr, _, err := c.GetPullRequest(user.UserName, repoName, pullUpdateFile.Index) assert.NoError(t, err)