2019-04-25 13:06:53 -04:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2019-10-13 11:13:33 -04:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2019-04-25 13:06:53 -04:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-02-07 08:53:15 -05:00
|
|
|
"net/url"
|
2020-05-27 09:48:09 -04:00
|
|
|
"strings"
|
2019-04-25 13:06:53 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PullRequestMeta PR info if an issue is a PR
|
|
|
|
type PullRequestMeta struct {
|
|
|
|
HasMerged bool `json:"merged"`
|
|
|
|
Merged *time.Time `json:"merged_at"`
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// RepositoryMeta basic repository information
|
|
|
|
type RepositoryMeta struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Owner string `json:"owner"`
|
|
|
|
FullName string `json:"full_name"`
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// Issue represents an issue in a repository
|
|
|
|
type Issue struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
URL string `json:"url"`
|
2020-12-01 00:07:23 -05:00
|
|
|
HTMLURL string `json:"html_url"`
|
2019-10-13 11:13:33 -04:00
|
|
|
Index int64 `json:"number"`
|
|
|
|
Poster *User `json:"user"`
|
|
|
|
OriginalAuthor string `json:"original_author"`
|
|
|
|
OriginalAuthorID int64 `json:"original_author_id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
Labels []*Label `json:"labels"`
|
|
|
|
Milestone *Milestone `json:"milestone"`
|
2020-12-01 00:07:23 -05:00
|
|
|
// deprecated
|
|
|
|
// TODO: rm on sdk 0.15.0
|
|
|
|
Assignee *User `json:"assignee"`
|
|
|
|
Assignees []*User `json:"assignees"`
|
2019-04-25 13:06:53 -04:00
|
|
|
// Whether the issue is open or closed
|
2019-10-13 11:13:33 -04:00
|
|
|
State StateType `json:"state"`
|
2020-09-15 22:01:41 -04:00
|
|
|
IsLocked bool `json:"is_locked"`
|
2019-10-13 11:13:33 -04:00
|
|
|
Comments int `json:"comments"`
|
|
|
|
Created time.Time `json:"created_at"`
|
|
|
|
Updated time.Time `json:"updated_at"`
|
|
|
|
Closed *time.Time `json:"closed_at"`
|
|
|
|
Deadline *time.Time `json:"due_date"`
|
2019-04-25 13:06:53 -04:00
|
|
|
PullRequest *PullRequestMeta `json:"pull_request"`
|
2020-05-27 09:48:09 -04:00
|
|
|
Repository *RepositoryMeta `json:"repository"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListIssueOption list issue options
|
|
|
|
type ListIssueOption struct {
|
2020-05-27 09:48:09 -04:00
|
|
|
ListOptions
|
|
|
|
State StateType
|
|
|
|
Type IssueType
|
|
|
|
Labels []string
|
|
|
|
Milestones []string
|
|
|
|
KeyWord string
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
// StateType issue state type
|
|
|
|
type StateType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// StateOpen pr/issue is opend
|
|
|
|
StateOpen StateType = "open"
|
|
|
|
// StateClosed pr/issue is closed
|
|
|
|
StateClosed StateType = "closed"
|
|
|
|
// StateAll is all
|
|
|
|
StateAll StateType = "all"
|
|
|
|
)
|
|
|
|
|
2020-04-01 04:35:42 -04:00
|
|
|
// IssueType is issue a pull or only an issue
|
|
|
|
type IssueType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// IssueTypeAll pr and issue
|
|
|
|
IssueTypeAll IssueType = ""
|
|
|
|
// IssueTypeIssue only issues
|
|
|
|
IssueTypeIssue IssueType = "issues"
|
|
|
|
// IssueTypePull only pulls
|
|
|
|
IssueTypePull IssueType = "pulls"
|
|
|
|
)
|
|
|
|
|
2020-02-07 08:53:15 -05:00
|
|
|
// QueryEncode turns options into querystring argument
|
|
|
|
func (opt *ListIssueOption) QueryEncode() string {
|
2020-05-27 09:48:09 -04:00
|
|
|
query := opt.getURLQuery()
|
2020-02-07 08:53:15 -05:00
|
|
|
|
|
|
|
if len(opt.State) > 0 {
|
2020-05-27 09:48:09 -04:00
|
|
|
query.Add("state", string(opt.State))
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
2020-05-27 09:48:09 -04:00
|
|
|
|
2020-02-07 08:53:15 -05:00
|
|
|
if len(opt.Labels) > 0 {
|
2020-05-27 09:48:09 -04:00
|
|
|
query.Add("labels", strings.Join(opt.Labels, ","))
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
2020-05-27 09:48:09 -04:00
|
|
|
|
2020-02-07 08:53:15 -05:00
|
|
|
if len(opt.KeyWord) > 0 {
|
|
|
|
query.Add("q", opt.KeyWord)
|
|
|
|
}
|
2020-05-27 09:48:09 -04:00
|
|
|
|
2020-04-01 04:35:42 -04:00
|
|
|
query.Add("type", string(opt.Type))
|
2020-02-07 08:53:15 -05:00
|
|
|
|
2020-05-27 09:48:09 -04:00
|
|
|
if len(opt.Milestones) > 0 {
|
|
|
|
query.Add("milestones", strings.Join(opt.Milestones, ","))
|
|
|
|
}
|
|
|
|
|
2020-02-07 08:53:15 -05:00
|
|
|
return query.Encode()
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListIssues returns all issues assigned the authenticated user
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
issues := make([]*Issue, 0, opt.PageSize)
|
|
|
|
|
2020-02-07 08:53:15 -05:00
|
|
|
link, _ := url.Parse("/repos/issues/search")
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
|
2020-11-14 05:31:01 -05:00
|
|
|
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil {
|
2020-05-27 09:48:09 -04:00
|
|
|
for i := 0; i < len(issues); i++ {
|
|
|
|
if issues[i].Repository != nil {
|
|
|
|
issues[i].Repository.Owner = strings.Split(issues[i].Repository.FullName, "/")[0]
|
|
|
|
}
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-01 00:07:23 -05:00
|
|
|
for i := range issues {
|
|
|
|
c.issueBackwardsCompatibility(issues[i])
|
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return issues, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoIssues returns all issues for a given repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, *Response, error) {
|
2020-05-27 09:48:09 -04:00
|
|
|
opt.setDefaults()
|
|
|
|
issues := make([]*Issue, 0, opt.PageSize)
|
|
|
|
|
2020-02-07 08:53:15 -05:00
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo))
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), jsonHeader, nil, &issues)
|
2020-11-14 05:31:01 -05:00
|
|
|
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil {
|
2020-05-27 09:48:09 -04:00
|
|
|
for i := 0; i < len(issues); i++ {
|
|
|
|
if issues[i].Repository != nil {
|
|
|
|
issues[i].Repository.Owner = strings.Split(issues[i].Repository.FullName, "/")[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 00:07:23 -05:00
|
|
|
for i := range issues {
|
|
|
|
c.issueBackwardsCompatibility(issues[i])
|
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
return issues, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetIssue returns a single issue for a given repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, *Response, error) {
|
2019-04-25 13:06:53 -04:00
|
|
|
issue := new(Issue)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index), nil, nil, issue)
|
2020-11-14 05:31:01 -05:00
|
|
|
if e := c.checkServerVersionGreaterThanOrEqual(version1_12_0); e != nil && issue.Repository != nil {
|
2020-05-27 09:48:09 -04:00
|
|
|
issue.Repository.Owner = strings.Split(issue.Repository.FullName, "/")[0]
|
|
|
|
}
|
2020-12-01 00:07:23 -05:00
|
|
|
c.issueBackwardsCompatibility(issue)
|
2020-09-15 22:01:41 -04:00
|
|
|
return issue, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateIssueOption options to create one issue
|
|
|
|
type CreateIssueOption struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
Title string `json:"title"`
|
2019-04-25 13:06:53 -04:00
|
|
|
Body string `json:"body"`
|
|
|
|
// username of assignee
|
2019-10-13 11:13:33 -04:00
|
|
|
Assignee string `json:"assignee"`
|
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Deadline *time.Time `json:"due_date"`
|
2019-04-25 13:06:53 -04:00
|
|
|
// milestone id
|
|
|
|
Milestone int64 `json:"milestone"`
|
|
|
|
// list of label ids
|
|
|
|
Labels []int64 `json:"labels"`
|
|
|
|
Closed bool `json:"closed"`
|
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
// Validate the CreateIssueOption struct
|
|
|
|
func (opt CreateIssueOption) Validate() error {
|
|
|
|
if len(strings.TrimSpace(opt.Title)) == 0 {
|
|
|
|
return fmt.Errorf("title is empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// CreateIssue create a new issue for a given repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, *Response, error) {
|
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
issue := new(Issue)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
|
2019-04-25 13:06:53 -04:00
|
|
|
jsonHeader, bytes.NewReader(body), issue)
|
2020-12-01 00:07:23 -05:00
|
|
|
c.issueBackwardsCompatibility(issue)
|
2020-09-15 22:01:41 -04:00
|
|
|
return issue, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditIssueOption options for editing an issue
|
|
|
|
type EditIssueOption struct {
|
2019-10-13 11:13:33 -04:00
|
|
|
Title string `json:"title"`
|
|
|
|
Body *string `json:"body"`
|
|
|
|
Assignee *string `json:"assignee"`
|
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Milestone *int64 `json:"milestone"`
|
2020-05-27 09:48:09 -04:00
|
|
|
State *StateType `json:"state"`
|
2019-10-13 11:13:33 -04:00
|
|
|
Deadline *time.Time `json:"due_date"`
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 22:01:41 -04:00
|
|
|
// Validate the EditIssueOption struct
|
|
|
|
func (opt EditIssueOption) Validate() error {
|
|
|
|
if len(opt.Title) != 0 && len(strings.TrimSpace(opt.Title)) == 0 {
|
|
|
|
return fmt.Errorf("title is empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:06:53 -04:00
|
|
|
// EditIssue modify an existing issue for a given repository
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, *Response, error) {
|
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-04-25 13:06:53 -04:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
|
|
|
issue := new(Issue)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("PATCH",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
|
2019-04-25 13:06:53 -04:00
|
|
|
jsonHeader, bytes.NewReader(body), issue)
|
2020-12-01 00:07:23 -05:00
|
|
|
c.issueBackwardsCompatibility(issue)
|
2020-09-15 22:01:41 -04:00
|
|
|
return issue, resp, err
|
2019-04-25 13:06:53 -04:00
|
|
|
}
|
2020-12-01 00:07:23 -05:00
|
|
|
|
|
|
|
func (c *Client) issueBackwardsCompatibility(issue *Issue) {
|
|
|
|
if c.checkServerVersionGreaterThanOrEqual(version1_12_0) != nil {
|
|
|
|
issue.HTMLURL = fmt.Sprintf("%s/%s/issues/%d", c.url, issue.Repository.FullName, issue.Index)
|
|
|
|
}
|
|
|
|
}
|