Skip to content

Commit 839daa8

Browse files
authored
Added option to disable migrations (#13114)
* Added option to disable migrations This patch introduces DISABLE_MIGRATIONS parameter in [repository] section of app.ini (by default set to false). If set to true it blocks access to repository migration feature. This mod hides also local repo import option in user editor if local repo importing or migrations is disabled. * Alter Example config DISABLE_MIGRATIONS set to false in example config to match its default value. * HTTP error 403 instead of 500 on denied access to migration * Parameter DISABLE_MIGRATIONS exposed via API Fixes: 04b04cf Author-Change-Id: IB#1105130
1 parent 3a500cf commit 839daa8

File tree

14 files changed

+52
-11
lines changed

14 files changed

+52
-11
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ DEFAULT_REPO_UNITS = repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,re
6666
PREFIX_ARCHIVE_FILES = true
6767
; Disable the creation of new mirrors. Pre-existing mirrors remain valid.
6868
DISABLE_MIRRORS = false
69+
; Disable migrating feature.
70+
DISABLE_MIGRATIONS = false
6971
; The default branch name of new repositories
7072
DEFAULT_BRANCH = master
7173
; Allow adoption of unadopted repositories

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
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.
7676
- `DISABLE_MIRRORS`: **false**: Disable the creation of **new** mirrors. Pre-existing mirrors remain valid.
77+
- `DISABLE_MIGRATIONS`: **false**: Disable migrating feature.
7778
- `DEFAULT_BRANCH`: **master**: Default branch name of all repositories.
7879
- `ALLOW_ADOPTION_OF_UNADOPTED_REPOSITORIES`: **false**: Allow non-admin users to adopt unadopted repositories
7980
- `ALLOW_DELETION_OF_UNADOPTED_REPOSITORIES`: **false**: Allow non-admin users to delete unadopted repositories

integrations/api_settings_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ func TestAPIExposedSettings(t *testing.T) {
4343

4444
DecodeJSON(t, resp, &repo)
4545
assert.EqualValues(t, &api.GeneralRepoSettings{
46-
MirrorsDisabled: setting.Repository.DisableMirrors,
47-
HTTPGitDisabled: setting.Repository.DisableHTTPGit,
46+
MirrorsDisabled: setting.Repository.DisableMirrors,
47+
HTTPGitDisabled: setting.Repository.DisableHTTPGit,
48+
MigrationsDisabled: setting.Repository.DisableMigrations,
4849
}, repo)
4950

5051
attachment := new(api.GeneralAttachmentSettings)

modules/context/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ func Contexter() macaron.Handler {
343343

344344
ctx.Data["EnableSwagger"] = setting.API.EnableSwagger
345345
ctx.Data["EnableOpenIDSignIn"] = setting.Service.EnableOpenIDSignIn
346+
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
346347

347348
c.Map(ctx)
348349
}

modules/cron/tasks_basic.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/migrations"
1313
repository_service "code.gitea.io/gitea/modules/repository"
14+
"code.gitea.io/gitea/modules/setting"
1415
mirror_service "code.gitea.io/gitea/services/mirror"
1516
)
1617

@@ -115,5 +116,7 @@ func initBasicTasks() {
115116
registerArchiveCleanup()
116117
registerSyncExternalUsers()
117118
registerDeletedBranchesCleanup()
118-
registerUpdateMigrationPosterID()
119+
if !setting.Repository.DisableMigrations {
120+
registerUpdateMigrationPosterID()
121+
}
119122
}

modules/setting/repository.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var (
4242
DefaultRepoUnits []string
4343
PrefixArchiveFiles bool
4444
DisableMirrors bool
45+
DisableMigrations bool
4546
DefaultBranch string
4647
AllowAdoptionOfUnadoptedRepositories bool
4748
AllowDeleteOfUnadoptedRepositories bool
@@ -152,6 +153,7 @@ var (
152153
DefaultRepoUnits: []string{},
153154
PrefixArchiveFiles: true,
154155
DisableMirrors: false,
156+
DisableMigrations: false,
155157
DefaultBranch: "master",
156158

157159
// Repository editor settings

modules/structs/settings.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ package structs
66

77
// GeneralRepoSettings contains global repository settings exposed by API
88
type GeneralRepoSettings struct {
9-
MirrorsDisabled bool `json:"mirrors_disabled"`
10-
HTTPGitDisabled bool `json:"http_git_disabled"`
9+
MirrorsDisabled bool `json:"mirrors_disabled"`
10+
HTTPGitDisabled bool `json:"http_git_disabled"`
11+
MigrationsDisabled bool `json:"migrations_disabled"`
1112
}
1213

1314
// GeneralUISettings contains global ui settings exposed by API

routers/admin/users.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ func EditUser(ctx *context.Context) {
191191
ctx.Data["PageIsAdmin"] = true
192192
ctx.Data["PageIsAdminUsers"] = true
193193
ctx.Data["DisableRegularOrgCreation"] = setting.Admin.DisableRegularOrgCreation
194+
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
194195

195196
prepareUserInfo(ctx)
196197
if ctx.Written() {
@@ -205,6 +206,7 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
205206
ctx.Data["Title"] = ctx.Tr("admin.users.edit_account")
206207
ctx.Data["PageIsAdmin"] = true
207208
ctx.Data["PageIsAdminUsers"] = true
209+
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
208210

209211
u := prepareUserInfo(ctx)
210212
if ctx.Written() {

routers/api/v1/repo/migrate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ func Migrate(ctx *context.APIContext, form api.MigrateRepoOptions) {
119119
return
120120
}
121121

122+
if setting.Repository.DisableMigrations {
123+
ctx.Error(http.StatusForbidden, "MigrationsGlobalDisabled", fmt.Errorf("the site administrator has disabled migrations"))
124+
return
125+
}
126+
122127
var opts = migrations.MigrateOptions{
123128
CloneAddr: remoteAddr,
124129
RepoName: form.RepoName,

routers/api/v1/settings/settings.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ func GetGeneralRepoSettings(ctx *context.APIContext) {
5757
// "200":
5858
// "$ref": "#/responses/GeneralRepoSettings"
5959
ctx.JSON(http.StatusOK, api.GeneralRepoSettings{
60-
MirrorsDisabled: setting.Repository.DisableMirrors,
61-
HTTPGitDisabled: setting.Repository.DisableHTTPGit,
60+
MirrorsDisabled: setting.Repository.DisableMirrors,
61+
HTTPGitDisabled: setting.Repository.DisableHTTPGit,
62+
MigrationsDisabled: setting.Repository.DisableMigrations,
6263
})
6364
}
6465

routers/repo/migrate.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package repo
77

88
import (
9+
"net/http"
910
"strings"
1011

1112
"code.gitea.io/gitea/models"
@@ -25,6 +26,11 @@ const (
2526

2627
// Migrate render migration of repository page
2728
func Migrate(ctx *context.Context) {
29+
if setting.Repository.DisableMigrations {
30+
ctx.Error(http.StatusForbidden, "Migrate: the site administrator has disabled migrations")
31+
return
32+
}
33+
2834
ctx.Data["Services"] = append([]structs.GitServiceType{structs.PlainGitService}, structs.SupportedFullGitService...)
2935
serviceType := ctx.QueryInt("service_type")
3036
if serviceType == 0 {
@@ -60,6 +66,11 @@ func Migrate(ctx *context.Context) {
6066
}
6167

6268
func handleMigrateError(ctx *context.Context, owner *models.User, err error, name string, tpl base.TplName, form *auth.MigrateRepoForm) {
69+
if setting.Repository.DisableMigrations {
70+
ctx.Error(http.StatusForbidden, "MigrateError: the site administrator has disabled migrations")
71+
return
72+
}
73+
6374
switch {
6475
case migrations.IsRateLimitError(err):
6576
ctx.RenderWithErr(ctx.Tr("form.visit_rate_limit"), tpl, form)
@@ -107,6 +118,11 @@ func handleMigrateError(ctx *context.Context, owner *models.User, err error, nam
107118

108119
// MigratePost response for migrating from external git repository
109120
func MigratePost(ctx *context.Context, form auth.MigrateRepoForm) {
121+
if setting.Repository.DisableMigrations {
122+
ctx.Error(http.StatusForbidden, "MigratePost: the site administrator has disabled migrations")
123+
return
124+
}
125+
110126
ctx.Data["Title"] = ctx.Tr("new_migrate")
111127
// Plain git should be first
112128
ctx.Data["service"] = structs.GitServiceType(form.Service)

templates/admin/user/edit.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<input name="allow_git_hook" type="checkbox" {{if .User.CanEditGitHook}}checked{{end}} {{if DisableGitHooks}}disabled{{end}}>
9696
</div>
9797
</div>
98-
<div class="inline field">
98+
<div class="inline field" {{if or (DisableImportLocal) (.DisableMigrations)}}hidden{{end}}>
9999
<div class="ui checkbox">
100100
<label><strong>{{.i18n.Tr "admin.users.allow_import_local"}}</strong></label>
101101
<input name="allow_import_local" type="checkbox" {{if .User.CanImportLocal}}checked{{end}} {{if DisableImportLocal}}disabled{{end}}>

templates/base/head_navbar.tmpl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@
8989
<a class="item" href="{{AppSubUrl}}/repo/create">
9090
<span class="fitted">{{svg "octicon-plus"}}</span> {{.i18n.Tr "new_repo"}}
9191
</a>
92-
<a class="item" href="{{AppSubUrl}}/repo/migrate">
93-
<span class="fitted">{{svg "octicon-repo-push"}}</span> {{.i18n.Tr "new_migrate"}}
94-
</a>
92+
{{if not .DisableMigrations}}
93+
<a class="item" href="{{AppSubUrl}}/repo/migrate">
94+
<span class="fitted">{{svg "octicon-repo-push"}}</span> {{.i18n.Tr "new_migrate"}}
95+
</a>
96+
{{end}}
9597
{{if .SignedUser.CanCreateOrganization}}
9698
<a class="item" href="{{AppSubUrl}}/org/create">
9799
<span class="fitted">{{svg "octicon-organization"}}</span> {{.i18n.Tr "new_org"}}

templates/swagger/v1_json.tmpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13688,6 +13688,10 @@
1368813688
"type": "boolean",
1368913689
"x-go-name": "HTTPGitDisabled"
1369013690
},
13691+
"migrations_disabled": {
13692+
"type": "boolean",
13693+
"x-go-name": "MigrationsDisabled"
13694+
},
1369113695
"mirrors_disabled": {
1369213696
"type": "boolean",
1369313697
"x-go-name": "MirrorsDisabled"

0 commit comments

Comments
 (0)