From b2766e3808b3b61e01a7e05d11f4d9ac3312354f Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Mon, 26 Aug 2024 21:46:34 -0500 Subject: [PATCH] feat: Add `IssueService.DeleteWorklogRecord` --- cloud/issue.go | 30 ++++++++++++++++++++++++++++++ cloud/issue_test.go | 17 +++++++++++++++++ onpremise/issue.go | 30 ++++++++++++++++++++++++++++++ onpremise/issue_test.go | 17 +++++++++++++++++ 4 files changed, 94 insertions(+) diff --git a/cloud/issue.go b/cloud/issue.go index 56d868ef..1e567eed 100644 --- a/cloud/issue.go +++ b/cloud/issue.go @@ -956,6 +956,36 @@ func (s *IssueService) DeleteComment(ctx context.Context, issueID, commentID str return nil } +// DeleteWorklogRecord Deletes a work log from an issueID. +// +// Jira API docs:https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-worklogs/#api-rest-api-3-issue-issueidorkey-worklog-id-delete +// +// TODO Double check this method if this works as expected, is using the latest API and the response is complete +// This double check effort is done for v2 - Remove this two lines if this is completed. +func (s *IssueService) DeleteWorklogRecord(ctx context.Context, issueID, worklogID string, options ...func(*http.Request) error) error { + apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s/worklog/%s", issueID, worklogID) + req, err := s.client.NewRequest(ctx, http.MethodDelete, apiEndpoint, nil) + if err != nil { + return err + } + + for _, option := range options { + err = option(req) + if err != nil { + return err + } + } + + resp, err := s.client.Do(req, nil) + if err != nil { + jerr := NewJiraError(resp, err) + return jerr + } + defer resp.Body.Close() + + return nil +} + // AddWorklogRecord adds a new worklog record to issueID. // // https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-issueIdOrKey-worklog-post diff --git a/cloud/issue_test.go b/cloud/issue_test.go index a8ad6487..6b23fd0a 100644 --- a/cloud/issue_test.go +++ b/cloud/issue_test.go @@ -250,6 +250,23 @@ func TestIssueService_DeleteComment(t *testing.T) { } } +func TestIssueService_DeleteWorklogRecord(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/api/2/issue/10000/worklog/10001", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodDelete) + testRequestURL(t, r, "/rest/api/2/issue/10000/worklog/10001") + + w.WriteHeader(http.StatusNoContent) + fmt.Fprint(w, `{}`) + }) + + err := testClient.Issue.DeleteWorklogRecord(context.Background(), "10000", "10001") + if err != nil { + t.Errorf("Error given: %s", err) + } +} + func TestIssueService_AddWorklogRecord(t *testing.T) { setup() defer teardown() diff --git a/onpremise/issue.go b/onpremise/issue.go index d1bca7aa..c25934b2 100644 --- a/onpremise/issue.go +++ b/onpremise/issue.go @@ -955,6 +955,36 @@ func (s *IssueService) DeleteComment(ctx context.Context, issueID, commentID str return nil } +// DeleteWorklogRecord Deletes a work log from an issueID. +// +// Jira API docs:https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-worklogs/#api-rest-api-3-issue-issueidorkey-worklog-id-delete +// +// TODO Double check this method if this works as expected, is using the latest API and the response is complete +// This double check effort is done for v2 - Remove this two lines if this is completed. +func (s *IssueService) DeleteWorklogRecord(ctx context.Context, issueID, worklogID string, options ...func(*http.Request) error) error { + apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s/worklog/%s", issueID, worklogID) + req, err := s.client.NewRequest(ctx, http.MethodDelete, apiEndpoint, nil) + if err != nil { + return err + } + + for _, option := range options { + err = option(req) + if err != nil { + return err + } + } + + resp, err := s.client.Do(req, nil) + if err != nil { + jerr := NewJiraError(resp, err) + return jerr + } + defer resp.Body.Close() + + return nil +} + // AddWorklogRecord adds a new worklog record to issueID. // // https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-issueIdOrKey-worklog-post diff --git a/onpremise/issue_test.go b/onpremise/issue_test.go index 36b6aa31..c037bd18 100644 --- a/onpremise/issue_test.go +++ b/onpremise/issue_test.go @@ -250,6 +250,23 @@ func TestIssueService_DeleteComment(t *testing.T) { } } +func TestIssueService_DeleteWorklogRecord(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/api/2/issue/10000/worklog/10001", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodDelete) + testRequestURL(t, r, "/rest/api/2/issue/10000/worklog/10001") + + w.WriteHeader(http.StatusNoContent) + fmt.Fprint(w, `{}`) + }) + + err := testClient.Issue.DeleteWorklogRecord(context.Background(), "10000", "10001") + if err != nil { + t.Errorf("Error given: %s", err) + } +} + func TestIssueService_AddWorklogRecord(t *testing.T) { setup() defer teardown()