Skip to content

Commit 2671c36

Browse files
authored
Merge pull request #583 from andygrunwald/followup-389
Cloud/Components: Add ComponentService.Get
2 parents 6c76fac + 28d994e commit 2671c36

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

cloud/component.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cloud
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
)
78

@@ -41,3 +42,22 @@ func (s *ComponentService) Create(ctx context.Context, options *CreateComponentO
4142

4243
return component, resp, nil
4344
}
45+
46+
// Get returns a component for the given componentID.
47+
//
48+
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-components/#api-rest-api-3-component-id-get
49+
func (s *ComponentService) Get(ctx context.Context, componentID string) (*ProjectComponent, *Response, error) {
50+
apiEndpoint := fmt.Sprintf("rest/api/3/component/%s", componentID)
51+
req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil)
52+
if err != nil {
53+
return nil, nil, err
54+
}
55+
56+
component := new(ProjectComponent)
57+
resp, err := s.client.Do(req, component)
58+
if err != nil {
59+
return nil, resp, NewJiraError(resp, err)
60+
}
61+
62+
return component, resp, nil
63+
}

cloud/component_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"os"
78
"testing"
89
)
910

@@ -28,3 +29,51 @@ func TestComponentService_Create_Success(t *testing.T) {
2829
t.Errorf("Error given: %s", err)
2930
}
3031
}
32+
33+
func TestComponentService_Get(t *testing.T) {
34+
setup()
35+
defer teardown()
36+
testAPIEndpoint := "/rest/api/3/component/42102"
37+
38+
raw, err := os.ReadFile("../testing/mock-data/component_get.json")
39+
if err != nil {
40+
t.Error(err.Error())
41+
}
42+
testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
43+
testMethod(t, r, "GET")
44+
testRequestURL(t, r, testAPIEndpoint)
45+
fmt.Fprint(w, string(raw))
46+
})
47+
48+
component, _, err := testClient.Component.Get(context.Background(), "42102")
49+
if err != nil {
50+
t.Errorf("Error given: %s", err)
51+
}
52+
if component == nil {
53+
t.Error("Expected component. Component is nil")
54+
}
55+
}
56+
57+
func TestComponentService_Get_NoComponent(t *testing.T) {
58+
setup()
59+
defer teardown()
60+
testAPIEndpoint := "/rest/api/3/component/99999999"
61+
62+
testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
63+
testMethod(t, r, "GET")
64+
testRequestURL(t, r, testAPIEndpoint)
65+
fmt.Fprint(w, nil)
66+
})
67+
68+
component, resp, err := testClient.Component.Get(context.Background(), "99999999")
69+
70+
if component != nil {
71+
t.Errorf("Expected nil. Got %+v", component)
72+
}
73+
if resp.Status == "404" {
74+
t.Errorf("Expected status 404. Got %s", resp.Status)
75+
}
76+
if err == nil {
77+
t.Error("No error given. Expected one")
78+
}
79+
}

testing/mock-data/component_get.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"self": "https://issues.apache.org/jira/rest/api/2/component/42102",
3+
"id": "42102",
4+
"name": "Some Component",
5+
"lead": {
6+
"self": "https://issues.apache.org/jira/rest/api/2/[email protected]",
7+
"key": "firstname.lastname",
8+
"name": "[email protected]",
9+
"avatarUrls": {
10+
"48x48": "https://issues.apache.org/jira/secure/useravatar?ownerId=firstname.lastname&avatarId=31851",
11+
"24x24": "https://issues.apache.org/jira/secure/useravatar?size=small&ownerId=firstname.lastname&avatarId=31851",
12+
"16x16": "https://issues.apache.org/jira/secure/useravatar?size=xsmall&ownerId=firstname.lastname&avatarId=31851",
13+
"32x32": "https://issues.apache.org/jira/secure/useravatar?size=medium&ownerId=firstname.lastname&avatarId=31851"
14+
},
15+
"displayName": "Firstname Lastname",
16+
"active": true
17+
},
18+
"assigneeType": "COMPONENT_LEAD",
19+
"assignee": {
20+
"self": "https://issues.apache.org/jira/rest/api/2/[email protected]",
21+
"key": "firstname.lastname",
22+
"name": "[email protected]",
23+
"avatarUrls": {
24+
"48x48": "https://issues.apache.org/jira/secure/useravatar?ownerId=firstname.lastname&avatarId=31851",
25+
"24x24": "https://issues.apache.org/jira/secure/useravatar?size=small&ownerId=firstname.lastname&avatarId=31851",
26+
"16x16": "https://issues.apache.org/jira/secure/useravatar?size=xsmall&ownerId=firstname.lastname&avatarId=31851",
27+
"32x32": "https://issues.apache.org/jira/secure/useravatar?size=medium&ownerId=firstname.lastname&avatarId=31851"
28+
},
29+
"displayName": "Firstname Lastname",
30+
"active": true
31+
},
32+
"realAssigneeType": "COMPONENT_LEAD",
33+
"realAssignee": {
34+
"self": "https://issues.apache.org/jira/rest/api/2/[email protected]",
35+
"key": "firstname.lastname",
36+
"name": "[email protected]",
37+
"avatarUrls": {
38+
"48x48": "https://issues.apache.org/jira/secure/useravatar?ownerId=firstname.lastname&avatarId=31851",
39+
"24x24": "https://issues.apache.org/jira/secure/useravatar?size=small&ownerId=firstname.lastname&avatarId=31851",
40+
"16x16": "https://issues.apache.org/jira/secure/useravatar?size=xsmall&ownerId=firstname.lastname&avatarId=31851",
41+
"32x32": "https://issues.apache.org/jira/secure/useravatar?size=medium&ownerId=firstname.lastname&avatarId=31851"
42+
},
43+
"displayName": "Firstname Lastname",
44+
"active": true
45+
},
46+
"isAssigneeTypeValid": true,
47+
"project": "ABC",
48+
"projectId": 12345,
49+
"archived": false
50+
}

0 commit comments

Comments
 (0)