@@ -194,19 +194,6 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
194194 var workflows []* jobparser.SingleWorkflow
195195 var entry * git.TreeEntry
196196
197- for _ , e := range entries {
198- if e .Name () != workflowID {
199- continue
200- }
201- entry = e
202- break
203- }
204-
205- content , err := actions .GetContentFromEntry (entry )
206- if err != nil {
207- return err
208- }
209-
210197 run := & actions_model.ActionRun {
211198 Title : strings .SplitN (runTargetCommit .CommitMessage , "\n " , 2 )[0 ],
212199 RepoID : repo .ID ,
@@ -223,76 +210,90 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
223210 Status : actions_model .StatusWaiting ,
224211 }
225212
226- giteaCtx := GenerateGiteaContext (run , nil )
227-
228- workflows , err = jobparser .Parse (content , jobparser .WithGitContext (giteaCtx .ToGitHubContext ()))
229- if err != nil {
230- return err
213+ for _ , e := range entries {
214+ if e .Name () != workflowID {
215+ continue
216+ }
217+ entry = e
218+ break
231219 }
232220
233- if len (workflows ) > 0 && workflows [0 ].RunName != "" {
234- run .Title = workflows [0 ].RunName
235- }
221+ if entry != nil {
222+ content , err := actions .GetContentFromEntry (entry )
223+ if err != nil {
224+ return err
225+ }
236226
237- if len (workflows ) == 0 {
238- return util .ErrorWrapLocale (
239- util .NewNotExistErrorf ("workflow %q doesn't exist" , workflowID ),
240- "actions.workflow.not_found" , workflowID ,
241- )
242- }
227+ giteaCtx := GenerateGiteaContext (run , nil )
243228
244- // get inputs from post
245- workflow := & model.Workflow {
246- RawOn : workflows [0 ].RawOn ,
247- }
248- inputsWithDefaults := make (map [string ]any )
249- if workflowDispatch := workflow .WorkflowDispatchConfig (); workflowDispatch != nil {
250- if err = processInputs (workflowDispatch , inputsWithDefaults ); err != nil {
229+ workflows , err = jobparser .Parse (content , jobparser .WithGitContext (giteaCtx .ToGitHubContext ()))
230+ if err != nil {
251231 return err
252232 }
253- }
254233
255- // ctx.Req.PostForm -> WorkflowDispatchPayload.Inputs -> ActionRun.EventPayload -> runner: ghc.Event
256- // https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
257- // https://docs.github.com/en/webhooks/webhook-events-and-payloads#workflow_dispatch
258- workflowDispatchPayload := & api.WorkflowDispatchPayload {
259- Workflow : workflowID ,
260- Ref : ref ,
261- Repository : convert .ToRepo (ctx , repo , access_model.Permission {AccessMode : perm .AccessModeNone }),
262- Inputs : inputsWithDefaults ,
263- Sender : convert .ToUserWithAccessMode (ctx , doer , perm .AccessModeNone ),
264- }
234+ if len (workflows ) > 0 && workflows [0 ].RunName != "" {
235+ run .Title = workflows [0 ].RunName
236+ }
265237
266- var eventPayload []byte
267- if eventPayload , err = workflowDispatchPayload .JSONPayload (); err != nil {
268- return fmt .Errorf ("JSONPayload: %w" , err )
269- }
270- run .EventPayload = string (eventPayload )
271-
272- // cancel running jobs of the same workflow
273- if err := CancelPreviousJobs (
274- ctx ,
275- run .RepoID ,
276- run .Ref ,
277- run .WorkflowID ,
278- run .Event ,
279- ); err != nil {
280- log .Error ("CancelRunningJobs: %v" , err )
281- }
238+ if len (workflows ) == 0 {
239+ return util .ErrorWrapLocale (
240+ util .NewNotExistErrorf ("workflow %q doesn't exist" , workflowID ),
241+ "actions.workflow.not_found" , workflowID ,
242+ )
243+ }
282244
283- // Insert the action run and its associated jobs into the database
284- if err := actions_model .InsertRun (ctx , run , workflows ); err != nil {
285- return fmt .Errorf ("InsertRun: %w" , err )
286- }
245+ // get inputs from post
246+ workflow := & model.Workflow {
247+ RawOn : workflows [0 ].RawOn ,
248+ }
249+ inputsWithDefaults := make (map [string ]any )
250+ if workflowDispatch := workflow .WorkflowDispatchConfig (); workflowDispatch != nil {
251+ if err = processInputs (workflowDispatch , inputsWithDefaults ); err != nil {
252+ return err
253+ }
254+ }
287255
288- allJobs , err := db .Find [actions_model.ActionRunJob ](ctx , actions_model.FindRunJobOptions {RunID : run .ID })
289- if err != nil {
290- log .Error ("FindRunJobs: %v" , err )
291- }
292- CreateCommitStatus (ctx , allJobs ... )
293- for _ , job := range allJobs {
294- notify_service .WorkflowJobStatusUpdate (ctx , repo , doer , job , nil )
295- }
256+ // ctx.Req.PostForm -> WorkflowDispatchPayload.Inputs -> ActionRun.EventPayload -> runner: ghc.Event
257+ // https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
258+ // https://docs.github.com/en/webhooks/webhook-events-and-payloads#workflow_dispatch
259+ workflowDispatchPayload := & api.WorkflowDispatchPayload {
260+ Workflow : workflowID ,
261+ Ref : ref ,
262+ Repository : convert .ToRepo (ctx , repo , access_model.Permission {AccessMode : perm .AccessModeNone }),
263+ Inputs : inputsWithDefaults ,
264+ Sender : convert .ToUserWithAccessMode (ctx , doer , perm .AccessModeNone ),
265+ }
266+
267+ var eventPayload []byte
268+ if eventPayload , err = workflowDispatchPayload .JSONPayload (); err != nil {
269+ return fmt .Errorf ("JSONPayload: %w" , err )
270+ }
271+ run .EventPayload = string (eventPayload )
272+
273+ // cancel running jobs of the same workflow
274+ if err := CancelPreviousJobs (
275+ ctx ,
276+ run .RepoID ,
277+ run .Ref ,
278+ run .WorkflowID ,
279+ run .Event ,
280+ ); err != nil {
281+ log .Error ("CancelRunningJobs: %v" , err )
282+ }
283+
284+ // Insert the action run and its associated jobs into the database
285+ if err := actions_model .InsertRun (ctx , run , workflows ); err != nil {
286+ return fmt .Errorf ("InsertRun: %w" , err )
287+ }
296288
289+ allJobs , err := db .Find [actions_model.ActionRunJob ](ctx , actions_model.FindRunJobOptions {RunID : run .ID })
290+ if err != nil {
291+ log .Error ("FindRunJobs: %v" , err )
292+ }
293+ CreateCommitStatus (ctx , allJobs ... )
294+ for _ , job := range allJobs {
295+ notify_service .WorkflowJobStatusUpdate (ctx , repo , doer , job , nil )
296+ }
297+ }
297298 return nil
298299}
0 commit comments