Skip to content

Commit

Permalink
feat: Implement issue link type POST
Browse files Browse the repository at this point in the history
  • Loading branch information
johanmeiring authored and ghostsquad committed Dec 12, 2019
1 parent 57538b9 commit 75b9df8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
36 changes: 35 additions & 1 deletion issuelinktype.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package jira

import "fmt"
import (
"encoding/json"
"fmt"
"io/ioutil"
)

// IssueLinkTypeService handles issue link types for the JIRA instance / API.
//
Expand Down Expand Up @@ -44,3 +48,33 @@ func (s *IssueLinkTypeService) Get(ID string) (*IssueLinkType, *Response, error)
}
return linkType, resp, nil
}

// Create creates an issue link type in JIRA.
//
// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-issueLinkType-post
func (s *IssueLinkTypeService) Create(linkType *IssueLinkType) (*IssueLinkType, *Response, error) {
apiEndpoint := "/rest/api/2/issueLinkType"
req, err := s.client.NewRequest("POST", apiEndpoint, linkType)
if err != nil {
return nil, nil, err
}

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

responseLinkType := new(IssueLinkType)
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
e := fmt.Errorf("Could not read the returned data")
return nil, resp, NewJiraError(resp, e)
}
err = json.Unmarshal(data, responseLinkType)
if err != nil {
e := fmt.Errorf("Could no unmarshal the data into struct")
return nil, resp, NewJiraError(resp, e)
}
return linkType, resp, nil
}
25 changes: 25 additions & 0 deletions issuelinktype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,28 @@ func TestIssueLinkTypeService_Get(t *testing.T) {
t.Error("Expected linkType. LinkType is nil")
}
}

func TestIssueLinkTypeService_Create(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/api/2/issueLinkType", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testRequestURL(t, r, "/rest/api/2/issueLinkType")

w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `{"id":"10021","name":"Problem/Incident","inward":"is caused by",
"outward":"causes","self":"https://www.example.com/jira/rest/api/2/issueLinkType/10021"}`)
})

lt := &IssueLinkType{
Name: "Problem/Incident",
Inward: "is caused by",
Outward: "causes",
}

if linkType, _, err := testClient.IssueLinkType.Create(lt); err != nil {
t.Errorf("Error given: %s", err)
} else if linkType == nil {
t.Error("Expected linkType. LinkType is nil")
}
}

0 comments on commit 75b9df8

Please sign in to comment.