Skip to content

Commit

Permalink
Merge pull request #53 from massakurai/add-delete-issue
Browse files Browse the repository at this point in the history
Support deleting issue API
  • Loading branch information
kenzo0107 authored Apr 26, 2024
2 parents 44e2d1d + 77a2555 commit 61eb940
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
21 changes: 21 additions & 0 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,27 @@ func (c *Client) UpdateIssueContext(ctx context.Context, issueIDOrKey string, in
return issue, nil
}

// DeleteIssue deletes an issue
func (c *Client) DeleteIssue(issueIDOrKey string) (*Issue, error) {
return c.DeleteIssueContext(context.Background(), issueIDOrKey)
}

// DeleteIssueContext deletes an issue with context
func (c *Client) DeleteIssueContext(ctx context.Context, issueIDOrKey string) (*Issue, error) {
u := fmt.Sprintf("/api/v2/issues/%v", issueIDOrKey)

req, err := c.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}

issue := new(Issue)
if err := c.Do(ctx, req, &issue); err != nil {
return nil, err
}
return issue, nil
}

// GetIssueComments get list of the issue comments
func (c *Client) GetIssueComments(issueIDOrKey string, opts *GetIssueCommentsOptions) ([]*IssueComment, error) {
return c.GetIssueCommentsContext(context.Background(), issueIDOrKey, opts)
Expand Down
46 changes: 46 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,52 @@ func TestUpdateIssueInvalidIssueKey(t *testing.T) {
}
}

func TestDeleteIssue(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/issues/BLG-1", func(w http.ResponseWriter, r *http.Request) {
if _, err := fmt.Fprint(w, testJSONIssue); err != nil {
t.Fatal(err)
}
})

expected, err := client.DeleteIssue("BLG-1")
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}

want := getTestIssue()
if !reflect.DeepEqual(want, expected) {
t.Fatal(ErrIncorrectResponse, errors.New(pretty.Compare(want, expected)))
}
}

func TestDeleteIssueFailed(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/issues/BLG-1", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})

_, err := client.DeleteIssue("BLG-1")
if err == nil {
t.Fatal("expected an error but got none")
}
}

func TestDeleteIssueInvalidIssueKey(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()

_, err := client.DeleteIssue("%")
if err == nil {
t.Fatal("expected an error but got none")
}
}

func TestGetIssueComments(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down

0 comments on commit 61eb940

Please sign in to comment.