Skip to content

Commit 231bc31

Browse files
committed
Support state filter for GetAllSprints call
* creates a GetAllSprintsOptions structure * adds a new GetAllSprintsWithOptions method to accept an int boardID and new options structure * adds filtering functionality to GetAllSprintsWithOptions method * adds SprintsList type for handling pagination results data from GetAllSprints request * updates tests
1 parent 98ede9f commit 231bc31

File tree

3 files changed

+94
-8
lines changed

3 files changed

+94
-8
lines changed

board.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jira
22

33
import (
44
"fmt"
5+
"strconv"
56
"time"
67
)
78

@@ -44,9 +45,21 @@ type BoardListOptions struct {
4445
SearchOptions
4546
}
4647

47-
// Wrapper struct for search result
48-
type sprintsResult struct {
49-
Sprints []Sprint `json:"values" structs:"values"`
48+
// GetAllSprintsOptions specifies the optional parameters to the BoardService.GetList
49+
type GetAllSprintsOptions struct {
50+
// State filters results to sprints in the specified states, comma-separate list
51+
State string `url:"state,omitempty"`
52+
53+
SearchOptions
54+
}
55+
56+
// SprintsList reflects a list of agile sprints
57+
type SprintsList struct {
58+
MaxResults int `json:"maxResults" structs:"maxResults"`
59+
StartAt int `json:"startAt" structs:"startAt"`
60+
Total int `json:"total" structs:"total"`
61+
IsLast bool `json:"isLast" structs:"isLast"`
62+
Values []Sprint `json:"values" structs:"values"`
5063
}
5164

5265
// Sprint represents a sprint on JIRA agile board
@@ -67,6 +80,9 @@ type Sprint struct {
6780
func (s *BoardService) GetAllBoards(opt *BoardListOptions) (*BoardsList, *Response, error) {
6881
apiEndpoint := "rest/agile/1.0/board"
6982
url, err := addOptions(apiEndpoint, opt)
83+
if err != nil {
84+
return nil, nil, err
85+
}
7086
req, err := s.client.NewRequest("GET", url, nil)
7187
if err != nil {
7288
return nil, nil, err
@@ -145,22 +161,44 @@ func (s *BoardService) DeleteBoard(boardID int) (*Board, *Response, error) {
145161
return nil, resp, err
146162
}
147163

148-
// GetAllSprints will returns all sprints from a board, for a given board Id.
164+
// GetAllSprints will return all sprints from a board, for a given board Id.
149165
// This only includes sprints that the user has permission to view.
150166
//
151167
// JIRA API docs: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board/{boardId}/sprint
152168
func (s *BoardService) GetAllSprints(boardID string) ([]Sprint, *Response, error) {
153-
apiEndpoint := fmt.Sprintf("rest/agile/1.0/board/%s/sprint", boardID)
154-
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
169+
id, err := strconv.Atoi(boardID)
170+
if err != nil {
171+
return nil, nil, err
172+
}
173+
174+
result, response, err := s.GetAllSprintsWithOptions(id, &GetAllSprintsOptions{})
175+
if err != nil {
176+
return nil, nil, err
177+
}
178+
179+
return result.Values, response, nil
180+
}
181+
182+
// GetAllSprintsWithOptions will return sprints from a board, for a given board Id and filtering options
183+
// This only includes sprints that the user has permission to view.
184+
//
185+
// JIRA API docs: https://docs.atlassian.com/jira-software/REST/cloud/#agile/1.0/board/{boardId}/sprint
186+
func (s *BoardService) GetAllSprintsWithOptions(boardID int, options *GetAllSprintsOptions) (*SprintsList, *Response, error) {
187+
apiEndpoint := fmt.Sprintf("rest/agile/1.0/board/%d/sprint", boardID)
188+
url, err := addOptions(apiEndpoint, options)
189+
if err != nil {
190+
return nil, nil, err
191+
}
192+
req, err := s.client.NewRequest("GET", url, nil)
155193
if err != nil {
156194
return nil, nil, err
157195
}
158196

159-
result := new(sprintsResult)
197+
result := new(SprintsList)
160198
resp, err := s.client.Do(req, result)
161199
if err != nil {
162200
err = NewJiraError(resp, err)
163201
}
164202

165-
return result.Sprints, resp, err
203+
return result, resp, err
166204
}

board_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,35 @@ func TestBoardService_GetAllSprints(t *testing.T) {
184184
t.Errorf("Expected 4 transitions. Got %d", len(sprints))
185185
}
186186
}
187+
188+
func TestBoardService_GetAllSprintsWithOptions(t *testing.T) {
189+
setup()
190+
defer teardown()
191+
192+
testAPIEndpoint := "/rest/agile/1.0/board/123/sprint"
193+
194+
raw, err := ioutil.ReadFile("./mocks/sprints_filtered.json")
195+
if err != nil {
196+
t.Error(err.Error())
197+
}
198+
199+
testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
200+
testMethod(t, r, "GET")
201+
testRequestURL(t, r, testAPIEndpoint)
202+
fmt.Fprint(w, string(raw))
203+
})
204+
205+
sprints, _, err := testClient.Board.GetAllSprintsWithOptions(123, &GetAllSprintsOptions{State: "active,future"})
206+
207+
if err != nil {
208+
t.Errorf("Got error: %v", err)
209+
}
210+
211+
if sprints == nil {
212+
t.Error("Expected sprint list. Got nil.")
213+
}
214+
215+
if len(sprints.Values) != 1 {
216+
t.Errorf("Expected 1 transition. Got %d", len(sprints.Values))
217+
}
218+
}

mocks/sprints_filtered.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"isLast": true,
3+
"maxResults": 50,
4+
"startAt": 0,
5+
"values": [
6+
{
7+
"endDate": "2016-06-28T14:24:00.000-07:00",
8+
"id": 832,
9+
"name": "Iteration-13-2",
10+
"originBoardId": 734,
11+
"self": "https://jira.com/rest/agile/1.0/sprint/832",
12+
"startDate": "2016-06-20T13:24:39.161-07:00",
13+
"state": "active"
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)