@ -2,29 +2,28 @@
// Use of this source code is governed by a MIT-style
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
package models
package updatechecker
import (
import (
"encoding/json"
"encoding/json"
"fmt"
"io/ioutil"
"io/ioutil"
"net/http"
"net/http"
"code.gitea.io/gitea/models/db "
"code.gitea.io/gitea/modules/appstate "
"code.gitea.io/gitea/modules/proxy"
"code.gitea.io/gitea/modules/proxy"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/setting"
"github.com/hashicorp/go-version"
"github.com/hashicorp/go-version"
)
)
// RemoteVersion stores the remote version from the JSON endpoint
// CheckerState stores the remote version from the JSON endpoint
type RemoteVersion struct {
type CheckerState struct {
ID int64 ` xorm:"pk autoincr" `
LatestVersion string
Version string ` xorm:"VARCHAR(50)" `
}
}
func init ( ) {
// Name returns the name of the state item for update checker
db . RegisterModel ( new ( RemoteVersion ) )
func ( r * CheckerState ) Name ( ) string {
return "update-checker"
}
}
// GiteaUpdateChecker returns error when new version of Gitea is available
// GiteaUpdateChecker returns error when new version of Gitea is available
@ -49,60 +48,33 @@ func GiteaUpdateChecker(httpEndpoint string) error {
return err
return err
}
}
type v struct {
type respType struct {
Latest struct {
Latest struct {
Version string ` json:"version" `
Version string ` json:"version" `
} ` json:"latest" `
} ` json:"latest" `
}
}
ver := v { }
respData := respType { }
err = json . Unmarshal ( body , & ver )
err = json . Unmarshal ( body , & respData )
if err != nil {
if err != nil {
return err
return err
}
}
return UpdateRemoteVersion ( ver . Latest . Version )
return UpdateRemoteVersion ( respData . Latest . Version )
}
}
// UpdateRemoteVersion updates the latest available version of Gitea
// UpdateRemoteVersion updates the latest available version of Gitea
func UpdateRemoteVersion ( version string ) ( err error ) {
func UpdateRemoteVersion ( version string ) ( err error ) {
sess := db . NewSession ( db . DefaultContext )
return appstate . AppState . Set ( & CheckerState { LatestVersion : version } )
defer sess . Close ( )
if err = sess . Begin ( ) ; err != nil {
return err
}
currentVersion := & RemoteVersion { ID : 1 }
has , err := sess . Get ( currentVersion )
if err != nil {
return fmt . Errorf ( "get: %v" , err )
} else if ! has {
currentVersion . ID = 1
currentVersion . Version = version
if _ , err = sess . InsertOne ( currentVersion ) ; err != nil {
return fmt . Errorf ( "insert: %v" , err )
}
return nil
}
if _ , err = sess . Update ( & RemoteVersion { ID : 1 , Version : version } ) ; err != nil {
return err
}
return sess . Commit ( )
}
}
// GetRemoteVersion returns the current remote version (or currently installed verson if fail to fetch from DB)
// GetRemoteVersion returns the current remote version (or currently installed verson if fail to fetch from DB)
func GetRemoteVersion ( ) string {
func GetRemoteVersion ( ) string {
e := db . GetEngine ( db . DefaultContext )
item := new ( CheckerState )
v := & RemoteVersion { ID : 1 }
if err := appstate . AppState . Get ( item ) ; err != nil {
_ , err := e . Get ( & v )
return ""
if err != nil {
// return current version if fail to fetch from DB
return setting . AppVer
}
}
return v . Version
return item . LatestVersion
}
}
// GetNeedUpdate returns true whether a newer version of Gitea is available
// GetNeedUpdate returns true whether a newer version of Gitea is available
@ -112,7 +84,12 @@ func GetNeedUpdate() bool {
// return false to fail silently
// return false to fail silently
return false
return false
}
}
remoteVer , err := version . NewVersion ( GetRemoteVersion ( ) )
remoteVerStr := GetRemoteVersion ( )
if remoteVerStr == "" {
// no remote version is known
return false
}
remoteVer , err := version . NewVersion ( remoteVerStr )
if err != nil {
if err != nil {
// return false to fail silently
// return false to fail silently
return false
return false