Skip to content

Commit

Permalink
Cloud/Components: Add ComponentService.Get
Browse files Browse the repository at this point in the history
Thanks goes to @krrishd for his early work in
#389
  • Loading branch information
andygrunwald committed Oct 19, 2022
1 parent 6c76fac commit 5d84a84
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cloud/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cloud

import (
"context"
"fmt"
"net/http"
)

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

return component, resp, nil
}

// Get returns a component for the given componentID.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-components/#api-rest-api-3-component-id-get
func (s *ComponentService) Get(ctx context.Context, componentID string) (*ProjectComponent, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/3/component/%s", componentID)
req, err := s.client.NewRequest(ctx, http.MethodGet, apiEndpoint, nil)
if err != nil {
return nil, nil, err
}

component := new(ProjectComponent)
resp, err := s.client.Do(req, component)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}

return component, resp, nil
}
49 changes: 49 additions & 0 deletions cloud/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
)
Expand All @@ -28,3 +29,51 @@ func TestComponentService_Create_Success(t *testing.T) {
t.Errorf("Error given: %s", err)
}
}

func TestComponentService_Get(t *testing.T) {
setup()
defer teardown()
testAPIEndpoint := "/rest/api/3/component/42102"

raw, err := ioutil.ReadFile("../testing/mock-data/component_get.json")
if err != nil {
t.Error(err.Error())
}
testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testRequestURL(t, r, testAPIEndpoint)
fmt.Fprint(w, string(raw))
})

component, _, err := testClient.Component.Get(context.Background(), "42102")
if err != nil {
t.Errorf("Error given: %s", err)
}
if component == nil {
t.Error("Expected component. Component is nil")
}
}

func TestComponentService_Get_NoComponent(t *testing.T) {
setup()
defer teardown()
testAPIEndpoint := "/rest/api/3/component/99999999"

testMux.HandleFunc(testAPIEndpoint, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testRequestURL(t, r, testAPIEndpoint)
fmt.Fprint(w, nil)
})

component, resp, err := testClient.Component.Get(context.Background(), "99999999")

if component != nil {
t.Errorf("Expected nil. Got %+v", component)
}
if resp.Status == "404" {
t.Errorf("Expected status 404. Got %s", resp.Status)
}
if err == nil {
t.Error("No error given. Expected one")
}
}
50 changes: 50 additions & 0 deletions testing/mock-data/component_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"self": "https://issues.apache.org/jira/rest/api/2/component/42102",
"id": "42102",
"name": "Some Component",
"lead": {
"self": "https://issues.apache.org/jira/rest/api/2/[email protected]",
"key": "firstname.lastname",
"name": "[email protected]",
"avatarUrls": {
"48x48": "https://issues.apache.org/jira/secure/useravatar?ownerId=firstname.lastname&avatarId=31851",
"24x24": "https://issues.apache.org/jira/secure/useravatar?size=small&ownerId=firstname.lastname&avatarId=31851",
"16x16": "https://issues.apache.org/jira/secure/useravatar?size=xsmall&ownerId=firstname.lastname&avatarId=31851",
"32x32": "https://issues.apache.org/jira/secure/useravatar?size=medium&ownerId=firstname.lastname&avatarId=31851"
},
"displayName": "Firstname Lastname",
"active": true
},
"assigneeType": "COMPONENT_LEAD",
"assignee": {
"self": "https://issues.apache.org/jira/rest/api/2/[email protected]",
"key": "firstname.lastname",
"name": "[email protected]",
"avatarUrls": {
"48x48": "https://issues.apache.org/jira/secure/useravatar?ownerId=firstname.lastname&avatarId=31851",
"24x24": "https://issues.apache.org/jira/secure/useravatar?size=small&ownerId=firstname.lastname&avatarId=31851",
"16x16": "https://issues.apache.org/jira/secure/useravatar?size=xsmall&ownerId=firstname.lastname&avatarId=31851",
"32x32": "https://issues.apache.org/jira/secure/useravatar?size=medium&ownerId=firstname.lastname&avatarId=31851"
},
"displayName": "Firstname Lastname",
"active": true
},
"realAssigneeType": "COMPONENT_LEAD",
"realAssignee": {
"self": "https://issues.apache.org/jira/rest/api/2/[email protected]",
"key": "firstname.lastname",
"name": "[email protected]",
"avatarUrls": {
"48x48": "https://issues.apache.org/jira/secure/useravatar?ownerId=firstname.lastname&avatarId=31851",
"24x24": "https://issues.apache.org/jira/secure/useravatar?size=small&ownerId=firstname.lastname&avatarId=31851",
"16x16": "https://issues.apache.org/jira/secure/useravatar?size=xsmall&ownerId=firstname.lastname&avatarId=31851",
"32x32": "https://issues.apache.org/jira/secure/useravatar?size=medium&ownerId=firstname.lastname&avatarId=31851"
},
"displayName": "Firstname Lastname",
"active": true
},
"isAssigneeTypeValid": true,
"project": "ABC",
"projectId": 12345,
"archived": false
}

0 comments on commit 5d84a84

Please sign in to comment.