feat: configurable default units for mirrors (#7902)

Partial implementation of b1f42a0cdd

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 <zettat123@gmail.com>
Idea-by: lng2020

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/7902): <!--number 7902 --><!--line 0 --><!--description Y29uZmlndXJhYmxlIGRlZmF1bHQgdW5pdHMgZm9yIG1pcnJvcnM=-->configurable default units for mirrors<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7902
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: 0ko <0ko@noreply.codeberg.org>
Co-committed-by: 0ko <0ko@noreply.codeberg.org>
This commit is contained in:
0ko 2025-06-03 08:12:29 +02:00 committed by Earl Warren
parent 374def9922
commit 9ecef99ab9
4 changed files with 37 additions and 4 deletions

View file

@ -1025,6 +1025,10 @@ LEVEL = Info
;; The set of allowed values and rules are the same as DEFAULT_REPO_UNITS. ;; The set of allowed values and rules are the same as DEFAULT_REPO_UNITS.
;DEFAULT_FORK_REPO_UNITS = repo.code,repo.pulls ;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 by placing them in a directory named after the repository
;PREFIX_ARCHIVE_FILES = true ;PREFIX_ARCHIVE_FILES = true
;; ;;

View file

@ -1,4 +1,5 @@
// Copyright 2017 The Gitea Authors. All rights reserved. // Copyright 2017 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package unit package unit
@ -69,7 +70,7 @@ func (u Type) LogString() string {
} }
var ( var (
// AllRepoUnitTypes contains all the unit types // AllRepoUnitTypes contains all units
AllRepoUnitTypes = []Type{ AllRepoUnitTypes = []Type{
TypeCode, TypeCode,
TypeIssues, TypeIssues,
@ -83,7 +84,7 @@ var (
TypeActions, TypeActions,
} }
// DefaultRepoUnits contains the default unit types // DefaultRepoUnits contains default units for regular repos
DefaultRepoUnits = []Type{ DefaultRepoUnits = []Type{
TypeCode, TypeCode,
TypeIssues, TypeIssues,
@ -95,12 +96,22 @@ var (
TypeActions, TypeActions,
} }
// ForkRepoUnits contains the default unit types for forks // ForkRepoUnits contains default units for forks
DefaultForkRepoUnits = []Type{ DefaultForkRepoUnits = []Type{
TypeCode, TypeCode,
TypePullRequests, TypePullRequests,
} }
// DefaultMirrorRepoUnits contains default units for mirrors
DefaultMirrorRepoUnits = []Type{
TypeCode,
TypeIssues,
TypeReleases,
TypeWiki,
TypeProjects,
TypePackages,
}
// NotAllowedDefaultRepoUnits contains units that can't be default // NotAllowedDefaultRepoUnits contains units that can't be default
NotAllowedDefaultRepoUnits = []Type{ NotAllowedDefaultRepoUnits = []Type{
TypeExternalWiki, TypeExternalWiki,
@ -172,6 +183,8 @@ func LoadUnitConfig() error {
if len(DefaultRepoUnits) == 0 { if len(DefaultRepoUnits) == 0 {
return errors.New("no default repository units found") return errors.New("no default repository units found")
} }
// Default fork repo units
setDefaultForkRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultForkRepoUnits...) setDefaultForkRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultForkRepoUnits...)
if len(invalidKeys) > 0 { if len(invalidKeys) > 0 {
log.Warn("Invalid keys in default fork repo units: %s", strings.Join(invalidKeys, ", ")) 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") 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 // Collect the allowed repo unit groups. Mutually exclusive units are
// grouped together. // grouped together.
AllowedRepoUnitGroups = [][]Type{} AllowedRepoUnitGroups = [][]Type{}

View file

@ -69,8 +69,11 @@ func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, re
// insert units for repo // insert units for repo
defaultUnits := unit.DefaultRepoUnits defaultUnits := unit.DefaultRepoUnits
if isFork { switch {
case isFork:
defaultUnits = unit.DefaultForkRepoUnits defaultUnits = unit.DefaultForkRepoUnits
case repo.IsMirror:
defaultUnits = unit.DefaultMirrorRepoUnits
} }
units := make([]repo_model.RepoUnit, 0, len(defaultUnits)) units := make([]repo_model.RepoUnit, 0, len(defaultUnits))
for _, tp := range defaultUnits { for _, tp := range defaultUnits {

View file

@ -1,4 +1,5 @@
// Copyright 2019 The Gitea Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved.
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
package setting package setting
@ -52,6 +53,7 @@ var (
DisabledRepoUnits []string DisabledRepoUnits []string
DefaultRepoUnits []string DefaultRepoUnits []string
DefaultForkRepoUnits []string DefaultForkRepoUnits []string
DefaultMirrorRepoUnits []string
PrefixArchiveFiles bool PrefixArchiveFiles bool
DisableMigrations bool DisableMigrations bool
DisableStars bool DisableStars bool
@ -175,6 +177,7 @@ var (
DisabledRepoUnits: []string{}, DisabledRepoUnits: []string{},
DefaultRepoUnits: []string{}, DefaultRepoUnits: []string{},
DefaultForkRepoUnits: []string{}, DefaultForkRepoUnits: []string{},
DefaultMirrorRepoUnits: []string{},
PrefixArchiveFiles: true, PrefixArchiveFiles: true,
DisableMigrations: false, DisableMigrations: false,
DisableStars: false, DisableStars: false,