Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add system setting table with cache and also add cache supports for user setting #18058

Merged
merged 45 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5f8cb5d
Add system setting table with cache and also add cache supports for u…
lunny Dec 21, 2021
7a8f582
Fix merge
lunny Apr 14, 2022
705f284
fix build
lunny May 11, 2022
7c0127a
Add ui for some config
lunny May 12, 2022
6dfbaee
Finished to change two system settings
lunny May 14, 2022
faea1da
Fix theme
lunny May 14, 2022
6fbfaf0
Fix lint
lunny May 14, 2022
9cdc89f
Fix wrong reference
lunny May 14, 2022
b21acd7
Fix build
lunny May 14, 2022
4d56b1a
Fix import
lunny May 16, 2022
9cf6895
Fix test
lunny May 26, 2022
790fdae
Fix test
lunny May 26, 2022
d8c215c
Fix fmt
lunny May 26, 2022
b968f1d
Fix bug
lunny May 26, 2022
6927ed8
Fix bug
lunny May 26, 2022
0d40c61
Use constant store setting keys
lunny May 26, 2022
ddcac80
Fix test
lunny May 26, 2022
9d7f2f8
Fix wrong file
lunny Jun 1, 2022
e51e3e2
Update models/migrations/v216.go
6543 Jun 1, 2022
f2dccd9
merge
lunny Jun 12, 2022
2f29003
merge
lunny Jun 12, 2022
bd7602e
Merge branch 'lunny/sys_setting' of github.com:lunny/gitea into lunny…
lunny Jun 12, 2022
22f0aba
Merge main branch
lunny Jun 13, 2022
5bda8b3
Fix some bugs
lunny Jun 15, 2022
ab0bfcf
Merge branch 'main' into lunny/sys_setting
lunny Jun 15, 2022
f642306
Merge branch 'main' into lunny/sys_setting
6543 Jun 15, 2022
29730f0
Merge main branch
lunny Jul 6, 2022
8898588
Merge branch 'lunny/sys_setting' of github.com:lunny/gitea into lunny…
lunny Jul 6, 2022
e1e81d7
Merge branch 'main' into lunny/sys_setting
6543 Jul 7, 2022
dfc562b
merge main branch
lunny Aug 10, 2022
b7c12e0
Merge branch 'lunny/sys_setting' of github.com:lunny/gitea into lunny…
lunny Aug 10, 2022
47ce36f
Merge branch 'main' into lunny/sys_setting
lunny Aug 12, 2022
c353679
Merge branch 'main' into lunny/sys_setting
6543 Aug 15, 2022
ebf25bf
Merge main branch
lunny Aug 17, 2022
10f4c6d
Fix typo
lunny Aug 17, 2022
8d59b44
Merge branch 'lunny/sys_setting' of github.com:lunny/gitea into lunny…
lunny Aug 17, 2022
f837aec
popup a tooltip when setting failed
lunny Aug 17, 2022
c2e6e22
Fix error
lunny Aug 17, 2022
eb9088b
Merge main branch
lunny Aug 25, 2022
ac22ef8
Merge branch 'main' into lunny/sys_setting
6543 Aug 28, 2022
c186697
merge main branch
lunny Oct 16, 2022
2537e57
Merge branch 'lunny/sys_setting' of github.com:lunny/gitea into lunny…
lunny Oct 16, 2022
5ce610a
Fix bug
lunny Oct 16, 2022
a0a2cd5
Update modules/system/setting.go
6543 Oct 16, 2022
ccda953
Merge branch 'main' into lunny/sys_setting
6543 Oct 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions contrib/pr/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -62,11 +61,7 @@ func runPR() {
}
setting.AppWorkPath = curDir
setting.StaticRootPath = curDir
setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
if err != nil {
log.Fatalf("url.Parse: %v\n", err)
}

