Skip to content

Commit 2d45eae

Browse files
committed
Change Tempo API version from 3 to 4
1 parent 86fa9a0 commit 2d45eae

File tree

4 files changed

+47
-29
lines changed

4 files changed

+47
-29
lines changed

gojira/cli.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
"strconv"
78
"sync"
89
"time"
910

@@ -81,7 +82,7 @@ func NewWorklogIssues() error {
8182
for i := range app.workLogs.logs {
8283
waitGroup.Add(1)
8384
go func(workLog *Worklog) {
84-
issue, err := NewJiraClient().GetIssue(workLog.Issue.Key)
85+
issue, err := NewJiraClient().GetIssue(strconv.Itoa(workLog.Issue.Id))
8586
if err != nil {
8687
errCh <- err // Send the error to the channel.
8788
return
@@ -278,7 +279,7 @@ func (issue Issue) LogWork(logTime *time.Time, timeSpent string) error {
278279
}
279280
if Config.UpdateExistingWorklog {
280281
for index, workLog := range todayWorklog {
281-
if workLog.Issue.Key == issue.Key {
282+
if strconv.Itoa(workLog.Issue.Id) == issue.Id {
282283
timeSpentSum := FormatTimeSpent(TimeSpentToSeconds(timeSpent) + workLog.TimeSpentSeconds)
283284
err := todayWorklog[index].Update(timeSpentSum)
284285
if err != nil {
@@ -288,7 +289,7 @@ func (issue Issue) LogWork(logTime *time.Time, timeSpent string) error {
288289
}
289290
}
290291
}
291-
worklog, err := NewWorklog(issue.Key, logTime, timeSpent)
292+
worklog, err := NewWorklog(issue.GetIdAsInt(), logTime, timeSpent)
292293
if err != nil {
293294
return err
294295
}

gojira/jira.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/base64"
66
"encoding/json"
77
"fmt"
8+
"strconv"
89
"strings"
910
"time"
1011
)
@@ -57,6 +58,7 @@ type JQLResponse struct {
5758

5859
type Issue struct {
5960
Key string `json:"key"`
61+
Id string `json:"id"`
6062
Fields struct {
6163
Summary string `json:"summary"`
6264
Status struct {
@@ -65,6 +67,14 @@ type Issue struct {
6567
} `json:"fields"`
6668
}
6769

70+
func (issue Issue) GetIdAsInt() int {
71+
value, err := strconv.ParseInt(issue.Id, 10, 64)
72+
if err != nil {
73+
return 0
74+
}
75+
return int(value)
76+
}
77+
6878
type WorklogResponse struct {
6979
Self string `json:"self"`
7080
Author struct {
@@ -111,13 +121,19 @@ func (jc *JiraClient) GetLatestIssues() (JQLResponse, error) {
111121
return jc.GetIssuesByJQL("assignee in (currentUser()) ORDER BY updated DESC, created DESC", 10)
112122
}
113123

114-
func (jc *JiraClient) GetIssuesByKeys(issueKeys []string) (JQLResponse, error) {
115-
issueKeysJQL := fmt.Sprintf("key in (%s) ORDER BY updated DESC, created DESC", strings.Join(issueKeys, ","))
124+
func (jc *JiraClient) GetIssuesByKeys(issueKeys []int) (JQLResponse, error) {
125+
// Convert []int to []string
126+
issueKeysStr := make([]string, len(issueKeys))
127+
for i, key := range issueKeys {
128+
issueKeysStr[i] = fmt.Sprintf("%d", key)
129+
}
130+
issueKeysJQL := fmt.Sprintf("key in (%s) ORDER BY updated DESC, created DESC", strings.Join(issueKeysStr, ","))
116131
return jc.GetIssuesByJQL(issueKeysJQL, len(issueKeys))
117132
}
118133

119134
func (jc *JiraClient) GetIssue(issueKey string) (Issue, error) {
120-
requestUrl := fmt.Sprintf("%s/rest/api/2/issue/%s?fields=summary,status", Config.JiraUrl, issueKey)
135+
// issueKey could be JIRA-123 (key) or just 234235 (id)
136+
requestUrl := fmt.Sprintf("%s/rest/api/2/issue/%s?fields=summary,status,id", Config.JiraUrl, issueKey)
121137
response, err := SendHttpRequest("GET", requestUrl, nil, jc.getHttpHeaders(), 200)
122138
if err != nil {
123139
return Issue{}, err
@@ -130,15 +146,15 @@ func (jc *JiraClient) GetIssue(issueKey string) (Issue, error) {
130146
return jiraIssue, nil
131147
}
132148

133-
func (jc *JiraClient) CreateWorklog(issueKey string, logTime *time.Time, timeSpent string) (WorklogResponse, error) {
149+
func (jc *JiraClient) CreateWorklog(issueId int, logTime *time.Time, timeSpent string) (WorklogResponse, error) {
134150
payload := map[string]string{
135151
"timeSpent": FormatTimeSpent(TimeSpentToSeconds(timeSpent)),
136152
"adjustEstimate": "leave",
137153
"started": logTime.Format("2006-01-02T15:04:05.000-0700"),
138154
}
139155
payloadJson, _ := json.Marshal(payload)
140156
requestBody := bytes.NewBuffer(payloadJson)
141-
requestUrl := fmt.Sprintf("%s/rest/api/2/issue/%s/worklog?notifyUsers=false", Config.JiraUrl, issueKey)
157+
requestUrl := fmt.Sprintf("%s/rest/api/2/issue/%d/worklog?notifyUsers=false", Config.JiraUrl, issueId)
142158
response, err := SendHttpRequest("POST", requestUrl, requestBody, jc.getHttpHeaders(), 201)
143159
if err != nil {
144160
return WorklogResponse{}, err
@@ -152,21 +168,21 @@ func (jc *JiraClient) CreateWorklog(issueKey string, logTime *time.Time, timeSpe
152168
return workLogRequest, nil
153169
}
154170

155-
func (jc *JiraClient) UpdateWorklog(issueKey string, jiraWorklogId int, timeSpentInSeconds int) error {
171+
func (jc *JiraClient) UpdateWorklog(issueId int, jiraWorklogId int, timeSpentInSeconds int) error {
156172
payload := JiraWorklogUpdate{
157173
TimeSpentSeconds: timeSpentInSeconds,
158174
}
159175
payloadJson, _ := json.Marshal(payload)
160176
requestBody := bytes.NewBuffer(payloadJson)
161-
requestUrl := fmt.Sprintf("%s/rest/api/2/issue/%s/worklog/%d?notifyUsers=false",
162-
Config.JiraUrl, issueKey, jiraWorklogId)
177+
requestUrl := fmt.Sprintf("%s/rest/api/2/issue/%d/worklog/%d?notifyUsers=false",
178+
Config.JiraUrl, issueId, jiraWorklogId)
163179
_, err := SendHttpRequest("PUT", requestUrl, requestBody, jc.getHttpHeaders(), 200)
164180
return err
165181
}
166182

167-
func (jc *JiraClient) DeleteWorklog(issueKey string, jiraWorklogId int) error {
168-
requestUrl := fmt.Sprintf("%s/rest/api/2/issue/%s/worklog/%d?notifyUsers=false",
169-
Config.JiraUrl, issueKey, jiraWorklogId)
183+
func (jc *JiraClient) DeleteWorklog(issueId int, jiraWorklogId int) error {
184+
requestUrl := fmt.Sprintf("%s/rest/api/2/issue/%d/worklog/%d?notifyUsers=false",
185+
Config.JiraUrl, issueId, jiraWorklogId)
170186
_, err := SendHttpRequest("DELETE", requestUrl, nil, jc.getHttpHeaders(), 204)
171187
return err
172188
}

gojira/tempo.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"strconv"
78
"time"
89
)
910

@@ -26,7 +27,7 @@ type WorklogsResponse struct {
2627
}
2728

2829
type WorklogUpdateRequest struct {
29-
IssueKey string `json:"issueKey"`
30+
Id string `json:"id"`
3031
StartDate string `json:"startDate"`
3132
StartTime string `json:"startTime"`
3233
Description string `json:"description"`
@@ -36,7 +37,7 @@ type WorklogUpdateRequest struct {
3637

3738
func (tc *TempoClient) GetWorklogs(fromDate, toDate time.Time) (WorklogsResponse, error) {
3839
// tempo is required only because of fetching worklogs by date range
39-
requestUrl := fmt.Sprintf("%s/worklogs/account/%s?from=%s&to=%s&limit=1000",
40+
requestUrl := fmt.Sprintf("%s/worklogs/user/%s?from=%s&to=%s&limit=1000",
4041
tc.Url, tc.JiraAccountId, fromDate.Format(dateLayout), toDate.Format(dateLayout))
4142
headers := map[string]string{
4243
"Authorization": fmt.Sprintf("Bearer %s", tc.Token),
@@ -58,7 +59,7 @@ func (tc *TempoClient) UpdateWorklog(worklog *Worklog, timeSpent string) error {
5859
timeSpentInSeconds := TimeSpentToSeconds(timeSpent)
5960

6061
payload := WorklogUpdateRequest{
61-
IssueKey: worklog.Issue.Key,
62+
Id: strconv.Itoa(worklog.Issue.Id),
6263
StartDate: worklog.StartDate,
6364
StartTime: worklog.StartTime,
6465
Description: worklog.Description,

gojira/worklog.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"time"
1010
)
1111

12-
func NewWorklog(issueKey string, logTime *time.Time, timeSpent string) (Worklog, error) {
13-
workLogResponse, err := NewJiraClient().CreateWorklog(issueKey, logTime, timeSpent)
12+
func NewWorklog(issueId int, logTime *time.Time, timeSpent string) (Worklog, error) {
13+
workLogResponse, err := NewJiraClient().CreateWorklog(issueId, logTime, timeSpent)
1414
if err != nil {
1515
return Worklog{}, err
1616
}
@@ -26,8 +26,8 @@ func NewWorklog(issueKey string, logTime *time.Time, timeSpent string) (Worklog,
2626
StartTime: logTime.Format("15:04:05"),
2727
TimeSpentSeconds: workLogResponse.Timespentseconds,
2828
Issue: struct {
29-
Key string `json:"key"`
30-
}{Key: issueKey},
29+
Id int `json:"id"`
30+
}{Id: issueId},
3131
}
3232
return workLog, nil
3333
}
@@ -36,7 +36,7 @@ type Worklog struct {
3636
TempoWorklogid int `json:"tempoWorklogId"`
3737
JiraWorklogID int `json:"jiraWorklogId"`
3838
Issue struct {
39-
Key string `json:"key"`
39+
Id int `json:"id"`
4040
} `json:"issue"`
4141
TimeSpentSeconds int `json:"timeSpentSeconds"`
4242
StartDate string `json:"startDate"`
@@ -85,7 +85,7 @@ func (wl *Worklogs) LogsOnDate(date *time.Time) ([]*Worklog, error) {
8585

8686
func findWorklogByIssueKey(worklogs []*Worklog, issueKey string) *Worklog {
8787
for _, log := range worklogs {
88-
if log.Issue.Key == issueKey {
88+
if strconv.Itoa(log.Issue.Id) == issueKey {
8989
return log
9090
}
9191
}
@@ -94,14 +94,14 @@ func findWorklogByIssueKey(worklogs []*Worklog, issueKey string) *Worklog {
9494

9595
func GetIssuesWithWorklogs(worklogs []*Worklog) ([]Issue, error) {
9696
var err error
97-
var worklogIssuesKeys []string
97+
var worklogIssueIds []int
9898
for _, worklog := range worklogs {
99-
worklogIssuesKeys = append(worklogIssuesKeys, worklog.Issue.Key)
99+
worklogIssueIds = append(worklogIssueIds, worklog.Issue.Id)
100100
}
101-
if len(worklogIssuesKeys) == 0 {
101+
if len(worklogIssueIds) == 0 {
102102
return []Issue{}, err
103103
}
104-
todaysIssues, err := NewJiraClient().GetIssuesByKeys(worklogIssuesKeys)
104+
todaysIssues, err := NewJiraClient().GetIssuesByKeys(worklogIssueIds)
105105
if err != nil {
106106
return []Issue{}, err
107107
}
@@ -187,7 +187,7 @@ func (wl *Worklog) Update(timeSpent string) error {
187187
err = NewTempoClient().UpdateWorklog(wl, timeSpent)
188188
} else {
189189
// make update request to jira if tempoWorklogId is not set
190-
err = NewJiraClient().UpdateWorklog(wl.Issue.Key, wl.JiraWorklogID, timeSpentInSeconds)
190+
err = NewJiraClient().UpdateWorklog(wl.Issue.Id, wl.JiraWorklogID, timeSpentInSeconds)
191191
}
192192
if err != nil {
193193
return err
@@ -203,7 +203,7 @@ func (wl *Worklogs) Delete(w *Worklog) error {
203203
if w.TempoWorklogid != 0 {
204204
err = NewTempoClient().DeleteWorklog(w.TempoWorklogid)
205205
} else {
206-
err = NewJiraClient().DeleteWorklog(w.Issue.Key, w.JiraWorklogID)
206+
err = NewJiraClient().DeleteWorklog(w.Issue.Id, w.JiraWorklogID)
207207
}
208208
if err != nil {
209209
logrus.Debug(w)

0 commit comments

Comments
 (0)