aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-10-15 23:46:06 +0800
committerGitHub <noreply@github.com>2023-10-15 17:46:06 +0200
commitcddf245c12223c783c856c87c21cc796ac425439 (patch)
tree21edf50797c62c3e70dbada95edc20d7ddfd53b8 /modules
parent7480aacdad9470458fa0142beab54b8011bbac25 (diff)
downloadgitea-cddf245c12223c783c856c87c21cc796ac425439.tar.gz
gitea-cddf245c12223c783c856c87c21cc796ac425439.zip
Replace more db.DefaultContext (#27628)
Target #27065
Diffstat (limited to 'modules')
-rw-r--r--modules/system/appstate.go6
-rw-r--r--modules/system/appstate_test.go11
-rw-r--r--modules/system/db.go10
-rw-r--r--modules/updatechecker/update_checker.go15
4 files changed, 24 insertions, 18 deletions
diff --git a/modules/system/appstate.go b/modules/system/appstate.go
index 765501d432..e065688a4f 100644
--- a/modules/system/appstate.go
+++ b/modules/system/appstate.go
@@ -3,10 +3,12 @@
package system
+import "context"
+
// StateStore is the interface to get/set app state items
type StateStore interface {
- Get(item StateItem) error
- Set(item StateItem) error
+ Get(ctx context.Context, item StateItem) error
+ Set(ctx context.Context, item StateItem) error
}
// StateItem provides the name for a state item. the name will be used to generate filenames, etc
diff --git a/modules/system/appstate_test.go b/modules/system/appstate_test.go
index 232362cf0c..d4b9e167c2 100644
--- a/modules/system/appstate_test.go
+++ b/modules/system/appstate_test.go
@@ -6,6 +6,7 @@ package system
import (
"testing"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
@@ -40,25 +41,25 @@ func TestAppStateDB(t *testing.T) {
as := &DBStore{}
item1 := new(testItem1)
- assert.NoError(t, as.Get(item1))
+ assert.NoError(t, as.Get(db.DefaultContext, item1))
assert.Equal(t, "", item1.Val1)
assert.EqualValues(t, 0, item1.Val2)
item1 = new(testItem1)
item1.Val1 = "a"
item1.Val2 = 2
- assert.NoError(t, as.Set(item1))
+ assert.NoError(t, as.Set(db.DefaultContext, item1))
item2 := new(testItem2)
item2.K = "V"
- assert.NoError(t, as.Set(item2))
+ assert.NoError(t, as.Set(db.DefaultContext, item2))
item1 = new(testItem1)
- assert.NoError(t, as.Get(item1))
+ assert.NoError(t, as.Get(db.DefaultContext, item1))
assert.Equal(t, "a", item1.Val1)
assert.EqualValues(t, 2, item1.Val2)
item2 = new(testItem2)
- assert.NoError(t, as.Get(item2))
+ assert.NoError(t, as.Get(db.DefaultContext, item2))
assert.Equal(t, "V", item2.K)
}
diff --git a/modules/system/db.go b/modules/system/db.go
index 3f2e4c59bb..05e9de0ae8 100644
--- a/modules/system/db.go
+++ b/modules/system/db.go
@@ -4,6 +4,8 @@
package system
import (
+ "context"
+
"code.gitea.io/gitea/models/system"
"code.gitea.io/gitea/modules/json"
@@ -14,8 +16,8 @@ import (
type DBStore struct{}
// Get reads the state item
-func (f *DBStore) Get(item StateItem) error {
- content, err := system.GetAppStateContent(item.Name())
+func (f *DBStore) Get(ctx context.Context, item StateItem) error {
+ content, err := system.GetAppStateContent(ctx, item.Name())
if err != nil {
return err
}
@@ -26,10 +28,10 @@ func (f *DBStore) Get(item StateItem) error {
}
// Set saves the state item
-func (f *DBStore) Set(item StateItem) error {
+func (f *DBStore) Set(ctx context.Context, item StateItem) error {
b, err := json.Marshal(item)
if err != nil {
return err
}
- return system.SaveAppStateContent(item.Name(), util.BytesToReadOnlyString(b))
+ return system.SaveAppStateContent(ctx, item.Name(), util.BytesToReadOnlyString(b))
}
diff --git a/modules/updatechecker/update_checker.go b/modules/updatechecker/update_checker.go
index bc3f93aad7..3c1e05d060 100644
--- a/modules/updatechecker/update_checker.go
+++ b/modules/updatechecker/update_checker.go
@@ -4,6 +4,7 @@
package updatechecker
import (
+ "context"
"io"
"net/http"
@@ -58,31 +59,31 @@ func GiteaUpdateChecker(httpEndpoint string) error {
return err
}
- return UpdateRemoteVersion(respData.Latest.Version)
+ return UpdateRemoteVersion(req.Context(), respData.Latest.Version)
}
// UpdateRemoteVersion updates the latest available version of Gitea
-func UpdateRemoteVersion(version string) (err error) {
- return system.AppState.Set(&CheckerState{LatestVersion: version})
+func UpdateRemoteVersion(ctx context.Context, version string) (err error) {
+ return system.AppState.Set(ctx, &CheckerState{LatestVersion: version})
}
// GetRemoteVersion returns the current remote version (or currently installed version if fail to fetch from DB)
-func GetRemoteVersion() string {
+func GetRemoteVersion(ctx context.Context) string {
item := new(CheckerState)
- if err := system.AppState.Get(item); err != nil {
+ if err := system.AppState.Get(ctx, item); err != nil {
return ""
}
return item.LatestVersion
}
// GetNeedUpdate returns true whether a newer version of Gitea is available
-func GetNeedUpdate() bool {
+func GetNeedUpdate(ctx context.Context) bool {
curVer, err := version.NewVersion(setting.AppVer)
if err != nil {
// return false to fail silently
return false
}
- remoteVerStr := GetRemoteVersion()
+ remoteVerStr := GetRemoteVersion(ctx)
if remoteVerStr == "" {
// no remote version is known
return false