Skip to content

Commit a4263d3

Browse files
GiteaBotZettat123
andauthored
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 <[email protected]>
1 parent 52a66d7 commit a4263d3

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

services/doctor/actions.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package doctor
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"code.gitea.io/gitea/models/db"
11+
repo_model "code.gitea.io/gitea/models/repo"
12+
unit_model "code.gitea.io/gitea/models/unit"
13+
"code.gitea.io/gitea/modules/log"
14+
"code.gitea.io/gitea/modules/optional"
15+
repo_service "code.gitea.io/gitea/services/repository"
16+
)
17+
18+
func disableMirrorActionsUnit(ctx context.Context, logger log.Logger, autofix bool) error {
19+
var reposToFix []*repo_model.Repository
20+
21+
for page := 1; ; page++ {
22+
repos, _, err := repo_model.SearchRepository(ctx, &repo_model.SearchRepoOptions{
23+
ListOptions: db.ListOptions{
24+
PageSize: repo_model.RepositoryListDefaultPageSize,
25+
Page: page,
26+
},
27+
Mirror: optional.Some(true),
28+
})
29+
if err != nil {
30+
return fmt.Errorf("SearchRepository: %w", err)
31+
}
32+
if len(repos) == 0 {
33+
break
34+
}
35+
36+
for _, repo := range repos {
37+
if repo.UnitEnabled(ctx, unit_model.TypeActions) {
38+
reposToFix = append(reposToFix, repo)
39+
}
40+
}
41+
}
42+
43+
if len(reposToFix) == 0 {
44+
logger.Info("Found no mirror with actions unit enabled")
45+
} else {
46+
logger.Warn("Found %d mirrors with actions unit enabled", len(reposToFix))
47+
}
48+
if !autofix || len(reposToFix) == 0 {
49+
return nil
50+
}
51+
52+
for _, repo := range reposToFix {
53+
if err := repo_service.UpdateRepositoryUnits(ctx, repo, nil, []unit_model.Type{unit_model.TypeActions}); err != nil {
54+
return err
55+
}
56+
}
57+
logger.Info("Fixed %d mirrors with actions unit enabled", len(reposToFix))
58+
59+
return nil
60+
}
61+
62+
func init() {
63+
Register(&Check{
64+
Title: "Disable the actions unit for all mirrors",
65+
Name: "disable-mirror-actions-unit",
66+
IsDefault: false,
67+
Run: disableMirrorActionsUnit,
68+
Priority: 9,
69+
})
70+
}

0 commit comments

Comments
 (0)