Skip to content

Commit b83e577

Browse files
authored
Merge branch 'main' into split-index-js
2 parents ffc35fb + 3728f1d commit b83e577

File tree

30 files changed

+1716
-42
lines changed

30 files changed

+1716
-42
lines changed

custom/conf/app.example.ini

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,19 @@ PATH =
19251925
;SCHEDULE = @every 168h
19261926
;OLDER_THAN = 8760h
19271927

1928+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1929+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1930+
;; Check for new Gitea versions
1931+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1932+
;[cron.update_checker]
1933+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1934+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1935+
;ENABLED = false
1936+
;RUN_AT_START = false
1937+
;ENABLE_SUCCESS_NOTICE = false
1938+
;SCHEDULE = @every 168h
1939+
;HTTP_ENDPOINT = https://dl.gitea.io/gitea/version.json
1940+
19281941
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19291942
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19301943
;; Git Operation timeout in seconds

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ or any corresponding location. When installing from a distribution, this will
2323
typically be found at `/etc/gitea/conf/app.ini`.
2424

2525
The defaults provided here are best-effort (not built automatically). They are
26-
accurately recorded in [app.example.ini](https://github.com/go-gitea/gitea/blob/master/custom/conf/app.example.ini)
27-
(s/master/\<tag|release\>). Any string in the format `%(X)s` is a feature powered
26+
accurately recorded in [app.example.ini](https://github.com/go-gitea/gitea/blob/main/custom/conf/app.example.ini)
27+
(s/main/\<tag|release\>). Any string in the format `%(X)s` is a feature powered
2828
by [ini](https://github.com/go-ini/ini/#recursive-values), for reading values recursively.
2929

3030
Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
@@ -824,9 +824,16 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef
824824
- `ENABLED`: **false**: Enable service.
825825
- `RUN_AT_START`: **false**: Run tasks at start up time (if ENABLED).
826826
- `NO_SUCCESS_NOTICE`: **false**: Set to true to switch off success notices.
827-
- `SCHEDULE`: **@every 128h**: Cron syntax for scheduling a work, e.g. `@every 128h`.
827+
- `SCHEDULE`: **@every 168h**: Cron syntax to set how often to check.
828828
- `OLDER_THAN`: **@every 8760h**: any action older than this expression will be deleted from database, suggest using `8760h` (1 year) because that's the max length of heatmap.
829829

830+
#### Cron - Check for new Gitea versions ('cron.update_checker')
831+
- `ENABLED`: **false**: Enable service.
832+
- `RUN_AT_START`: **false**: Run tasks at start up time (if ENABLED).
833+
- `ENABLE_SUCCESS_NOTICE`: **true**: Set to false to switch off success notices.
834+
- `SCHEDULE`: **@every 168h**: Cron syntax for scheduling a work, e.g. `@every 168h`.
835+
- `HTTP_ENDPOINT`: **https://dl.gitea.io/gitea/version.json**: the endpoint that Gitea will check for newer versions
836+
830837
## Git (`git`)
831838

832839
- `PATH`: **""**: The path of git executable. If empty, Gitea searches through the PATH environment.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ require (
5757
github.com/google/go-querystring v1.1.0 // indirect
5858
github.com/google/uuid v1.2.0
5959
github.com/gorilla/context v1.1.1
60+
github.com/gorilla/feeds v1.1.1
6061
github.com/gorilla/mux v1.8.0 // indirect
6162
github.com/gorilla/sessions v1.2.1 // indirect
6263
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,8 @@ github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8
598598
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
599599
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
600600
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
601+
github.com/gorilla/feeds v1.1.1 h1:HwKXxqzcRNg9to+BbvJog4+f3s/xzvtZXICcQGutYfY=
602+
github.com/gorilla/feeds v1.1.1/go.mod h1:Nk0jZrvPFZX1OBe5NPiddPw7CfwF6Q9eqzaBbaightA=
601603
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
602604
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
603605
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ var migrations = []Migration{
350350
NewMigration("Add renamed_branch table", addRenamedBranchTable),
351351
// v198 -> v199
352352
NewMigration("Add issue content history table", addTableIssueContentHistory),
353+
// v199 -> v200
354+
NewMigration("Add remote version table", addRemoteVersionTable),
353355
}
354356

355357
// GetCurrentDBVersion returns the current db version

models/migrations/v199.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
10+
"xorm.io/xorm"
11+
)
12+
13+
func addRemoteVersionTable(x *xorm.Engine) error {
14+
type RemoteVersion struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
Version string `xorm:"VARCHAR(50)"`
17+
}
18+
19+
if err := x.Sync2(new(RemoteVersion)); err != nil {
20+
return fmt.Errorf("Sync2: %v", err)
21+
}
22+
return nil
23+
}

models/update_checker.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"io/ioutil"
11+
"net/http"
12+
13+
"code.gitea.io/gitea/models/db"
14+
"code.gitea.io/gitea/modules/proxy"
15+
"code.gitea.io/gitea/modules/setting"
16+
17+
"github.com/hashicorp/go-version"
18+
)
19+
20+
// RemoteVersion stores the remote version from the JSON endpoint
21+
type RemoteVersion struct {
22+
ID int64 `xorm:"pk autoincr"`
23+
Version string `xorm:"VARCHAR(50)"`
24+
}
25+
26+
func init() {
27+
db.RegisterModel(new(RemoteVersion))
28+
}
29+
30+
// GiteaUpdateChecker returns error when new version of Gitea is available
31+
func GiteaUpdateChecker(httpEndpoint string) error {
32+
httpClient := &http.Client{
33+
Transport: &http.Transport{
34+
Proxy: proxy.Proxy(),
35+
},
36+
}
37+
38+
req, err := http.NewRequest("GET", httpEndpoint, nil)
39+
if err != nil {
40+
return err
41+
}
42+
resp, err := httpClient.Do(req)
43+
if err != nil {
44+
return err
45+
}
46+
defer resp.Body.Close()
47+
body, err := ioutil.ReadAll(resp.Body)
48+
if err != nil {
49+
return err
50+
}
51+
52+
type v struct {
53+
Latest struct {
54+
Version string `json:"version"`
55+
} `json:"latest"`
56+
}
57+
ver := v{}
58+
err = json.Unmarshal(body, &ver)
59+
if err != nil {
60+
return err
61+
}
62+
63+
return UpdateRemoteVersion(ver.Latest.Version)
64+
65+
}
66+
67+
// UpdateRemoteVersion updates the latest available version of Gitea
68+
func UpdateRemoteVersion(version string) (err error) {
69+
sess := db.NewSession(db.DefaultContext)
70+
defer sess.Close()
71+
if err = sess.Begin(); err != nil {
72+
return err
73+
}
74+
75+
currentVersion := &RemoteVersion{ID: 1}
76+
has, err := sess.Get(currentVersion)
77+
if err != nil {
78+
return fmt.Errorf("get: %v", err)
79+
} else if !has {
80+
currentVersion.ID = 1
81+
currentVersion.Version = version
82+
83+
if _, err = sess.InsertOne(currentVersion); err != nil {
84+
return fmt.Errorf("insert: %v", err)
85+
}
86+
return nil
87+
}
88+
89+
if _, err = sess.Update(&RemoteVersion{ID: 1, Version: version}); err != nil {
90+
return err
91+
}
92+
93+
return sess.Commit()
94+
}
95+
96+
// GetRemoteVersion returns the current remote version (or currently installed verson if fail to fetch from DB)
97+
func GetRemoteVersion() string {
98+
e := db.GetEngine(db.DefaultContext)
99+
v := &RemoteVersion{ID: 1}
100+
_, err := e.Get(&v)
101+
if err != nil {
102+
// return current version if fail to fetch from DB
103+
return setting.AppVer
104+
}
105+
return v.Version
106+
}
107+
108+
// GetNeedUpdate returns true whether a newer version of Gitea is available
109+
func GetNeedUpdate() bool {
110+
curVer, err := version.NewVersion(setting.AppVer)
111+
if err != nil {
112+
// return false to fail silently
113+
return false
114+
}
115+
remoteVer, err := version.NewVersion(GetRemoteVersion())
116+
if err != nil {
117+
// return false to fail silently
118+
return false
119+
}
120+
return curVer.LessThan(remoteVer)
121+
}

modules/context/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func (ctx *Context) PlainText(status int, bs []byte) {
320320
ctx.Resp.WriteHeader(status)
321321
ctx.Resp.Header().Set("Content-Type", "text/plain;charset=utf-8")
322322
if _, err := ctx.Resp.Write(bs); err != nil {
323-
ctx.ServerError("Render JSON failed", err)
323+
ctx.ServerError("Write bytes failed", err)
324324
}
325325
}
326326

modules/cron/tasks_extended.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ func registerDeleteOldActions() {
131131
})
132132
}
133133

