diff --git a/issue.go b/issue.go index 3537e04..d970fd6 100644 --- a/issue.go +++ b/issue.go @@ -558,6 +558,23 @@ func (s *IssueService) PostAttachment(issueID string, r io.Reader, attachmentNam return attachment, 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. +// +// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue/{issueIdOrKey}/worklog-getIssueWorklog +func (s *IssueService) GetWorklogs(issueID string) (*Worklog, *Response, error) { + apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s/worklog", issueID) + + req, err := s.client.NewRequest("GET", apiEndpoint, nil) + if err != nil { + return nil, nil, err + } + + v := new(Worklog) + resp, err := s.client.Do(req, v) + return v, resp, err +} + // Create creates an issue or a sub-task from a JSON representation. // Creating a sub-task is similar to creating a regular issue, with two important differences: // The issueType field must correspond to a sub-task issue type and you must provide a parent field in the issue create request containing the id or key of the parent issue. diff --git a/issue_test.go b/issue_test.go index cfe3dce..8e1203a 100644 --- a/issue_test.go +++ b/issue_test.go @@ -1148,3 +1148,31 @@ func TestIssueService_Delete(t *testing.T) { t.Errorf("Error given: %s", err) } } + +func TestIssueService_GetWorklogs(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/api/2/issue/10002/worklog", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testRequestURL(t, r, "/rest/api/2/issue/10002/worklog") + + fmt.Fprint(w, `{"startAt": 1,"maxResults": 40,"total": 1,"worklogs": [{"id": "3","self": "http://kelpie9:8081/rest/api/2/issue/10002/worklog/3","author":{"self":"http://www.example.com/jira/rest/api/2/user?username=fred","name":"fred","displayName":"Fred F. User","active":false},"updateAuthor":{"self":"http://www.example.com/jira/rest/api/2/user?username=fred","name":"fred","displayName":"Fred F. User","active":false},"created":"2016-03-16T04:22:37.356+0000","updated":"2016-03-16T04:22:37.356+0000","comment":"","started":"2016-03-16T04:22:37.356+0000","timeSpent": "1h","timeSpentSeconds": 3600,"issueId":"10002"}]}`) + }) + + worklog, _, err := testClient.Issue.GetWorklogs("10002") + if worklog == nil { + t.Error("Expected worklog. Worklog is nil") + } + + if len(worklog.Worklogs) != 1 { + t.Error("Expected 1 worklog") + } + + if worklog.Worklogs[0].Author.Name != "fred" { + t.Error("Expected worklog author to be fred") + } + + if err != nil { + t.Errorf("Error given: %s", err) + } +}