| 
 | 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