From 9ecef99ab9b9a56c775680a186661ec6af8eaabf Mon Sep 17 00:00:00 2001 From: 0ko <0ko@noreply.codeberg.org> Date: Tue, 3 Jun 2025 08:12:29 +0200 Subject: [PATCH] feat: configurable default units for mirrors (#7902) Partial implementation of https://github.com/go-gitea/gitea/commit/b1f42a0cdddc8db9eef87041d6bcb328b2ef35fc Docs update: https://codeberg.org/forgejo/docs/pulls/1197 Closes #6561 * issue author only provided a reason for the option for mirrors, and there's no known reason for implementing the same option for templates yet, but this change will not make it harder to add that separately. Pull requests and Actions do not make sense for mirrors to have them, at least by default. Pull requests because changes will get overridden by upstream, Actions get triggered and are failing, filling the actions table in the DB with unwanted content, and there's a security concern, too. ## Testing * log in to https://v12.next.forgejo.org * migrate repository * example lightweight repo: https://codeberg.org/forgejo-contrib/delightful-forgejo * tick "This repository will be a mirror" * verify that the repo doesn't have these tabs: Pull requests, Actions Testing note: there's `models/unit/unit_test.go`, but I don't completely get how it works and was not able to append it with the new setting while keeping it working. Co-authored-by: Zettat123 Idea-by: lng2020 ## Release notes - Features - [PR](https://codeberg.org/forgejo/forgejo/pulls/7902): configurable default units for mirrors Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7902 Reviewed-by: Earl Warren Co-authored-by: 0ko <0ko@noreply.codeberg.org> Co-committed-by: 0ko <0ko@noreply.codeberg.org> --- custom/conf/app.example.ini | 4 ++++ models/unit/unit.go | 29 ++++++++++++++++++++++++++--- modules/repository/create.go | 5 ++++- modules/setting/repository.go | 3 +++ 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 41599d411e..da657df60c 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -1025,6 +1025,10 @@ LEVEL = Info ;; The set of allowed values and rules are the same as DEFAULT_REPO_UNITS. ;DEFAULT_FORK_REPO_UNITS = repo.code,repo.pulls ;; +;; Comma separated list of default mirror repo units. +;; The set of allowed values and rules are the same as DEFAULT_REPO_UNITS. +;DEFAULT_MIRROR_REPO_UNITS = repo.code,repo.releases,repo.issues,repo.wiki,repo.projects,repo.packages +;; ;; Prefix archive files by placing them in a directory named after the repository ;PREFIX_ARCHIVE_FILES = true ;; diff --git a/models/unit/unit.go b/models/unit/unit.go index 6251d44c9b..a14f3ff364 100644 --- a/models/unit/unit.go +++ b/models/unit/unit.go @@ -1,4 +1,5 @@ // Copyright 2017 The Gitea Authors. All rights reserved. +// Copyright 2024 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package unit @@ -69,7 +70,7 @@ func (u Type) LogString() string { } var ( - // AllRepoUnitTypes contains all the unit types + // AllRepoUnitTypes contains all units AllRepoUnitTypes = []Type{ TypeCode, TypeIssues, @@ -83,7 +84,7 @@ var ( TypeActions, } - // DefaultRepoUnits contains the default unit types + // DefaultRepoUnits contains default units for regular repos DefaultRepoUnits = []Type{ TypeCode, TypeIssues, @@ -95,12 +96,22 @@ var ( TypeActions, } - // ForkRepoUnits contains the default unit types for forks + // ForkRepoUnits contains default units for forks DefaultForkRepoUnits = []Type{ TypeCode, TypePullRequests, } + // DefaultMirrorRepoUnits contains default units for mirrors + DefaultMirrorRepoUnits = []Type{ + TypeCode, + TypeIssues, + TypeReleases, + TypeWiki, + TypeProjects, + TypePackages, + } + // NotAllowedDefaultRepoUnits contains units that can't be default NotAllowedDefaultRepoUnits = []Type{ TypeExternalWiki, @@ -172,6 +183,8 @@ func LoadUnitConfig() error { if len(DefaultRepoUnits) == 0 { return errors.New("no default repository units found") } + + // Default fork repo units setDefaultForkRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultForkRepoUnits...) if len(invalidKeys) > 0 { log.Warn("Invalid keys in default fork repo units: %s", strings.Join(invalidKeys, ", ")) @@ -181,6 +194,16 @@ func LoadUnitConfig() error { return errors.New("no default fork repository units found") } + // Default mirror repo units + setDefaultMirrorRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultMirrorRepoUnits...) + if len(invalidKeys) > 0 { + log.Warn("Invalid keys in default mirror repo units: %s", strings.Join(invalidKeys, ", ")) + } + DefaultMirrorRepoUnits = validateDefaultRepoUnits(DefaultMirrorRepoUnits, setDefaultMirrorRepoUnits) + if len(DefaultMirrorRepoUnits) == 0 { + return errors.New("no default mirror repository units found") + } + // Collect the allowed repo unit groups. Mutually exclusive units are // grouped together. AllowedRepoUnitGroups = [][]Type{} diff --git a/modules/repository/create.go b/modules/repository/create.go index 060b995bc5..becfed0370 100644 --- a/modules/repository/create.go +++ b/modules/repository/create.go @@ -69,8 +69,11 @@ func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, re // insert units for repo defaultUnits := unit.DefaultRepoUnits - if isFork { + switch { + case isFork: defaultUnits = unit.DefaultForkRepoUnits + case repo.IsMirror: + defaultUnits = unit.DefaultMirrorRepoUnits } units := make([]repo_model.RepoUnit, 0, len(defaultUnits)) for _, tp := range defaultUnits { diff --git a/modules/setting/repository.go b/modules/setting/repository.go index c9e70560d0..7e774f0139 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -1,4 +1,5 @@ // Copyright 2019 The Gitea Authors. All rights reserved. +// Copyright 2024 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package setting @@ -52,6 +53,7 @@ var ( DisabledRepoUnits []string DefaultRepoUnits []string DefaultForkRepoUnits []string + DefaultMirrorRepoUnits []string PrefixArchiveFiles bool DisableMigrations bool DisableStars bool @@ -175,6 +177,7 @@ var ( DisabledRepoUnits: []string{}, DefaultRepoUnits: []string{}, DefaultForkRepoUnits: []string{}, + DefaultMirrorRepoUnits: []string{}, PrefixArchiveFiles: true, DisableMigrations: false, DisableStars: false,