@@ -282,77 +282,72 @@ func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID strin
282282// InsertRun inserts a run
283283// The title will be cut off at 255 characters if it's longer than 255 characters.
284284func InsertRun (ctx context.Context , run * ActionRun , jobs []* jobparser.SingleWorkflow ) error {
285- ctx , committer , err := db .TxContext (ctx )
286- if err != nil {
287- return err
288- }
289- defer committer .Close ()
290-
291- index , err := db .GetNextResourceIndex (ctx , "action_run_index" , run .RepoID )
292- if err != nil {
293- return err
294- }
295- run .Index = index
296- run .Title = util .EllipsisDisplayString (run .Title , 255 )
297-
298- if err := db .Insert (ctx , run ); err != nil {
299- return err
300- }
301-
302- if run .Repo == nil {
303- repo , err := repo_model .GetRepositoryByID (ctx , run .RepoID )
285+ return db .WithTx (ctx , func (ctx context.Context ) error {
286+ index , err := db .GetNextResourceIndex (ctx , "action_run_index" , run .RepoID )
304287 if err != nil {
305288 return err
306289 }
307- run .Repo = repo
308- }
290+ run .Index = index
291+ run . Title = util . EllipsisDisplayString ( run . Title , 255 )
309292
310- if err := updateRepoRunsNumbers (ctx , run .Repo ); err != nil {
311- return err
312- }
313-
314- runJobs := make ([]* ActionRunJob , 0 , len (jobs ))
315- var hasWaiting bool
316- for _ , v := range jobs {
317- id , job := v .Job ()
318- needs := job .Needs ()
319- if err := v .SetJob (id , job .EraseNeeds ()); err != nil {
293+ if err := db .Insert (ctx , run ); err != nil {
320294 return err
321295 }
322- payload , _ := v .Marshal ()
323- status := StatusWaiting
324- if len (needs ) > 0 || run .NeedApproval {
325- status = StatusBlocked
326- } else {
327- hasWaiting = true
296+
297+ if run .Repo == nil {
298+ repo , err := repo_model .GetRepositoryByID (ctx , run .RepoID )
299+ if err != nil {
300+ return err
301+ }
302+ run .Repo = repo
328303 }
329- job .Name = util .EllipsisDisplayString (job .Name , 255 )
330- runJobs = append (runJobs , & ActionRunJob {
331- RunID : run .ID ,
332- RepoID : run .RepoID ,
333- OwnerID : run .OwnerID ,
334- CommitSHA : run .CommitSHA ,
335- IsForkPullRequest : run .IsForkPullRequest ,
336- Name : job .Name ,
337- WorkflowPayload : payload ,
338- JobID : id ,
339- Needs : needs ,
340- RunsOn : job .RunsOn (),
341- Status : status ,
342- })
343- }
344- if err := db .Insert (ctx , runJobs ); err != nil {
345- return err
346- }
347304
348- // if there is a job in the waiting status, increase tasks version.
349- if hasWaiting {
350- if err := IncreaseTaskVersion (ctx , run .OwnerID , run .RepoID ); err != nil {
305+ if err := updateRepoRunsNumbers (ctx , run .Repo ); err != nil {
351306 return err
352307 }
353- }
354308
355- return committer .Commit ()
309+ runJobs := make ([]* ActionRunJob , 0 , len (jobs ))
310+ var hasWaiting bool
311+ for _ , v := range jobs {
312+ id , job := v .Job ()
313+ needs := job .Needs ()
314+ if err := v .SetJob (id , job .EraseNeeds ()); err != nil {
315+ return err
316+ }
317+ payload , _ := v .Marshal ()
318+ status := StatusWaiting
319+ if len (needs ) > 0 || run .NeedApproval {
320+ status = StatusBlocked
321+ } else {
322+ hasWaiting = true
323+ }
324+ job .Name = util .EllipsisDisplayString (job .Name , 255 )
325+ runJobs = append (runJobs , & ActionRunJob {
326+ RunID : run .ID ,
327+ RepoID : run .RepoID ,
328+ OwnerID : run .OwnerID ,
329+ CommitSHA : run .CommitSHA ,
330+ IsForkPullRequest : run .IsForkPullRequest ,
331+ Name : job .Name ,
332+ WorkflowPayload : payload ,
333+ JobID : id ,
334+ Needs : needs ,
335+ RunsOn : job .RunsOn (),
336+ Status : status ,
337+ })
338+ }
339+ if err := db .Insert (ctx , runJobs ); err != nil {
340+ return err
341+ }
342+
343+ // if there is a job in the waiting status, increase tasks version.
344+ if hasWaiting {
345+ if err := IncreaseTaskVersion (ctx , run .OwnerID , run .RepoID ); err != nil {
346+ return err
347+ }
348+ }
349+ return nil
350+ })
356351}
357352
358353func GetRunByRepoAndID (ctx context.Context , repoID , runID int64 ) (* ActionRun , error ) {
0 commit comments