diff --git a/issuelinktype.go b/issuelinktype.go index 2116217..6bb6a4d 100644 --- a/issuelinktype.go +++ b/issuelinktype.go @@ -1,6 +1,10 @@ package jira -import "fmt" +import ( + "encoding/json" + "fmt" + "io/ioutil" +) // IssueLinkTypeService handles issue link types for the JIRA instance / API. // @@ -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 +} diff --git a/issuelinktype_test.go b/issuelinktype_test.go index fd23b92..ef25d7b 100644 --- a/issuelinktype_test.go +++ b/issuelinktype_test.go @@ -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") + } +}