Use RepositoryMeta struct on Issues (#267)

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/267
Reviewed-by: lafriks <lafriks@noreply.gitea.io>
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
6543 2020-02-04 15:15:35 +00:00 committed by techknowlogick
parent 3c04db72f4
commit f84cd3442d
2 changed files with 34 additions and 5 deletions

View file

@ -10,6 +10,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
)
@ -19,6 +20,14 @@ type PullRequestMeta struct {
Merged *time.Time `json:"merged_at"`
}
// RepositoryMeta basic repository information
type RepositoryMeta struct {
ID int64 `json:"id"`
Name string `json:"name"`
Owner string `json:"owner"`
FullName string `json:"full_name"`
}
// Issue represents an issue in a repository
type Issue struct {
ID int64 `json:"id"`
@ -41,7 +50,7 @@ type Issue struct {
Closed *time.Time `json:"closed_at"`
Deadline *time.Time `json:"due_date"`
PullRequest *PullRequestMeta `json:"pull_request"`
Repository *Repository `json:"repository"`
Repository *RepositoryMeta `json:"repository"`
}
// ListIssueOption list issue options
@ -95,7 +104,15 @@ func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) {
link, _ := url.Parse("/repos/issues/search")
issues := make([]*Issue, 0, 10)
link.RawQuery = opt.QueryEncode()
return issues, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
for i := 0; i < len(issues); i++ {
if issues[i].Repository != nil {
issues[i].Repository.Owner = strings.Split(issues[i].Repository.FullName, "/")[0]
}
}
}
return issues, err
}
// ListRepoIssues returns all issues for a given repository
@ -103,13 +120,25 @@ func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Iss
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo))
link.RawQuery = opt.QueryEncode()
issues := make([]*Issue, 0, 10)
return issues, c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil {
for i := 0; i < len(issues); i++ {
if issues[i].Repository != nil {
issues[i].Repository.Owner = strings.Split(issues[i].Repository.FullName, "/")[0]
}
}
}
return issues, err
}
// GetIssue returns a single issue for a given repository
func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, error) {
issue := new(Issue)
return issue, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
if e := c.CheckServerVersionConstraint(">=1.12.0"); e != nil && issue.Repository != nil {
issue.Repository.Owner = strings.Split(issue.Repository.FullName, "/")[0]
}
return issue, err
}
// CreateIssueOption options to create one issue

View file

@ -89,7 +89,7 @@ func TestNotifications(t *testing.T) {
assert.Len(t, nList, 0)
// ReadThread
iState := "closed"
iState := StateClosed
c.sudo = ""
_, err = c.EditIssue(repoB.Owner.UserName, repoB.Name, issue.Index, EditIssueOption{State: &iState})
assert.NoError(t, err)