Skip to content

Commit 1ef2eb5

Browse files
authored
Remove scheduled action tasks if the repo is archived (#30224)
Fix #30220
1 parent 751997a commit 1ef2eb5

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

routers/api/v1/repo/repo.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
"time"
1414

15+
actions_model "code.gitea.io/gitea/models/actions"
1516
activities_model "code.gitea.io/gitea/models/activities"
1617
"code.gitea.io/gitea/models/db"
1718
"code.gitea.io/gitea/models/organization"
@@ -31,6 +32,7 @@ import (
3132
"code.gitea.io/gitea/modules/validation"
3233
"code.gitea.io/gitea/modules/web"
3334
"code.gitea.io/gitea/routers/api/v1/utils"
35+
actions_service "code.gitea.io/gitea/services/actions"
3436
"code.gitea.io/gitea/services/context"
3537
"code.gitea.io/gitea/services/convert"
3638
"code.gitea.io/gitea/services/issue"
@@ -1035,13 +1037,21 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
10351037
ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
10361038
return err
10371039
}
1040+
if err := actions_model.CleanRepoScheduleTasks(ctx, repo); err != nil {
1041+
log.Error("CleanRepoScheduleTasks for archived repo %s/%s: %v", ctx.Repo.Owner.Name, repo.Name, err)
1042+
}
10381043
log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
10391044
} else {
10401045
if err := repo_model.SetArchiveRepoState(ctx, repo, *opts.Archived); err != nil {
10411046
log.Error("Tried to un-archive a repo: %s", err)
10421047
ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
10431048
return err
10441049
}
1050+
if ctx.Repo.Repository.UnitEnabled(ctx, unit_model.TypeActions) {
1051+
if err := actions_service.DetectAndHandleSchedules(ctx, repo); err != nil {
1052+
log.Error("DetectAndHandleSchedules for un-archived repo %s/%s: %v", ctx.Repo.Owner.Name, repo.Name, err)
1053+
}
1054+
}
10451055
log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
10461056
}
10471057
}

routers/web/repo/setting/setting.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"code.gitea.io/gitea/models"
16+
actions_model "code.gitea.io/gitea/models/actions"
1617
"code.gitea.io/gitea/models/db"
1718
"code.gitea.io/gitea/models/organization"
1819
repo_model "code.gitea.io/gitea/models/repo"
@@ -29,6 +30,7 @@ import (
2930
"code.gitea.io/gitea/modules/util"
3031
"code.gitea.io/gitea/modules/validation"
3132
"code.gitea.io/gitea/modules/web"
33+
actions_service "code.gitea.io/gitea/services/actions"
3234
asymkey_service "code.gitea.io/gitea/services/asymkey"
3335
"code.gitea.io/gitea/services/context"
3436
"code.gitea.io/gitea/services/forms"
@@ -897,6 +899,10 @@ func SettingsPost(ctx *context.Context) {
897899
return
898900
}
899901

902+
if err := actions_model.CleanRepoScheduleTasks(ctx, repo); err != nil {
903+
log.Error("CleanRepoScheduleTasks for archived repo %s/%s: %v", ctx.Repo.Owner.Name, repo.Name, err)
904+
}
905+
900906
ctx.Flash.Success(ctx.Tr("repo.settings.archive.success"))
901907

902908
log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
@@ -915,6 +921,12 @@ func SettingsPost(ctx *context.Context) {
915921
return
916922
}
917923

924+
if ctx.Repo.Repository.UnitEnabled(ctx, unit_model.TypeActions) {
925+
if err := actions_service.DetectAndHandleSchedules(ctx, repo); err != nil {
926+
log.Error("DetectAndHandleSchedules for un-archived repo %s/%s: %v", ctx.Repo.Owner.Name, repo.Name, err)
927+
}
928+
}
929+
918930
ctx.Flash.Success(ctx.Tr("repo.settings.unarchive.success"))
919931

920932
log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)

services/actions/notifier_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func notify(ctx context.Context, input *notifyInput) error {
117117
log.Debug("ignore executing %v for event %v whose doer is %v", getMethod(ctx), input.Event, input.Doer.Name)
118118
return nil
119119
}
120-
if input.Repo.IsEmpty {
120+
if input.Repo.IsEmpty || input.Repo.IsArchived {
121121
return nil
122122
}
123123
if unit_model.TypeActions.UnitGlobalDisabled() {
@@ -501,7 +501,7 @@ func handleSchedules(
501501

502502
// DetectAndHandleSchedules detects the schedule workflows on the default branch and create schedule tasks
503503
func DetectAndHandleSchedules(ctx context.Context, repo *repo_model.Repository) error {
504-
if repo.IsEmpty {
504+
if repo.IsEmpty || repo.IsArchived {
505505
return nil
506506
}
507507

services/actions/schedule_tasks.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func startTasks(ctx context.Context) error {
6666
}
6767
}
6868

69+
if row.Repo.IsArchived {
70+
// Skip if the repo is archived
71+
continue
72+
}
73+
6974
cfg, err := row.Repo.GetUnit(ctx, unit.TypeActions)
7075
if err != nil {
7176
if repo_model.IsErrUnitTypeNotExist(err) {

0 commit comments

Comments
 (0)