@@ -19,8 +19,8 @@ package integrationconfigs
1919import (
2020 "context"
2121 "encoding/json"
22+ "errors"
2223 "fmt"
23- "io"
2424 "net/http"
2525 "regexp"
2626
@@ -93,17 +93,32 @@ func (h *handler) runHandler(w http.ResponseWriter, req *http.Request, et git.Ev
9393 },
9494 }
9595
96+ userReqPre := & cicdv1.IntegrationConfigAPIReqRunPreBody {}
97+ userReqPost := & cicdv1.IntegrationConfigAPIReqRunPostBody {}
98+
9699 switch et {
97100 case git .EventTypePullRequest :
98- pr , err := buildPullRequestWebhook (req .Body , userEscaped )
101+ decoder := json .NewDecoder (req .Body )
102+ if err := decoder .Decode (userReqPre ); err != nil {
103+ log .Info (err .Error ())
104+ _ = utils .RespondError (w , http .StatusBadRequest , fmt .Sprintf ("req: %s, cannot build pull_request webhook" , reqID ))
105+ return
106+ }
107+ pr , err := buildPullRequestWebhook (userReqPre , userEscaped )
99108 if err != nil {
100109 log .Info (err .Error ())
101110 _ = utils .RespondError (w , http .StatusBadRequest , fmt .Sprintf ("req: %s, cannot build pull_request webhook" , reqID ))
102111 return
103112 }
104113 wh .PullRequest = pr
105114 case git .EventTypePush :
106- push , err := buildPushWebhook (req .Body )
115+ decoder := json .NewDecoder (req .Body )
116+ if err := decoder .Decode (userReqPost ); err != nil {
117+ log .Info (err .Error ())
118+ _ = utils .RespondError (w , http .StatusBadRequest , fmt .Sprintf ("req: %s, cannot build pull_request webhook" , reqID ))
119+ return
120+ }
121+ push , err := buildPushWebhook (userReqPost )
107122 if err != nil {
108123 log .Info (err .Error ())
109124 _ = utils .RespondError (w , http .StatusBadRequest , fmt .Sprintf ("req: %s, cannot build push webhook" , reqID ))
@@ -115,6 +130,26 @@ func (h *handler) runHandler(w http.ResponseWriter, req *http.Request, et git.Ev
115130 Name : fmt .Sprintf ("trigger-%s-end" , userEscaped ),
116131 }
117132
133+ // Update IntegrationConfig based on the request
134+ switch et {
135+ case git .EventTypePullRequest :
136+ updatedIC , err := updateIntegrationConfigPre (ic , userReqPre , et )
137+ if err != nil {
138+ log .Info (err .Error ())
139+ _ = utils .RespondError (w , http .StatusBadRequest , "cannot update pull_request integrationconfig. please check jobName is valid" )
140+ return
141+ }
142+ ic = updatedIC
143+ case git .EventTypePush :
144+ updatedIC , err := updateIntegrationConfigPost (ic , userReqPost , et )
145+ if err != nil {
146+ log .Info (err .Error ())
147+ _ = utils .RespondError (w , http .StatusBadRequest , "cannot update push integrationconfig. please check jobName is valid" )
148+ return
149+ }
150+ ic = updatedIC
151+ }
152+
118153 // Trigger Run!
119154 if err := server .HandleEvent (wh , ic , "dispatcher" ); err != nil {
120155 log .Info (err .Error ())
@@ -125,13 +160,87 @@ func (h *handler) runHandler(w http.ResponseWriter, req *http.Request, et git.Ev
125160 _ = utils .RespondJSON (w , struct {}{})
126161}
127162
128- func buildPullRequestWebhook (body io.Reader , user string ) (* git.PullRequest , error ) {
129- userReq := & cicdv1.IntegrationConfigAPIReqRunPreBody {}
130- decoder := json .NewDecoder (body )
131- if err := decoder .Decode (userReq ); err != nil {
132- return nil , err
163+ func updateIntegrationConfigPost (ic * cicdv1.IntegrationConfig , userReqBody * cicdv1.IntegrationConfigAPIReqRunPostBody , et git.EventType ) (* cicdv1.IntegrationConfig , error ) {
164+ targetJob := & ic .Spec .Jobs .PostSubmit
165+ var existingJob * cicdv1.Job
166+
167+ for _ , addParams := range userReqBody .AddTektonTaskParams {
168+ jobName := addParams .JobName
169+ if jobName == "" {
170+ return nil , errors .New ("JobName must be set" )
171+ }
172+
173+ for i , job := range * targetJob {
174+ if job .Name == jobName {
175+ existingJob = & (* targetJob )[i ]
176+ break
177+ }
178+ }
179+
180+ if existingJob == nil {
181+ return nil , fmt .Errorf ("job with name '%s' not found" , jobName )
182+ }
183+
184+ for _ , taskDef := range addParams .TektonTask {
185+ // Check if a parameter with the same name already exists
186+ for i , existingParam := range existingJob .TektonTask .Params {
187+ if existingParam .Name == taskDef .Name {
188+ existingJob .TektonTask .Params = append (existingJob .TektonTask .Params [:i ], existingJob .TektonTask .Params [i + 1 :]... )
189+ break
190+ }
191+ }
192+
193+ existingJob .TektonTask .Params = append (existingJob .TektonTask .Params , cicdv1.ParameterValue {
194+ Name : taskDef .Name ,
195+ StringVal : taskDef .StringVal ,
196+ })
197+ }
198+ }
199+
200+ return ic , nil
201+ }
202+
203+ func updateIntegrationConfigPre (ic * cicdv1.IntegrationConfig , userReqBody * cicdv1.IntegrationConfigAPIReqRunPreBody , et git.EventType ) (* cicdv1.IntegrationConfig , error ) {
204+ targetJob := & ic .Spec .Jobs .PreSubmit
205+ var existingJob * cicdv1.Job
206+
207+ for _ , addParams := range userReqBody .AddTektonTaskParams {
208+ jobName := addParams .JobName
209+ if jobName == "" {
210+ return nil , errors .New ("JobName must be set" )
211+ }
212+
213+ for i , job := range * targetJob {
214+ if job .Name == jobName {
215+ existingJob = & (* targetJob )[i ]
216+ break
217+ }
218+ }
219+
220+ if existingJob == nil {
221+ return nil , fmt .Errorf ("job with name '%s' not found" , jobName )
222+ }
223+
224+ for _ , taskDef := range addParams .TektonTask {
225+ // Check if a parameter with the same name already exists
226+ for i , existingParam := range existingJob .TektonTask .Params {
227+ if existingParam .Name == taskDef .Name {
228+ existingJob .TektonTask .Params = append (existingJob .TektonTask .Params [:i ], existingJob .TektonTask .Params [i + 1 :]... )
229+ break
230+ }
231+ }
232+
233+ existingJob .TektonTask .Params = append (existingJob .TektonTask .Params , cicdv1.ParameterValue {
234+ Name : taskDef .Name ,
235+ StringVal : taskDef .StringVal ,
236+ })
237+ }
133238 }
134239
240+ return ic , nil
241+ }
242+
243+ func buildPullRequestWebhook (userReq * cicdv1.IntegrationConfigAPIReqRunPreBody , user string ) (* git.PullRequest , error ) {
135244 baseBranch := userReq .BaseBranch
136245 headBranch := userReq .HeadBranch
137246 if baseBranch == "" {
@@ -158,13 +267,7 @@ func buildPullRequestWebhook(body io.Reader, user string) (*git.PullRequest, err
158267 }, nil
159268}
160269
161- func buildPushWebhook (body io.Reader ) (* git.Push , error ) {
162- userReq := & cicdv1.IntegrationConfigAPIReqRunPostBody {}
163- decoder := json .NewDecoder (body )
164- if err := decoder .Decode (userReq ); err != nil {
165- return nil , err
166- }
167-
270+ func buildPushWebhook (userReq * cicdv1.IntegrationConfigAPIReqRunPostBody ) (* git.Push , error ) {
168271 branch := userReq .Branch
169272 if branch == "" {
170273 branch = defaultBranch
0 commit comments