1
0
mirror of https://gitcode.com/gitea/gitea.git synced 2025-06-08 13:28:25 +08:00
Files
.devcontainer
.gitea
.github
assets
build
cmd
contrib
custom
docker
docs
models
modules
actions
activitypub
analyze
assetfs
auth
avatar
base
cache
charset
container
context
contexttest
csv
emoji
eventsource
generate
git
gitgraph
gitrepo
graceful
hcaptcha
highlight
hostmatcher
html
httpcache
httplib
indexer
issue
json
label
lfs
log
markup
mcaptcha
metrics
migration
nosql
optional
options
packages
paginator
pprof
private
process
proxy
proxyprotocol
public
queue
recaptcha
references
regexplru
repository
secret
session
setting
sitemap
ssh
storage
structs
svg
sync
system
templates
test
testlogger
timeutil
translation
turnstile
typesniffer
updatechecker
update_checker.go
upload
uri
user
util
validation
web
webhook
options
public
routers
services
snap
templates
tests
web_src
.air.toml
.changelog.yml
.dockerignore
.editorconfig
.eslintrc.yaml
.gitattributes
.gitignore
.gitpod.yml
.golangci.yml
.ignore
.markdownlint.yaml
.npmrc
.spectral.yaml
.stylelintrc.yaml
.yamllint.yaml
BSDmakefile
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
DCO
Dockerfile
Dockerfile.rootless
LICENSE
MAINTAINERS
Makefile
README.md
README_ZH.md
SECURITY.md
build.go
go.mod
go.sum
main.go
package-lock.json
package.json
playwright.config.js
poetry.lock
poetry.toml
pyproject.toml
vitest.config.js
webpack.config.js
gitea/modules/updatechecker/update_checker.go
2023-10-15 17:46:06 +02:00

98 lines
2.3 KiB
Go

// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package updatechecker
import (
"context"
"io"
"net/http"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/proxy"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/system"
"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 := io.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(req.Context(), respData.Latest.Version)
}
// UpdateRemoteVersion updates the latest available version of Gitea
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(ctx context.Context) string {
item := new(CheckerState)
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(ctx context.Context) bool {
curVer, err := version.NewVersion(setting.AppVer)
if err != nil {
// return false to fail silently
return false
}
remoteVerStr := GetRemoteVersion(ctx)
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)
}