688ee1978e
Add Docs on how to deal with this breaking change doResponse should return responce in all cases, to prosses things manualy if wanted helper func return http response Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/416 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
142 lines
4.5 KiB
Go
142 lines
4.5 KiB
Go
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
// 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"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Milestone milestone is a collection of issues on one repository
|
|
type Milestone struct {
|
|
ID int64 `json:"id"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
State StateType `json:"state"`
|
|
OpenIssues int `json:"open_issues"`
|
|
ClosedIssues int `json:"closed_issues"`
|
|
Created time.Time `json:"created_at"`
|
|
Updated *time.Time `json:"updated_at"`
|
|
Closed *time.Time `json:"closed_at"`
|
|
Deadline *time.Time `json:"due_on"`
|
|
}
|
|
|
|
// ListMilestoneOption list milestone options
|
|
type ListMilestoneOption struct {
|
|
ListOptions
|
|
// open, closed, all
|
|
State StateType
|
|
Name string
|
|
}
|
|
|
|
// QueryEncode turns options into querystring argument
|
|
func (opt *ListMilestoneOption) QueryEncode() string {
|
|
query := opt.getURLQuery()
|
|
if opt.State != "" {
|
|
query.Add("state", string(opt.State))
|
|
}
|
|
if len(opt.Name) != 0 {
|
|
query.Add("name", opt.Name)
|
|
}
|
|
return query.Encode()
|
|
}
|
|
|
|
// ListRepoMilestones list all the milestones of one repository
|
|
func (c *Client) ListRepoMilestones(owner, repo string, opt ListMilestoneOption) ([]*Milestone, *Response, error) {
|
|
opt.setDefaults()
|
|
milestones := make([]*Milestone, 0, opt.PageSize)
|
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/milestones", owner, repo))
|
|
link.RawQuery = opt.QueryEncode()
|
|
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &milestones)
|
|
return milestones, resp, err
|
|
}
|
|
|
|
// GetMilestone get one milestone by repo name and milestone id
|
|
func (c *Client) GetMilestone(owner, repo string, id int64) (*Milestone, *Response, error) {
|
|
milestone := new(Milestone)
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil, milestone)
|
|
return milestone, resp, err
|
|
}
|
|
|
|
// CreateMilestoneOption options for creating a milestone
|
|
type CreateMilestoneOption struct {
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
State StateType `json:"state"`
|
|
Deadline *time.Time `json:"due_on"`
|
|
}
|
|
|
|
// Validate the CreateMilestoneOption struct
|
|
func (opt CreateMilestoneOption) Validate() error {
|
|
if len(strings.TrimSpace(opt.Title)) == 0 {
|
|
return fmt.Errorf("title is empty")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CreateMilestone create one milestone with options
|
|
func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption) (*Milestone, *Response, error) {
|
|
if err := opt.Validate(); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
body, err := json.Marshal(&opt)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
milestone := new(Milestone)
|
|
resp, err := c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/milestones", owner, repo), jsonHeader, bytes.NewReader(body), milestone)
|
|
|
|
// make creating closed milestones need gitea >= v1.13.0
|
|
// this make it backwards compatible
|
|
if err == nil && opt.State == StateClosed && milestone.State != StateClosed {
|
|
closed := StateClosed
|
|
return c.EditMilestone(owner, repo, milestone.ID, EditMilestoneOption{
|
|
State: &closed,
|
|
})
|
|
}
|
|
|
|
return milestone, resp, err
|
|
}
|
|
|
|
// EditMilestoneOption options for editing a milestone
|
|
type EditMilestoneOption struct {
|
|
Title string `json:"title"`
|
|
Description *string `json:"description"`
|
|
State *StateType `json:"state"`
|
|
Deadline *time.Time `json:"due_on"`
|
|
}
|
|
|
|
// Validate the EditMilestoneOption struct
|
|
func (opt EditMilestoneOption) Validate() error {
|
|
if len(opt.Title) != 0 && len(strings.TrimSpace(opt.Title)) == 0 {
|
|
return fmt.Errorf("title is empty")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditMilestone modify milestone with options
|
|
func (c *Client) EditMilestone(owner, repo string, id int64, opt EditMilestoneOption) (*Milestone, *Response, error) {
|
|
if err := opt.Validate(); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
body, err := json.Marshal(&opt)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
milestone := new(Milestone)
|
|
resp, err := c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), milestone)
|
|
return milestone, resp, err
|
|
}
|
|
|
|
// DeleteMilestone delete one milestone by milestone id
|
|
func (c *Client) DeleteMilestone(owner, repo string, id int64) (*Response, error) {
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil)
|
|
return resp, err
|
|
}
|