-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
396 lines (351 loc) · 11.4 KB
/
task.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
thehive5 implements functionality to interact with the most recent version of thehive.
https://www.strangebee.com/thehive/
*/
package thehive5
import (
"encoding/json"
"net/url"
"strconv"
"time"
)
// CaseTask contains all task informations
type CaseTask struct {
Title string `json:"title"`
Group string `json:"group,omitempty"`
Description string `json:"description,omitempty"`
Status string `json:"status,omitempty"`
Flag bool `json:"flag,omitempty"`
StartDate time.Time `json:"startDate,omitempty"`
EndDate time.Time `json:"endDate,omitempty"`
DueDate time.Time `json:"dueDate,omitempty"`
Order *int `json:"order,omitempty"`
Assignee string `json:"assignee,omitempty"`
Mandatory bool `json:"mandatory,omitempty"`
}
// Marshalling the CaseTask requests
func (ct *CaseTask) MarshalJSON() ([]byte, error) {
type Alias CaseTask
var (
startDateInt64 *int64
endDateInt64 *int64
dueDateInt64 *int64
)
if !ct.StartDate.IsZero() {
// We ensure that all data sent to the hive is in UTC format
setDate := ct.StartDate.UTC().UnixMilli()
startDateInt64 = &setDate
}
if !ct.EndDate.IsZero() {
// We ensure that all data sent to the hive is in UTC format
endDate := ct.EndDate.UTC().UnixMilli()
endDateInt64 = &endDate
}
if !ct.DueDate.IsZero() {
// We ensure that all data sent to the hive is in UTC format
dueDate := ct.DueDate.UTC().UnixMilli()
dueDateInt64 = &dueDate
}
return json.Marshal(&struct {
StartDate *int64 `json:"startDate,omitempty"`
EndDate *int64 `json:"endDate,omitempty"`
DueDate *int64 `json:"dueDate,omitempty"`
*Alias
}{
StartDate: startDateInt64,
EndDate: endDateInt64,
DueDate: dueDateInt64,
Alias: (*Alias)(ct),
})
}
// TaskLog contains all task log informations
type TaskLog struct {
Message string `json:"message"`
StartDate time.Time `json:"startDate,omitempty"`
IncludeInTimeline time.Time `json:"includeInTimeline,omitempty"`
Attachments *interface{} `json:"attachments,omitempty"`
}
// Marshalling the TaskLog requests
func (tl *TaskLog) MarshalJSON() ([]byte, error) {
type Alias TaskLog
var (
startDateInt64 *int64
includeInTimelineInt64 *int64
)
if !tl.StartDate.IsZero() {
// We ensure that all data sent to the hive is in UTC format
tmp := tl.StartDate.UTC().UnixMilli()
startDateInt64 = &tmp
}
if !tl.IncludeInTimeline.IsZero() {
// We ensure that all data sent to the hive is in UTC format
tmp := tl.IncludeInTimeline.UTC().UnixMilli()
includeInTimelineInt64 = &tmp
}
return json.Marshal(&struct {
StartDate *int64 `json:"startDate,omitempty"`
IncludeInTimeline *int64 `json:"includeInTimeline,omitempty"`
*Alias
}{
StartDate: startDateInt64,
IncludeInTimeline: includeInTimelineInt64,
Alias: (*Alias)(tl),
})
}
// TaskLogResponse contains all task log responded
type TaskLogResponse struct {
Id string `json:"_id"`
Type string `json:"_type"`
CreatedBy string `json:"_createdBy"`
UpdatedBy string `json:"_updatedBy"`
CreatedAt time.Time `json:"_createdAt"`
UpdatedAt time.Time `json:"_updatedAt"`
Message string `json:"message"`
Date time.Time `json:"date"`
Attachments []interface{} `json:"attachments"`
Owner string `json:"owner"`
IncludeInTimeline time.Time `json:"includeInTimeline"`
ExtraData struct{} `json:"extraData"`
}
// TaskLogResponse contains all task log responded
type shadowTaskLogResponse struct {
Id string `json:"_id"`
Type string `json:"_type"`
CreatedBy string `json:"_createdBy"`
UpdatedBy string `json:"_updatedBy"`
CreatedAt int64 `json:"_createdAt"`
UpdatedAt int64 `json:"_updatedAt"`
Message string `json:"message"`
Date int64 `json:"date"`
Attachments []interface{} `json:"attachments"`
Owner string `json:"owner"`
IncludeInTimeline int64 `json:"includeInTimeline"`
ExtraData struct{} `json:"extraData"`
}
// shadow Unmarshalling function for TaskLogResponse
func (tl *TaskLogResponse) UnmarshalJSON(data []byte) error {
shadow := new(shadowTaskLogResponse)
err := json.Unmarshal(data, &shadow)
if err != nil {
return err
}
tl.Id = shadow.Id
tl.Type = shadow.Type
tl.CreatedBy = shadow.CreatedBy
tl.UpdatedBy = shadow.UpdatedBy
tl.UpdatedAt = convertInt64ToTime(shadow.UpdatedAt)
tl.Message = shadow.Message
tl.Date = convertInt64ToTime(shadow.Date)
tl.Attachments = shadow.Attachments
tl.Owner = shadow.Owner
tl.IncludeInTimeline = convertInt64ToTime(shadow.IncludeInTimeline)
tl.ExtraData = shadow.ExtraData
return nil
}
// CaseTaskResponse stores the response of a task that was added to a case in The Hive
type CaseTaskResponse struct {
Id string `json:"_id"`
Type string `json:"_type"`
CreatedBy string `json:"_createdBy"`
UpdatedBy string `json:"_updatedBy"`
CreatedAt time.Time `json:"_createdAt"`
UpdatedAt time.Time `json:"_updatedAt"`
Title string `json:"title"`
Group string `json:"group"`
Description string `json:"description"`
Status string `json:"status"`
Flag bool `json:"flag"`
StartDate time.Time `json:"startDate"`
EndDate time.Time `json:"endDate"`
Assignee string `json:"assignee"`
Order int `json:"order"`
DueDate time.Time `json:"dueDate"`
Mandatory bool `json:"mandatory"`
ExtraData struct{} `json:"extraData"`
}
// CaseTaskResponse stores the response of a task that was added to a case in The Hive
type shadowCaseTaskResponse struct {
Id string `json:"_id"`
Type string `json:"_type"`
CreatedBy string `json:"_createdBy"`
UpdatedBy string `json:"_updatedBy"`
CreatedAt int64 `json:"_createdAt"`
UpdatedAt int64 `json:"_updatedAt"`
Title string `json:"title"`
Group string `json:"group"`
Description string `json:"description"`
Status string `json:"status"`
Flag bool `json:"flag"`
StartDate int64 `json:"startDate"`
EndDate int64 `json:"endDate"`
Assignee string `json:"assignee"`
Order int `json:"order"`
DueDate int64 `json:"dueDate"`
Mandatory bool `json:"mandatory"`
ExtraData struct{} `json:"extraData"`
}
// shadow Unmarshalling function for CaseTaskResponse
func (ct *CaseTaskResponse) UnmarshalJSON(data []byte) error {
shadow := new(shadowCaseTaskResponse)
err := json.Unmarshal(data, &shadow)
if err != nil {
return err
}
ct.Id = shadow.Id
ct.Type = shadow.Type
ct.CreatedBy = shadow.CreatedBy
ct.UpdatedBy = shadow.UpdatedBy
ct.CreatedAt = convertInt64ToTime(shadow.CreatedAt)
ct.UpdatedAt = convertInt64ToTime(shadow.UpdatedAt)
ct.Title = shadow.Title
ct.Group = shadow.Group
ct.Description = shadow.Description
ct.Status = shadow.Status
ct.Flag = shadow.Flag
ct.Mandatory = shadow.Mandatory
ct.StartDate = convertInt64ToTime(shadow.StartDate)
ct.EndDate = convertInt64ToTime(shadow.EndDate)
ct.Assignee = shadow.Assignee
ct.Order = shadow.Order
ct.DueDate = convertInt64ToTime(shadow.DueDate)
ct.ExtraData = shadow.ExtraData
return nil
}
// executeTaskSearchQuery is a helper function to do query related searches
func (hive *Hivedata) executeTaskSearchQuery(query []byte) ([]CaseTaskResponse, error) {
url, err := url.JoinPath(hive.Url, "/api/v1/query")
if err != nil {
return nil, err
}
ret, err := hive.webRequest(url, POST, query)
if err != nil {
return nil, err
}
var parsedRet []CaseTaskResponse
err = json.Unmarshal(ret, &parsedRet)
return parsedRet, err
}
// executeTaskLogSearchQuery is a helper function to do query related searches
func (hive *Hivedata) executeTaskLogSearchQuery(query []byte) ([]TaskLogResponse, error) {
url, err := url.JoinPath(hive.Url, "/api/v1/query")
if err != nil {
return nil, err
}
ret, err := hive.webRequest(url, POST, query)
if err != nil {
return nil, err
}
var parsedRet []TaskLogResponse
err = json.Unmarshal(ret, &parsedRet)
return parsedRet, err
}
// UpdateTask updates an existing task
// Only returns data if an error occured
func (hive *Hivedata) UpdateTask(taskId string, task *CaseTask) error {
url, err := url.JoinPath(hive.Url, "/api/v1/task/", taskId)
if err != nil {
return err
}
jsondata, err := json.Marshal(task)
if err != nil {
return err
}
_, err = hive.webRequest(url, PATCH, jsondata)
return err
}
// AddTaskToCase creates a new task and adds to an existing case
func (hive *Hivedata) AddTaskToCase(caseId int, task *CaseTask) (*CaseTaskResponse, error) {
url, err := url.JoinPath(hive.Url, "/api/v1/case/", strconv.Itoa(caseId), "/task")
if err != nil {
return nil, err
}
jsondata, err := json.Marshal(task)
if err != nil {
return nil, err
}
ret, err := hive.webRequest(url, POST, jsondata)
if err != nil {
return nil, err
}
parsedRet := CaseTaskResponse{}
err = json.Unmarshal(ret, &parsedRet)
if err != nil {
return nil, err
}
return &parsedRet, err
}
// DeleteTask deletes an existing task from a case
// only returns data if an error occured
func (hive *Hivedata) DeleteTask(taskId string) error {
url, err := url.JoinPath(hive.Url, "/api/v1/task/", taskId)
if err != nil {
return err
}
_, err = hive.webRequest(url, DELETE, nil)
return err
}
// GetTask returns a single CaseTaskResponse object or error
// A task ID must be provided
func (hive *Hivedata) GetTask(taskId string) (*CaseTaskResponse, error) {
url, err := url.JoinPath(hive.Url, "/api/v1/task/", taskId)
if err != nil {
return nil, err
}
ret, err := hive.webRequest(url, GET, nil)
if err != nil {
return nil, err
}
parsedRet := CaseTaskResponse{}
err = json.Unmarshal(ret, &parsedRet)
if err != nil {
return nil, err
}
return &parsedRet, nil
}
// GetTaskLog returns all log entries of a task
// A task ID must be provided
func (hive *Hivedata) CreateTaskLog(taskId string, log *TaskLog) (*TaskLogResponse, error) {
url, err := url.JoinPath(hive.Url, "/api/v1/task/", taskId, "/log")
if err != nil {
return nil, err
}
jsondata, err := json.Marshal(log)
if err != nil {
return nil, err
}
ret, err := hive.webRequest(url, POST, jsondata)
if err != nil {
return nil, err
}
parsedRet := TaskLogResponse{}
err = json.Unmarshal(ret, &parsedRet)
if err != nil {
return nil, err
}
return &parsedRet, nil
}
// GetTaskLogs returns all logs associated with a task
// It returns a task log slice or an error
func (hive *Hivedata) GetTaskLogs(taskId string) ([]TaskLogResponse, error) {
query, err := hive.createSearchQuery(
SearchQuery{Name: "getTask", IdOrName: taskId},
SearchQuery{Name: "logs"},
SearchQuery{Name: "sort", Sort: &[1]map[string]string{{"_createdAt": "asc"}}})
if err != nil {
return nil, err
}
return hive.executeTaskLogSearchQuery(query)
}
// GetCaseTasks returns all tasks associated with a case
// It returns a task slice or an error
func (hive *Hivedata) GetCaseTasks(caseId int) ([]CaseTaskResponse, error) {
caseNumber := strconv.Itoa(caseId)
query, err := hive.createSearchQuery(
SearchQuery{Name: "getCase", IdOrName: caseNumber},
SearchQuery{Name: "tasks"},
SearchQuery{Name: "sort", Sort: &[1]map[string]string{{"_createdAt": "asc"}}})
if err != nil {
return nil, err
}
return hive.executeTaskSearchQuery(query)
}