-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmilestones.go
103 lines (89 loc) · 2.91 KB
/
milestones.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package taigo
import (
"errors"
"fmt"
"net/http"
"strconv"
"github.com/google/go-querystring/query"
)
// MilestoneService is a handle to actions related to Milestones
//
// https://taigaio.github.io/taiga-doc/dist/api.html#milestones
type MilestoneService struct {
client *Client
defaultProjectID int
Endpoint string
}
// List => https://taigaio.github.io/taiga-doc/dist/api.html#Milestones-list
func (s *MilestoneService) List(queryParams *MilestonesQueryParams) ([]Milestone, *MilestoneTotalInfo, error) {
// prepare url & parameters
url := s.client.MakeURL(s.Endpoint)
switch {
case queryParams != nil:
paramValues, _ := query.Values(queryParams)
url = fmt.Sprintf("%s?%s", url, paramValues.Encode())
case s.defaultProjectID != 0:
url = url + projectIDQueryParam(s.defaultProjectID)
}
// execute requests
var Milestones []Milestone
httpResponse, err := s.client.Request.Get(url, &Milestones)
if err != nil {
return nil, nil, err
}
mti := &MilestoneTotalInfo{}
mti.LoadFromHeaders(httpResponse)
return Milestones, mti, nil
}
// Create => https://taigaio.github.io/taiga-doc/dist/api.html#milestones-create
//
// Mandatory fields: Project, Name, EstimatedStart, EstimatedFinish
func (s *MilestoneService) Create(milestone *Milestone) (*Milestone, error) {
url := s.client.MakeURL(s.Endpoint)
var respMilestone Milestone
// Check for required fields
if (isEmpty(milestone.Project) ||
isEmpty(milestone.Name)) ||
isEmpty(milestone.EstimatedStart) ||
isEmpty(milestone.EstimatedFinish) {
return nil, errors.New("a mandatory field is missing. See API documentataion")
}
_, err := s.client.Request.Post(url, &milestone, &respMilestone)
if err != nil {
return nil, err
}
return &respMilestone, nil
}
// Get => https://taigaio.github.io/taiga-doc/dist/api.html#Milestones-get
func (s *MilestoneService) Get(milestoneID int) (*Milestone, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(milestoneID))
var m Milestone
_, err := s.client.Request.Get(url, &m)
if err != nil {
return nil, err
}
return &m, nil
}
// Edit edits an Milestone via a PATCH request => https://taigaio.github.io/taiga-doc/dist/api.html#milestones-edit
// Available Meta: MilestoneDetail
func (s *MilestoneService) Edit(milestone *Milestone) (*Milestone, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(milestone.ID))
var m Milestone
if milestone.ID == 0 {
return nil, errors.New("passed Milestone does not have an ID yet. Does it exist?")
}
_, err := s.client.Request.Patch(url, &milestone, &m)
if err != nil {
return nil, err
}
return &m, nil
}
// Delete => https://taigaio.github.io/taiga-doc/dist/api.html#milestones-delete
func (s *MilestoneService) Delete(milestoneID int) (*http.Response, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(milestoneID))
return s.client.Request.Delete(url)
}
// Stats
// Watch a milestone
// Stop watching a milestone
// List milestone watchers