2021-10-16 14:14:34 +08:00
|
|
|
// 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.
|
|
|
|
|
2021-10-22 00:10:49 +08:00
|
|
|
package updatechecker
|
2021-10-16 14:14:34 +08:00
|
|
|
|
|
|
|
import (
|
2022-01-10 09:48:13 +08:00
|
|
|
"io"
|
2021-10-16 14:14:34 +08:00
|
|
|
"net/http"
|
|
|
|
|
2021-12-17 09:15:02 +08:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-10-16 14:14:34 +08:00
|
|
|
"code.gitea.io/gitea/modules/proxy"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-10-17 07:29:26 +08:00
|
|
|
"code.gitea.io/gitea/modules/system"
|
2021-10-16 14:14:34 +08:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-version"
|
|
|
|
)
|
|
|
|
|
2021-10-22 00:10:49 +08:00
|
|
|
// CheckerState stores the remote version from the JSON endpoint
|
|
|
|
type CheckerState struct {
|
|
|
|
LatestVersion string
|
2021-10-16 14:14:34 +08:00
|
|
|
}
|
|
|
|
|
2021-10-22 00:10:49 +08:00
|
|
|
// Name returns the name of the state item for update checker
|
|
|
|
func (r *CheckerState) Name() string {
|
|
|
|
return "update-checker"
|
2021-10-16 14:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
2022-01-10 09:48:13 +08:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2021-10-16 14:14:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 00:10:49 +08:00
|
|
|
type respType struct {
|
2021-10-16 14:14:34 +08:00
|
|
|
Latest struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
} `json:"latest"`
|
|
|
|
}
|
2021-10-22 00:10:49 +08:00
|
|
|
respData := respType{}
|
|
|
|
err = json.Unmarshal(body, &respData)
|
2021-10-16 14:14:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-22 00:10:49 +08:00
|
|
|
return UpdateRemoteVersion(respData.Latest.Version)
|
2021-10-16 14:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRemoteVersion updates the latest available version of Gitea
|
|
|
|
func UpdateRemoteVersion(version string) (err error) {
|
2022-10-17 07:29:26 +08:00
|
|
|
return system.AppState.Set(&CheckerState{LatestVersion: version})
|
2021-10-16 14:14:34 +08:00
|
|
|
}
|
|
|
|
|
2022-01-10 17:32:37 +08:00
|
|
|
// GetRemoteVersion returns the current remote version (or currently installed version if fail to fetch from DB)
|
2021-10-16 14:14:34 +08:00
|
|
|
func GetRemoteVersion() string {
|
2021-10-22 00:10:49 +08:00
|
|
|
item := new(CheckerState)
|
2022-10-17 07:29:26 +08:00
|
|
|
if err := system.AppState.Get(item); err != nil {
|
2021-10-22 00:10:49 +08:00
|
|
|
return ""
|
2021-10-16 14:14:34 +08:00
|
|
|
}
|
2021-10-22 00:10:49 +08:00
|
|
|
return item.LatestVersion
|
2021-10-16 14:14:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-10-22 00:10:49 +08:00
|
|
|
remoteVerStr := GetRemoteVersion()
|
|
|
|
if remoteVerStr == "" {
|
|
|
|
// no remote version is known
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
remoteVer, err := version.NewVersion(remoteVerStr)
|
2021-10-16 14:14:34 +08:00
|
|
|
if err != nil {
|
|
|
|
// return false to fail silently
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return curVer.LessThan(remoteVer)
|
|
|
|
}
|