Skip to content

Commit d90b803

Browse files
committed
Make mirror feature more configurable (go-gitea#16957)
1 parent 741d01d commit d90b803

File tree

17 files changed

+126
-40
lines changed

17 files changed

+126
-40
lines changed

custom/conf/app.example.ini

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,6 @@ PATH =
793793
;; Prefix archive files by placing them in a directory named after the repository
794794
;PREFIX_ARCHIVE_FILES = true
795795
;;
796-
;; Disable the creation of new mirrors. Pre-existing mirrors remain valid.
797-
;DISABLE_MIRRORS = false
798-
;;
799796
;; Disable migrating feature.
800797
;DISABLE_MIGRATIONS = false
801798
;;
@@ -1934,6 +1931,12 @@ PATH =
19341931
;[mirror]
19351932
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19361933
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1934+
;; Whether gitea's mirror function can be used or not.
1935+
;ENABLED = true
1936+
;; Disable the creation of **new** mirrors. Pre-existing mirrors remain valid.
1937+
;DISABLE_NEW_PULL = false
1938+
;; Disable the creation of **new** push mirrors. Pre-existing mirrors remain valid.
1939+
;DISABLE_NEW_PUSH = false
19371940
;; Default interval as a duration between each check
19381941
;DEFAULT_INTERVAL = 8h
19391942
;; 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.
@@ -949,6 +948,9 @@ Task queue configuration has been moved to `queue.task`. However, the below conf
949948

950949
## Mirror (`mirror`)
951950

951+
- `ENABLED`: **true**: Whether gitea's mirror function can be used or not.
952+
- `DISABLE_NEW_PULL`: **false**: Disable the creation of **new** mirrors. Pre-existing mirrors remain valid.
953+
- `DISABLE_NEW_PUSH`: **false**: Disable the creation of **new** push mirrors. Pre-existing mirrors remain valid.
952954
- `DEFAULT_INTERVAL`: **8h**: Default interval between each check
953955
- `MIN_INTERVAL`: **10m**: Minimum interval for checking. (Must be >1m).
954956

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,

models/repo_pushmirror.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package models
66

77
import (
88
"errors"
9+
"fmt"
910
"time"
1011

1112
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/setting"
1214
"code.gitea.io/gitea/modules/timeutil"
1315

1416
"xorm.io/xorm"
@@ -57,6 +59,9 @@ func (m *PushMirror) GetRemoteName() string {
5759

5860
// InsertPushMirror inserts a push-mirror to database
5961
func InsertPushMirror(m *PushMirror) error {
62+
if setting.Mirror.DisableNewPush {
63+
return fmt.Errorf("creation of new push mirror's are disabled")
64+
}
6065
_, err := x.Insert(m)
6166
return err
6267
}

modules/setting/mirror.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
if err := Cfg.Section("mirror").MapTo(&Mirror); err != nil {
32+
log.Fatal("Failed to map Mirror settings: %v", err)
33+
}
34+
35+
// fallback to old config repository.DISABLE_MIRRORS
36+
if Cfg.Section("repository").Key("DISABLE_MIRRORS").MustBool(false) {
37+
Mirror.DisableNewPull = true
38+
}
39+
40+
if !Mirror.Enabled {
41+
Mirror.DisableNewPull = true
42+
Mirror.DisableNewPush = true
43+
}
44+
45+
if Mirror.MinInterval.Minutes() < 1 {
46+
log.Warn("Mirror.MinInterval is too low, set to 1 minute")
47+
Mirror.MinInterval = 1 * time.Minute
48+
}
49+
if Mirror.DefaultInterval < Mirror.MinInterval {
50+
log.Warn("Mirror.DefaultInterval is less than Mirror.MinInterval, set to 8 hours")
51+
Mirror.DefaultInterval = time.Hour * 8
52+
}
53+
}

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
@@ -154,7 +153,6 @@ var (
154153
DisabledRepoUnits: []string{},
155154
DefaultRepoUnits: []string{},
156155
PrefixArchiveFiles: true,
157-
DisableMirrors: false,
158156
DisableMigrations: false,
159157
DisableStars: false,
160158
DefaultBranch: "master",

modules/setting/setting.go

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

348348
ManifestData string
349349

350-
// Mirror settings
351-
Mirror struct {
352-
DefaultInterval time.Duration
353-
MinInterval time.Duration
354-
}
355-
356350
// API settings
357351
API = struct {
358352
EnableSwagger bool
@@ -936,17 +930,7 @@ func NewContext() {
936930

937931
newGit()
938932

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

951935
Langs = Cfg.Section("i18n").Key("LANGS").Strings(",")
952936
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 new mirror creations"))
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.Status(http.StatusBadRequest)
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,

0 commit comments

Comments
 (0)