From e25bafc579e300cb958864c8a5b25ec991e59ceb Mon Sep 17 00:00:00 2001 From: Oleh Roshnivskyi Date: Sat, 21 Jul 2018 23:42:58 +0300 Subject: [PATCH] Add CreatedTime() (time.Time, error) func to ChangelogHistory --- issue.go | 10 ++++++++++ issue_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/issue.go b/issue.go index 0949ea1..e308a4b 100644 --- a/issue.go +++ b/issue.go @@ -1088,3 +1088,13 @@ func (s *IssueService) RemoveWatcher(issueID string, userName string) (*Response return resp, err } + +func (c ChangelogHistory) CreatedTime() (time.Time, error) { + var t time.Time + // Ignore null + if string(c.Created) == "null" { + return t, nil + } + t, err := time.Parse("2006-01-02T15:04:05.999-0700", c.Created) + return t, err +} diff --git a/issue_test.go b/issue_test.go index 8a378b7..adbfd01 100644 --- a/issue_test.go +++ b/issue_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/trivago/tgo/tcontainer" + "time" ) func TestIssueService_Get_Success(t *testing.T) { @@ -1308,3 +1309,34 @@ func TestIssueService_GetWatchers(t *testing.T) { t.Error("Expected watcher name fred") } } + + +func TestIssueService_Get_Fields_Changelog(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/api/2/issue/10002", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testRequestURL(t, r, "/rest/api/2/issue/10002") + + fmt.Fprint(w, `{"expand":"changelog","id":"10002","self":"http://www.example.com/jira/rest/api/2/issue/10002","key":"EX-1","changelog":{"startAt": 0,"maxResults": 1, "total": 1, "histories": [{"id": "10002", "author": {"self": "http://www.example.com/jira/rest/api/2/user?username=fred", "name": "fred", "key": "fred", "emailAddress": "fred@example.com", "avatarUrls": {"48x48": "http://www.example.com/secure/useravatar?ownerId=fred&avatarId=33072", "24x24": "http://www.example.com/secure/useravatar?size=small&ownerId=fred&avatarId=33072", "16x16": "http://www.example.com/secure/useravatar?size=xsmall&ownerId=fred&avatarId=33072", "32x32": "http://www.example.com/secure/useravatar?size=medium&ownerId=fred&avatarId=33072"},"displayName":"Fred","active": true,"timeZone":"Australia/Sydney"},"created":"2018-06-20T16:50:35.000+0300","items":[{"field":"Rank","fieldtype":"custom","from":"","fromString":"","to":"","toString":"Ranked higher"}]}]}}`) + }) + + issue, _, _ := testClient.Issue.Get("10002", &GetQueryOptions{Expand:"changelog"}) + if issue == nil { + t.Error("Expected issue. Issue is nil") + } + + if len(issue.Changelog.Histories) != 1 { + t.Errorf("Expected one history item, %v found", len(issue.Changelog.Histories)) + } + + if issue.Changelog.Histories[0].Created != "2018-06-20T16:50:35.000+0300" { + t.Errorf("Expected created time of history item 2018-06-20T16:50:35.000+0300, %v got", issue.Changelog.Histories[0].Created) + } + + tm, _ := time.Parse("2006-01-02T15:04:05.999-0700", "2018-06-20T16:50:35.000+0300") + + if ct, _ := issue.Changelog.Histories[0].CreatedTime(); ct != tm { + t.Errorf("Expected CreatedTime func return 2018-06-20T16:50:35.000+0300 time, %v got", ct) + } +}