Add ListPullRequestFiles (#607)

Was added in 1dfa28ffa5.

Co-authored-by: qwerty287 <ndev@web.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/607
Reviewed-by: 6543 <6543@obermui.de>
Reviewed-by: Gusted <williamzijl7@hotmail.com>
Co-authored-by: qwerty287 <qwerty287@noreply.gitea.io>
Co-committed-by: qwerty287 <qwerty287@noreply.gitea.io>
This commit is contained in:
qwerty287 2022-10-17 02:33:29 +08:00 committed by 6543
parent 8f846bdb9b
commit 1c9c71d84a
2 changed files with 41 additions and 0 deletions

View file

@ -60,6 +60,19 @@ type PullRequest struct {
Closed *time.Time `json:"closed_at"` 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 // ListPullRequestsOptions options for listing pull requests
type ListPullRequestsOptions struct { type ListPullRequestsOptions struct {
ListOptions ListOptions
@ -348,3 +361,21 @@ func fixPullHeadSha(client *Client, pr *PullRequest) error {
} }
return nil 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
}

View file

@ -71,6 +71,16 @@ func TestPull(t *testing.T) {
assert.EqualValues(t, "LICENSE", commits[0].Files[0].Filename) 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 // 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)