2016-03-14 03:18:27 +00:00
|
|
|
// 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.
|
|
|
|
|
2016-11-07 01:57:49 +00:00
|
|
|
package gitea
|
2016-03-14 03:18:27 +00:00
|
|
|
|
2016-08-24 22:30:53 +01:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-01-31 02:41:33 +00:00
|
|
|
"net/url"
|
2020-06-09 11:47:55 +01:00
|
|
|
"strings"
|
2019-10-13 02:34:01 +01:00
|
|
|
"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"`
|
|
|
|
Closed *time.Time `json:"closed_at"`
|
|
|
|
Deadline *time.Time `json:"due_on"`
|
|
|
|
}
|
2016-08-24 22:30:53 +01:00
|
|
|
|
2020-01-31 02:41:33 +00:00
|
|
|
// ListMilestoneOption list milestone options
|
|
|
|
type ListMilestoneOption struct {
|
|
|
|
ListOptions
|
|
|
|
// open, closed, all
|
|
|
|
State StateType
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryEncode turns options into querystring argument
|
|
|
|
func (opt *ListMilestoneOption) QueryEncode() string {
|
2020-02-05 07:59:55 +00:00
|
|
|
query := opt.getURLQuery()
|
2020-01-31 02:41:33 +00:00
|
|
|
if opt.State != "" {
|
|
|
|
query.Add("state", string(opt.State))
|
|
|
|
}
|
|
|
|
return query.Encode()
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// ListRepoMilestones list all the milestones of one repository
|
2020-01-31 02:41:33 +00:00
|
|
|
func (c *Client) ListRepoMilestones(owner, repo string, opt ListMilestoneOption) ([]*Milestone, error) {
|
2020-02-05 07:59:55 +00:00
|
|
|
opt.setDefaults()
|
|
|
|
milestones := make([]*Milestone, 0, opt.PageSize)
|
|
|
|
|
2020-01-31 02:41:33 +00:00
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/milestones", owner, repo))
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
|
|
|
return milestones, c.getParsedResponse("GET", link.String(), nil, nil, &milestones)
|
2016-08-24 22:30:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// GetMilestone get one milestone by repo name and milestone id
|
2016-08-24 22:36:36 +01:00
|
|
|
func (c *Client) GetMilestone(owner, repo string, id int64) (*Milestone, error) {
|
2016-08-24 22:30:53 +01:00
|
|
|
milestone := new(Milestone)
|
|
|
|
return milestone, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil, milestone)
|
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// CreateMilestoneOption options for creating a milestone
|
|
|
|
type CreateMilestoneOption struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Description string `json:"description"`
|
2020-06-19 05:49:31 +01:00
|
|
|
State StateType `json:"state"`
|
2019-10-13 02:34:01 +01:00
|
|
|
Deadline *time.Time `json:"due_on"`
|
|
|
|
}
|
|
|
|
|
2020-06-09 11:47:55 +01:00
|
|
|
// Validate the CreateMilestoneOption struct
|
|
|
|
func (opt CreateMilestoneOption) Validate() error {
|
|
|
|
if len(strings.TrimSpace(opt.Title)) == 0 {
|
|
|
|
return fmt.Errorf("title is empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// CreateMilestone create one milestone with options
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) CreateMilestone(owner, repo string, opt CreateMilestoneOption) (*Milestone, error) {
|
2020-06-09 11:47:55 +01:00
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-24 22:30:53 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
milestone := new(Milestone)
|
2020-06-19 05:49:31 +01:00
|
|
|
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, err
|
2016-08-24 22:36:36 +01:00
|
|
|
}
|
|
|
|
|
2019-10-13 02:34:01 +01:00
|
|
|
// EditMilestoneOption options for editing a milestone
|
|
|
|
type EditMilestoneOption struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Description *string `json:"description"`
|
2020-06-04 10:19:02 +01:00
|
|
|
State *StateType `json:"state"`
|
2019-10-13 02:34:01 +01:00
|
|
|
Deadline *time.Time `json:"due_on"`
|
|
|
|
}
|
|
|
|
|
2020-06-09 11:47:55 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// EditMilestone modify milestone with options
|
2019-10-13 02:34:01 +01:00
|
|
|
func (c *Client) EditMilestone(owner, repo string, id int64, opt EditMilestoneOption) (*Milestone, error) {
|
2020-06-09 11:47:55 +01:00
|
|
|
if err := opt.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-24 22:30:53 +01:00
|
|
|
body, err := json.Marshal(&opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
milestone := new(Milestone)
|
|
|
|
return milestone, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), jsonHeader, bytes.NewReader(body), milestone)
|
|
|
|
}
|
|
|
|
|
2016-11-10 09:44:00 +00:00
|
|
|
// DeleteMilestone delete one milestone by milestone id
|
2016-08-24 22:30:53 +01:00
|
|
|
func (c *Client) DeleteMilestone(owner, repo string, id int64) error {
|
|
|
|
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/milestones/%d", owner, repo, id), nil, nil)
|
|
|
|
return err
|
|
|
|
}
|