-
Notifications
You must be signed in to change notification settings - Fork 2
/
jira.go
299 lines (276 loc) · 8.87 KB
/
jira.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package jiraworklog
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
)
//var errUnknownProject = errors.New("Unknown Project")
var ErrIssueNotFound = errors.New("Jira Issue not found")
type JiraReader interface {
WorklogsUpdated(timestamp int64) (UpdatedWorklogs, error)
WorklogDetails(ids []int) ([]Worklog, error)
Issue(idOrKey string) (Issue, error)
}
type Jira struct {
Config *Config
client *http.Client
}
func NewJira(c *Config) *Jira {
return &Jira{
Config: c,
client: &http.Client{},
}
}
func (j *Jira) WorklogsUpdated(timestamp int64) (UpdatedWorklogs, error) {
worklog := UpdatedWorklogs{}
since := ""
if timestamp > 0 {
since = "?since=" + strconv.FormatInt(timestamp, 10)
}
req, err := http.NewRequest("GET", j.Config.Jira.URL+"/worklog/updated"+since, nil)
req.SetBasicAuth(j.Config.Jira.Username, j.Config.Jira.Password)
resp, err := j.client.Do(req)
if err != nil {
return worklog, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return worklog, fmt.Errorf("Not 200 response %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&worklog)
if err != nil {
return worklog, err
}
return worklog, nil
}
func (j *Jira) WorklogDetails(ids []int) ([]Worklog, error) {
worklogList := WorklogList{
IDs: ids,
}
b, err := json.Marshal(worklogList)
if err != nil {
return nil, err
}
var worklogs []Worklog
req, err := http.NewRequest("POST", j.Config.Jira.URL+"/worklog/list", bytes.NewBuffer(b))
req.SetBasicAuth(j.Config.Jira.Username, j.Config.Jira.Password)
req.Header.Add("Content-Type", "application/json")
resp, err := j.client.Do(req)
if err != nil {
return worklogs, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return worklogs, fmt.Errorf("Not 200 response %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&worklogs)
if err != nil {
return worklogs, err
}
return worklogs, nil
}
func (j *Jira) Issue(idOrKey string) (Issue, error) {
issue := Issue{}
req, err := http.NewRequest("GET", j.Config.Jira.URL+"/issue/"+idOrKey+"?fields=priority,summary,parent,status,aggregateprogress,progress,issuetype,timespent,aggregatetimespent,timeoriginalestimate,aggregatetimeoriginalestimate,timetracking,resolutiondate,created,statuscategorychangedate", nil)
req.SetBasicAuth(j.Config.Jira.Username, j.Config.Jira.Password)
resp, err := j.client.Do(req)
if err != nil {
return issue, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
if resp.StatusCode == 404 {
return issue, ErrIssueNotFound
}
return issue, fmt.Errorf("Not 200 response %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&issue)
if err != nil {
return issue, err
}
return issue, nil
}
type UpdatedWorklogs struct {
Values []struct {
WorklogID int `json:"worklogId"`
UpdatedTime int64 `json:"updatedTime"`
Properties []interface{} `json:"properties"`
} `json:"values"`
Since int64 `json:"since"`
Until int64 `json:"until"`
Self string `json:"self"`
LastPage bool `json:"lastPage"`
}
type Worklog struct {
Self string `json:"self"`
Author struct {
Self string `json:"self"`
//Name string `json:"name"`
//Key string `json:"key"`
AccountID string `json:"accountId"`
//EmailAddress string `json:"emailAddress"`
AvatarUrls struct {
Four8X48 string `json:"48x48"`
Two4X24 string `json:"24x24"`
One6X16 string `json:"16x16"`
Three2X32 string `json:"32x32"`
} `json:"avatarUrls"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
TimeZone string `json:"timeZone"`
AccountType string `json:"accountType"`
} `json:"author"`
UpdateAuthor struct {
Self string `json:"self"`
Name string `json:"name"`
Key string `json:"key"`
AccountID string `json:"accountId"`
EmailAddress string `json:"emailAddress"`
AvatarUrls struct {
Four8X48 string `json:"48x48"`
Two4X24 string `json:"24x24"`
One6X16 string `json:"16x16"`
Three2X32 string `json:"32x32"`
} `json:"avatarUrls"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
TimeZone string `json:"timeZone"`
AccountType string `json:"accountType"`
} `json:"updateAuthor"`
Created string `json:"created"`
Updated string `json:"updated"`
Started string `json:"started"`
TimeSpent string `json:"timeSpent"`
TimeSpentSeconds int `json:"timeSpentSeconds"`
ID string `json:"id"`
IssueID string `json:"issueId"`
}
type WorklogList struct {
IDs []int `json:"ids"`
}
type Issue struct {
Expand string `json:"expand"`
ID string `json:"id"`
Self string `json:"self"`
Key string `json:"key"`
Fields struct {
Summary string `json:"summary"`
Created string `json:created`
ResolutionDate *string `json:resolutiondate`
StatusCategoryChangeDate *string `json:"statuscategorychangedate"`
Issuetype struct {
Self string `json:"self"`
ID string `json:"id"`
Description string `json:"description"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
Subtask bool `json:"subtask"`
AvatarID int `json:"avatarId"`
} `json:"issuetype"`
Parent struct {
ID string `json:"id"`
Key string `json:"key"`
Self string `json:"self"`
Fields struct {
Summary string `json:"summary"`
Status struct {
Self string `json:"self"`
Description string `json:"description"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
ID string `json:"id"`
StatusCategory struct {
Self string `json:"self"`
ID int `json:"id"`
Key string `json:"key"`
ColorName string `json:"colorName"`
Name string `json:"name"`
} `json:"statusCategory"`
} `json:"status"`
Priority struct {
Self string `json:"self"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
ID string `json:"id"`
} `json:"priority"`
Issuetype struct {
Self string `json:"self"`
ID string `json:"id"`
Description string `json:"description"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
Subtask bool `json:"subtask"`
AvatarID int `json:"avatarId"`
} `json:"issuetype"`
} `json:"fields"`
} `json:"parent"`
Timespent int `json:"timespent"`
Timeoriginalestimate int `json:"timeoriginalestimate"`
Description struct {
Version int `json:"version"`
Type string `json:"type"`
Content []struct {
Type string `json:"type"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
} `json:"content"`
} `json:"description"`
Progress struct {
Progress int `json:"progress"`
Total int `json:"total"`
Percent int `json:"percent"`
} `json:"progress"`
Aggregateprogress struct {
Progress int `json:"progress"`
Total int `json:"total"`
Percent int `json:"percent"`
} `json:"aggregateprogress"`
Aggregatetimespent int `json:"aggregatetimespent"`
Aggregatetimeoriginalestimate int `json:"aggregatetimeoriginalestimate"`
Priority struct {
Self string `json:"self"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
ID string `json:"id"`
} `json:"priority"`
Status struct {
Self string `json:"self"`
Description string `json:"description"`
IconURL string `json:"iconUrl"`
Name string `json:"name"`
ID string `json:"id"`
StatusCategory struct {
Self string `json:"self"`
ID int `json:"id"`
Key string `json:"key"`
ColorName string `json:"colorName"`
Name string `json:"name"`
} `json:"statusCategory"`
} `json:"status"`
Timetracking struct {
OriginalEstimate string `json:"originalEstimate"`
RemainingEstimate string `json:"remainingEstimate"`
TimeSpent string `json:"timeSpent"`
OriginalEstimateSeconds int `json:"originalEstimateSeconds"`
RemainingEstimateSeconds int `json:"remainingEstimateSeconds"`
TimeSpentSeconds int `json:"timeSpentSeconds"`
} `json:"timetracking"`
} `json:"fields"`
}
func (i Issue) HasParent() bool {
return i.Fields.Parent.ID != ""
}
func (i Issue) ParentID() string {
if i.HasParent() {
return i.Fields.Parent.ID
}
return ""
}