/
sprint.go
206 lines (172 loc) · 4.19 KB
/
sprint.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package jira
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
// Sprint states.
const (
SprintStateActive = "active"
SprintStateClosed = "closed"
SprintStateFuture = "future"
)
// SprintResult holds response from /board/{boardID}/sprint endpoint.
type SprintResult struct {
MaxResults int `json:"maxResults"`
StartAt int `json:"startAt"`
IsLast bool `json:"isLast"`
Sprints []*Sprint `json:"values"`
}
// Sprints fetches all sprints for a given board.
//
// qp is an additional query parameters in key, value pair format, eg: state=closed.
func (c *Client) Sprints(boardID int, qp string, from, limit int) (*SprintResult, error) {
res, err := c.GetV1(
context.Background(),
fmt.Sprintf("/board/%d/sprint?%s&startAt=%d&maxResults=%d", boardID, qp, from, limit),
nil,
)
if err != nil {
return nil, err
}
if res == nil {
return nil, ErrEmptyResponse
}
defer func() { _ = res.Body.Close() }()
if res.StatusCode != http.StatusOK {
return nil, formatUnexpectedResponse(res)
}
var out SprintResult
err = json.NewDecoder(res.Body).Decode(&out)
return &out, err
}
// SprintsInBoards fetches sprints across given board IDs.
//
// qp is an additional query parameters in key, value pair format, eg: state=closed.
func (c *Client) SprintsInBoards(boardIDs []int, qp string, limit int) []*Sprint {
n := len(boardIDs)
ch := make(chan []*Sprint, n)
for _, boardID := range boardIDs {
go func(id int) {
s, err := c.lastNSprints(id, qp, limit)
if err != nil {
ch <- nil
return
}
injectBoardID(s.Sprints, id)
ch <- s.Sprints
}(boardID)
}
var sprints []*Sprint
seen := make(map[int]struct{}, n)
for i := 0; i < n; i++ {
v := <-ch
for _, s := range v {
if _, ok := seen[s.ID]; ok {
continue
}
sprints = append(sprints, s)
seen[s.ID] = struct{}{}
}
}
reverse(sprints)
return sprints
}
// SprintIssues fetches issues in the given sprint.
func (c *Client) SprintIssues(sprintID int, jql string, from, limit uint) (*SearchResult, error) {
path := fmt.Sprintf("/sprint/%d/issue?startAt=%d&maxResults=%d", sprintID, from, limit)
if jql != "" {
path += fmt.Sprintf("&jql=%s", url.QueryEscape(jql))
}
res, err := c.GetV1(context.Background(), path, nil)
if err != nil {
return nil, err
}
if res == nil {
return nil, ErrEmptyResponse
}
defer func() { _ = res.Body.Close() }()
if res.StatusCode != http.StatusOK {
return nil, formatUnexpectedResponse(res)
}
var out SearchResult
err = json.NewDecoder(res.Body).Decode(&out)
return &out, err
}
// SprintIssuesAdd adds issues to the sprint.
func (c *Client) SprintIssuesAdd(id string, issues ...string) error {
path := fmt.Sprintf("/sprint/%s/issue", id)
data := struct {
Issues []string `json:"issues"`
}{Issues: issues}
body, err := json.Marshal(&data)
if err != nil {
return err
}
res, err := c.PostV1(context.Background(), path, body, Header{
"Accept": "application/json",
"Content-Type": "application/json",
})
if err != nil {
return err
}
if res == nil {
return ErrEmptyResponse
}
defer func() { _ = res.Body.Close() }()
if res.StatusCode != http.StatusNoContent {
return formatUnexpectedResponse(res)
}
return nil
}
// LastNSprints fetches sprint in descending order.
//
// Jira api to get all sprints doesn't provide an option to sort results and
// returns result in ascending order by default. So, we will have to send
// multiple requests to get the results we are interested in.
func (c *Client) lastNSprints(boardID int, qp string, limit int) (*SprintResult, error) {
var (
s *SprintResult
err error
n, total int
)
for {
s, err = c.Sprints(boardID, qp, n, limit)
if err != nil {
break
}
if s.IsLast {
total = s.StartAt + len(s.Sprints)
break
}
n += limit
}
if err != nil {
return nil, err
}
if total == 0 {
return nil, ErrNoResult
}
n = total - limit
if n < 0 {
return s, err
}
return c.Sprints(boardID, qp, n, limit)
}
func injectBoardID(sprints []*Sprint, boardID int) {
for _, s := range sprints {
s.BoardID = boardID
}
}
func reverse(s []*Sprint) {
n := len(s)
if n < 2 {
return
}
for i := 0; i < n/2; i++ {
j := n - i - 1
s[i], s[j] = s[j], s[i]
}
}