-
Notifications
You must be signed in to change notification settings - Fork 5
/
epics.go
190 lines (167 loc) · 5.73 KB
/
epics.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package taigo
import (
"errors"
"fmt"
"net/http"
"strconv"
"github.com/google/go-querystring/query"
)
// EpicService is a handle to actions related to Epics
//
// https://taigaio.github.io/taiga-doc/dist/api.html#epics
type EpicService struct {
client *Client
defaultProjectID int
Endpoint string
}
// List => https://taigaio.github.io/taiga-doc/dist/api.html#epics-list
//
// Available Meta: *EpicDetailLIST
func (s *EpicService) List(queryParams *EpicsQueryParams) ([]Epic, error) {
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)
}
var epics EpicDetailLIST
_, err := s.client.Request.Get(url, &epics)
if err != nil {
return nil, err
}
return epics.AsEpics()
}
// Create => https://taigaio.github.io/taiga-doc/dist/api.html#epics-create
//
// Available Meta: *EpicDetail
func (s *EpicService) Create(epic *Epic) (*Epic, error) {
url := s.client.MakeURL(s.Endpoint)
var e EpicDetail
// Check for required fields
// project, subject
if isEmpty(epic.Project) || isEmpty(epic.Subject) {
return nil, errors.New("a mandatory field(Project, Subject) is missing. See API documentataion")
}
_, err := s.client.Request.Post(url, &epic, &e)
if err != nil {
return nil, err
}
return e.AsEpic()
}
// Get => https://taigaio.github.io/taiga-doc/dist/api.html#epics-get
//
// Available Meta: *EpicDetailGET
func (s *EpicService) Get(epicID int) (*Epic, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(epicID))
var e EpicDetailGET
_, err := s.client.Request.Get(url, &e)
if err != nil {
return nil, err
}
return e.AsEpic()
}
// GetByRef => https://taigaio.github.io/taiga-doc/dist/api.html#epics-get-by-ref
//
// The passed epicRef should be an int taken from the Epic's URL
// The passed *Project struct should have at least one of the following fields set:
// ID (int)
// Slug (string)
// If none of the above fields are set, an error is returned.
// If both fields are set, *Project.ID will be preferred.
//
// Available Meta: *EpicDetailGET
func (s *EpicService) GetByRef(epicRef int, project *Project) (*Epic, error) {
var e EpicDetailGET
var url string
switch {
case project.ID > 0:
url = s.client.MakeURL(fmt.Sprintf("%s/by_ref?ref=%d&project=%d", s.Endpoint, (epicRef), project.ID))
case len(project.Slug) > 0:
url = s.client.MakeURL(fmt.Sprintf("%s/by_ref?ref=%d&project__slug=%s", s.Endpoint, epicRef, project.Slug))
default:
return nil, errors.New("no ID or Ref defined in passed project struct")
}
_, err := s.client.Request.Get(url, &e)
if err != nil {
return nil, err
}
return e.AsEpic()
}
// Edit edits an Epic via a PATCH request => https://taigaio.github.io/taiga-doc/dist/api.html#epics-edit
// Available Meta: EpicDetail
func (s *EpicService) Edit(epic *Epic) (*Epic, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(epic.ID))
var e EpicDetail
if epic.ID == 0 {
return nil, errors.New("passed Epic does not have an ID yet. Does it exist?")
}
// Taiga OCC
remoteEpic, err := s.Get(epic.ID)
if err != nil {
return nil, err
}
epic.Version = remoteEpic.Version
_, err = s.client.Request.Patch(url, &epic, &e)
if err != nil {
return nil, err
}
return e.AsEpic()
}
// Delete => https://taigaio.github.io/taiga-doc/dist/api.html#epics-delete
func (s *EpicService) Delete(epicID int) (*http.Response, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(epicID))
return s.client.Request.Delete(url)
}
// BulkCreation => https://taigaio.github.io/taiga-doc/dist/api.html#epics-bulk-create
/*
This is not yet implemented, placeholder only.
It seems to be pointless to implement this operation here. A for loop around `Create` is much more efficient and accurate.
*/
// func (s *EpicService) BulkCreation() {}
// EpicFiltersData => https://taigaio.github.io/taiga-doc/dist/api.html#epics-get-filters-data
/*
This is not yet implemented, placeholder only.
It seems to be pointless to implement this operation here. A for loop around `Create` is much more efficient and accurate.
*/
// func (s *EpicService) EpicFiltersData() {}
// ListRelatedUserStories => https://taigaio.github.io/taiga-doc/dist/api.html#epics-related-user-stories-list
func (s *EpicService) ListRelatedUserStories(epicID int) ([]EpicRelatedUserStoryDetail, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(epicID), "related_userstories")
var e []EpicRelatedUserStoryDetail
_, err := s.client.Request.Get(url, &e)
if err != nil {
return nil, err
}
return e, nil
}
// CreateRelatedUserStory => https://taigaio.github.io/taiga-doc/dist/api.html#epics-related-user-stories-create
//
// Mandatory parameters: `EpicID`; `UserStoryID`
// Accepted UserStory values: `UserStory.ID`
func (s *EpicService) CreateRelatedUserStory(EpicID int, UserStoryID int) (*EpicRelatedUserStoryDetail, error) {
url := s.client.MakeURL(s.Endpoint, strconv.Itoa(EpicID), "related_userstories")
e := EpicRelatedUserStoryDetail{EpicID: EpicID, UserStoryID: UserStoryID}
_, err := s.client.Request.Post(url, &e, &e)
if err != nil {
return nil, err
}
return &e, nil
}
// CreateAttachment creates a new Epic attachment => https://taigaio.github.io/taiga-doc/dist/api.html#epics-create-attachment
//
func (s *EpicService) CreateAttachment(attachment *Attachment, epic *Epic) (*Attachment, error) {
url := s.client.MakeURL(s.Endpoint, "attachments")
return newfileUploadRequest(s.client, url, attachment, epic)
}
// Clone takes an *Epic struct with loaded properties and duplicates it
//
// Available Meta: *EpicDetail
func (e *Epic) Clone(s *EpicService) (*Epic, error) {
// Clean up data
e.ID = 0
e.Version = 0
e.Ref = 0
return s.Create(e)
}