Cancel previous runs of the same PR automatically (#29961)

Follow #25716. Also cancel previous runs for `pull_request_sync`.

It's not a bug since it original PR said "if the event is push".

The main change is
https://github.com/go-gitea/gitea/pull/29961/files#diff-08adda3f8ae0360937f46abb1f4418603bd3518522baa356be11c6c7ac4abcc3.

And also rename `CancelRunningJobs` to `CancelPreviousJobs` to make it
more clear.

(cherry picked from commit b150ff0bab3fc6c419edf1569a0271ebcb9734fa)
This commit is contained in:
Jason Song 2024-03-21 15:01:35 +08:00 committed by Earl Warren
parent 869286595f
commit 547d32c492
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
5 changed files with 18 additions and 17 deletions

View file

@ -170,15 +170,16 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
return err return err
} }
// CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow. // CancelPreviousJobs cancels all previous jobs of the same repository, reference, workflow, and event.
func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error { // It's useful when a new run is triggered, and all previous runs needn't be continued anymore.
// Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'. func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error {
// Find all runs in the specified repository, reference, and workflow with non-final status
runs, total, err := db.FindAndCount[ActionRun](ctx, FindRunOptions{ runs, total, err := db.FindAndCount[ActionRun](ctx, FindRunOptions{
RepoID: repoID, RepoID: repoID,
Ref: ref, Ref: ref,
WorkflowID: workflowID, WorkflowID: workflowID,
TriggerEvent: event, TriggerEvent: event,
Status: []Status{StatusRunning, StatusWaiting}, Status: []Status{StatusRunning, StatusWaiting, StatusBlocked},
}) })
if err != nil { if err != nil {
return err return err

View file

@ -127,14 +127,14 @@ func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) er
return fmt.Errorf("DeleteCronTaskByRepo: %v", err) return fmt.Errorf("DeleteCronTaskByRepo: %v", err)
} }
// cancel running cron jobs of this repository and delete old schedules // cancel running cron jobs of this repository and delete old schedules
if err := CancelRunningJobs( if err := CancelPreviousJobs(
ctx, ctx,
repo.ID, repo.ID,
repo.DefaultBranch, repo.DefaultBranch,
"", "",
webhook_module.HookEventSchedule, webhook_module.HookEventSchedule,
); err != nil { ); err != nil {
return fmt.Errorf("CancelRunningJobs: %v", err) return fmt.Errorf("CancelPreviousJobs: %v", err)
} }
return nil return nil
} }

View file

@ -348,17 +348,17 @@ func handleWorkflows(
continue continue
} }
// cancel running jobs if the event is push // cancel running jobs if the event is push or pull_request_sync
if run.Event == webhook_module.HookEventPush { if run.Event == webhook_module.HookEventPush ||
// cancel running jobs of the same workflow run.Event == webhook_module.HookEventPullRequestSync {
if err := actions_model.CancelRunningJobs( if err := actions_model.CancelPreviousJobs(
ctx, ctx,
run.RepoID, run.RepoID,
run.Ref, run.Ref,
run.WorkflowID, run.WorkflowID,
run.Event, run.Event,
); err != nil { ); err != nil {
log.Error("CancelRunningJobs: %v", err) log.Error("CancelPreviousJobs: %v", err)
} }
} }

View file

@ -55,14 +55,14 @@ func startTasks(ctx context.Context) error {
// cancel running jobs if the event is push // cancel running jobs if the event is push
if row.Schedule.Event == webhook_module.HookEventPush { if row.Schedule.Event == webhook_module.HookEventPush {
// cancel running jobs of the same workflow // cancel running jobs of the same workflow
if err := actions_model.CancelRunningJobs( if err := actions_model.CancelPreviousJobs(
ctx, ctx,
row.RepoID, row.RepoID,
row.Schedule.Ref, row.Schedule.Ref,
row.Schedule.WorkflowID, row.Schedule.WorkflowID,
webhook_module.HookEventSchedule, webhook_module.HookEventSchedule,
); err != nil { ); err != nil {
log.Error("CancelRunningJobs: %v", err) log.Error("CancelPreviousJobs: %v", err)
} }
} }

View file

@ -371,14 +371,14 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
log.Error("DeleteCronTaskByRepo: %v", err) log.Error("DeleteCronTaskByRepo: %v", err)
} }
// cancel running cron jobs of this repository and delete old schedules // cancel running cron jobs of this repository and delete old schedules
if err := actions_model.CancelRunningJobs( if err := actions_model.CancelPreviousJobs(
ctx, ctx,
repo.ID, repo.ID,
from, from,
"", "",
webhook_module.HookEventSchedule, webhook_module.HookEventSchedule,
); err != nil { ); err != nil {
log.Error("CancelRunningJobs: %v", err) log.Error("CancelPreviousJobs: %v", err)
} }
err2 = gitRepo.SetDefaultBranch(ctx, repo, to) err2 = gitRepo.SetDefaultBranch(ctx, repo, to)
@ -536,14 +536,14 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
log.Error("DeleteCronTaskByRepo: %v", err) log.Error("DeleteCronTaskByRepo: %v", err)
} }
// cancel running cron jobs of this repository and delete old schedules // cancel running cron jobs of this repository and delete old schedules
if err := actions_model.CancelRunningJobs( if err := actions_model.CancelPreviousJobs(
ctx, ctx,
repo.ID, repo.ID,
oldDefaultBranchName, oldDefaultBranchName,
"", "",
webhook_module.HookEventSchedule, webhook_module.HookEventSchedule,
); err != nil { ); err != nil {
log.Error("CancelRunningJobs: %v", err) log.Error("CancelPreviousJobs: %v", err)
} }
if err := gitRepo.SetDefaultBranch(newBranchName); err != nil { if err := gitRepo.SetDefaultBranch(newBranchName); err != nil {