Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ComponentService.Get methods #389

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion component.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jira

import "context"
import (
"context"
"fmt"
)

// ComponentService handles components for the Jira instance / API.//
// Jira API docs: https://docs.atlassian.com/software/jira/docs/api/REST/7.10.1/#api/2/component
Expand Down Expand Up @@ -42,3 +45,26 @@ func (s *ComponentService) CreateWithContext(ctx context.Context, options *Creat
func (s *ComponentService) Create(options *CreateComponentOptions) (*ProjectComponent, *Response, error) {
return s.CreateWithContext(context.Background(), options)
}

// GetWithContext returns a full representation of the JIRA component for the given component ID.
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-components/
func (s *ComponentService) GetWithContext(ctx context.Context, componentID string) (*ProjectComponent, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/2/component/%s", componentID)
req, err := s.client.NewRequestWithContext(ctx, "GET", 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
}

// Get wraps GetWithContext using the background context.
func (s *ComponentService) Get(componentID string) (*ProjectComponent, *Response, error) {
return s.GetWithContext(context.Background(), componentID)
}
50 changes: 50 additions & 0 deletions component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jira

import (
"fmt"
"io/ioutil"
"net/http"
"testing"
)
Expand All @@ -27,3 +28,52 @@ func TestComponentService_Create_Success(t *testing.T) {
t.Errorf("Error given: %s", err)
}
}

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

raw, err := ioutil.ReadFile("./mocks/component.json")
if err != nil {
t.Error(err.Error())
}
testMux.HandleFunc(testAPIEdpoint, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testRequestURL(t, r, testAPIEdpoint)
fmt.Fprint(w, string(raw))
})

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

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

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

component, resp, err := testClient.Component.Get("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.Errorf("Error given: %s", err)
}
}
50 changes: 50 additions & 0 deletions mocks/component.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
}