diff --git a/jira.go b/jira.go index 1317bd3e..6548c484 100644 --- a/jira.go +++ b/jira.go @@ -38,6 +38,7 @@ type Client struct { Priority *PriorityService Field *FieldService Resolution *ResolutionService + StatusCategory *StatusCategoryService } // NewClient returns a new JIRA API client. @@ -77,6 +78,7 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) { c.Priority = &PriorityService{client: c} c.Field = &FieldService{client: c} c.Resolution = &ResolutionService{client: c} + c.StatusCategory = &StatusCategoryService{client: c} return c, nil } diff --git a/jira_test.go b/jira_test.go index 43a888a6..32c6ea3e 100644 --- a/jira_test.go +++ b/jira_test.go @@ -121,6 +121,9 @@ func TestNewClient_WithServices(t *testing.T) { if c.Resolution == nil { t.Error("No ResolutionService provided") } + if c.StatusCategory == nil { + t.Error("No StatusCategoryService provided") + } } func TestCheckResponse(t *testing.T) { diff --git a/mocks/all_statuscategories.json b/mocks/all_statuscategories.json new file mode 100644 index 00000000..ec597113 --- /dev/null +++ b/mocks/all_statuscategories.json @@ -0,0 +1,30 @@ +[ + { + "self": "https://issues.apache.org/jira/rest/api/2/statuscategory/1", + "id": 1, + "key": "undefined", + "colorName": "medium-gray", + "name": "No Category" + }, + { + "self": "https://issues.apache.org/jira/rest/api/2/statuscategory/2", + "id": 2, + "key": "new", + "colorName": "blue-gray", + "name": "To Do" + }, + { + "self": "https://issues.apache.org/jira/rest/api/2/statuscategory/4", + "id": 4, + "key": "indeterminate", + "colorName": "yellow", + "name": "In Progress" + }, + { + "self": "https://issues.apache.org/jira/rest/api/2/statuscategory/3", + "id": 3, + "key": "done", + "colorName": "green", + "name": "Done" + } +] diff --git a/statuscategory.go b/statuscategory.go index c5365568..05db4207 100644 --- a/statuscategory.go +++ b/statuscategory.go @@ -1,5 +1,12 @@ package jira +// StatusCategoryService handles status categories for the JIRA instance / API. +// +// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-Statuscategory +type StatusCategoryService struct { + client *Client +} + // StatusCategory represents the category a status belongs to. // Those categories can be user defined in every JIRA instance. type StatusCategory struct { @@ -17,3 +24,21 @@ const ( StatusCategoryToDo = "new" StatusCategoryUndefined = "undefined" ) + +// GetList gets all status categories from JIRA +// +// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-statuscategory-get +func (s *StatusCategoryService) GetList() ([]StatusCategory, *Response, error) { + apiEndpoint := "rest/api/2/statuscategory" + req, err := s.client.NewRequest("GET", apiEndpoint, nil) + if err != nil { + return nil, nil, err + } + + statusCategoryList := []StatusCategory{} + resp, err := s.client.Do(req, &statusCategoryList) + if err != nil { + return nil, resp, NewJiraError(resp, err) + } + return statusCategoryList, resp, nil +} diff --git a/statuscategory_test.go b/statuscategory_test.go new file mode 100644 index 00000000..3f8b7ecb --- /dev/null +++ b/statuscategory_test.go @@ -0,0 +1,32 @@ +package jira + +import ( + "fmt" + "io/ioutil" + "net/http" + "testing" +) + +func TestStatusCategoryService_GetList(t *testing.T) { + setup() + defer teardown() + testAPIEdpoint := "/rest/api/2/statuscategory" + + raw, err := ioutil.ReadFile("./mocks/all_statuscategories.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)) + }) + + statusCategory, _, err := testClient.StatusCategory.GetList() + if statusCategory == nil { + t.Error("Expected statusCategory list. StatusCategory list is nil") + } + if err != nil { + t.Errorf("Error given: %s", err) + } +}