134+
func registerUpdateGiteaChecker() {
135+
type UpdateCheckerConfig struct {
136+
BaseConfig
137+
HTTPEndpoint string
138+
}
139+
RegisterTaskFatal("update_checker", &UpdateCheckerConfig{
140+
BaseConfig: BaseConfig{
141+
Enabled: true,
142+
RunAtStart: false,
143+
Schedule: "@every 168h",
144+
},
145+
HTTPEndpoint: "https://dl.gitea.io/gitea/version.json",
146+
}, func(ctx context.Context, _ *models.User, config Config) error {
147+
updateCheckerConfig := config.(*UpdateCheckerConfig)
148+
return models.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint)
149+
})
150+
}
151+
134152
func initExtendedTasks() {
135153
registerDeleteInactiveUsers()
136154
registerDeleteRepositoryArchives()
@@ -142,4 +160,5 @@ func initExtendedTasks() {
142160
registerDeleteMissingRepositories()
143161
registerRemoveRandomAvatars()
144162
registerDeleteOldActions()
163+
registerUpdateGiteaChecker()
145164
}

options/locale/locale_en-US.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ view_home = View %s
228228
search_repos = Find a repository…
229229
filter = Other Filters
230230
filter_by_team_repositories = Filter by team repositories
231+
feed_of = Feed of "%s"
231232
232233
show_archived = Archived
233234
show_both_archived_unarchived = Showing both archived and unarchived
@@ -2777,6 +2778,8 @@ publish_release = `released <a href="%s/releases/tag/%s"> "%[4]s" </a> at <a hr
27772778
review_dismissed = `dismissed review from <b>%[4]s</b> for <a href="%[1]s/pulls/%[2]s">%[3]s#%[2]s</a>`
27782779
review_dismissed_reason = Reason:
27792780
create_branch = created branch <a href="%[1]s/src/branch/%[2]s">%[3]s</a> in <a href="%[1]s">%[4]s</a>
2781+
stared_repo = stared <a href="%[1]s">%[2]s</a>
2782+
watched_repo = started watching <a href="%[1]s">%[2]s</a>
27802783

27812784
[tool]
27822785
ago = %s ago

routers/web/admin/admin.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func Dashboard(ctx *context.Context) {
125125
ctx.Data["PageIsAdmin"] = true
126126
ctx.Data["PageIsAdminDashboard"] = true
127127
ctx.Data["Stats"] = models.GetStatistic()
128+
ctx.Data["NeedUpdate"] = models.GetNeedUpdate()
129+
ctx.Data["RemoteVersion"] = models.GetRemoteVersion()
128130
// FIXME: update periodically
129131
updateSystemStatus()
130132
ctx.Data["SysStatus"] = sysStatus

0 commit comments

Comments
 (0)