Repository: andygrunwald/go-jira
Pull request #583 (2022-10-19 17:54:38)

Cloud/Components: Add ComponentService.Get
Thanks goes to @krrishd for his early work in
https://github.com/andygrunwald/go-jira/pull/389


Patch:
diff --git a/cloud/component.go b/cloud/component.go
index bac214c..66eb604 100644
--- a/cloud/component.go
+++ b/cloud/component.go
@@ -2,6 +2,7 @@ package cloud
 
 import (
 	"context"
+	"fmt"
 	"net/http"
 )
 
@@ -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
+}


Test patch:
diff --git a/cloud/component_test.go b/cloud/component_test.go
index 780cfdb..4bba036 100644
--- a/cloud/component_test.go
+++ b/cloud/component_test.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"net/http"
+	"os"
 	"testing"
 )
 
@@ -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 := os.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")
+	}
+}
diff --git a/testing/mock-data/component_get.json b/testing/mock-data/component_get.json
new file mode 100644
index 0000000..cda055d
--- /dev/null
+++ b/testing/mock-data/component_get.json
@@ -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/user?username=firstname.lastname@apache.org",
+      "key": "firstname.lastname",
+      "name": "firstname.lastname@apache.org",
+      "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/user?username=firstname.lastname@apache.org",
+      "key": "firstname.lastname",
+      "name": "firstname.lastname@apache.org",
+      "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/user?username=firstname.lastname@apache.org",
+      "key": "firstname.lastname",
+      "name": "firstname.lastname@apache.org",
+      "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
+  }
\ No newline at end of file