@@ -25,9 +25,9 @@ func (api *API) updateAsCodePipelineHandler() service.Handler {
25
25
vars := mux .Vars (r )
26
26
key := vars [permProjectKey ]
27
27
name := vars ["pipelineKey" ]
28
+
28
29
branch := FormString (r , "branch" )
29
30
message := FormString (r , "message" )
30
- fromRepo := FormString (r , "repo" )
31
31
32
32
var p sdk.Pipeline
33
33
if err := service .UnmarshalBody (r , & p ); err != nil {
@@ -51,21 +51,32 @@ func (api *API) updateAsCodePipelineHandler() service.Handler {
51
51
}
52
52
53
53
if pipelineDB .FromRepository == "" {
54
- return sdk .WithStack (sdk .ErrForbidden )
54
+ return sdk .NewErrorFrom (sdk .ErrForbidden , "current pipeline is not ascode" )
55
55
}
56
56
57
- apps , err := application .LoadAsCode (api .mustDB (), key , fromRepo )
57
+ wkHolder , err := workflow .LoadByRepo (ctx , api .Cache , api .mustDB (), * proj , pipelineDB .FromRepository , workflow.LoadOptions {
58
+ WithTemplate : true ,
59
+ })
58
60
if err != nil {
59
61
return err
60
62
}
63
+ if wkHolder .TemplateInstance != nil {
64
+ return sdk .NewErrorFrom (sdk .ErrForbidden , "cannot edit a pipeline that was generated by a template" )
65
+ }
61
66
62
- app , err := application .LoadByIDWithClearVCSStrategyPassword (api .mustDB (), apps [0 ].ID )
63
- if err != nil {
64
- return err
67
+ var rootApp * sdk.Application
68
+ if wkHolder .WorkflowData .Node .Context != nil && wkHolder .WorkflowData .Node .Context .ApplicationID != 0 {
69
+ rootApp , err = application .LoadByIDWithClearVCSStrategyPassword (api .mustDB (), wkHolder .WorkflowData .Node .Context .ApplicationID )
70
+ if err != nil {
71
+ return err
72
+ }
73
+ }
74
+ if rootApp == nil {
75
+ return sdk .NewErrorFrom (sdk .ErrWrongRequest , "cannot find the root application of the workflow %s that hold the pipeline" , wkHolder .Name )
65
76
}
66
77
67
78
u := getAPIConsumer (ctx )
68
- ope , err := pipeline .UpdatePipelineAsCode (ctx , api .Cache , api .mustDB (), * proj , p , app .VCSServer , app .RepositoryFullname , branch , message , app .RepositoryStrategy , u )
79
+ ope , err := pipeline .UpdatePipelineAsCode (ctx , api .Cache , api .mustDB (), * proj , p , rootApp .VCSServer , rootApp .RepositoryFullname , branch , message , rootApp .RepositoryStrategy , u )
69
80
if err != nil {
70
81
return err
71
82
}
@@ -78,7 +89,7 @@ func (api *API) updateAsCodePipelineHandler() service.Handler {
78
89
Name : pipelineDB .Name ,
79
90
Operation : ope ,
80
91
}
81
- asCodeEvent := ascode .UpdateAsCodeResult (ctx , api .mustDB (), api .Cache , * proj , & apps [ 0 ] , ed , u )
92
+ asCodeEvent := ascode .UpdateAsCodeResult (ctx , api .mustDB (), api .Cache , * proj , * rootApp , ed , u )
82
93
if asCodeEvent != nil {
83
94
event .PublishAsCodeEvent (ctx , proj .Key , * asCodeEvent , u )
84
95
}
@@ -284,6 +295,11 @@ func (api *API) getPipelineHandler() service.Handler {
284
295
withWorkflows := FormBool (r , "withWorkflows" )
285
296
withAsCodeEvent := FormBool (r , "withAsCodeEvents" )
286
297
298
+ proj , err := project .Load (api .mustDB (), projectKey )
299
+ if err != nil {
300
+ return err
301
+ }
302
+
287
303
p , err := pipeline .LoadPipeline (ctx , api .mustDB (), projectKey , pipelineName , true )
288
304
if err != nil {
289
305
return sdk .WrapError (err , "cannot load pipeline %s" , pipelineName )
@@ -306,6 +322,16 @@ func (api *API) getPipelineHandler() service.Handler {
306
322
p .Usage .Workflows = wf
307
323
}
308
324
325
+ if p .FromRepository != "" {
326
+ wkAscodeHolder , err := workflow .LoadByRepo (ctx , api .Cache , api .mustDB (), * proj , p .FromRepository , workflow.LoadOptions {
327
+ WithTemplate : true ,
328
+ })
329
+ if err != nil {
330
+ return sdk .NewErrorFrom (err , "cannot found workflow holder of the pipeline" )
331
+ }
332
+ p .WorkflowAscodeHolder = wkAscodeHolder
333
+ }
334
+
309
335
return service .WriteJSON (w , p , http .StatusOK )
310
336
}
311
337
}
0 commit comments