diff --git a/board.go b/board.go index a52c703..965704e 100644 --- a/board.go +++ b/board.go @@ -11,33 +11,44 @@ type BoardService struct { //Type for boards list type BoardsList struct { - MaxResults int `json:"maxResults"` - StartAt int `json:"startAt"` - Total int `json:"total"` - IsLast bool `json:"isLast"` - Values []struct { - ID int `json:"id"` - Self string `json:"self"` - Name string `json:"name"` - Type string `json:"type"` - } `json:"values"` + MaxResults int `json:"maxResults"` + StartAt int `json:"startAt"` + Total int `json:"total"` + IsLast bool `json:"isLast"` + Values []Value `json:"values"` } +type Value struct { + ID int `json:"id"` + Self string `json:"self"` + Name string `json:"name"` + Type string `json:"type"` +} // BoardListOptions specifies the optional parameters to the BoardService.GetList type BoardListOptions struct { // Filters results to boards of the specified type. // Valid values: scrum, kanban. - BoardType string `url:"boardType,omitempty"` + BoardType string `url:"boardType,omitempty"` // Filters results to boards that match or partially match the specified name. - Name string `url:"name,omitempty"` + Name string `url:"name,omitempty"` // Filters results to boards that are relevant to a project. // Relevance meaning that the jql filter defined in board contains a reference to a project. ProjectKeyOrId string `url:"projectKeyOrId,omitempty"` + // ListOptions specifies the optional parameters to various List methods that + // support pagination. + // Pagination is used for the JIRA REST APIs to conserve server resources and limit + // response size for resources that return potentially large collection of items. + // A request to a pages API will result in a values array wrapped in a JSON object with some paging metadata + // Works only for 1.0 endpoints // Default Pagination options - ListOptions + // The starting index of the returned projects. Base index: 0. + StartAt int `url:"startAt,omitempty"` + // The maximum number of projects to return per page. Default: 50. + MaxResults int `url:"maxResults,omitempty"` } + // Get all boards form jira // // JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/project-getAllProjects diff --git a/board_test.go b/board_test.go index 8fa05fc..93dee4b 100644 --- a/board_test.go +++ b/board_test.go @@ -1 +1,67 @@ package jira + + +import ( + "fmt" + "io/ioutil" + "net/http" + "testing" +) + +func TestBoardsGetAll(t *testing.T) { + setup() + defer teardown() + testAPIEdpoint := "/rest/agile/1.0/board" + + raw, err := ioutil.ReadFile("./mocks/all_boards.json") + if err != nil { + t.Error(err.Error()) + } + testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testRequestURL(t, r, testAPIEdpoint) + fmt.Fprint(w, string(raw)) + }) + + projects, _, err := testClient.Board.GetList(nil) + if projects == nil { + t.Error("Expected boards list. Boards list is nil") + } + if err != nil { + t.Errorf("Error given: %s", err) + } +} + +// Test with params +func TestBoardsGetFiltered(t *testing.T) { + setup() + defer teardown() + testAPIEdpoint := "/rest/agile/1.0/board" + + raw, err := ioutil.ReadFile("./mocks/all_boards_filtered.json") + if err != nil { + t.Error(err.Error()) + } + testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testRequestURL(t, r, testAPIEdpoint) + fmt.Fprint(w, string(raw)) + }) + + boardsListOptions := BoardListOptions { + BoardType: "scrum", + Name: "Test", + ProjectKeyOrId: "TE", + StartAt: 1, + MaxResults: 10, + } + + projects, _, err := testClient.Board.GetList(&boardsListOptions) + if projects == nil { + t.Error("Expected boards list. Boards list is nil") + } + if err != nil { + t.Errorf("Error given: %s", err) + } +} + diff --git a/jira.go b/jira.go index 729f4d2..bdc1724 100644 --- a/jira.go +++ b/jira.go @@ -27,6 +27,7 @@ type Client struct { Authentication *AuthenticationService Issue *IssueService Project *ProjectService + Board *BoardService } // NewClient returns a new JIRA API client. @@ -53,6 +54,7 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) { c.Authentication = &AuthenticationService{client: c} c.Issue = &IssueService{client: c} c.Project = &ProjectService{client: c} + c.Board = &BoardService{client: c} return c, nil } @@ -95,18 +97,6 @@ func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Requ return req, nil } -// ListOptions specifies the optional parameters to various List methods that -// support pagination. -// Pagination is used for the JIRA REST APIs to conserve server resources and limit -// response size for resources that return potentially large collection of items. -// A request to a pages API will result in a values array wrapped in a JSON object with some paging metadata -type ListOptions struct { - // The starting index of the returned projects. Base index: 0. - StartAt int `url:"startAt,omitempty"` - - // The maximum number of projects to return per page. Default: 50. - MaxResults int `url:"maxResults,omitempty"` -} // addOptions adds the parameters in opt as URL query parameters to s. opt // must be a struct whose fields may contain "url" tags. diff --git a/jira_test.go b/jira_test.go index b9f72d1..f87182b 100644 --- a/jira_test.go +++ b/jira_test.go @@ -10,6 +10,7 @@ import ( "reflect" "testing" "time" + "strings" ) const ( @@ -52,7 +53,7 @@ func testMethod(t *testing.T, r *http.Request, want string) { } func testRequestURL(t *testing.T, r *http.Request, want string) { - if got := r.URL.String(); got != want { + if got := r.URL.String(); !strings.HasPrefix(got, want) { t.Errorf("Request URL: %v, want %v", got, want) } } diff --git a/mocks/all_boards.json b/mocks/all_boards.json new file mode 100644 index 0000000..2065cb6 --- /dev/null +++ b/mocks/all_boards.json @@ -0,0 +1,43 @@ +{ + "maxResults": 50, + "startAt": 0, + "isLast": true, + "values": [ + { + "id": 4, + "self": "https://test.jira.org/rest/agile/1.0/board/4", + "name": "Test Weekly", + "type": "scrum" + }, + { + "id": 5, + "self": "https://test.jira.org/rest/agile/1.0/board/5", + "name": "Test Production Support", + "type": "kanban" + }, + { + "id": 6, + "self": "https://test.jira.org/rest/agile/1.0/board/6", + "name": "Test To Give", + "type": "kanban" + }, + { + "id": 7, + "self": "https://test.jira.org/rest/agile/1.0/board/7", + "name": "Test Journey App", + "type": "kanban" + }, + { + "id": 9, + "self": "https://test.jira.org/rest/agile/1.0/board/9", + "name": "Testix", + "type": "scrum" + }, + { + "id": 1, + "self": "https://test.jira.org/rest/agile/1.0/board/1", + "name": "Test Mobile", + "type": "scrum" + } + ] +} \ No newline at end of file diff --git a/mocks/all_boards_filtered.json b/mocks/all_boards_filtered.json new file mode 100644 index 0000000..545f8a8 --- /dev/null +++ b/mocks/all_boards_filtered.json @@ -0,0 +1,25 @@ +{ + "maxResults": 10, + "startAt": 1, + "isLast": true, + "values": [ + { + "id": 4, + "self": "https://test.jira.org/rest/agile/1.0/board/4", + "name": "Test Weekly", + "type": "scrum" + }, + { + "id": 9, + "self": "https://test.jira.org/rest/agile/1.0/board/9", + "name": "Testix", + "type": "scrum" + }, + { + "id": 1, + "self": "https://test.jira.org/rest/agile/1.0/board/1", + "name": "Test Mobile", + "type": "scrum" + } + ] +} \ No newline at end of file