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

feat: Add ComponentService.Update function #679

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion cloud/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,25 @@ func (s *ComponentService) Get(ctx context.Context, componentID string) (*Projec
return component, resp, nil
}

// TODO Add "Update component" method. See https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-components/#api-rest-api-3-component-id-put
// Update updates an existing component.
//
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-components/#api-rest-api-3-component-id-put
func (s *ComponentService) Update(ctx context.Context, component *ProjectComponent) (*ProjectComponent, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/3/component/%s", component.ID)
req, err := s.client.NewRequest(ctx, http.MethodPut, apiEndpoint, component)
if err != nil {
return nil, nil, err
}

// Returning the same pointer here is pointless, we return the component from the response instead.
updatedComponent := new(ProjectComponent)
resp, err := s.client.Do(req, updatedComponent)
if err != nil {
return nil, resp, NewJiraError(resp, err)
}

return updatedComponent, resp, nil
}

// TODO Add "Delete component" method. See https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-components/#api-rest-api-3-component-id-delete

Expand Down
38 changes: 38 additions & 0 deletions cloud/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -32,6 +33,43 @@ func TestComponentService_Create_Success(t *testing.T) {
}
}

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

// Mock simple component with a concrete name
component := ProjectComponent{}
component.ID = "42102"
component.Name = "Brand new component"

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

componentUpdated, _, err := testClient.Component.Update(context.Background(), &component)

// Now we check, that the received component contains values directly from the server response
// updatedNameFromServerResponse = "Some Component UPDATED" (see the ../testing/mock-data/component_updated.json file)
if componentUpdated == nil {
t.Error("Expected component. Component is nil")
}
if err != nil {
t.Errorf("Error given: %s", err)
}

// Check that "UPDATED" is inside the name, see the ../testing/mock-data/component_updated.json file
if !strings.Contains(componentUpdated.Name, "UPDATED") {
t.Errorf("Expected 'UPDATED' in name. Got %s", componentUpdated.Name)
}
}

func TestComponentService_Get(t *testing.T) {
setup()
defer teardown()
Expand Down
47 changes: 47 additions & 0 deletions cloud/examples/component_update/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"fmt"

jira "github.com/andygrunwald/go-jira/v2/cloud"
)

func main() {
jiraURL := "https://go-jira-opensource.atlassian.net/"

// Jira docs: https://support.atlassian.com/atlassian-account/docs/manage-api-tokens-for-your-atlassian-account/
// Create a new API token: https://id.atlassian.com/manage-profile/security/api-tokens
tp := jira.BasicAuthTransport{
Username: "<username>",
APIToken: "<api-token>",
}
client, err := jira.NewClient(jiraURL, tp.Client())
if err != nil {
panic(err)
}

component, _, err := client.Component.Get(context.Background(), "10000")
if err != nil {
panic(err)
}
if component == nil {
fmt.Println("Component not found")
}

// Update component
component.Name = "New Name"

updatedComponent, _, err := client.Component.Update(context.Background(), component)
if err != nil {
panic(err)
}

// updatedComponent SHOULD -in-theory- be the same as the component sent to the update method
if updatedComponent.Name != component.Name {
fmt.Println("This should not happen, received component different from the sent one!")
}

fmt.Printf("Updated component: %+v\n", updatedComponent)
fmt.Println("Success!")
}
50 changes: 50 additions & 0 deletions testing/mock-data/component_updated.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 UPDATED",
"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
}