setting.GravatarSource = "https://secure.gravatar.com/avatar/"
setting.AppURL = "http://localhost:8080/"
setting.HTTPPort = "8080"
setting.SSH.Domain = "localhost"
Expand Down
117 changes: 0 additions & 117 deletions models/admin/notice_test.go

This file was deleted.

15 changes: 11 additions & 4 deletions models/avatars/avatar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sync"

"code.gitea.io/gitea/models/db"
system_model "code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -72,7 +73,7 @@ func GetEmailForHash(md5Sum string) (string, error) {
// LibravatarURL returns the URL for the given email. Slow due to the DNS lookup.
// This function should only be called if a federated avatar service is enabled.
func LibravatarURL(email string) (*url.URL, error) {
urlStr, err := setting.LibravatarService.FromEmail(email)
urlStr, err := system_model.LibravatarService.FromEmail(email)
if err != nil {
log.Error("LibravatarService.FromEmail(email=%s): error %v", email, err)
return nil, err
Expand Down Expand Up @@ -149,8 +150,10 @@ func generateEmailAvatarLink(email string, size int, final bool) string {
return DefaultAvatarLink()
}

enableFederatedAvatar, _ := system_model.GetSetting(system_model.KeyPictureEnableFederatedAvatar)

var err error
if setting.EnableFederatedAvatar && setting.LibravatarService != nil {
if enableFederatedAvatar != nil && enableFederatedAvatar.GetValueBool() && system_model.LibravatarService != nil {
emailHash := saveEmailHash(email)
if final {
// for final link, we can spend more time on slow external query
Expand All @@ -166,12 +169,16 @@ func generateEmailAvatarLink(email string, size int, final bool) string {
urlStr += "?size=" + strconv.Itoa(size)
}
return urlStr
} else if !setting.DisableGravatar {
}

disableGravatar, _ := system_model.GetSetting(system_model.KeyPictureDisableGravatar)
if disableGravatar != nil && !disableGravatar.GetValueBool() {
// copy GravatarSourceURL, because we will modify its Path.
avatarURLCopy := *setting.GravatarSourceURL
avatarURLCopy := *system_model.GravatarSourceURL
avatarURLCopy.Path = path.Join(avatarURLCopy.Path, HashEmail(email))
return generateRecognizedAvatarURL(avatarURLCopy, size)
}

return DefaultAvatarLink()
}

Expand Down
32 changes: 18 additions & 14 deletions models/avatars/avatar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,57 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package avatars
package avatars_test

import (
"net/url"
"testing"

avatars_model "code.gitea.io/gitea/models/avatars"
system_model "code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
)

const gravatarSource = "https://secure.gravatar.com/avatar/"

func disableGravatar() {
setting.EnableFederatedAvatar = false
setting.LibravatarService = nil
setting.DisableGravatar = true
func disableGravatar(t *testing.T) {
err := system_model.SetSettingNoVersion(system_model.KeyPictureEnableFederatedAvatar, "false")
assert.NoError(t, err)
err = system_model.SetSettingNoVersion(system_model.KeyPictureDisableGravatar, "true")
assert.NoError(t, err)
system_model.LibravatarService = nil
}

func enableGravatar(t *testing.T) {
setting.DisableGravatar = false
var err error
setting.GravatarSourceURL, err = url.Parse(gravatarSource)
err := system_model.SetSettingNoVersion(system_model.KeyPictureDisableGravatar, "false")
assert.NoError(t, err)
setting.GravatarSource = gravatarSource
err = system_model.Init()
assert.NoError(t, err)
}

func TestHashEmail(t *testing.T) {
assert.Equal(t,
"d41d8cd98f00b204e9800998ecf8427e",
HashEmail(""),
avatars_model.HashEmail(""),
)
assert.Equal(t,
"353cbad9b58e69c96154ad99f92bedc7",
HashEmail("gitea@example.com"),
avatars_model.HashEmail("gitea@example.com"),
)
}

func TestSizedAvatarLink(t *testing.T) {
setting.AppSubURL = "/testsuburl"

disableGravatar()
disableGravatar(t)
assert.Equal(t, "/testsuburl/assets/img/avatar_default.png",
GenerateEmailAvatarFastLink("gitea@example.com", 100))
avatars_model.GenerateEmailAvatarFastLink("gitea@example.com", 100))

enableGravatar(t)
assert.Equal(t,
"https://secure.gravatar.com/avatar/353cbad9b58e69c96154ad99f92bedc7?d=identicon&s=100",
GenerateEmailAvatarFastLink("gitea@example.com", 100),
avatars_model.GenerateEmailAvatarFastLink("gitea@example.com", 100),
)
}
2 changes: 1 addition & 1 deletion models/admin/main_test.go → models/avatars/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package admin_test
package avatars_test

import (
"path/filepath"
Expand Down
15 changes: 15 additions & 0 deletions models/fixtures/system_setting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-
id: 1
setting_key: 'disable_gravatar'
setting_value: 'false'
version: 1
created: 1653533198
updated: 1653533198

-
id: 2
setting_key: 'enable_federated_avatar'
setting_value: 'false'
version: 1
created: 1653533198
updated: 1653533198
4 changes: 2 additions & 2 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"strconv"
"strings"

admin_model "code.gitea.io/gitea/models/admin"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/foreignreference"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/perm"
access_model "code.gitea.io/gitea/models/perm/access"
project_model "code.gitea.io/gitea/models/project"
repo_model "code.gitea.io/gitea/models/repo"
system_model "code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
Expand Down Expand Up @@ -2435,7 +2435,7 @@ func DeleteOrphanedIssues() error {

// Remove issue attachment files.
for i := range attachmentPaths {
admin_model.RemoveAllWithNotice(db.DefaultContext, "Delete issue attachment", attachmentPaths[i])
system_model.RemoveAllWithNotice(db.DefaultContext, "Delete issue attachment", attachmentPaths[i])
}
return nil
}
2 changes: 2 additions & 0 deletions models/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"

_ "code.gitea.io/gitea/models/system"

"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ var migrations = []Migration{
NewMigration("Add badges to users", createUserBadgesTable),
// v225 -> v226
NewMigration("Alter gpg_key/public_key content TEXT fields to MEDIUMTEXT", alterPublicGPGKeyContentFieldsToMediumText),
// v226 -> v227
NewMigration("Create key/value table for system settings", createSystemSettingsTable),
}

// GetCurrentDBVersion returns the current db version
Expand Down
64 changes: 64 additions & 0 deletions models/migrations/v226.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"fmt"
"strconv"

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/xorm"
)

type SystemSetting struct {
ID int64 `xorm:"pk autoincr"`
SettingKey string `xorm:"varchar(255) unique"` // ensure key is always lowercase
SettingValue string `xorm:"text"`
Version int `xorm:"version"` // prevent to override
Created timeutil.TimeStamp `xorm:"created"`
Updated timeutil.TimeStamp `xorm:"updated"`
}

func insertSettingsIfNotExist(x *xorm.Engine, sysSettings []*SystemSetting) error {
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}
for _, setting := range sysSettings {
exist, err := sess.Table("system_setting").Where("setting_key=?", setting.SettingKey).Exist()
if err != nil {
return err
}
if !exist {
if _, err := sess.Insert(setting); err != nil {
return err
}
}
}
return sess.Commit()
}

func createSystemSettingsTable(x *xorm.Engine) error {
if err := x.Sync2(new(SystemSetting)); err != nil {
return fmt.Errorf("sync2: %v", err)
}

// migrate xx to database
sysSettings := []*SystemSetting{
{
SettingKey: "picture.disable_gravatar",
SettingValue: strconv.FormatBool(setting.DisableGravatar),
},
{
SettingKey: "picture.enable_federated_avatar",
SettingValue: strconv.FormatBool(setting.EnableFederatedAvatar),
},
}

return insertSettingsIfNotExist(x, sysSettings)
}
Loading