From 8ac3b920be1505c13d7dc50504ca9fd0cb571412 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Tue, 5 Nov 2024 16:42:38 +0800 Subject: [PATCH 1/4] add disable-mirror-actions-unit command --- cmd/doctor.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmd/doctor.go b/cmd/doctor.go index e433f4adc5d3c..aecb5049ffd4c 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -14,10 +14,14 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/migrations" migrate_base "code.gitea.io/gitea/models/migrations/base" + repo_model "code.gitea.io/gitea/models/repo" + unit_model "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/doctor" + repo_service "code.gitea.io/gitea/services/repository" "github.com/urfave/cli/v2" "xorm.io/xorm" @@ -33,6 +37,7 @@ var CmdDoctor = &cli.Command{ cmdDoctorCheck, cmdRecreateTable, cmdDoctorConvert, + cmdDisableMirrorActionsUnit, }, } @@ -138,6 +143,48 @@ func runRecreateTable(ctx *cli.Context) error { }) } +var cmdDisableMirrorActionsUnit = &cli.Command{ + Name: "disable-mirror-actions-unit", + Usage: "Disable the actions unit for all mirrors", + Action: runDisableMirrorActionsUnit, +} + +func runDisableMirrorActionsUnit(_ *cli.Context) error { + stdCtx, cancel := installSignals() + defer cancel() + + if err := initDB(stdCtx); err != nil { + return err + } + + const pageSize = repo_model.RepositoryListDefaultPageSize + for page := 1; ; page++ { + repos, count, err := repo_model.SearchRepository(stdCtx, &repo_model.SearchRepoOptions{ + ListOptions: db.ListOptions{ + PageSize: pageSize, + Page: page, + }, + Mirror: optional.Some(true), + }) + if err != nil { + return fmt.Errorf("SearchRepository: %w", err) + } + if len(repos) == 0 { + break + } + log.Info("Processing %d-%d of %d repos", (page-1)*pageSize+1, (page-1)*pageSize+len(repos), count) + for _, repo := range repos { + if err := repo_service.UpdateRepositoryUnits(stdCtx, repo, nil, []unit_model.Type{unit_model.TypeActions}); err != nil { + return err + } + } + } + + log.Info("Finish processing") + + return nil +} + func setupDoctorDefaultLogger(ctx *cli.Context, colorize bool) { // Silence the default loggers setupConsoleLogger(log.FATAL, log.CanColorStderr, os.Stderr) From 15bf56e5d8aee37a7be7acc45b3ae24b1d81d1f4 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 6 Nov 2024 14:08:50 +0800 Subject: [PATCH 2/4] add disableMirrorActionsUnit --- cmd/doctor.go | 47 -------------------------- services/doctor/actions.go | 69 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 47 deletions(-) create mode 100644 services/doctor/actions.go diff --git a/cmd/doctor.go b/cmd/doctor.go index aecb5049ffd4c..e433f4adc5d3c 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -14,14 +14,10 @@ import ( "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/migrations" migrate_base "code.gitea.io/gitea/models/migrations/base" - repo_model "code.gitea.io/gitea/models/repo" - unit_model "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/doctor" - repo_service "code.gitea.io/gitea/services/repository" "github.com/urfave/cli/v2" "xorm.io/xorm" @@ -37,7 +33,6 @@ var CmdDoctor = &cli.Command{ cmdDoctorCheck, cmdRecreateTable, cmdDoctorConvert, - cmdDisableMirrorActionsUnit, }, } @@ -143,48 +138,6 @@ func runRecreateTable(ctx *cli.Context) error { }) } -var cmdDisableMirrorActionsUnit = &cli.Command{ - Name: "disable-mirror-actions-unit", - Usage: "Disable the actions unit for all mirrors", - Action: runDisableMirrorActionsUnit, -} - -func runDisableMirrorActionsUnit(_ *cli.Context) error { - stdCtx, cancel := installSignals() - defer cancel() - - if err := initDB(stdCtx); err != nil { - return err - } - - const pageSize = repo_model.RepositoryListDefaultPageSize - for page := 1; ; page++ { - repos, count, err := repo_model.SearchRepository(stdCtx, &repo_model.SearchRepoOptions{ - ListOptions: db.ListOptions{ - PageSize: pageSize, - Page: page, - }, - Mirror: optional.Some(true), - }) - if err != nil { - return fmt.Errorf("SearchRepository: %w", err) - } - if len(repos) == 0 { - break - } - log.Info("Processing %d-%d of %d repos", (page-1)*pageSize+1, (page-1)*pageSize+len(repos), count) - for _, repo := range repos { - if err := repo_service.UpdateRepositoryUnits(stdCtx, repo, nil, []unit_model.Type{unit_model.TypeActions}); err != nil { - return err - } - } - } - - log.Info("Finish processing") - - return nil -} - func setupDoctorDefaultLogger(ctx *cli.Context, colorize bool) { // Silence the default loggers setupConsoleLogger(log.FATAL, log.CanColorStderr, os.Stderr) diff --git a/services/doctor/actions.go b/services/doctor/actions.go new file mode 100644 index 0000000000000..d8be9a6423795 --- /dev/null +++ b/services/doctor/actions.go @@ -0,0 +1,69 @@ +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 { + 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, + }) +} From 408de633eb299891a38b49261261fe365da6fe14 Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 6 Nov 2024 14:15:51 +0800 Subject: [PATCH 3/4] fix --- services/doctor/actions.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/doctor/actions.go b/services/doctor/actions.go index d8be9a6423795..1a09f7fb9703d 100644 --- a/services/doctor/actions.go +++ b/services/doctor/actions.go @@ -42,8 +42,7 @@ func disableMirrorActionsUnit(ctx context.Context, logger log.Logger, autofix bo } else { logger.Warn("Found %d mirrors with actions unit enabled", len(reposToFix)) } - - if !autofix { + if !autofix || len(reposToFix) == 0 { return nil } @@ -52,7 +51,6 @@ func disableMirrorActionsUnit(ctx context.Context, logger log.Logger, autofix bo return err } } - logger.Info("Fixed %d mirrors with actions unit enabled", len(reposToFix)) return nil From bdd72f88eb8d014f970070efea9c8c5946ad626a Mon Sep 17 00:00:00 2001 From: Zettat123 Date: Wed, 6 Nov 2024 14:23:47 +0800 Subject: [PATCH 4/4] fix copyright --- services/doctor/actions.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/doctor/actions.go b/services/doctor/actions.go index 1a09f7fb9703d..7c44fb83920c3 100644 --- a/services/doctor/actions.go +++ b/services/doctor/actions.go @@ -1,3 +1,6 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + package doctor import (