You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.3 KiB
99 lines
2.3 KiB
3 years ago
|
// 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.
|
||
|
|
||
3 years ago
|
package updatechecker
|
||
3 years ago
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/modules/appstate"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/proxy"
|
||
|
"code.gitea.io/gitea/modules/setting"
|
||
|
|
||
|
"github.com/hashicorp/go-version"
|
||
|
)
|
||
|
|
||
3 years ago
|
// CheckerState stores the remote version from the JSON endpoint
|
||
|
type CheckerState struct {
|
||
|
LatestVersion string
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
// Name returns the name of the state item for update checker
|
||
|
func (r *CheckerState) Name() string {
|
||
|
return "update-checker"
|
||
3 years ago
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
3 years ago
|
type respType struct {
|
||
3 years ago
|
Latest struct {
|
||
|
Version string `json:"version"`
|
||
|
} `json:"latest"`
|
||
|
}
|
||
3 years ago
|
respData := respType{}
|
||
|
err = json.Unmarshal(body, &respData)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
return UpdateRemoteVersion(respData.Latest.Version)
|
||
3 years ago
|
|
||
|
}
|
||
|
|
||
|
// UpdateRemoteVersion updates the latest available version of Gitea
|
||
|
func UpdateRemoteVersion(version string) (err error) {
|
||
3 years ago
|
return appstate.AppState.Set(&CheckerState{LatestVersion: version})
|
||
3 years ago
|
}
|
||
|
|
||
|
// GetRemoteVersion returns the current remote version (or currently installed verson if fail to fetch from DB)
|
||
|
func GetRemoteVersion() string {
|
||
3 years ago
|
item := new(CheckerState)
|
||
|
if err := appstate.AppState.Get(item); err != nil {
|
||
|
return ""
|
||
3 years ago
|
}
|
||
3 years ago
|
return item.LatestVersion
|
||
3 years ago
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|
||
3 years ago
|
remoteVerStr := GetRemoteVersion()
|
||
|
if remoteVerStr == "" {
|
||
|
// no remote version is known
|
||
|
return false
|
||
|
}
|
||
|
remoteVer, err := version.NewVersion(remoteVerStr)
|
||
3 years ago
|
if err != nil {
|
||
|
// return false to fail silently
|
||
|
return false
|
||
|
}
|
||
|
return curVer.LessThan(remoteVer)
|
||
|
}
|