diff --git a/issue.go b/issue.go index 2e35001..d86edc8 100644 --- a/issue.go +++ b/issue.go @@ -649,6 +649,24 @@ func (s *IssueService) PostAttachment(issueID string, r io.Reader, attachmentNam return attachment, resp, nil } +// DeleteAttachment deletes an attachment of a given attachmentID +func (s *IssueService) DeleteAttachment(attachmentID string) (*Response, error) { + apiEndpoint := fmt.Sprintf("rest/api/2/attachment/%s", attachmentID) + + req, err := s.client.NewRequest("DELETE", apiEndpoint, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(req, nil) + if err != nil { + jerr := NewJiraError(resp, err) + return resp, jerr + } + + return resp, nil +} + // GetWorklogs gets all the worklogs for an issue. // This method is especially important if you need to read all the worklogs, not just the first page. // diff --git a/issue_test.go b/issue_test.go index 130127b..0ef27f6 100644 --- a/issue_test.go +++ b/issue_test.go @@ -539,6 +539,36 @@ func TestIssueService_PostAttachment_NoAttachment(t *testing.T) { } } +func TestIssueService_DeleteAttachment(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/api/2/attachment/10054", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + testRequestURL(t, r, "/rest/api/2/attachment/10054") + + w.WriteHeader(http.StatusNoContent) + fmt.Fprint(w, `{}`) + }) + + resp, err := testClient.Issue.DeleteAttachment("10054") + if resp.StatusCode != 204 { + t.Error("Expected attachment not deleted.") + if resp.StatusCode == 403 { + t.Error("User not permitted to delete attachment") + } + if resp.StatusCode == 404 { + t.Error("Attachment not found") + } + } else { + t.Log("Attachment deleted") + } + if err != nil { + t.Errorf("Error given: %s", err) + } else { + t.Log("No error") + } +} + func TestIssueService_Search(t *testing.T) { setup() defer teardown()