From 44781f9f5c4ede618660d8cfe42437f0e8dc22a0 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Tue, 25 Jul 2023 11:15:55 +0800 Subject: Implement auto-cancellation of concurrent jobs if the event is push (#25716) - cancel running jobs if the event is push - Add a new function `CancelRunningJobs` to cancel all running jobs of a run - Update `FindRunOptions` struct to include `Ref` field and update its condition in `toConds` function - Implement auto cancellation of running jobs in the same workflow in `notify` function related task: https://github.com/go-gitea/gitea/pull/22751/ --------- Signed-off-by: Bo-Yi Wu Signed-off-by: appleboy Co-authored-by: Jason Song Co-authored-by: delvh --- services/actions/notifier_helper.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'services/actions') diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 90ad3001ba..764d24a7db 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -230,16 +230,31 @@ func notify(ctx context.Context, input *notifyInput) error { log.Error("jobparser.Parse: %v", err) continue } + + // cancel running jobs if the event is push + if run.Event == webhook_module.HookEventPush { + // cancel running jobs of the same workflow + if err := actions_model.CancelRunningJobs( + ctx, + run.RepoID, + run.Ref, + run.WorkflowID, + ); err != nil { + log.Error("CancelRunningJobs: %v", err) + } + } + if err := actions_model.InsertRun(ctx, run, jobs); err != nil { log.Error("InsertRun: %v", err) continue } - if jobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: run.ID}); err != nil { + + alljobs, _, err := actions_model.FindRunJobs(ctx, actions_model.FindRunJobOptions{RunID: run.ID}) + if err != nil { log.Error("FindRunJobs: %v", err) - } else { - CreateCommitStatus(ctx, jobs...) + continue } - + CreateCommitStatus(ctx, alljobs...) } return nil } -- cgit v1.2.3