Skip to content

Commit 7d8595b

Browse files
committed
maybe add a special unit named as misc to sore these special repo level configs
1 parent 2d77e8b commit 7d8595b

File tree

9 files changed

+80
-40
lines changed

9 files changed

+80
-40
lines changed

models/repo/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,8 @@ func (repo *Repository) MustGetUnit(ctx context.Context, tp unit.Type) *RepoUnit
468468
Type: tp,
469469
Config: cfg,
470470
}
471-
case unit.TypePackages:
472-
cfg := new(PackagesConfig)
471+
case unit.TypeMisc:
472+
cfg := new(MiscConfig)
473473
return &RepoUnit{
474474
Type: tp,
475475
Config: cfg,

models/repo/repo_unit.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,11 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
264264
r.Config = new(IssuesConfig)
265265
case unit.TypeActions:
266266
r.Config = new(ActionsConfig)
267-
case unit.TypePackages:
268-
r.Config = new(PackagesConfig)
269267
case unit.TypeProjects:
270268
r.Config = new(ProjectsConfig)
271-
case unit.TypeCode, unit.TypeReleases, unit.TypeWiki:
269+
case unit.TypeMisc:
270+
r.Config = new(MiscConfig)
271+
case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypePackages:
272272
fallthrough
273273
default:
274274
r.Config = new(UnitConfig)
@@ -321,23 +321,23 @@ func (r *RepoUnit) ProjectsConfig() *ProjectsConfig {
321321
return r.Config.(*ProjectsConfig)
322322
}
323323

324-
// PackagesConfig returns config for unit.PackagesConfig
325-
func (r *RepoUnit) PackagesConfig() *PackagesConfig {
326-
return r.Config.(*PackagesConfig)
324+
// MiscConfig returns config for unit.MiscConfig
325+
func (r *RepoUnit) MiscConfig() *MiscConfig {
326+
return r.Config.(*MiscConfig)
327327
}
328328

329-
// PackagesConfig describes package config
330-
type PackagesConfig struct {
329+
// MiscConfig describes repo misc config
330+
type MiscConfig struct {
331331
GoModuleSubDir string
332332
}
333333

334-
// FromDB fills up a PackagesConfig from serialized format.
335-
func (cfg *PackagesConfig) FromDB(bs []byte) error {
334+
// FromDB fills up a MiscConfig from serialized format.
335+
func (cfg *MiscConfig) FromDB(bs []byte) error {
336336
return json.UnmarshalHandleDoubleEncode(bs, &cfg)
337337
}
338338

339-
// ToDB exports a PackagesConfig to a serialized format.
340-
func (cfg *PackagesConfig) ToDB() ([]byte, error) {
339+
// ToDB exports a MiscConfig to a serialized format.
340+
func (cfg *MiscConfig) ToDB() ([]byte, error) {
341341
return json.Marshal(cfg)
342342
}
343343

models/unit/unit.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/container"
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/setting"
17+
"code.gitea.io/gitea/modules/util"
1718
)
1819

1920
// Type is Unit's Type
@@ -34,6 +35,10 @@ const (
3435
TypePackages // 9 Packages
3536
TypeActions // 10 Actions
3637

38+
// misc is a special uint to store special configs for a repo which is not related to any unit,
39+
// it should be considered as enabled anyway
40+
TypeMisc Type = 999 // 999 misc
41+
3742
// FIXME: TEAM-UNIT-PERMISSION: the team unit "admin" permission's design is not right, when a new unit is added in the future,
3843
// admin team won't inherit the correct admin permission for the new unit, need to have a complete fix before adding any new unit.
3944
)
@@ -65,6 +70,7 @@ var (
6570
TypeProjects,
6671
TypePackages,
6772
TypeActions,
73+
TypeMisc,
6874
}
6975

7076
// DefaultRepoUnits contains the default unit types
@@ -77,12 +83,14 @@ var (
7783
TypeProjects,
7884
TypePackages,
7985
TypeActions,
86+
TypeMisc,
8087
}
8188

8289
// ForkRepoUnits contains the default unit types for forks
8390
DefaultForkRepoUnits = []Type{
8491
TypeCode,
8592
TypePullRequests,
93+
TypeMisc,
8694
}
8795

8896
// DefaultMirrorRepoUnits contains the default unit types for mirrors
@@ -93,6 +101,7 @@ var (
93101
TypeWiki,
94102
TypeProjects,
95103
TypePackages,
104+
TypeMisc,
96105
}
97106

98107
// DefaultTemplateRepoUnits contains the default unit types for templates
@@ -104,12 +113,14 @@ var (
104113
TypeWiki,
105114
TypeProjects,
106115
TypePackages,
116+
TypeMisc,
107117
}
108118

109119
// NotAllowedDefaultRepoUnits contains units that can't be default
110120
NotAllowedDefaultRepoUnits = []Type{
111121
TypeExternalWiki,
112122
TypeExternalTracker,
123+
TypeMisc,
113124
}
114125

115126
disabledRepoUnitsAtomic atomic.Pointer[[]Type] // the units that have been globally disabled
@@ -163,6 +174,11 @@ func LoadUnitConfig() error {
163174
if len(invalidKeys) > 0 {
164175
log.Warn("Invalid keys in disabled repo units: %s", strings.Join(invalidKeys, ", "))
165176
}
177+
if slices.Contains(disabledRepoUnits, TypeMisc) {
178+
log.Warn("Misc unit should not be disabled")
179+
disabledRepoUnits = util.SliceRemoveAll(disabledRepoUnits, TypeMisc)
180+
}
181+
166182
DisabledRepoUnitsSet(disabledRepoUnits)
167183

168184
setDefaultRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultRepoUnits...)
@@ -328,6 +344,15 @@ var (
328344
perm.AccessModeOwner,
329345
}
330346

347+
UnitMisc = Unit{
348+
TypeMisc,
349+
"repo.misc",
350+
"/misc",
351+
"misc.unit.desc",
352+
999,
353+
perm.AccessModeOwner,
354+
}
355+
331356
// Units contains all the units
332357
Units = map[Type]Unit{
333358
TypeCode: UnitCode,
@@ -340,6 +365,7 @@ var (
340365
TypeProjects: UnitProjects,
341366
TypePackages: UnitPackages,
342367
TypeActions: UnitActions,
368+
TypeMisc: UnitMisc,
343369
}
344370
)
345371

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,7 @@ settings.rename_branch_from=old branch name
26292629
settings.rename_branch_to=new branch name
26302630
settings.rename_branch=Rename branch
26312631
2632+
misc = Misc
26322633
settings.go_module_sub_dir = Golang module subdirectory
26332634
settings.go_module_sub_dir_desc = The subdirectory in repository redirect for go module (which is supported since go 1.25, see <a target="_blank" rel="noopener noreferrer" href="%[1]s">%[2]s</a> for more detail)
26342635

routers/api/v1/repo/repo.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -994,17 +994,9 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
994994

995995
if opts.HasPackages != nil && !unit_model.TypePackages.UnitGlobalDisabled() {
996996
if *opts.HasPackages {
997-
unit := repo.MustGetUnit(ctx, unit_model.TypePackages)
998-
cfg := unit.PackagesConfig()
999-
1000-
if opts.GoModuleSubDir != nil {
1001-
cfg.GoModuleSubDir = *opts.GoModuleSubDir
1002-
}
1003-
1004997
units = append(units, repo_model.RepoUnit{
1005998
RepoID: repo.ID,
1006999
Type: unit_model.TypePackages,
1007-
Config: cfg,
10081000
})
10091001
} else {
10101002
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypePackages)
@@ -1022,6 +1014,22 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
10221014
}
10231015
}
10241016

1017+
miscCfg := repo.MustGetUnit(ctx, unit_model.TypeMisc).MiscConfig()
1018+
misCfgUpdated := false
1019+
1020+
if opts.GoModuleSubDir != nil {
1021+
miscCfg.GoModuleSubDir = *opts.GoModuleSubDir
1022+
misCfgUpdated = true
1023+
}
1024+
1025+
if misCfgUpdated {
1026+
units = append(units, repo_model.RepoUnit{
1027+
RepoID: repo.ID,
1028+
Type: unit_model.TypeMisc,
1029+
Config: miscCfg,
1030+
})
1031+
}
1032+
10251033
if len(units)+len(deleteUnitTypes) > 0 {
10261034
if err := repo_service.UpdateRepositoryUnits(ctx, repo, units, deleteUnitTypes); err != nil {
10271035
ctx.APIErrorInternal(err)

routers/web/repo/setting/setting.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,7 @@ func handleSettingsPostAdvanced(ctx *context.Context) {
608608
}
609609

610610
if form.EnablePackages && !unit_model.TypePackages.UnitGlobalDisabled() {
611-
units = append(units, newRepoUnit(repo, unit_model.TypePackages, &repo_model.PackagesConfig{
612-
GoModuleSubDir: form.GoModuleSubDir,
613-
}))
611+
units = append(units, newRepoUnit(repo, unit_model.TypePackages, nil))
614612
} else if !unit_model.TypePackages.UnitGlobalDisabled() {
615613
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypePackages)
616614
}
@@ -640,6 +638,10 @@ func handleSettingsPostAdvanced(ctx *context.Context) {
640638
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypePullRequests)
641639
}
642640

641+
miscCfg := repo.MustGetUnit(ctx, unit_model.TypeMisc).MiscConfig()
642+
miscCfg.GoModuleSubDir = form.GoModuleSubDir
643+
units = append(units, newRepoUnit(repo, unit_model.TypeMisc, miscCfg))
644+
643645
if len(units) == 0 {
644646
ctx.Flash.Error(ctx.Tr("repo.settings.update_settings_no_unit"))
645647
ctx.Redirect(ctx.Repo.RepoLink + "/settings")

services/context/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func NewTemplateContextForWeb(ctx *Context) TemplateContext {
119119
"RepoUnitTypeProjects": unit.TypeProjects,
120120
"RepoUnitTypePackages": unit.TypePackages,
121121
"RepoUnitTypeActions": unit.TypeActions,
122+
"RepoUnitTypeMisc": unit.TypeMisc,
122123
}
123124
return tmplCtx
124125
}

services/context/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ func EarlyResponseForGoGetMeta(ctx *Context) {
346346

347347
// GetGoModuleSubDirConfig retrieves the subdirectory configuration for a Go module.
348348
func GetGoModuleSubDirConfig(ctx *Context, repo *repo_model.Repository) string {
349-
pkgCfg := repo.MustGetUnit(ctx, unit_model.TypePackages).PackagesConfig()
349+
miscCfg := repo.MustGetUnit(ctx, unit_model.TypeMisc).MiscConfig()
350350

351-
return strings.TrimSpace(pkgCfg.GoModuleSubDir)
351+
return strings.TrimSpace(miscCfg.GoModuleSubDir)
352352
}
353353

354354
// RedirectToRepo redirect to a differently-named repository

templates/repo/settings/options.tmpl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -499,28 +499,16 @@
499499
</div>
500500
</div>
501501

502-
<div class="divider"></div>
503502
{{$isPackagesEnabled := .Repository.UnitEnabled ctx ctx.Consts.RepoUnitTypePackages}}
504503
{{$isPackagesGlobalDisabled := ctx.Consts.RepoUnitTypePackages.UnitGlobalDisabled}}
505-
{{$pkgUnit := .Repository.MustGetUnit ctx ctx.Consts.RepoUnitTypePackages}}
506504
<div class="inline field">
507505
<label>{{ctx.Locale.Tr "repo.packages"}}</label>
508506
<div class="ui checkbox{{if $isPackagesGlobalDisabled}} disabled{{end}}"{{if $isPackagesGlobalDisabled}} data-tooltip-content="{{ctx.Locale.Tr "repo.unit_disabled"}}"{{end}}>
509-
<input class="enable-system" name="enable_packages" type="checkbox" data-target="#packages_box" {{if $isPackagesEnabled}}checked{{end}}>
507+
<input class="enable-system" name="enable_packages" type="checkbox" {{if $isPackagesEnabled}}checked{{end}}>
510508
<label>{{ctx.Locale.Tr "repo.settings.packages_desc"}}</label>
511509
</div>
512510
</div>
513511

514-
<div class="field{{if not $isPackagesEnabled}} disabled{{end}}" id="packages_box">
515-
<div class="field tw-pl-4 {{if $isPackagesGlobalDisabled}}disabled{{end}}">
516-
<label for="go_module_sub_dir">{{ctx.Locale.Tr "repo.settings.go_module_sub_dir"}}</label>
517-
<input id="go_module_sub_dir" name="go_module_sub_dir" value="{{$pkgUnit.PackagesConfig.GoModuleSubDir}}">
518-
<p class="help">{{ctx.Locale.Tr "repo.settings.go_module_sub_dir_desc" "https://github.com/golang/go/commit/835e36fc7f631f74233edfd4ab43b6b56833db86" "github.com/golang/go@835e36fc7f"}}</p>
519-
</div>
520-
</div>
521-
522-
<div class="divider"></div>
523-
524512
{{if .EnableActions}}
525513
{{$isActionsEnabled := .Repository.UnitEnabled ctx ctx.Consts.RepoUnitTypeActions}}
526514
{{$isActionsGlobalDisabled := ctx.Consts.RepoUnitTypeActions.UnitGlobalDisabled}}
@@ -659,6 +647,20 @@
659647
</div>
660648
{{end}}
661649

650+
<div class="divider"></div>
651+
{{$miscUnit := .Repository.MustGetUnit ctx ctx.Consts.RepoUnitTypeMisc}}
652+
<div class="inline field">
653+
<label>{{ctx.Locale.Tr "repo.misc"}}</label>
654+
</div>
655+
656+
<div class="field" id="misc_box">
657+
<div class="field tw-pl-4">
658+
<label for="go_module_sub_dir">{{ctx.Locale.Tr "repo.settings.go_module_sub_dir"}}</label>
659+
<input id="go_module_sub_dir" name="go_module_sub_dir" value="{{$miscUnit.MiscConfig.GoModuleSubDir}}">
660+
<p class="help">{{ctx.Locale.Tr "repo.settings.go_module_sub_dir_desc" "https://github.com/golang/go/commit/835e36fc7f631f74233edfd4ab43b6b56833db86" "github.com/golang/go@835e36fc7f"}}</p>
661+
</div>
662+
</div>
663+
662664
<div class="divider"></div>
663665
<div class="field">
664666
<button class="ui primary button">{{ctx.Locale.Tr "repo.settings.update_settings"}}</button>

0 commit comments

Comments
 (0)