diff options
author | Giteabot <teabot@gitea.io> | 2024-11-14 02:47:56 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-13 18:47:56 +0000 |
commit | a4263d341c1d0cfb4fd17279d00e2b14f9ca35c1 (patch) | |
tree | c478d6ae5442d2b287a841c8af261c2956a2e340 | |
parent | 52a66d78d444451a5f2a9219635511f8791a0a6d (diff) | |
download | gitea-a4263d341c1d0cfb4fd17279d00e2b14f9ca35c1.tar.gz gitea-a4263d341c1d0cfb4fd17279d00e2b14f9ca35c1.zip |
Add a doctor check to disable the "Actions" unit for mirrors (#32424) (#32497)
Backport #32424 by @Zettat123
Resolve #32232
Users can disable the "Actions" unit for all mirror repos by running
```
gitea doctor check --run disable-mirror-actions-unit --fix
```
Co-authored-by: Zettat123 <zettat123@gmail.com>
-rw-r--r-- | services/doctor/actions.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/services/doctor/actions.go b/services/doctor/actions.go new file mode 100644 index 0000000000..7c44fb8392 --- /dev/null +++ b/services/doctor/actions.go @@ -0,0 +1,70 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package doctor + +import ( + "context" + "fmt" + + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" + unit_model "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/optional" + repo_service "code.gitea.io/gitea/services/repository" +) + +func disableMirrorActionsUnit(ctx context.Context, logger log.Logger, autofix bool) error { + var reposToFix []*repo_model.Repository + + for page := 1; ; page++ { + repos, _, err := repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{ + ListOptions: db.ListOptions{ + PageSize: repo_model.RepositoryListDefaultPageSize, + Page: page, + }, + Mirror: optional.Some(true), + }) + if err != nil { + return fmt.Errorf("SearchRepository: %w", err) + } + if len(repos) == 0 { + break + } + + for _, repo := range repos { + if repo.UnitEnabled(ctx, unit_model.TypeActions) { + reposToFix = append(reposToFix, repo) + } + } + } + + if len(reposToFix) == 0 { + logger.Info("Found no mirror with actions unit enabled") + } else { + logger.Warn("Found %d mirrors with actions unit enabled", len(reposToFix)) + } + if !autofix || len(reposToFix) == 0 { + return nil + } + + for _, repo := range reposToFix { + if err := repo_service.UpdateRepositoryUnits(ctx, repo, nil, []unit_model.Type{unit_model.TypeActions}); err != nil { + return err + } + } + logger.Info("Fixed %d mirrors with actions unit enabled", len(reposToFix)) + + return nil +} + +func init() { + Register(&Check{ + Title: "Disable the actions unit for all mirrors", + Name: "disable-mirror-actions-unit", + IsDefault: false, + Run: disableMirrorActionsUnit, + Priority: 9, + }) +} |