aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Song <i@wolfogre.com>2024-03-21 15:01:35 +0800
committerGitHub <noreply@github.com>2024-03-21 15:01:35 +0800
commitb150ff0bab3fc6c419edf1569a0271ebcb9734fa (patch)
tree244e3ab96a5fad845f3a8cbe8b9612f317aa92ca
parent3ee39db34efd532626d710de6717bf3c6255c10e (diff)
downloadgitea-b150ff0bab3fc6c419edf1569a0271ebcb9734fa.tar.gz
gitea-b150ff0bab3fc6c419edf1569a0271ebcb9734fa.zip
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.
-rw-r--r--models/actions/run.go9
-rw-r--r--models/actions/schedule.go4
-rw-r--r--services/actions/notifier_helper.go10
-rw-r--r--services/actions/schedule_tasks.go4
-rw-r--r--services/repository/branch.go8
5 files changed, 18 insertions, 17 deletions
diff --git a/models/actions/run.go b/models/actions/run.go
index 7b3125949b..fa9db0b554 100644
--- a/models/actions/run.go
+++ b/models/actions/run.go
@@ -170,15 +170,16 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
return err
}
-// CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow.
-func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error {
- // Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'.
+// CancelPreviousJobs cancels all previous jobs of the same repository, reference, workflow, and event.
+// It's useful when a new run is triggered, and all previous runs needn't be continued anymore.
+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{
RepoID: repoID,
Ref: ref,
WorkflowID: workflowID,
TriggerEvent: event,
- Status: []Status{StatusRunning, StatusWaiting},
+ Status: []Status{StatusRunning, StatusWaiting, StatusBlocked},
})
if err != nil {
return err
diff --git a/models/actions/schedule.go b/models/actions/schedule.go
index d450e7aa07..3646a046a0 100644
--- a/models/actions/schedule.go
+++ b/models/actions/schedule.go
@@ -127,14 +127,14 @@ func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) er
return fmt.Errorf("DeleteCronTaskByRepo: %v", err)
}
// cancel running cron jobs of this repository and delete old schedules
- if err := CancelRunningJobs(
+ if err := CancelPreviousJobs(
ctx,
repo.ID,
repo.DefaultBranch,
"",
webhook_module.HookEventSchedule,
); err != nil {
- return fmt.Errorf("CancelRunningJobs: %v", err)
+ return fmt.Errorf("CancelPreviousJobs: %v", err)
}
return nil
}
diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go
index fafb6ab40e..66a19844c2 100644
--- a/services/actions/notifier_helper.go
+++ b/services/actions/notifier_helper.go
@@ -317,17 +317,17 @@ func handleWorkflows(
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(
+ // cancel running jobs if the event is push or pull_request_sync
+ if run.Event == webhook_module.HookEventPush ||
+ run.Event == webhook_module.HookEventPullRequestSync {
+ if err := actions_model.CancelPreviousJobs(
ctx,
run.RepoID,
run.Ref,
run.WorkflowID,
run.Event,
); err != nil {
- log.Error("CancelRunningJobs: %v", err)
+ log.Error("CancelPreviousJobs: %v", err)
}
}
diff --git a/services/actions/schedule_tasks.go b/services/actions/schedule_tasks.go
index 79dd84e0cc..59862fd0d8 100644
--- a/services/actions/schedule_tasks.go
+++ b/services/actions/schedule_tasks.go
@@ -55,14 +55,14 @@ func startTasks(ctx context.Context) error {
// cancel running jobs if the event is push
if row.Schedule.Event == webhook_module.HookEventPush {
// cancel running jobs of the same workflow
- if err := actions_model.CancelRunningJobs(
+ if err := actions_model.CancelPreviousJobs(
ctx,
row.RepoID,
row.Schedule.Ref,
row.Schedule.WorkflowID,
webhook_module.HookEventSchedule,
); err != nil {
- log.Error("CancelRunningJobs: %v", err)
+ log.Error("CancelPreviousJobs: %v", err)
}
}
diff --git a/services/repository/branch.go b/services/repository/branch.go
index 0353c75fe9..229ac54f30 100644
--- a/services/repository/branch.go
+++ b/services/repository/branch.go
@@ -410,14 +410,14 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
log.Error("DeleteCronTaskByRepo: %v", err)
}
// cancel running cron jobs of this repository and delete old schedules
- if err := actions_model.CancelRunningJobs(
+ if err := actions_model.CancelPreviousJobs(
ctx,
repo.ID,
from,
"",
webhook_module.HookEventSchedule,
); err != nil {
- log.Error("CancelRunningJobs: %v", err)
+ log.Error("CancelPreviousJobs: %v", err)
}
err2 = gitrepo.SetDefaultBranch(ctx, repo, to)
@@ -575,14 +575,14 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
log.Error("DeleteCronTaskByRepo: %v", err)
}
// cancel running cron jobs of this repository and delete old schedules
- if err := actions_model.CancelRunningJobs(
+ if err := actions_model.CancelPreviousJobs(
ctx,
repo.ID,
oldDefaultBranchName,
"",
webhook_module.HookEventSchedule,
); err != nil {
- log.Error("CancelRunningJobs: %v", err)
+ log.Error("CancelPreviousJobs: %v", err)
}
if err := gitrepo.SetDefaultBranch(ctx, repo, newBranchName); err != nil {