forked from fabric8-services/fabric8-wit
/
workitem.go
396 lines (374 loc) · 14.4 KB
/
workitem.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
package main
import (
"fmt"
"html"
"strconv"
"golang.org/x/net/context"
"github.com/almighty/almighty-core/app"
"github.com/almighty/almighty-core/application"
"github.com/almighty/almighty-core/criteria"
"github.com/almighty/almighty-core/errors"
"github.com/almighty/almighty-core/jsonapi"
"github.com/almighty/almighty-core/login"
query "github.com/almighty/almighty-core/query/simple"
"github.com/almighty/almighty-core/rendering"
"github.com/almighty/almighty-core/rest"
"github.com/almighty/almighty-core/workitem"
"github.com/goadesign/goa"
errs "github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
)
// Defines the constants to be used in json api "type" attribute
const (
APIStringTypeUser = "identities"
APIStringTypeWorkItem = "workitems"
APIStringTypeWorkItemType = "workitemtypes"
)
// WorkitemController implements the workitem resource.
type WorkitemController struct {
*goa.Controller
db application.DB
}
// NewWorkitemController creates a workitem controller.
func NewWorkitemController(service *goa.Service, db application.DB) *WorkitemController {
if db == nil {
panic("db must not be nil")
}
return &WorkitemController{Controller: service.NewController("WorkitemController"), db: db}
}
// List runs the list action.
// Prev and Next links will be present only when there actually IS a next or previous page.
// Last will always be present. Total Item count needs to be computed from the "Last" link.
func (c *WorkitemController) List(ctx *app.ListWorkitemContext) error {
var additionalQuery []string
exp, err := query.Parse(ctx.Filter)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError("could not parse filter", err))
}
if ctx.FilterAssignee != nil {
assignee := ctx.FilterAssignee
exp = criteria.And(exp, criteria.Equals(criteria.Field("system.assignees"), criteria.Literal([]string{*assignee})))
additionalQuery = append(additionalQuery, "filter[assignee]="+*assignee)
}
if ctx.FilterIteration != nil {
iteration := ctx.FilterIteration
exp = criteria.And(exp, criteria.Equals(criteria.Field(workitem.SystemIteration), criteria.Literal(string(*iteration))))
additionalQuery = append(additionalQuery, "filter[iteration]="+*iteration)
}
if ctx.FilterWorkitemtype != nil {
wit := ctx.FilterWorkitemtype
exp = criteria.And(exp, criteria.Equals(criteria.Field("Type"), criteria.Literal([]string{*wit})))
additionalQuery = append(additionalQuery, "filter[workitemtype]="+*wit)
}
if ctx.FilterArea != nil {
area := ctx.FilterArea
exp = criteria.And(exp, criteria.Equals(criteria.Field(workitem.SystemArea), criteria.Literal(string(*area))))
additionalQuery = append(additionalQuery, "filter[area]="+*area)
}
offset, limit := computePagingLimts(ctx.PageOffset, ctx.PageLimit)
return application.Transactional(c.db, func(tx application.Application) error {
result, tc, err := tx.WorkItems().List(ctx.Context, exp, &offset, &limit)
count := int(tc)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, "Error listing work items"))
}
response := app.WorkItem2List{
Links: &app.PagingLinks{},
Meta: &app.WorkItemListResponseMeta{TotalCount: count},
Data: ConvertWorkItems(ctx.RequestData, result),
}
setPagingLinks(response.Links, buildAbsoluteURL(ctx.RequestData), len(result), offset, limit, count, additionalQuery...)
return ctx.OK(&response)
})
}
// Update does PATCH workitem
func (c *WorkitemController) Update(ctx *app.UpdateWorkitemContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
if ctx.Payload == nil || ctx.Payload.Data == nil || ctx.Payload.Data.ID == nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError("missing data.ID element in request", nil))
}
wi, err := appl.WorkItems().Load(ctx, *ctx.Payload.Data.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, fmt.Sprintf("Failed to load work item with id %v", *ctx.Payload.Data.ID)))
}
// Type changes of WI are not allowed which is why we overwrite it the
// type with the old one after the WI has been converted.
oldType := wi.Type
err = ConvertJSONAPIToWorkItem(appl, *ctx.Payload.Data, wi)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, err)
}
wi.Type = oldType
wi, err = appl.WorkItems().Save(ctx, *wi)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, "Error updating work item"))
}
wi2 := ConvertWorkItem(ctx.RequestData, wi)
resp := &app.WorkItem2Single{
Data: wi2,
Links: &app.WorkItemLinks{
Self: buildAbsoluteURL(ctx.RequestData),
},
}
return ctx.OK(resp)
})
}
// Create does POST workitem
func (c *WorkitemController) Create(ctx *app.CreateWorkitemContext) error {
currentUser, err := login.ContextIdentity(ctx)
if err != nil {
jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrUnauthorized(err.Error()))
return ctx.Unauthorized(jerrors)
}
var wit *string
if ctx.Payload.Data != nil && ctx.Payload.Data.Relationships != nil &&
ctx.Payload.Data.Relationships.BaseType != nil && ctx.Payload.Data.Relationships.BaseType.Data != nil {
wit = &ctx.Payload.Data.Relationships.BaseType.Data.ID
}
if wit == nil { // TODO Figure out path source etc. Should be a required relation
return jsonapi.JSONErrorResponse(ctx, errors.NewBadParameterError("Data.Relationships.BaseType.Data.ID", err))
}
wi := app.WorkItem{
Fields: make(map[string]interface{}),
}
return application.Transactional(c.db, func(appl application.Application) error {
err := ConvertJSONAPIToWorkItem(appl, *ctx.Payload.Data, &wi)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, fmt.Sprintf("Error creating work item")))
}
wi, err := appl.WorkItems().Create(ctx, *wit, wi.Fields, currentUser)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, fmt.Sprintf("Error creating work item")))
}
wi2 := ConvertWorkItem(ctx.RequestData, wi)
resp := &app.WorkItem2Single{
Data: wi2,
Links: &app.WorkItemLinks{
Self: buildAbsoluteURL(ctx.RequestData),
},
}
ctx.ResponseData.Header().Set("Location", app.WorkitemHref(wi2.ID))
return ctx.Created(resp)
})
}
// Show does GET workitem
func (c *WorkitemController) Show(ctx *app.ShowWorkitemContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
comments := WorkItemIncludeCommentsAndTotal(ctx, c.db, ctx.ID)
wi, err := appl.WorkItems().Load(ctx, ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrap(err, fmt.Sprintf("Fail to load work item with id %v", ctx.ID)))
}
wi2 := ConvertWorkItem(ctx.RequestData, wi, comments)
resp := &app.WorkItem2Single{
Data: wi2,
}
return ctx.OK(resp)
})
}
// Delete does DELETE workitem
func (c *WorkitemController) Delete(ctx *app.DeleteWorkitemContext) error {
return application.Transactional(c.db, func(appl application.Application) error {
err := appl.WorkItems().Delete(ctx, ctx.ID)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrapf(err, "error deleting work item %s", ctx.ID))
}
if err := appl.WorkItemLinks().DeleteRelatedLinks(ctx, ctx.ID); err != nil {
return jsonapi.JSONErrorResponse(ctx, errs.Wrapf(err, "failed to delete work item links related to work item %s", ctx.ID))
}
return ctx.OK([]byte{})
})
}
// ConvertJSONAPIToWorkItem is responsible for converting given WorkItem model object into a
// response resource object by jsonapi.org specifications
func ConvertJSONAPIToWorkItem(appl application.Application, source app.WorkItem2, target *app.WorkItem) error {
// construct default values from input WI
version, err := getVersion(source.Attributes["version"])
if err != nil {
return err
}
target.Version = version
if source.Relationships != nil && source.Relationships.Assignees != nil {
if source.Relationships.Assignees.Data == nil {
delete(target.Fields, workitem.SystemAssignees)
} else {
var ids []string
for _, d := range source.Relationships.Assignees.Data {
assigneeUUID, err := uuid.FromString(*d.ID)
if err != nil {
return errors.NewBadParameterError("data.relationships.assignees.data.id", *d.ID)
}
if ok := appl.Identities().IsValid(context.Background(), assigneeUUID); !ok {
return errors.NewBadParameterError("data.relationships.assignees.data.id", *d.ID)
}
ids = append(ids, assigneeUUID.String())
}
target.Fields[workitem.SystemAssignees] = ids
}
}
if source.Relationships != nil && source.Relationships.Iteration != nil {
if source.Relationships.Iteration.Data == nil {
delete(target.Fields, workitem.SystemIteration)
} else {
d := source.Relationships.Iteration.Data
iterationUUID, err := uuid.FromString(*d.ID)
if err != nil {
return errors.NewBadParameterError("data.relationships.iteration.data.id", *d.ID)
}
if _, err = appl.Iterations().Load(context.Background(), iterationUUID); err != nil {
return errors.NewBadParameterError("data.relationships.iteration.data.id", *d.ID)
}
target.Fields[workitem.SystemIteration] = iterationUUID.String()
}
}
if source.Relationships != nil && source.Relationships.Area != nil {
if source.Relationships.Area.Data == nil {
delete(target.Fields, workitem.SystemArea)
} else {
d := source.Relationships.Area.Data
areaUUID, err := uuid.FromString(*d.ID)
if err != nil {
return errors.NewBadParameterError("data.relationships.area.data.id", *d.ID)
}
if _, err = appl.Areas().Load(context.Background(), areaUUID); err != nil {
return errors.NewBadParameterError("data.relationships.area.data.id", *d.ID)
}
target.Fields[workitem.SystemArea] = areaUUID.String()
}
}
if source.Relationships != nil && source.Relationships.BaseType != nil {
if source.Relationships.BaseType.Data != nil {
target.Type = source.Relationships.BaseType.Data.ID
}
}
for key, val := range source.Attributes {
// convert legacy description to markup content
if key == workitem.SystemDescription {
if m := rendering.NewMarkupContentFromValue(val); m != nil {
target.Fields[key] = *m
}
} else {
target.Fields[key] = val
}
}
if description, ok := target.Fields[workitem.SystemDescription].(rendering.MarkupContent); ok {
// verify the description markup
if !rendering.IsMarkupSupported(description.Markup) {
return errors.NewBadParameterError("data.relationships.attributes[system.description].markup", description.Markup)
}
}
return nil
}
func getVersion(version interface{}) (int, error) {
if version != nil {
v, err := strconv.Atoi(fmt.Sprintf("%v", version))
if err != nil {
return -1, errors.NewBadParameterError("data.attributes.version", version)
}
return v, nil
}
return -1, nil
}
// WorkItemConvertFunc is a open ended function to add additional links/data/relations to a Comment during
// conversion from internal to API
type WorkItemConvertFunc func(*goa.RequestData, *app.WorkItem, *app.WorkItem2)
// ConvertWorkItems is responsible for converting given []WorkItem model object into a
// response resource object by jsonapi.org specifications
func ConvertWorkItems(request *goa.RequestData, wis []*app.WorkItem, additional ...WorkItemConvertFunc) []*app.WorkItem2 {
ops := []*app.WorkItem2{}
for _, wi := range wis {
ops = append(ops, ConvertWorkItem(request, wi, additional...))
}
return ops
}
// ConvertWorkItem is responsible for converting given WorkItem model object into a
// response resource object by jsonapi.org specifications
func ConvertWorkItem(request *goa.RequestData, wi *app.WorkItem, additional ...WorkItemConvertFunc) *app.WorkItem2 {
// construct default values from input WI
selfURL := rest.AbsoluteURL(request, app.WorkitemHref(wi.ID))
sourceLinkTypesURL := rest.AbsoluteURL(request, app.WorkitemtypeHref(wi.Type)+sourceLinkTypesRouteEnd)
targetLinkTypesURL := rest.AbsoluteURL(request, app.WorkitemtypeHref(wi.Type)+targetLinkTypesRouteEnd)
op := &app.WorkItem2{
ID: &wi.ID,
Type: APIStringTypeWorkItem,
Attributes: map[string]interface{}{
"version": wi.Version,
},
Relationships: &app.WorkItemRelationships{
BaseType: &app.RelationBaseType{
Data: &app.BaseTypeData{
ID: wi.Type,
Type: APIStringTypeWorkItemType,
},
},
},
Links: &app.GenericLinksForWorkItem{
Self: &selfURL,
SourceLinkTypes: &sourceLinkTypesURL,
TargetLinkTypes: &targetLinkTypesURL,
},
}
// Move fields into Relationships or Attributes as needed
// TODO: Loop based on WorKItemType and match against Field.Type instead of directly to field value
for name, val := range wi.Fields {
switch name {
case workitem.SystemAssignees:
if val != nil {
valArr := val.([]interface{})
op.Relationships.Assignees = &app.RelationGenericList{
Data: ConvertUsersSimple(request, valArr),
}
}
case workitem.SystemCreator:
if val != nil {
valStr := val.(string)
op.Relationships.Creator = &app.RelationGeneric{
Data: ConvertUserSimple(request, valStr),
}
}
case workitem.SystemIteration:
if val != nil {
valStr := val.(string)
op.Relationships.Iteration = &app.RelationGeneric{
Data: ConvertIterationSimple(request, valStr),
}
}
case workitem.SystemArea:
if val != nil {
valStr := val.(string)
op.Relationships.Area = &app.RelationGeneric{
Data: ConvertAreaSimple(request, valStr),
}
}
case workitem.SystemTitle:
// 'HTML escape' the title to prevent script injection
op.Attributes[name] = html.EscapeString(val.(string))
case workitem.SystemDescription:
description := rendering.NewMarkupContentFromValue(val)
if description != nil {
op.Attributes[name] = (*description).Content
op.Attributes[workitem.SystemDescriptionMarkup] = (*description).Markup
// let's include the rendered description while 'HTML escaping' it to prevent script injection
op.Attributes[workitem.SystemDescriptionRendered] =
rendering.RenderMarkupToHTML(html.EscapeString((*description).Content), (*description).Markup)
}
default:
op.Attributes[name] = val
}
}
if op.Relationships.Assignees == nil {
op.Relationships.Assignees = &app.RelationGenericList{Data: nil}
}
if op.Relationships.Iteration == nil {
op.Relationships.Iteration = &app.RelationGeneric{Data: nil}
}
if op.Relationships.Area == nil {
op.Relationships.Area = &app.RelationGeneric{Data: nil}
}
// Always include Comments Link, but optionally use WorkItemIncludeCommentsAndTotal
WorkItemIncludeComments(request, wi, op)
for _, add := range additional {
add(request, wi, op)
}
return op
}