From 6f43363fd60f11bb769bed6d99cfb54069b77bd9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 24 Jul 2023 18:03:07 +0800 Subject: [PATCH 1/4] Display deprecated warning in admin panel pages --- modules/setting/config_provider.go | 16 +++++++++------- modules/setting/deprecated.go | 22 ++++++++++++++++++++++ modules/setting/lfs.go | 3 ++- routers/web/web.go | 14 +++++++++++++- templates/admin/layout_head.tmpl | 4 +++- 5 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 modules/setting/deprecated.go diff --git a/modules/setting/config_provider.go b/modules/setting/config_provider.go index 86712792a745f..ab41d5d6d9fd9 100644 --- a/modules/setting/config_provider.go +++ b/modules/setting/config_provider.go @@ -318,13 +318,15 @@ func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting any) { func deprecatedSetting(rootCfg ConfigProvider, oldSection, oldKey, newSection, newKey, version string) { if rootCfg.Section(oldSection).HasKey(oldKey) { - log.Error("Deprecated fallback `[%s]` `%s` present. Use `[%s]` `%s` instead. This fallback will be/has been removed in %s", oldSection, oldKey, newSection, newKey, version) - } -} - -func deprecatedSettingFatal(rootCfg ConfigProvider, oldSection, oldKey, newSection, newKey, version string) { - if rootCfg.Section(oldSection).HasKey(oldKey) { - log.Fatal("Deprecated fallback `[%s]` `%s` present. Use `[%s]` `%s` instead. This fallback will be/has been removed in %s. Shutting down", oldSection, oldKey, newSection, newKey, version) + dw := DeprecatedWarning{ + OldSection: oldSection, + OldKey: oldKey, + NewSection: newSection, + NewKey: newKey, + Version: version, + } + log.Error("%v", dw.String()) + DeprecatedWarnings = append(DeprecatedWarnings, dw) } } diff --git a/modules/setting/deprecated.go b/modules/setting/deprecated.go new file mode 100644 index 0000000000000..cf705adb2608f --- /dev/null +++ b/modules/setting/deprecated.go @@ -0,0 +1,22 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package setting + +import "fmt" + +type DeprecatedWarning struct { + OldSection string + OldKey string + NewSection string + NewKey string + Version string +} + +func (dw DeprecatedWarning) String() string { + return fmt.Sprintf("Deprecated fallback `[%s]` `%s` present. Use `[%s]` `%s` instead. This fallback will be/has been removed in %s", + dw.OldSection, dw.OldKey, dw.NewSection, dw.NewKey, dw.Version) +} + +// DeprecatedWarnings all warnings which need administrator to change +var DeprecatedWarnings []DeprecatedWarning diff --git a/modules/setting/lfs.go b/modules/setting/lfs.go index 784a99582d4c5..4917509742efe 100644 --- a/modules/setting/lfs.go +++ b/modules/setting/lfs.go @@ -34,7 +34,8 @@ func loadLFSFrom(rootCfg ConfigProvider) error { // Specifically default PATH to LFS_CONTENT_PATH // DEPRECATED should not be removed because users maybe upgrade from lower version to the latest version // if these are removed, the warning will not be shown - deprecatedSettingFatal(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH", "v1.19.0") + deprecatedSetting(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH", "v1.19.0") + var err error LFS.Storage, err = getStorage(rootCfg, "lfs", "", lfsSec) diff --git a/routers/web/web.go b/routers/web/web.go index 6d5ccad484fa0..66cb312d37503 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -5,6 +5,7 @@ package web import ( gocontext "context" + "fmt" "net/http" "code.gitea.io/gitea/models/perm" @@ -644,7 +645,18 @@ func registerRoutes(m *web.Route) { m.Get("", admin.RedirectToDefaultSetting) addSettingsRunnersRoutes() }) - }, adminReq, ctxDataSet("EnableOAuth2", setting.OAuth2.Enable, "EnablePackages", setting.Packages.Enabled)) + }, adminReq, ctxDataSet( + "EnableOAuth2", setting.OAuth2.Enable, + "EnablePackages", setting.Packages.Enabled, + ), func(ctx *context.Context) { + if len(setting.DeprecatedWarnings) > 0 { + content := setting.DeprecatedWarnings[0].String() + if len(setting.DeprecatedWarnings) > 1 { + content += fmt.Sprintf(" (and %d more)", len(setting.DeprecatedWarnings)-1) + } + ctx.Flash.Error(content) + } + }) // ***** END: Admin ***** m.Group("", func() { diff --git a/templates/admin/layout_head.tmpl b/templates/admin/layout_head.tmpl index 71410ea36ba60..a793d1c32d6fb 100644 --- a/templates/admin/layout_head.tmpl +++ b/templates/admin/layout_head.tmpl @@ -1,9 +1,11 @@ {{template "base/head" .ctxData}}
+
+ {{template "base/alert" .ctxData}} +
{{template "admin/navbar" .ctxData}}
- {{template "base/alert" .ctxData}} {{/* block: admin-setting-content */}} {{if false}}{{/* to make html structure "likely" complete to prevent IDE warnings */}} From 26dcce80bbec568a10cfc9b28abea0504621591b Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 24 Jul 2023 18:16:47 +0800 Subject: [PATCH 2/4] Use old configuration if new lfs configuration not set --- modules/setting/lfs.go | 8 +++++++- modules/setting/lfs_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/modules/setting/lfs.go b/modules/setting/lfs.go index 4917509742efe..eb4190e2c9532 100644 --- a/modules/setting/lfs.go +++ b/modules/setting/lfs.go @@ -35,7 +35,13 @@ func loadLFSFrom(rootCfg ConfigProvider) error { // DEPRECATED should not be removed because users maybe upgrade from lower version to the latest version // if these are removed, the warning will not be shown deprecatedSetting(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH", "v1.19.0") - + + if val := sec.Key("LFS_CONTENT_PATH").String(); val != "" { + if lfsSec == nil { + lfsSec = rootCfg.Section("lfs") + } + lfsSec.Key("PATH").MustString(val) + } var err error LFS.Storage, err = getStorage(rootCfg, "lfs", "", lfsSec) diff --git a/modules/setting/lfs_test.go b/modules/setting/lfs_test.go index 3313cae0eb38a..10c54fec0aa6f 100644 --- a/modules/setting/lfs_test.go +++ b/modules/setting/lfs_test.go @@ -21,6 +21,30 @@ func Test_getStorageInheritNameSectionTypeForLFS(t *testing.T) { assert.EqualValues(t, "minio", LFS.Storage.Type) assert.EqualValues(t, "lfs/", LFS.Storage.MinioConfig.BasePath) + iniStr = ` +[server] +LFS_CONTENT_PATH = path_ignored +[lfs] +PATH = path_used +` + cfg, err = NewConfigProviderFromData(iniStr) + assert.NoError(t, err) + assert.NoError(t, loadLFSFrom(cfg)) + + assert.EqualValues(t, "local", LFS.Storage.Type) + assert.Contains(t, LFS.Storage.Path, "path_used") + + iniStr = ` +[server] +LFS_CONTENT_PATH = deprecatedpath +` + cfg, err = NewConfigProviderFromData(iniStr) + assert.NoError(t, err) + assert.NoError(t, loadLFSFrom(cfg)) + + assert.EqualValues(t, "local", LFS.Storage.Type) + assert.Contains(t, LFS.Storage.Path, "deprecatedpath") + iniStr = ` [storage.lfs] STORAGE_TYPE = minio From e75c4e8ce05bde74f6a0a8a6080212a256297e85 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 24 Jul 2023 23:29:44 +0800 Subject: [PATCH 3/4] Use a method but not inline for middleware --- routers/web/web.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/routers/web/web.go b/routers/web/web.go index 66cb312d37503..818b9dda24b14 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -543,6 +543,15 @@ func registerRoutes(m *web.Route) { m.Get("/avatar/{hash}", user.AvatarByEmailHash) adminReq := auth_service.VerifyAuthWithOptions(&auth_service.VerifyOptions{SignInRequired: true, AdminRequired: true}) + displayDeprecatedWarning := func(ctx *context.Context) { + if len(setting.DeprecatedWarnings) > 0 { + content := setting.DeprecatedWarnings[0].String() + if len(setting.DeprecatedWarnings) > 1 { + content += fmt.Sprintf(" (and %d more)", len(setting.DeprecatedWarnings)-1) + } + ctx.Flash.Error(content) + } + } // ***** START: Admin ***** m.Group("/admin", func() { @@ -648,15 +657,7 @@ func registerRoutes(m *web.Route) { }, adminReq, ctxDataSet( "EnableOAuth2", setting.OAuth2.Enable, "EnablePackages", setting.Packages.Enabled, - ), func(ctx *context.Context) { - if len(setting.DeprecatedWarnings) > 0 { - content := setting.DeprecatedWarnings[0].String() - if len(setting.DeprecatedWarnings) > 1 { - content += fmt.Sprintf(" (and %d more)", len(setting.DeprecatedWarnings)-1) - } - ctx.Flash.Error(content) - } - }) + ), displayDeprecatedWarning) // ***** END: Admin ***** m.Group("", func() { From 71e4810be78f9adf0eb6287cdedb363f358a0cc6 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 25 Jul 2023 13:48:22 +0800 Subject: [PATCH 4/4] fix --- modules/setting/config_provider.go | 15 ++++++--------- modules/setting/deprecated.go | 22 ---------------------- routers/web/admin/admin.go | 11 +++++++++++ routers/web/admin/config.go | 2 ++ routers/web/web.go | 15 +-------------- templates/admin/layout_head.tmpl | 4 ++-- web_src/css/admin.css | 1 + 7 files changed, 23 insertions(+), 47 deletions(-) delete mode 100644 modules/setting/deprecated.go diff --git a/modules/setting/config_provider.go b/modules/setting/config_provider.go index ab41d5d6d9fd9..d00164df9e0d2 100644 --- a/modules/setting/config_provider.go +++ b/modules/setting/config_provider.go @@ -316,17 +316,14 @@ func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting any) { } } +// DeprecatedWarnings contains the warning message for various deprecations, including: setting option, file/folder, etc +var DeprecatedWarnings []string + func deprecatedSetting(rootCfg ConfigProvider, oldSection, oldKey, newSection, newKey, version string) { if rootCfg.Section(oldSection).HasKey(oldKey) { - dw := DeprecatedWarning{ - OldSection: oldSection, - OldKey: oldKey, - NewSection: newSection, - NewKey: newKey, - Version: version, - } - log.Error("%v", dw.String()) - DeprecatedWarnings = append(DeprecatedWarnings, dw) + msg := fmt.Sprintf("Deprecated config option `[%s]` `%s` present. Use `[%s]` `%s` instead. This fallback will be/has been removed in %s", oldSection, oldKey, newSection, newKey, version) + log.Error("%v", msg) + DeprecatedWarnings = append(DeprecatedWarnings, msg) } } diff --git a/modules/setting/deprecated.go b/modules/setting/deprecated.go deleted file mode 100644 index cf705adb2608f..0000000000000 --- a/modules/setting/deprecated.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2023 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package setting - -import "fmt" - -type DeprecatedWarning struct { - OldSection string - OldKey string - NewSection string - NewKey string - Version string -} - -func (dw DeprecatedWarning) String() string { - return fmt.Sprintf("Deprecated fallback `[%s]` `%s` present. Use `[%s]` `%s` instead. This fallback will be/has been removed in %s", - dw.OldSection, dw.OldKey, dw.NewSection, dw.NewKey, dw.Version) -} - -// DeprecatedWarnings all warnings which need administrator to change -var DeprecatedWarnings []DeprecatedWarning diff --git a/routers/web/admin/admin.go b/routers/web/admin/admin.go index 225a8c6705368..fe3e1d2206a07 100644 --- a/routers/web/admin/admin.go +++ b/routers/web/admin/admin.go @@ -113,6 +113,16 @@ func updateSystemStatus() { sysStatus.NumGC = m.NumGC } +func prepareDeprecatedWarningsAlert(ctx *context.Context) { + if len(setting.DeprecatedWarnings) > 0 { + content := setting.DeprecatedWarnings[0] + if len(setting.DeprecatedWarnings) > 1 { + content += fmt.Sprintf(" (and %d more)", len(setting.DeprecatedWarnings)-1) + } + ctx.Flash.Error(content, true) + } +} + // Dashboard show admin panel dashboard func Dashboard(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("admin.dashboard") @@ -123,6 +133,7 @@ func Dashboard(ctx *context.Context) { updateSystemStatus() ctx.Data["SysStatus"] = sysStatus ctx.Data["SSH"] = setting.SSH + prepareDeprecatedWarningsAlert(ctx) ctx.HTML(http.StatusOK, tplDashboard) } diff --git a/routers/web/admin/config.go b/routers/web/admin/config.go index b7d9976815cb1..ba0d862645198 100644 --- a/routers/web/admin/config.go +++ b/routers/web/admin/config.go @@ -171,6 +171,8 @@ func Config(ctx *context.Context) { ctx.Data["Loggers"] = log.GetManager().DumpLoggers() + prepareDeprecatedWarningsAlert(ctx) + ctx.HTML(http.StatusOK, tplConfig) } diff --git a/routers/web/web.go b/routers/web/web.go index 818b9dda24b14..6d5ccad484fa0 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -5,7 +5,6 @@ package web import ( gocontext "context" - "fmt" "net/http" "code.gitea.io/gitea/models/perm" @@ -543,15 +542,6 @@ func registerRoutes(m *web.Route) { m.Get("/avatar/{hash}", user.AvatarByEmailHash) adminReq := auth_service.VerifyAuthWithOptions(&auth_service.VerifyOptions{SignInRequired: true, AdminRequired: true}) - displayDeprecatedWarning := func(ctx *context.Context) { - if len(setting.DeprecatedWarnings) > 0 { - content := setting.DeprecatedWarnings[0].String() - if len(setting.DeprecatedWarnings) > 1 { - content += fmt.Sprintf(" (and %d more)", len(setting.DeprecatedWarnings)-1) - } - ctx.Flash.Error(content) - } - } // ***** START: Admin ***** m.Group("/admin", func() { @@ -654,10 +644,7 @@ func registerRoutes(m *web.Route) { m.Get("", admin.RedirectToDefaultSetting) addSettingsRunnersRoutes() }) - }, adminReq, ctxDataSet( - "EnableOAuth2", setting.OAuth2.Enable, - "EnablePackages", setting.Packages.Enabled, - ), displayDeprecatedWarning) + }, adminReq, ctxDataSet("EnableOAuth2", setting.OAuth2.Enable, "EnablePackages", setting.Packages.Enabled)) // ***** END: Admin ***** m.Group("", func() { diff --git a/templates/admin/layout_head.tmpl b/templates/admin/layout_head.tmpl index a793d1c32d6fb..0974c5fbeda12 100644 --- a/templates/admin/layout_head.tmpl +++ b/templates/admin/layout_head.tmpl @@ -1,7 +1,7 @@ {{template "base/head" .ctxData}}
-
- {{template "base/alert" .ctxData}} +
+ {{template "base/alert" .ctxData}}
{{template "admin/navbar" .ctxData}} diff --git a/web_src/css/admin.css b/web_src/css/admin.css index ec3cfbf22a3dd..f590a04180fc4 100644 --- a/web_src/css/admin.css +++ b/web_src/css/admin.css @@ -1,4 +1,5 @@ .admin-container { + margin-top: 15px; display: flex !important; gap: 16px; }