diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2021-10-22 00:10:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-21 17:10:49 +0100 |
commit | 960c322586eceb9598bb0a9985a8dd987dc74807 (patch) | |
tree | 8a95695e81f571c82728994a006620b08e7fc8ab /modules | |
parent | 67561e79f15f23a00ad07df36de0ee54d69efc78 (diff) | |
download | gitea-960c322586eceb9598bb0a9985a8dd987dc74807.tar.gz gitea-960c322586eceb9598bb0a9985a8dd987dc74807.zip |
Refactor update checker to use AppState (#17387)
We have the `AppState` module now, it can store app related data easily. We do not need to create separate tables for each feature.
So the update checker can use `AppState` instead of a one-row dedicate table.
And the code of update checker is moved from `models` to `modules`.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/cron/tasks_extended.go | 3 | ||||
-rw-r--r-- | modules/updatechecker/update_checker.go | 98 |
2 files changed, 100 insertions, 1 deletions
diff --git a/modules/cron/tasks_extended.go b/modules/cron/tasks_extended.go index 6645e71d2c..9a37c40faf 100644 --- a/modules/cron/tasks_extended.go +++ b/modules/cron/tasks_extended.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/updatechecker" ) func registerDeleteInactiveUsers() { @@ -145,7 +146,7 @@ func registerUpdateGiteaChecker() { HTTPEndpoint: "https://dl.gitea.io/gitea/version.json", }, func(ctx context.Context, _ *models.User, config Config) error { updateCheckerConfig := config.(*UpdateCheckerConfig) - return models.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint) + return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint) }) } diff --git a/modules/updatechecker/update_checker.go b/modules/updatechecker/update_checker.go new file mode 100644 index 0000000000..01242189fa --- /dev/null +++ b/modules/updatechecker/update_checker.go @@ -0,0 +1,98 @@ +// Copyright 2021 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 updatechecker + +import ( + "encoding/json" + "io/ioutil" + "net/http" + + "code.gitea.io/gitea/modules/appstate" + "code.gitea.io/gitea/modules/proxy" + "code.gitea.io/gitea/modules/setting" + + "github.com/hashicorp/go-version" +) + +// CheckerState stores the remote version from the JSON endpoint +type CheckerState struct { + LatestVersion string +} + +// Name returns the name of the state item for update checker +func (r *CheckerState) Name() string { + return "update-checker" +} + +// GiteaUpdateChecker returns error when new version of Gitea is available +func GiteaUpdateChecker(httpEndpoint string) error { + httpClient := &http.Client{ + Transport: &http.Transport{ + Proxy: proxy.Proxy(), + }, + } + + req, err := http.NewRequest("GET", httpEndpoint, nil) + if err != nil { + return err + } + resp, err := httpClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + type respType struct { + Latest struct { + Version string `json:"version"` + } `json:"latest"` + } + respData := respType{} + err = json.Unmarshal(body, &respData) + if err != nil { + return err + } + + return UpdateRemoteVersion(respData.Latest.Version) + +} + +// UpdateRemoteVersion updates the latest available version of Gitea +func UpdateRemoteVersion(version string) (err error) { + return appstate.AppState.Set(&CheckerState{LatestVersion: version}) +} + +// GetRemoteVersion returns the current remote version (or currently installed verson if fail to fetch from DB) +func GetRemoteVersion() string { + item := new(CheckerState) + if err := appstate.AppState.Get(item); err != nil { + return "" + } + return item.LatestVersion +} + +// GetNeedUpdate returns true whether a newer version of Gitea is available +func GetNeedUpdate() bool { + curVer, err := version.NewVersion(setting.AppVer) + if err != nil { + // return false to fail silently + return false + } + remoteVerStr := GetRemoteVersion() + if remoteVerStr == "" { + // no remote version is known + return false + } + remoteVer, err := version.NewVersion(remoteVerStr) + if err != nil { + // return false to fail silently + return false + } + return curVer.LessThan(remoteVer) +} |