Skip to content

Commit 63d7cbc

Browse files
6543zeripathdelvh
authored
Make mirror feature more configurable (#16957)
Rename`[repository]` `DISABLE_MIRRORS` to `[mirror]` `DISABLE_NEW_PULL` and add `ENABLED` and `DISABLE_NEW_PUSH` with the below meanings: - `ENABLED`: **true**: Enables the mirror functionality. Set to **false** to disable all mirrors. - `DISABLE_NEW_PULL`: **false**: Disable the creation of **new** mirrors. Pre-existing mirrors remain valid. - `DISABLE_NEW_PUSH`: **false**: Disable the creation of **new** push mirrors. Pre-existing mirrors remain valid. Co-authored-by: zeripath <[email protected]> Co-authored-by: delvh <[email protected]>
1 parent ded438f commit 63d7cbc

File tree

16 files changed

+161
-74
lines changed

16 files changed

+161
-74
lines changed

custom/conf/app.example.ini

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,6 @@ PATH =
800800
;; Prefix archive files by placing them in a directory named after the repository
801801
;PREFIX_ARCHIVE_FILES = true
802802
;;
803-
;; Disable the creation of new mirrors. Pre-existing mirrors remain valid.
804-
;DISABLE_MIRRORS = false
805-
;;
806803
;; Disable migrating feature.
807804
;DISABLE_MIGRATIONS = false
808805
;;
@@ -1945,6 +1942,12 @@ PATH =
19451942
;[mirror]
19461943
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19471944
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1945+
;; Enables the mirror functionality. Set to **false** to disable all mirrors.
1946+
;ENABLED = true
1947+
;; Disable the creation of **new** pull mirrors. Pre-existing mirrors remain valid. Will be ignored if `mirror.ENABLED` is `false`.
1948+
;DISABLE_NEW_PULL = false
1949+
;; Disable the creation of **new** push mirrors. Pre-existing mirrors remain valid. Will be ignored if `mirror.ENABLED` is `false`.
1950+
;DISABLE_NEW_PUSH = false
19481951
;; Default interval as a duration between each check
19491952
;DEFAULT_INTERVAL = 8h
19501953
;; Min interval as a duration must be > 1m

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
7373
- `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\]
7474
- `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.
7575
- `PREFIX_ARCHIVE_FILES`: **true**: Prefix archive files by placing them in a directory named after the repository.
76-
- `DISABLE_MIRRORS`: **false**: Disable the creation of **new** mirrors. Pre-existing mirrors remain valid.
7776
- `DISABLE_MIGRATIONS`: **false**: Disable migrating feature.
7877
- `DISABLE_STARS`: **false**: Disable stars feature.
7978
- `DEFAULT_BRANCH`: **master**: Default branch name of all repositories.
@@ -955,6 +954,9 @@ Task queue configuration has been moved to `queue.task`. However, the below conf
955954

956955
## Mirror (`mirror`)
957956

957+
- `ENABLED`: **true**: Enables the mirror functionality. Set to **false** to disable all mirrors.
958+
- `DISABLE_NEW_PULL`: **false**: Disable the creation of **new** pull mirrors. Pre-existing mirrors remain valid. Will be ignored if `mirror.ENABLED` is `false`.
959+
- `DISABLE_NEW_PUSH`: **false**: Disable the creation of **new** push mirrors. Pre-existing mirrors remain valid. Will be ignored if `mirror.ENABLED` is `false`.
958960
- `DEFAULT_INTERVAL`: **8h**: Default interval between each check
959961
- `MIN_INTERVAL`: **10m**: Minimum interval for checking. (Must be >1m).
960962

integrations/api_settings_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestAPIExposedSettings(t *testing.T) {
4343

4444
DecodeJSON(t, resp, &repo)
4545
assert.EqualValues(t, &api.GeneralRepoSettings{
46-
MirrorsDisabled: setting.Repository.DisableMirrors,
46+
MirrorsDisabled: !setting.Mirror.Enabled,
4747
HTTPGitDisabled: setting.Repository.DisableHTTPGit,
4848
MigrationsDisabled: setting.Repository.DisableMigrations,
4949
TimeTrackingDisabled: false,

modules/setting/mirror.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package setting
6+
7+
import (
8+
"time"
9+
10+
"code.gitea.io/gitea/modules/log"
11+
)
12+
13+
var (
14+
// Mirror settings
15+
Mirror = struct {
16+
Enabled bool
17+
DisableNewPull bool
18+
DisableNewPush bool
19+
DefaultInterval time.Duration
20+
MinInterval time.Duration
21+
}{
22+
Enabled: true,
23+
DisableNewPull: false,
24+
DisableNewPush: false,
25+
MinInterval: 10 * time.Minute,
26+
DefaultInterval: 8 * time.Hour,
27+
}
28+
)
29+
30+
func newMirror() {
31+
// Handle old configuration through `[repository]` `DISABLE_MIRRORS`
32+
// - please note this was badly named and only disabled the creation of new pull mirrors
33+
if Cfg.Section("repository").Key("DISABLE_MIRRORS").MustBool(false) {
34+
log.Warn("Deprecated DISABLE_MIRRORS config is used, please change your config and use the options within the [mirror] section")
35+
// TODO: enable on v1.17.0: log.Error("Deprecated fallback used, will be removed in v1.18.0")
36+
Mirror.DisableNewPull = true
37+
}
38+
if err := Cfg.Section("mirror").MapTo(&Mirror); err != nil {
39+
log.Fatal("Failed to map Mirror settings: %v", err)
40+
}
41+
42+
if !Mirror.Enabled {
43+
Mirror.DisableNewPull = true
44+
Mirror.DisableNewPush = true
45+
}
46+
47+
if Mirror.MinInterval.Minutes() < 1 {
48+
log.Warn("Mirror.MinInterval is too low, set to 1 minute")
49+
Mirror.MinInterval = 1 * time.Minute
50+
}
51+
if Mirror.DefaultInterval < Mirror.MinInterval {
52+
if time.Hour*8 < Mirror.MinInterval {
53+
Mirror.DefaultInterval = Mirror.MinInterval
54+
} else {
55+
Mirror.DefaultInterval = time.Hour * 8
56+
}
57+
log.Warn("Mirror.DefaultInterval is less than Mirror.MinInterval, set to %s", Mirror.DefaultInterval.String())
58+
}
59+
}

modules/setting/repository.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ var (
4141
DisabledRepoUnits []string
4242
DefaultRepoUnits []string
4343
PrefixArchiveFiles bool
44-
DisableMirrors bool
4544
DisableMigrations bool
4645
DisableStars bool `ini:"DISABLE_STARS"`
4746
DefaultBranch string
@@ -155,7 +154,6 @@ var (
155154
DisabledRepoUnits: []string{},
156155
DefaultRepoUnits: []string{},
157156
PrefixArchiveFiles: true,
158-
DisableMirrors: false,
159157
DisableMigrations: false,
160158
DisableStars: false,
161159
DefaultBranch: "master",

modules/setting/setting.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,6 @@ var (
348348

349349
ManifestData string
350350

351-
// Mirror settings
352-
Mirror struct {
353-
DefaultInterval time.Duration
354-
MinInterval time.Duration
355-
}
356-
357351
// API settings
358352
API = struct {
359353
EnableSwagger bool
@@ -938,17 +932,7 @@ func NewContext() {
938932

939933
newGit()
940934

941-
sec = Cfg.Section("mirror")
942-
Mirror.MinInterval = sec.Key("MIN_INTERVAL").MustDuration(10 * time.Minute)
943-
Mirror.DefaultInterval = sec.Key("DEFAULT_INTERVAL").MustDuration(8 * time.Hour)
944-
if Mirror.MinInterval.Minutes() < 1 {
945-
log.Warn("Mirror.MinInterval is too low")
946-
Mirror.MinInterval = 1 * time.Minute
947-
}
948-
if Mirror.DefaultInterval < Mirror.MinInterval {
949-
log.Warn("Mirror.DefaultInterval is less than Mirror.MinInterval")
950-
Mirror.DefaultInterval = time.Hour * 8
951-
}
935+
newMirror()
952936

953937
Langs = Cfg.Section("i18n").Key("LANGS").Strings(",")
954938
if len(Langs) == 0 {

routers/api/v1/repo/migrate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ func Migrate(ctx *context.APIContext) {
108108

109109
gitServiceType := convert.ToGitServiceType(form.Service)
110110

111-
if form.Mirror && setting.Repository.DisableMirrors {
112-
ctx.Error(http.StatusForbidden, "MirrorsGlobalDisabled", fmt.Errorf("the site administrator has disabled mirrors"))
111+
if form.Mirror && setting.Mirror.DisableNewPull {
112+
ctx.Error(http.StatusForbidden, "MirrorsGlobalDisabled", fmt.Errorf("the site administrator has disabled the creation of new pull mirrors"))
113113
return
114114
}
115115

routers/api/v1/repo/mirror.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/modules/setting"
1213
mirror_service "code.gitea.io/gitea/services/mirror"
1314
)
1415

@@ -42,6 +43,11 @@ func MirrorSync(ctx *context.APIContext) {
4243
ctx.Error(http.StatusForbidden, "MirrorSync", "Must have write access")
4344
}
4445

46+
if !setting.Mirror.Enabled {
47+
ctx.Error(http.StatusBadRequest, "MirrorSync", "Mirror feature is disabled")
48+
return
49+
}
50+
4551
mirror_service.StartToMirror(repo.ID)
4652

4753
ctx.Status(http.StatusOK)

routers/api/v1/settings/settings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func GetGeneralRepoSettings(ctx *context.APIContext) {
5858
// "200":
5959
// "$ref": "#/responses/GeneralRepoSettings"
6060
ctx.JSON(http.StatusOK, api.GeneralRepoSettings{
61-
MirrorsDisabled: setting.Repository.DisableMirrors,
61+
MirrorsDisabled: !setting.Mirror.Enabled,
6262
HTTPGitDisabled: setting.Repository.DisableHTTPGit,
6363
MigrationsDisabled: setting.Repository.DisableMigrations,
6464
StarsDisabled: setting.Repository.DisableStars,

routers/web/org/home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func Home(ctx *context.Context) {
141141
ctx.Data["Members"] = members
142142
ctx.Data["Teams"] = org.Teams
143143

144-
ctx.Data["DisabledMirrors"] = setting.Repository.DisableMirrors
144+
ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull
145145

146146
pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
147147
pager.SetDefaultParams(ctx)

0 commit comments

Comments
 (0)