Skip to content

Commit a6efe39

Browse files
lily-maraNate Mara
authored andcommitted
Add ComponentService to allow creation of Components
1 parent 98a84a4 commit a6efe39

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

component.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package jira
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
)
8+
9+
// ComponentService handles components for the JIRA instance / API.
10+
//
11+
// JIRA API docs: https://docs.atlassian.com/software/jira/docs/api/REST/7.10.1/#api/2/component
12+
type ComponentService struct {
13+
client *Client
14+
}
15+
16+
// CreateComponentOptions are passed to the ComponentService.Create function to create a new JIRA component
17+
type CreateComponentOptions struct {
18+
Name string
19+
Description string
20+
LeadUserName string
21+
AssigneeType string
22+
Lead *User
23+
Assignee *User
24+
Project string
25+
ProjectID *int
26+
}
27+
28+
// FullComponent is a JIRA component with all details filled in
29+
type FullComponent struct {
30+
Name string `json:"name,omitempty" structs:"name,omitempty"`
31+
Description string `json:"description,omitempty" structs:"description,omitempty"`
32+
Lead *User `json:"lead,omitempty" structs:"lead,omitempty"`
33+
Assignee *User `json:"assignee,omitempty" structs:"assignee,omitempty"`
34+
LeadUserName string `json:"leadUserName,omitempty" structs:"leadUserName,omitempty"`
35+
AssigneeType string `json:"assigneeType,omitempty" structs:"assigneeType,omitempty"`
36+
RealAssigneeType string `json:"realAssigneeType,omitempty" structs:"realAssigneeType,omitempty"`
37+
RealAssignee *User `json:"realAssignee,omitempty" structs:"realAssignee,omitempty"`
38+
IsAssigneeTypeValid bool `json:"isAssigneeTypeValid,omitempty" structs:"isAssigneeTypeValid,omitempty"`
39+
Project string `json:"project,omitempty" structs:"project,omitempty"`
40+
ProjectID int `json:"projectId,omitempty" structs:"projectId,omitempty"`
41+
}
42+
43+
// Create creates a new JIRA component based on the given options.
44+
func (s *ComponentService) Create(options *CreateComponentOptions) (*FullComponent, *Response, error) {
45+
apiEndpoint := "rest/api/2/component"
46+
req, err := s.client.NewRequest("POST", apiEndpoint, options)
47+
if err != nil {
48+
return nil, nil, err
49+
}
50+
resp, err := s.client.Do(req, nil)
51+
if err != nil {
52+
// incase of error return the resp for further inspection
53+
return nil, resp, err
54+
}
55+
56+
component := new(FullComponent)
57+
defer resp.Body.Close()
58+
data, err := ioutil.ReadAll(resp.Body)
59+
if err != nil {
60+
return nil, resp, fmt.Errorf("Could not read the returned data")
61+
}
62+
err = json.Unmarshal(data, component)
63+
if err != nil {
64+
return nil, resp, fmt.Errorf("Could not unmarshall the data into struct")
65+
}
66+
return component, resp, nil
67+
}

component_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package jira
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
)
8+
9+
func TestComponentService_Create_Success(t *testing.T) {
10+
setup()
11+
defer teardown()
12+
testMux.HandleFunc("/rest/api/2/component", func(w http.ResponseWriter, r *http.Request) {
13+
testMethod(t, r, "POST")
14+
testRequestURL(t, r, "/rest/api/2/component")
15+
16+
w.WriteHeader(http.StatusCreated)
17+
fmt.Fprint(w, `{ "self": "http://www.example.com/jira/rest/api/2/component/10000", "id": "10000", "name": "Component 1", "description": "This is a JIRA component", "lead": { "self": "http://www.example.com/jira/rest/api/2/user?username=fred", "name": "fred", "avatarUrls": { "48x48": "http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred", "24x24": "http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred", "16x16": "http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred", "32x32": "http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred" }, "displayName": "Fred F. User", "active": false }, "assigneeType": "PROJECT_LEAD", "assignee": { "self": "http://www.example.com/jira/rest/api/2/user?username=fred", "name": "fred", "avatarUrls": { "48x48": "http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred", "24x24": "http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred", "16x16": "http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred", "32x32": "http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred" }, "displayName": "Fred F. User", "active": false }, "realAssigneeType": "PROJECT_LEAD", "realAssignee": { "self": "http://www.example.com/jira/rest/api/2/user?username=fred", "name": "fred", "avatarUrls": { "48x48": "http://www.example.com/jira/secure/useravatar?size=large&ownerId=fred", "24x24": "http://www.example.com/jira/secure/useravatar?size=small&ownerId=fred", "16x16": "http://www.example.com/jira/secure/useravatar?size=xsmall&ownerId=fred", "32x32": "http://www.example.com/jira/secure/useravatar?size=medium&ownerId=fred" }, "displayName": "Fred F. User", "active": false }, "isAssigneeTypeValid": false, "project": "HSP", "projectId": 10000 }`)
18+
})
19+
20+
component, _, err := testClient.Component.Create(&CreateComponentOptions{
21+
Name: "foo-bar",
22+
})
23+
if component == nil {
24+
t.Error("Expected component. Component is nil")
25+
}
26+
if err != nil {
27+
t.Errorf("Error given: %s", err)
28+
}
29+
}

jira.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Client struct {
3737
Version *VersionService
3838
Priority *PriorityService
3939
Field *FieldService
40+
Component *ComponentService
4041
}
4142

4243
// NewClient returns a new JIRA API client.
@@ -75,6 +76,7 @@ func NewClient(httpClient *http.Client, baseURL string) (*Client, error) {
7576
c.Version = &VersionService{client: c}
7677
c.Priority = &PriorityService{client: c}
7778
c.Field = &FieldService{client: c}
79+
c.Component = &ComponentService{client: c}
7880

7981
return c, nil
8082
}

0 commit comments

Comments
 (0)