2016-03-14 03:18:27 +00:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2019-05-11 16:38:52 +01:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2016-03-14 03:18:27 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2016-11-07 01:57:49 +00:00
|
|
|
package gitea
|
2016-03-14 03:18:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-01-26 02:46:38 +00:00
|
|
|
"net/url"
|
2020-02-04 15:15:35 +00:00
|
|
|
"strings"
|
2019-10-13 02:34:01 +01:00
|
|
|
"time"
|
2016-08-14 11:32:53 +01:00
|
|
|
)
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// PullRequestMeta PR info if an issue is a PR
|
|
|
|
type PullRequestMeta struct {
|
|
|
|
HasMerged bool `json:"merged"`
|
|
|
|
Merged *time.Time `json:"merged_at"`
|
|
|
|
}
|
2016-03-14 03:18:27 +00:00
|
|
|
|
2020-02-04 15:15:35 +00: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-10-13 02:34:01 +01:00
|
|
|
// Issue represents an issue in a repository
|
|
|
|
type Issue struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
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"`
|
|
|
|
Assignee *User `json:"assignee"`
|
|
|
|
Assignees []*User `json:"assignees"`
|
|
|
|
// Whether the issue is open or closed
|
|
|
|
State StateType `json:"state"`
|
|
|
|
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"`
|
|
|
|
PullRequest *PullRequestMeta `json:"pull_request"`
|
2020-02-04 15:15:35 +00:00
|
|
|
Repository *RepositoryMeta `json:"repository"`
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListIssueOption list issue options
|
|
|
|
type ListIssueOption struct {
|
2020-02-05 07:59:55 +00:00
|
|
|
ListOptions
|
2020-02-04 07:58:46 +00:00
|
|
|
State StateType
|
2020-01-26 02:46:38 +00:00
|
|
|
Labels []string
|
|
|
|
KeyWord string
|
|
|
|
}
|
|
|
|
|
2020-02-04 07:58:46 +00:00
|
|
|
// StateType issue state type
|
|
|
|
type StateType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// StateOpen pr is opend
|
|
|
|
StateOpen StateType = "open"
|
|
|
|
// StateClosed pr is closed
|
|
|
|
StateClosed StateType = "closed"
|
|
|
|
// StateAll is all
|
|
|
|
StateAll StateType = "all"
|
|
|
|
)
|
|
|
|
|
2020-01-26 19:08:47 +00:00
|
|
|
// QueryEncode turns options into querystring argument
|
2020-01-26 02:46:38 +00:00
|
|
|
func (opt *ListIssueOption) QueryEncode() string {
|
2020-02-05 07:59:55 +00:00
|
|
|
query := opt.getURLQuery()
|
2020-01-26 02:46:38 +00:00
|
|
|
if len(opt.State) > 0 {
|
2020-02-04 07:58:46 +00:00
|
|
|
query.Add("state", string(opt.State))
|
2020-01-26 02:46:38 +00:00
|
|
|
}
|
|
|
|
if len(opt.Labels) > 0 {
|
|
|
|
var lq string
|
|
|
|
for _, l := range opt.Labels {
|
|
|
|
if len(lq) > 0 {
|
|
|
|
lq += ","
|
|
|
|
}
|
|
|
|
lq += l
|
|
|
|
}
|
|
|
|
query.Add("labels", lq)
|
|
|
|
}
|
|
|
|
if len(opt.KeyWord) > 0 {
|
|
|
|
query.Add("q", opt.KeyWord)
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.Encode()
|
2019-10-13 02:34:01 +01:00
|
|
|
}
|
2016-03-14 03:18:27 +00:00
|
|
|
|
2016-11-09 16:03:04 +00:00
|
|
|
// ListIssues returns all issues assigned the authenticated user
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) ListIssues(opt ListIssueOption) ([]*Issue, error) {
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
issues := make([]*Issue, 0, opt.PageSize)
|
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
link, _ := url.Parse("/repos/issues/search")
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-02-04 15:15:35 +00:00
|
|
|
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
|
2016-11-09 16:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoIssues returns all issues for a given repository
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) ListRepoIssues(owner, repo string, opt ListIssueOption) ([]*Issue, error) {
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
issues := make([]*Issue, 0, opt.PageSize)
|
|
|
|
|
2020-01-26 02:46:38 +00:00
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/issues", owner, repo))
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-02-04 15:15:35 +00:00
|
|
|
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
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 16:03:04 +00:00
|
|
|
// GetIssue returns a single issue for a given repository
|
2016-08-02 19:59:54 +01:00
|
|
|
func (c *Client) GetIssue(owner, repo string, index int64) (*Issue, error) {
|
2016-03-14 03:18:27 +00:00
|
|
|
issue := new(Issue)
|
2020-02-04 15:15:35 +00:00
|
|
|
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
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// CreateIssueOption options to create one issue
|
|
|
|
type CreateIssueOption struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body string `json:"body"`
|
|
|
|
// username of assignee
|
|
|
|
Assignee string `json:"assignee"`
|
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Deadline *time.Time `json:"due_date"`
|
|
|
|
// milestone id
|
|
|
|
Milestone int64 `json:"milestone"`
|
|
|
|
// list of label ids
|
|
|
|
Labels []int64 `json:"labels"`
|
|
|
|
Closed bool `json:"closed"`
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:03:04 +00:00
|
|
|
// CreateIssue create a new issue for a given repository
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue, error) {
|
2016-03-14 03:18:27 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issue := new(Issue)
|
|
|
|
return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
|
2016-07-17 01:46:54 +01:00
|
|
|
jsonHeader, bytes.NewReader(body), issue)
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// EditIssueOption options for editing an issue
|
|
|
|
type EditIssueOption struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Body *string `json:"body"`
|
|
|
|
Assignee *string `json:"assignee"`
|
|
|
|
Assignees []string `json:"assignees"`
|
|
|
|
Milestone *int64 `json:"milestone"`
|
2020-02-04 07:58:46 +00:00
|
|
|
State *StateType `json:"state"`
|
2019-10-13 02:34:01 +01:00
|
|
|
Deadline *time.Time `json:"due_date"`
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:03:04 +00:00
|
|
|
// EditIssue modify an existing issue for a given repository
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) EditIssue(owner, repo string, index int64, opt EditIssueOption) (*Issue, error) {
|
2016-03-14 03:18:27 +00:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issue := new(Issue)
|
|
|
|
return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
|
2016-07-17 01:46:54 +01:00
|
|
|
jsonHeader, bytes.NewReader(body), issue)
|
2016-03-14 03:18:27 +00:00
|
|
|
}
|