@@ -2,7 +2,6 @@ package api
2
2
3
3
import (
4
4
"context"
5
- "database/sql"
6
5
"fmt"
7
6
"io/ioutil"
8
7
"net/http"
@@ -232,9 +231,7 @@ func (api *API) postWorkflowRollbackHandler() service.Handler {
232
231
if err != nil {
233
232
return sdk .WrapError (err , "cannot begin transaction" )
234
233
}
235
- defer func () {
236
- _ = tx .Rollback ()
237
- }()
234
+ defer tx .Rollback () // nolint
238
235
239
236
newWf , _ , errP := workflow .ParseAndImport (ctx , tx , api .Cache , * proj , wf , exportWf , u , workflow.ImportOptions {Force : true , WorkflowName : workflowName })
240
237
if errP != nil {
@@ -265,57 +262,47 @@ func (api *API) postWorkflowLabelHandler() service.Handler {
265
262
266
263
var label sdk.Label
267
264
if err := service .UnmarshalBody (r , & label ); err != nil {
268
- return sdk .WrapError (err , "cannot read body" )
265
+ return err
266
+ }
267
+ if label .ID == 0 && label .Name == "" {
268
+ return sdk .NewErrorFrom (sdk .ErrWrongRequest , "label ID or label name should not be empty" )
269
269
}
270
270
271
- proj , err := project .Load (ctx , db , key ,
272
- project .LoadOptions .WithApplicationWithDeploymentStrategies ,
273
- project .LoadOptions .WithPipelines ,
274
- project .LoadOptions .WithEnvironments ,
275
- project .LoadOptions .WithGroups ,
276
- project .LoadOptions .WithIntegrations ,
277
- )
271
+ tx , err := db .Begin ()
278
272
if err != nil {
279
- return sdk .WrapError (err , "cannot load project %s" , key )
273
+ return sdk .WrapError (err , "cannot create new transaction" )
280
274
}
281
- label . ProjectID = proj . ID
275
+ defer tx . Rollback () //nolint
282
276
283
- tx , errTx := db . Begin ( )
284
- if errTx != nil {
285
- return sdk .WrapError (errTx , "cannot create new transaction" )
277
+ proj , err := project . Load ( ctx , tx , key )
278
+ if err != nil {
279
+ return sdk .WrapError (err , "cannot load project %s" , key )
286
280
}
287
- defer tx . Rollback () //nolint
281
+ label . ProjectID = proj . ID
288
282
289
283
if label .ID == 0 {
290
- if label .Name == "" {
291
- return sdk .NewErrorFrom (sdk .ErrWrongRequest , "label ID or label name should not be empty" )
284
+ existingLabel , err := project .LabelByName (ctx , tx , proj .ID , label .Name )
285
+ if err != nil && ! sdk .ErrorIs (err , sdk .ErrNotFound ) {
286
+ return err
292
287
}
293
-
294
- lbl , err := project .LabelByName (tx , proj .ID , label .Name )
295
- if err != nil {
296
- if sdk .Cause (err ) != sql .ErrNoRows {
297
- return sdk .WrapError (err , "cannot load label by name" )
298
- }
299
- // If label doesn't exist create him
288
+ if existingLabel == nil {
300
289
if err := project .InsertLabel (tx , & label ); err != nil {
301
290
return sdk .WrapError (err , "cannot create new label" )
302
291
}
303
292
} else {
304
- label .ID = lbl .ID
293
+ label .ID = existingLabel .ID
305
294
}
306
295
}
307
296
308
- wf , err := workflow .Load (ctx , tx , api .Cache , * proj , workflowName , workflow.LoadOptions {WithLabels : true })
297
+ wf , err := workflow .Load (ctx , tx , api .Cache , * proj , workflowName , workflow.LoadOptions {Minimal : true })
309
298
if err != nil {
310
299
return sdk .WrapError (err , "cannot load workflow %s/%s" , key , workflowName )
311
300
}
312
301
313
302
if err := workflow .LabelWorkflow (tx , label .ID , wf .ID ); err != nil {
314
303
return sdk .WrapError (err , "cannot link label %d to workflow %s" , label .ID , wf .Name )
315
304
}
316
- newWf := * wf
317
305
label .WorkflowID = wf .ID
318
- newWf .Labels = append (newWf .Labels , label )
319
306
320
307
if err := tx .Commit (); err != nil {
321
308
return sdk .WithStack (err )
@@ -331,25 +318,19 @@ func (api *API) deleteWorkflowLabelHandler() service.Handler {
331
318
vars := mux .Vars (r )
332
319
key := vars ["key" ]
333
320
workflowName := vars ["permWorkflowName" ]
334
- labelID , errV := requestVarInt (r , "labelID" )
335
- if errV != nil {
336
- return sdk .WrapError (errV , "cannot convert to int labelID" )
321
+ labelID , err := requestVarInt (r , "labelID" )
322
+ if err != nil {
323
+ return sdk .WrapError (err , "cannot convert to int labelID" )
337
324
}
338
325
339
326
db := api .mustDB ()
340
327
341
- proj , err := project .Load (ctx , db , key ,
342
- project .LoadOptions .WithApplicationWithDeploymentStrategies ,
343
- project .LoadOptions .WithPipelines ,
344
- project .LoadOptions .WithEnvironments ,
345
- project .LoadOptions .WithGroups ,
346
- project .LoadOptions .WithIntegrations ,
347
- )
328
+ proj , err := project .Load (ctx , db , key )
348
329
if err != nil {
349
330
return sdk .WrapError (err , "cannot load project %s" , key )
350
331
}
351
332
352
- wf , err := workflow .Load (ctx , db , api .Cache , * proj , workflowName , workflow.LoadOptions {})
333
+ wf , err := workflow .Load (ctx , db , api .Cache , * proj , workflowName , workflow.LoadOptions {Minimal : true })
353
334
if err != nil {
354
335
return sdk .WrapError (err , "cannot load workflow %s/%s" , key , workflowName )
355
336
}
@@ -369,9 +350,6 @@ func (api *API) postWorkflowHandler() service.Handler {
369
350
key := vars [permProjectKey ]
370
351
371
352
p , err := project .Load (ctx , api .mustDB (), key ,
372
- project .LoadOptions .WithApplicationWithDeploymentStrategies ,
373
- project .LoadOptions .WithPipelines ,
374
- project .LoadOptions .WithEnvironments ,
375
353
project .LoadOptions .WithGroups ,
376
354
project .LoadOptions .WithIntegrations ,
377
355
)
@@ -390,9 +368,9 @@ func (api *API) postWorkflowHandler() service.Handler {
390
368
data .ProjectID = p .ID
391
369
data .ProjectKey = key
392
370
393
- tx , errT := api .mustDB ().Begin ()
394
- if errT != nil {
395
- return sdk .WithStack (errT )
371
+ tx , err := api .mustDB ().Begin ()
372
+ if err != nil {
373
+ return sdk .WithStack (err )
396
374
}
397
375
defer tx .Rollback () // nolint
398
376
@@ -429,12 +407,7 @@ func (api *API) putWorkflowHandler() service.Handler {
429
407
key := vars ["key" ]
430
408
name := vars ["permWorkflowName" ]
431
409
432
- p , err := project .Load (ctx , api .mustDB (), key ,
433
- project .LoadOptions .WithApplicationWithDeploymentStrategies ,
434
- project .LoadOptions .WithPipelines ,
435
- project .LoadOptions .WithEnvironments ,
436
- project .LoadOptions .WithIntegrations ,
437
- )
410
+ p , err := project .Load (ctx , api .mustDB (), key , project .LoadOptions .WithIntegrations )
438
411
if err != nil {
439
412
return sdk .WrapError (err , "cannot load Project %s" , key )
440
413
}
0 commit comments