Skip to content

Commit b150ff0

Browse files
authored
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.
1 parent 3ee39db commit b150ff0

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

models/actions/run.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,16 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
170170
return err
171171
}
172172

173-
// CancelRunningJobs cancels all running and waiting jobs associated with a specific workflow.
174-
func CancelRunningJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error {
175-
// Find all runs in the specified repository, reference, and workflow with statuses 'Running' or 'Waiting'.
173+
// CancelPreviousJobs cancels all previous jobs of the same repository, reference, workflow, and event.
174+
// It's useful when a new run is triggered, and all previous runs needn't be continued anymore.
175+
func CancelPreviousJobs(ctx context.Context, repoID int64, ref, workflowID string, event webhook_module.HookEventType) error {
176+
// Find all runs in the specified repository, reference, and workflow with non-final status
176177
runs, total, err := db.FindAndCount[ActionRun](ctx, FindRunOptions{
177178
RepoID: repoID,
178179
Ref: ref,
179180
WorkflowID: workflowID,
180181
TriggerEvent: event,
181-
Status: []Status{StatusRunning, StatusWaiting},
182+
Status: []Status{StatusRunning, StatusWaiting, StatusBlocked},
182183
})
183184
if err != nil {
184185
return err

models/actions/schedule.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ func CleanRepoScheduleTasks(ctx context.Context, repo *repo_model.Repository) er
127127
return fmt.Errorf("DeleteCronTaskByRepo: %v", err)
128128
}
129129
// cancel running cron jobs of this repository and delete old schedules
130-
if err := CancelRunningJobs(
130+
if err := CancelPreviousJobs(
131131
ctx,
132132
repo.ID,
133133
repo.DefaultBranch,
134134
"",
135135
webhook_module.HookEventSchedule,
136136
); err != nil {
137-
return fmt.Errorf("CancelRunningJobs: %v", err)
137+
return fmt.Errorf("CancelPreviousJobs: %v", err)
138138
}
139139
return nil
140140
}

services/actions/notifier_helper.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,17 @@ func handleWorkflows(
317317
continue
318318
}
319319

320-
// cancel running jobs if the event is push
321-
if run.Event == webhook_module.HookEventPush {
322-
// cancel running jobs of the same workflow
323-
if err := actions_model.CancelRunningJobs(
320+
// cancel running jobs if the event is push or pull_request_sync
321+
if run.Event == webhook_module.HookEventPush ||
322+
run.Event == webhook_module.HookEventPullRequestSync {
323+
if err := actions_model.CancelPreviousJobs(
324324
ctx,
325325
run.RepoID,
326326
run.Ref,
327327
run.WorkflowID,
328328
run.Event,
329329
); err != nil {
330-
log.Error("CancelRunningJobs: %v", err)
330+
log.Error("CancelPreviousJobs: %v", err)
331331
}
332332
}
333333

services/actions/schedule_tasks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ func startTasks(ctx context.Context) error {
5555
// cancel running jobs if the event is push
5656
if row.Schedule.Event == webhook_module.HookEventPush {
5757
// cancel running jobs of the same workflow
58-
if err := actions_model.CancelRunningJobs(
58+
if err := actions_model.CancelPreviousJobs(
5959
ctx,
6060
row.RepoID,
6161
row.Schedule.Ref,
6262
row.Schedule.WorkflowID,
6363
webhook_module.HookEventSchedule,
6464
); err != nil {
65-
log.Error("CancelRunningJobs: %v", err)
65+
log.Error("CancelPreviousJobs: %v", err)
6666
}
6767
}
6868

services/repository/branch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,14 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
410410
log.Error("DeleteCronTaskByRepo: %v", err)
411411
}
412412
// cancel running cron jobs of this repository and delete old schedules
413-
if err := actions_model.CancelRunningJobs(
413+
if err := actions_model.CancelPreviousJobs(
414414
ctx,
415415
repo.ID,
416416
from,
417417
"",
418418
webhook_module.HookEventSchedule,
419419
); err != nil {
420-
log.Error("CancelRunningJobs: %v", err)
420+
log.Error("CancelPreviousJobs: %v", err)
421421
}
422422

423423
err2 = gitrepo.SetDefaultBranch(ctx, repo, to)
@@ -575,14 +575,14 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
575575
log.Error("DeleteCronTaskByRepo: %v", err)
576576
}
577577
// cancel running cron jobs of this repository and delete old schedules
578-
if err := actions_model.CancelRunningJobs(
578+
if err := actions_model.CancelPreviousJobs(
579579
ctx,
580580
repo.ID,
581581
oldDefaultBranchName,
582582
"",
583583
webhook_module.HookEventSchedule,
584584
); err != nil {
585-
log.Error("CancelRunningJobs: %v", err)
585+
log.Error("CancelPreviousJobs: %v", err)
586586
}
587587

588588
if err := gitrepo.SetDefaultBranch(ctx, repo, newBranchName); err != nil {

0 commit comments

Comments
 (0)