Skip to content

Commit 2741546

Browse files
Repositories: by default disable all units except code and pulls on forks (#22541)
Most of the time forks are used for contributing code only, so not having issues, projects, release and packages is a better default for such cases. They can still be enabled in the settings. A new option `DEFAULT_FORK_REPO_UNITS` is added to configure the default units on forks. Also add missing `repo.packages` unit to documentation. code by: @brechtvl ## ⚠️ BREAKING ⚠️ When forking a repository, the fork will now have issues, projects, releases, packages and wiki disabled. These can be enabled in the repository settings afterwards. To change back to the previous default behavior, configure `DEFAULT_FORK_REPO_UNITS` to be the same value as `DEFAULT_REPO_UNITS`. Co-authored-by: Brecht Van Lommel <[email protected]>
1 parent c2774d9 commit 2741546

File tree

8 files changed

+60
-31
lines changed

8 files changed

+60
-31
lines changed

custom/conf/app.example.ini

+7-3
Original file line numberDiff line numberDiff line change
@@ -927,14 +927,18 @@ ROUTER = console
927927
;USE_COMPAT_SSH_URI = false
928928
;;
929929
;; Close issues as long as a commit on any branch marks it as fixed
930-
;; Comma separated list of globally disabled repo units. Allowed values: repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki, repo.projects
930+
;; Comma separated list of globally disabled repo units. Allowed values: repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki, repo.projects, repo.packages
931931
;DISABLED_REPO_UNITS =
932932
;;
933-
;; Comma separated list of default repo units. Allowed values: repo.code, repo.releases, repo.issues, repo.pulls, repo.wiki, repo.projects.
933+
;; Comma separated list of default new repo units. Allowed values: repo.code, repo.releases, repo.issues, repo.pulls, repo.wiki, repo.projects, repo.packages.
934934
;; Note: Code and Releases can currently not be deactivated. If you specify default repo units you should still list them for future compatibility.
935935
;; External wiki and issue tracker can't be enabled by default as it requires additional settings.
936936
;; Disabled repo units will not be added to new repositories regardless if it is in the default list.
937-
;DEFAULT_REPO_UNITS = repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects
937+
;DEFAULT_REPO_UNITS = repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects,repo.packages
938+
;;
939+
;; Comma separated list of default forked repo units.
940+
;; The set of allowed values and rules are the same as DEFAULT_REPO_UNITS.
941+
;DEFAULT_FORK_REPO_UNITS = repo.code,repo.pulls
938942
;;
939943
;; Prefix archive files by placing them in a directory named after the repository
940944
;PREFIX_ARCHIVE_FILES = true

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ In addition there is _`StaticRootPath`_ which can be set as a built-in at build
104104
- `ENABLE_PUSH_CREATE_USER`: **false**: Allow users to push local repositories to Gitea and have them automatically created for a user.
105105
- `ENABLE_PUSH_CREATE_ORG`: **false**: Allow users to push local repositories to Gitea and have them automatically created for an org.
106106
- `DISABLED_REPO_UNITS`: **_empty_**: Comma separated list of globally disabled repo units. Allowed values: \[repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki, repo.projects\]
107-
- `DEFAULT_REPO_UNITS`: **repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects**: Comma separated list of default repo units. Allowed values: \[repo.code, repo.releases, repo.issues, repo.pulls, repo.wiki, repo.projects\]. Note: Code and Releases can currently not be deactivated. If you specify default repo units you should still list them for future compatibility. External wiki and issue tracker can't be enabled by default as it requires additional settings. Disabled repo units will not be added to new repositories regardless if it is in the default list.
107+
- `DEFAULT_REPO_UNITS`: **repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects,repo.packages**: Comma separated list of default new repo units. Allowed values: \[repo.code, repo.releases, repo.issues, repo.pulls, repo.wiki, repo.projects\]. Note: Code and Releases can currently not be deactivated. If you specify default repo units you should still list them for future compatibility. External wiki and issue tracker can't be enabled by default as it requires additional settings. Disabled repo units will not be added to new repositories regardless if it is in the default list.
108+
- `DEFAULT_FORK_REPO_UNITS`: **repo.code,repo.pulls**: Comma separated list of default forked repo units. The set of allowed values and rules is the same as `DEFAULT_REPO_UNITS`.
108109
- `PREFIX_ARCHIVE_FILES`: **true**: Prefix archive files by placing them in a directory named after the repository.
109110
- `DISABLE_MIGRATIONS`: **false**: Disable migrating feature.
110111
- `DISABLE_STARS`: **false**: Disable stars feature.

models/unit/unit.go

+38-20
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ var (
9494
TypePackages,
9595
}
9696

97+
// ForkRepoUnits contains the default unit types for forks
98+
DefaultForkRepoUnits = []Type{
99+
TypeCode,
100+
TypePullRequests,
101+
}
102+
97103
// NotAllowedDefaultRepoUnits contains units that can't be default
98104
NotAllowedDefaultRepoUnits = []Type{
99105
TypeExternalWiki,
@@ -110,26 +116,41 @@ var (
110116
DisabledRepoUnits = []Type{}
111117
)
112118

113-
// LoadUnitConfig load units from settings
114-
func LoadUnitConfig() {
115-
setDefaultRepoUnits := FindUnitTypes(setting.Repository.DefaultRepoUnits...)
116-
// Default repo units set if setting is not empty
117-
if len(setDefaultRepoUnits) > 0 {
119+
// Get valid set of default repository units from settings
120+
func validateDefaultRepoUnits(defaultUnits, settingDefaultUnits []Type) []Type {
121+
units := defaultUnits
122+
123+
// Use setting if not empty
124+
if len(settingDefaultUnits) > 0 {
118125
// MustRepoUnits required as default
119-
DefaultRepoUnits = make([]Type, len(MustRepoUnits))
120-
copy(DefaultRepoUnits, MustRepoUnits)
121-
for _, defaultU := range setDefaultRepoUnits {
122-
if !defaultU.CanBeDefault() {
123-
log.Warn("Not allowed as default unit: %s", defaultU.String())
126+
units = make([]Type, len(MustRepoUnits))
127+
copy(units, MustRepoUnits)
128+
for _, settingUnit := range settingDefaultUnits {
129+
if !settingUnit.CanBeDefault() {
130+
log.Warn("Not allowed as default unit: %s", settingUnit.String())
124131
continue
125132
}
126133
// MustRepoUnits already added
127-
if defaultU.CanDisable() {
128-
DefaultRepoUnits = append(DefaultRepoUnits, defaultU)
134+
if settingUnit.CanDisable() {
135+
units = append(units, settingUnit)
129136
}
130137
}
131138
}
132139

140+
// Remove disabled units
141+
for _, disabledUnit := range DisabledRepoUnits {
142+
for i, unit := range units {
143+
if unit == disabledUnit {
144+
units = append(units[:i], units[i+1:]...)
145+
}
146+
}
147+
}
148+
149+
return units
150+
}
151+
152+
// LoadUnitConfig load units from settings
153+
func LoadUnitConfig() {
133154
DisabledRepoUnits = FindUnitTypes(setting.Repository.DisabledRepoUnits...)
134155
// Check that must units are not disabled
135156
for i, disabledU := range DisabledRepoUnits {
@@ -138,14 +159,11 @@ func LoadUnitConfig() {
138159
DisabledRepoUnits = append(DisabledRepoUnits[:i], DisabledRepoUnits[i+1:]...)
139160
}
140161
}
141-
// Remove disabled units from default units
142-
for _, disabledU := range DisabledRepoUnits {
143-
for i, defaultU := range DefaultRepoUnits {
144-
if defaultU == disabledU {
145-
DefaultRepoUnits = append(DefaultRepoUnits[:i], DefaultRepoUnits[i+1:]...)
146-
}
147-
}
148-
}
162+
163+
setDefaultRepoUnits := FindUnitTypes(setting.Repository.DefaultRepoUnits...)
164+
DefaultRepoUnits = validateDefaultRepoUnits(DefaultRepoUnits, setDefaultRepoUnits)
165+
setDefaultForkRepoUnits := FindUnitTypes(setting.Repository.DefaultForkRepoUnits...)
166+
DefaultForkRepoUnits = validateDefaultRepoUnits(DefaultForkRepoUnits, setDefaultForkRepoUnits)
149167
}
150168

151169
// UnitGlobalDisabled checks if unit type is global disabled

modules/repository/create.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
)
3131

3232
// CreateRepositoryByExample creates a repository for the user/organization.
33-
func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository, overwriteOrAdopt bool) (err error) {
33+
func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository, overwriteOrAdopt, isFork bool) (err error) {
3434
if err = repo_model.IsUsableRepoName(repo.Name); err != nil {
3535
return err
3636
}
@@ -67,8 +67,12 @@ func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, re
6767
}
6868

6969
// insert units for repo
70-
units := make([]repo_model.RepoUnit, 0, len(unit.DefaultRepoUnits))
71-
for _, tp := range unit.DefaultRepoUnits {
70+
defaultUnits := unit.DefaultRepoUnits
71+
if isFork {
72+
defaultUnits = unit.DefaultForkRepoUnits
73+
}
74+
units := make([]repo_model.RepoUnit, 0, len(defaultUnits))
75+
for _, tp := range defaultUnits {
7276
if tp == unit.TypeIssues {
7377
units = append(units, repo_model.RepoUnit{
7478
RepoID: repo.ID,
@@ -212,7 +216,7 @@ func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_m
212216
var rollbackRepo *repo_model.Repository
213217

214218
if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
215-
if err := CreateRepositoryByExample(ctx, doer, u, repo, false); err != nil {
219+
if err := CreateRepositoryByExample(ctx, doer, u, repo, false, false); err != nil {
216220
return err
217221
}
218222

modules/repository/generate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
319319
TrustModel: templateRepo.TrustModel,
320320
}
321321

322-
if err = CreateRepositoryByExample(ctx, doer, owner, generateRepo, false); err != nil {
322+
if err = CreateRepositoryByExample(ctx, doer, owner, generateRepo, false, false); err != nil {
323323
return nil, err
324324
}
325325

modules/setting/repository.go

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ var (
4141
EnablePushCreateOrg bool
4242
DisabledRepoUnits []string
4343
DefaultRepoUnits []string
44+
DefaultForkRepoUnits []string
4445
PrefixArchiveFiles bool
4546
DisableMigrations bool
4647
DisableStars bool `ini:"DISABLE_STARS"`
@@ -157,6 +158,7 @@ var (
157158
EnablePushCreateOrg: false,
158159
DisabledRepoUnits: []string{},
159160
DefaultRepoUnits: []string{},
161+
DefaultForkRepoUnits: []string{},
160162
PrefixArchiveFiles: true,
161163
DisableMigrations: false,
162164
DisableStars: false,

services/repository/adopt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func AdoptRepository(doer, u *user_model.User, opts repo_module.CreateRepoOption
6767
}
6868
}
6969

70-
if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true); err != nil {
70+
if err := repo_module.CreateRepositoryByExample(ctx, doer, u, repo, true, false); err != nil {
7171
return err
7272
}
7373
if err := adoptRepository(ctx, repoPath, doer, repo, opts); err != nil {

services/repository/fork.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
119119
}()
120120

121121
err = db.WithTx(ctx, func(txCtx context.Context) error {
122-
if err = repo_module.CreateRepositoryByExample(txCtx, doer, owner, repo, false); err != nil {
122+
if err = repo_module.CreateRepositoryByExample(txCtx, doer, owner, repo, false, true); err != nil {
123123
return err
124124
}
125125

0 commit comments

Comments
 (0)