diff --git a/field.go b/field.go new file mode 100644 index 0000000..2555092 --- /dev/null +++ b/field.go @@ -0,0 +1,44 @@ +package jira + +// PriorityService handles priorities for the JIRA instance / API. +// +// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-Priority +type FieldService struct { + client *Client +} + +// Priority represents a priority of a JIRA issue. +// Typical types are "Normal", "Moderate", "Urgent", ... +type Field struct { + ID string `json:"id,omitempty" structs:"id,omitempty"` + Key string `json:"key,omitempty" structs:"key,omitempty"` + Name string `json:"name,omitempty" structs:"name,omitempty"` + Custom bool `json:"custom,omitempty" structs:"custom,omitempty"` + Navigable bool `json:"navigable,omitempty" structs:"navigable,omitempty"` + Searchable bool `json:"searchable,omitempty" structs:"searchable,omitempty"` + ClauseNames []string `json:"clauseNames,omitempty" structs:"clauseNames,omitempty"` + Schema FieldSchema `json:"schema,omitempty" structs:"schema,omitempty"` +} + +type FieldSchema struct { + Type string `json:"type,omitempty" structs:"type,omitempty"` + System string `json:"system,omitempty" structs:"system,omitempty"` +} + +// GetList gets all fields from JIRA +// +// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-priority-get +func (s *FieldService) GetList() ([]Field, *Response, error) { + apiEndpoint := "rest/api/2/field" + req, err := s.client.NewRequest("GET", apiEndpoint, nil) + if err != nil { + return nil, nil, err + } + + fieldList := []Field{} + resp, err := s.client.Do(req, &fieldList) + if err != nil { + return nil, resp, NewJiraError(resp, err) + } + return fieldList, resp, nil +} diff --git a/field_test.go b/field_test.go new file mode 100644 index 0000000..3121061 --- /dev/null +++ b/field_test.go @@ -0,0 +1,32 @@ +package jira + +import ( + "fmt" + "io/ioutil" + "net/http" + "testing" +) + +func TestFieldService_GetList(t *testing.T) { + setup() + defer teardown() + testAPIEdpoint := "/rest/api/2/field" + + raw, err := ioutil.ReadFile("./mocks/all_fields.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)) + }) + + fields, _, err := testClient.Field.GetList() + if fields == nil { + t.Error("Expected priority list. Priority list is nil") + } + if err != nil { + t.Errorf("Error given: %s", err) + } +} diff --git a/jira.go b/jira.go index 0cf2442..b6e6b6f 100644 --- a/jira.go +++ b/jira.go @@ -36,6 +36,7 @@ type Client struct { Group *GroupService Version *VersionService Priority *PriorityService + Field *FieldService } // NewClient returns a new JIRA API client. @@ -73,6 +74,7 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) { c.Group = &GroupService{client: c} c.Version = &VersionService{client: c} c.Priority = &PriorityService{client: c} + c.Field = &FieldService{client: c} return c, nil } diff --git a/mocks/all_fields.json b/mocks/all_fields.json new file mode 100644 index 0000000..554ef56 --- /dev/null +++ b/mocks/all_fields.json @@ -0,0 +1,33 @@ +[ + { + "id": "description", + "name": "Description", + "custom": false, + "orderable": true, + "navigable": true, + "searchable": true, + "clauseNames": [ + "description" + ], + "schema": { + "type": "string", + "system": "description" + } + }, + { + "id": "summary", + "key": "summary", + "name": "Summary", + "custom": false, + "orderable": true, + "navigable": true, + "searchable": true, + "clauseNames": [ + "summary" + ], + "schema": { + "type": "string", + "system": "summary" + } + } +]