Skip to content

Commit 433e65c

Browse files
committed
Add support for 'fields' API
1 parent ab73d57 commit 433e65c

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

field.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package jira
2+
3+
// PriorityService handles priorities for the JIRA instance / API.
4+
//
5+
// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-Priority
6+
type FieldService struct {
7+
client *Client
8+
}
9+
10+
// Priority represents a priority of a JIRA issue.
11+
// Typical types are "Normal", "Moderate", "Urgent", ...
12+
type Field struct {
13+
ID string `json:"id,omitempty" structs:"id,omitempty"`
14+
Key string `json:"key,omitempty" structs:"key,omitempty"`
15+
Name string `json:"name,omitempty" structs:"name,omitempty"`
16+
Custom bool `json:"custom,omitempty" structs:"custom,omitempty"`
17+
Navigable bool `json:"navigable,omitempty" structs:"navigable,omitempty"`
18+
Searchable bool `json:"searchable,omitempty" structs:"searchable,omitempty"`
19+
ClauseNames []string `json:"clauseNames,omitempty" structs:"clauseNames,omitempty"`
20+
Schema FieldSchema `json:"schema,omitempty" structs:"schema,omitempty"`
21+
}
22+
23+
type FieldSchema struct {
24+
Type string `json:"type,omitempty" structs:"type,omitempty"`
25+
System string `json:"system,omitempty" structs:"system,omitempty"`
26+
}
27+
28+
// GetList gets all fields from JIRA
29+
//
30+
// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-priority-get
31+
func (s *FieldService) GetList() ([]Field, *Response, error) {
32+
apiEndpoint := "rest/api/2/field"
33+
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
34+
if err != nil {
35+
return nil, nil, err
36+
}
37+
38+
fieldList := []Field{}
39+
resp, err := s.client.Do(req, &fieldList)
40+
if err != nil {
41+
return nil, resp, NewJiraError(resp, err)
42+
}
43+
return fieldList, resp, nil
44+
}

field_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package jira
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"testing"
8+
)
9+
10+
func TestFieldService_GetList(t *testing.T) {
11+
setup()
12+
defer teardown()
13+
testAPIEdpoint := "/rest/api/2/field"
14+
15+
raw, err := ioutil.ReadFile("./mocks/all_fields.json")
16+
if err != nil {
17+
t.Error(err.Error())
18+
}
19+
testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) {
20+
testMethod(t, r, "GET")
21+
testRequestURL(t, r, testAPIEdpoint)
22+
fmt.Fprint(w, string(raw))
23+
})
24+
25+
fields, _, err := testClient.Field.GetList()
26+
if fields == nil {
27+
t.Error("Expected priority list. Priority list is nil")
28+
}
29+
if err != nil {
30+
t.Errorf("Error given: %s", err)
31+
}
32+
}

jira.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Client struct {
3636
Group *GroupService
3737
Version *VersionService
3838
Priority *PriorityService
39+
Field *FieldService
3940
}
4041

4142
// NewClient returns a new JIRA API client.
@@ -73,6 +74,7 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) {
7374
c.Group = &GroupService{client: c}
7475
c.Version = &VersionService{client: c}
7576
c.Priority = &PriorityService{client: c}
77+
c.Field = &FieldService{client: c}
7678

7779
return c, nil
7880
}

mocks/all_fields.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{
3+
"id": "description",
4+
"name": "Description",
5+
"custom": false,
6+
"orderable": true,
7+
"navigable": true,
8+
"searchable": true,
9+
"clauseNames": [
10+
"description"
11+
],
12+
"schema": {
13+
"type": "string",
14+
"system": "description"
15+
}
16+
},
17+
{
18+
"id": "summary",
19+
"key": "summary",
20+
"name": "Summary",
21+
"custom": false,
22+
"orderable": true,
23+
"navigable": true,
24+
"searchable": true,
25+
"clauseNames": [
26+
"summary"
27+
],
28+
"schema": {
29+
"type": "string",
30+
"system": "summary"
31+
}
32+
}
33+
]

0 commit comments

Comments
 (0)