0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-02-21 15:48:59 +01:00

refactor: remove instance notice level handling and related localization

This commit is contained in:
Nicolas 2026-02-19 18:51:14 +01:00
parent b92f648555
commit d82c0ee3fb
4 changed files with 2 additions and 47 deletions

View File

@ -54,41 +54,16 @@ type RepositoryStruct struct {
GitGuideRemoteName *config.Value[string]
}
const (
InstanceNoticeLevelInfo = "info"
InstanceNoticeLevelSuccess = "success"
InstanceNoticeLevelWarning = "warning"
InstanceNoticeLevelDanger = "danger"
)
type InstanceNotice struct {
Enabled bool
Message string
Level string
StartTime int64
EndTime int64
}
func DefaultInstanceNotice() InstanceNotice {
return InstanceNotice{
Level: InstanceNoticeLevelInfo,
}
}
func IsValidInstanceNoticeLevel(level string) bool {
switch level {
case InstanceNoticeLevelInfo, InstanceNoticeLevelSuccess, InstanceNoticeLevelWarning, InstanceNoticeLevelDanger:
return true
default:
return false
}
}
func (n *InstanceNotice) Normalize() {
if !IsValidInstanceNoticeLevel(n.Level) {
n.Level = InstanceNoticeLevelInfo
}
return InstanceNotice{}
}
func (n *InstanceNotice) IsActive(now int64) bool {
@ -105,9 +80,7 @@ func (n *InstanceNotice) IsActive(now int64) bool {
}
func GetInstanceNotice(ctx context.Context) InstanceNotice {
notice := Config().InstanceNotice.Banner.Value(ctx)
notice.Normalize()
return notice
return Config().InstanceNotice.Banner.Value(ctx)
}
type InstanceNoticeStruct struct {

View File

@ -3283,11 +3283,6 @@
"admin.config.instance_notice.message": "Message",
"admin.config.instance_notice.message_placeholder": "This message supports Markdown.",
"admin.config.instance_notice.message_required": "Message is required when the banner is enabled.",
"admin.config.instance_notice.level": "Level",
"admin.config.instance_notice.level.info": "Info",
"admin.config.instance_notice.level.success": "Success",
"admin.config.instance_notice.level.warning": "Warning",
"admin.config.instance_notice.level.danger": "Danger",
"admin.config.instance_notice.message_too_long": "Message must be at most %d characters.",
"admin.config.instance_notice.start_time": "Start time",
"admin.config.instance_notice.end_time": "End time",

View File

@ -213,7 +213,6 @@ func parseDatetimeLocalValue(raw string) (int64, error) {
func SetInstanceNotice(ctx *context.Context) {
saveInstanceNotice := func(instanceNotice setting.InstanceNotice) {
instanceNotice.Normalize()
marshaled, err := json.Marshal(instanceNotice)
if err != nil {
ctx.ServerError("Marshal", err)
@ -240,7 +239,6 @@ func SetInstanceNotice(ctx *context.Context) {
enabled := ctx.FormBool("enabled")
message := strings.TrimSpace(ctx.FormString("message"))
level := strings.TrimSpace(ctx.FormString("level"))
startTime, err := parseDatetimeLocalValue(strings.TrimSpace(ctx.FormString("start_time")))
if err != nil {
ctx.Flash.Error(ctx.Tr("admin.config.instance_notice.invalid_time"))
@ -253,9 +251,6 @@ func SetInstanceNotice(ctx *context.Context) {
ctx.Redirect(setting.AppSubURL + "/-/admin/config#instance-notice")
return
}
if !setting.IsValidInstanceNoticeLevel(level) {
level = setting.InstanceNoticeLevelInfo
}
if enabled && message == "" {
ctx.Flash.Error(ctx.Tr("admin.config.instance_notice.message_required"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config#instance-notice")
@ -275,7 +270,6 @@ func SetInstanceNotice(ctx *context.Context) {
instanceNotice := setting.InstanceNotice{
Enabled: enabled,
Message: message,
Level: level,
StartTime: startTime,
EndTime: endTime,
}

View File

@ -26,7 +26,6 @@ func TestInstanceNoticeVisibility(t *testing.T) {
setInstanceNoticeForTest(t, setting.InstanceNotice{
Enabled: true,
Message: "Planned **upgrade** in progress.",
Level: setting.InstanceNoticeLevelWarning,
})
t.Run("AnonymousUserSeesBanner", func(t *testing.T) {
@ -60,7 +59,6 @@ func TestInstanceNoticeTimeWindow(t *testing.T) {
setInstanceNoticeForTest(t, setting.InstanceNotice{
Enabled: true,
Message: "Future banner",
Level: setting.InstanceNoticeLevelInfo,
StartTime: now + 3600,
EndTime: now + 7200,
})
@ -71,7 +69,6 @@ func TestInstanceNoticeTimeWindow(t *testing.T) {
setInstanceNoticeForTest(t, setting.InstanceNotice{
Enabled: true,
Message: "Expired banner",
Level: setting.InstanceNoticeLevelInfo,
StartTime: now - 7200,
EndTime: now - 3600,
})
@ -88,25 +85,21 @@ func TestInstanceNoticeAdminCRUD(t *testing.T) {
req := NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
"enabled": "true",
"message": "Admin set banner",
"level": "danger",
})
adminSession.MakeRequest(t, req, http.StatusSeeOther)
notice := setting.GetInstanceNotice(t.Context())
assert.True(t, notice.Enabled)
assert.Equal(t, "Admin set banner", notice.Message)
assert.Equal(t, setting.InstanceNoticeLevelDanger, notice.Level)
req = NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
"enabled": "true",
"message": strings.Repeat("a", 2001),
"level": "warning",
})
adminSession.MakeRequest(t, req, http.StatusSeeOther)
notice = setting.GetInstanceNotice(t.Context())
assert.Equal(t, "Admin set banner", notice.Message)
assert.Equal(t, setting.InstanceNoticeLevelDanger, notice.Level)
req = NewRequestWithValues(t, "POST", "/-/admin/config/instance_notice", map[string]string{
"action": "delete",