Skip to content

Commit b3ec816

Browse files
committed
feat: enhance version api
1 parent 08a0bf7 commit b3ec816

File tree

11 files changed

+388
-113
lines changed

11 files changed

+388
-113
lines changed

api/jiaozifs.gen.go

Lines changed: 284 additions & 94 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ components:
387387
api_version:
388388
type: string
389389
description: runtime version
390-
390+
latest_version:
391+
type: string
391392
ObjectUserMetadata:
392393
type: object
393394
additionalProperties:
@@ -1156,7 +1157,6 @@ paths:
11561157
403:
11571158
description: Forbidden
11581159

1159-
/auth/login:
11601160
/{user}/{reopsitory}/branches:
11611161
parameters:
11621162
- in: path

cmd/daemon.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package cmd
33
import (
44
"context"
55

6+
"github.com/jiaozifs/jiaozifs/version"
7+
68
"github.com/gorilla/sessions"
79
"github.com/jiaozifs/jiaozifs/auth"
810

@@ -45,6 +47,8 @@ var daemonCmd = &cobra.Command{
4547
stop, err := fx_opt.New(cmd.Context(),
4648
fx_opt.Override(new(context.Context), cmd.Context()),
4749
fx_opt.Override(new(utils.Shutdown), shutdown),
50+
//version
51+
fx_opt.Override(new(version.IChecker), version.NewVersionChecker),
4852
//config
4953
fx_opt.Override(new(*config.Config), cfg),
5054
fx_opt.Override(new(*config.APIConfig), &cfg.API),

cmd/version.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var versionCmd = &cobra.Command{
4343

4444
fmt.Println("Runtime Version ", okResp.JSON200.Version)
4545
fmt.Println("Runtime API Version ", okResp.JSON200.ApiVersion)
46+
fmt.Println("LatestVersion Version ", okResp.JSON200.LatestVersion)
4647
return nil
4748
},
4849
}

controller/common_ctl.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"context"
55
"net/http"
66

7+
logging "github.com/ipfs/go-log/v2"
8+
9+
"github.com/jiaozifs/jiaozifs/utils"
10+
711
"github.com/go-openapi/swag"
812
"github.com/jiaozifs/jiaozifs/config"
913

@@ -12,10 +16,13 @@ import (
1216
"go.uber.org/fx"
1317
)
1418

19+
var commonLog = logging.Logger("common")
20+
1521
type CommonController struct {
1622
fx.In
1723

18-
Config *config.Config
24+
VersionChecker version.IChecker
25+
Config *config.Config
1926
}
2027

2128
func (c CommonController) GetVersion(_ context.Context, w *api.JiaozifsResponse, _ *http.Request) {
@@ -25,9 +32,20 @@ func (c CommonController) GetVersion(_ context.Context, w *api.JiaozifsResponse,
2532
return
2633
}
2734

35+
latestVersionResp, err := c.VersionChecker.CheckLatestVersion()
36+
if err == nil {
37+
commonLog.Errorf("fetch latest version failed: %v", err)
38+
}
39+
40+
var latestVersion *string
41+
if latestVersionResp != nil {
42+
latestVersion = utils.String(latestVersionResp.LatestVersion)
43+
}
44+
2845
w.JSON(api.VersionResult{
29-
ApiVersion: swagger.Info.Version,
30-
Version: version.UserVersion(),
46+
ApiVersion: swagger.Info.Version,
47+
Version: version.UserVersion(),
48+
LatestVersion: latestVersion,
3149
})
3250
}
3351

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ require (
3030
github.com/google/go-cmp v0.6.0
3131
github.com/google/uuid v1.4.0
3232
github.com/gorilla/sessions v1.2.2
33+
github.com/hashicorp/go-version v1.6.0
3334
github.com/hnlq715/golang-lru v0.4.0
3435
github.com/ipfs/go-log/v2 v2.5.1
3536
github.com/matoous/go-nanoid/v2 v2.0.0
@@ -49,6 +50,7 @@ require (
4950
github.com/stretchr/testify v1.8.4
5051
github.com/thanhpk/randstr v1.0.6
5152
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc
53+
github.com/treeverse/lakefs v1.3.1
5254
github.com/uptrace/bun v1.1.16
5355
github.com/uptrace/bun/dialect/pgdialect v1.1.16
5456
github.com/uptrace/bun/driver/pgdriver v1.1.16
@@ -100,7 +102,6 @@ require (
100102
github.com/fatih/color v1.15.0 // indirect
101103
github.com/fsnotify/fsnotify v1.6.0 // indirect
102104
github.com/go-openapi/jsonpointer v0.19.6 // indirect
103-
github.com/go-sql-driver/mysql v1.7.0 // indirect
104105
github.com/gogo/protobuf v1.3.2 // indirect
105106
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
106107
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
@@ -182,6 +183,7 @@ require (
182183
google.golang.org/grpc v1.58.3 // indirect
183184
google.golang.org/protobuf v1.31.0 // indirect
184185
gopkg.in/ini.v1 v1.67.0 // indirect
186+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
185187
gopkg.in/yaml.v3 v3.0.1 // indirect
186188
mellium.im/sasl v0.3.1 // indirect
187189
)

go.sum

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
4646
cloud.google.com/go/storage v1.33.0 h1:PVrDOkIC8qQVa1P3SXGpQvfuJhN2LHOoyZvWs8D2X5M=
4747
cloud.google.com/go/storage v1.33.0/go.mod h1:Hhh/dogNRGca7IWv1RC2YqEn0c0G77ctA/OxflYkiD8=
4848
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
49+
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible h1:fcYLmCpyNYRnvJbPerq7U0hS+6+I79yEDJBqVNcqUzU=
4950
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.0 h1:fb8kj/Dh4CSwgsOzHeZY4Xh68cFVbzXx+ONXGMY//4w=
5051
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.0/go.mod h1:uReU2sSxZExRPBAg3qKzmAucSi51+SP1OhohieR821Q=
5152
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.4.0 h1:BMAjVKJM0U/CYF27gA0ZMmXGkOcvfFtD0oHVZ1TIPRI=
@@ -60,6 +61,7 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg6
6061
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.1 h1:WpB/QDNLpMw72xHJc34BNNykqSOeEJDAWkhf0u12/Jk=
6162
github.com/AzureAD/microsoft-authentication-library-for-go v1.1.1/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
6263
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
64+
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
6365
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
6466
github.com/MadAppGang/httplog v1.3.0 h1:1XU54TO8kiqTeO+7oZLKAM3RP/cJ7SadzslRcKspVHo=
6567
github.com/MadAppGang/httplog v1.3.0/go.mod h1:gpYEdkjh/Cda6YxtDy4AB7KY+fR7mb3SqBZw74A5hJ4=
@@ -190,7 +192,6 @@ github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+
190192
github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU=
191193
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
192194
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
193-
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
194195
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
195196
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
196197
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
@@ -283,6 +284,8 @@ github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kX
283284
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
284285
github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY=
285286
github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
287+
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
288+
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
286289
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
287290
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
288291
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
@@ -458,6 +461,8 @@ github.com/thanhpk/randstr v1.0.6 h1:psAOktJFD4vV9NEVb3qkhRSMvYh4ORRaj1+w/hn4B+o
458461
github.com/thanhpk/randstr v1.0.6/go.mod h1:M/H2P1eNLZzlDwAzpkkkUvoyNNMbzRGhESZuEQk3r0U=
459462
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
460463
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
464+
github.com/treeverse/lakefs v1.3.1 h1:oCxDq0ODIl/i/QBl9eeGIl/Rx7KCWlQEIm88IiS+JMg=
465+
github.com/treeverse/lakefs v1.3.1/go.mod h1:YHY5WiQ8RX+m3QtVyZS/34NqEzmg2GyxuY88sUrrfRY=
461466
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
462467
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
463468
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
@@ -850,6 +855,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
850855
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
851856
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
852857
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
858+
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
859+
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
853860
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
854861
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
855862
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ GOGENERATE=$(GOCMD) generate
66
all: build
77
.PHONY: all
88

9-
ldflags=-X=github.com/jiaozofs/jiaozifs/version.CurrentCommit=+git.$(subst -,.,$(shell git describe --always --match=NeVeRmAtCh --dirty 2>/dev/null || git rev-parse --short HEAD 2>/dev/null))
9+
ldflags=-X=github.com/jiaozifs/jiaozifs/version.CurrentCommit=+git.$(subst -,.,$(shell git describe --always --match=NeVeRmAtCh --dirty 2>/dev/null || git rev-parse --short HEAD 2>/dev/null))
1010
ifneq ($(strip $(LDFLAGS)),)
1111
ldflags+=-extldflags=$(LDFLAGS)
1212
endif

version/latest.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
const (
1515
latestVersionTimeout = 10 * time.Second
1616

17-
DefaultReleasesURL = "https://github.com/treeverse/lakeFS/releases"
17+
DefaultReleasesURL = "https://github.com/jiaozifs/jiaozifs/releases"
1818
githubBaseURL = "https://api.github.com/"
1919

20-
GithubRepoOwner = "treeverse"
21-
GithubRepoName = "lakeFS"
20+
GithubRepoOwner = "jiaozifs"
21+
GithubRepoName = "jiaozifs"
2222
)
2323

2424
var ErrHTTPStatus = errors.New("unexpected HTTP status code")
@@ -122,6 +122,14 @@ func CheckLatestVersion(targetVersion string) (*LatestVersionResponse, error) {
122122
return nil, fmt.Errorf("tag parse %s: %w", targetVersion, err)
123123
}
124124

125+
if IsVersionUnreleased() {
126+
return &LatestVersionResponse{
127+
Outdated: false,
128+
LatestVersion: targetV.String(),
129+
CurrentVersion: UserVersion(),
130+
}, nil
131+
}
132+
125133
currentV, err := goversion.NewVersion(UserVersion())
126134
if err != nil {
127135
return nil, fmt.Errorf("version parse %s: %w", UserVersion(), err)
@@ -130,6 +138,41 @@ func CheckLatestVersion(targetVersion string) (*LatestVersionResponse, error) {
130138
return &LatestVersionResponse{
131139
Outdated: currentV.LessThan(targetV),
132140
LatestVersion: targetV.String(),
133-
CurrentVersion: currentV.String(),
141+
CurrentVersion: UserVersion(),
134142
}, nil
135143
}
144+
145+
type IChecker interface {
146+
CheckLatestVersion() (*LatestVersionResponse, error)
147+
}
148+
149+
type Checker struct {
150+
Client http.Client
151+
Version string
152+
latestReleases Source
153+
}
154+
155+
func NewVersionChecker() *Checker {
156+
return &Checker{
157+
Client: http.Client{},
158+
Version: UserVersion(),
159+
latestReleases: NewDefaultVersionSource(time.Hour),
160+
}
161+
}
162+
163+
// CheckLatestVersion will return the latest version of the current package compared to the current version
164+
func (a *Checker) CheckLatestVersion() (*LatestVersionResponse, error) {
165+
if a == nil || a.latestReleases == nil {
166+
return &LatestVersionResponse{}, nil
167+
}
168+
169+
latest, err := a.latestReleases.FetchLatestVersion()
170+
if err != nil {
171+
return nil, err
172+
}
173+
result, err := CheckLatestVersion(latest)
174+
if err != nil {
175+
return nil, err
176+
}
177+
return result, nil
178+
}

version/latest_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"testing"
66

7+
"github.com/jiaozifs/jiaozifs/version"
78
"github.com/stretchr/testify/require"
8-
"github.com/treeverse/lakefs/pkg/version"
99
)
1010

1111
type checkLatestVersionTestCase struct {
@@ -18,21 +18,21 @@ type checkLatestVersionTestCase struct {
1818
func TestCheckLatestVersion(t *testing.T) {
1919
cases := []checkLatestVersionTestCase{
2020
{
21-
CurrentVersion: version.Version,
21+
CurrentVersion: version.UserVersion(),
2222
LatestVersion: "1.0.0",
2323
ExpectedOutdated: false,
2424
},
2525
{
26-
CurrentVersion: "0.0.1",
26+
CurrentVersion: "v0.0.1",
2727
LatestVersion: "1.2.3",
2828
ExpectedOutdated: true,
2929
},
3030
{
31-
CurrentVersion: "1.2.3",
31+
CurrentVersion: "v1.2.3",
3232
LatestVersion: "1.2.3",
3333
},
3434
{
35-
CurrentVersion: "1.2.3",
35+
CurrentVersion: "v1.2.3",
3636
LatestVersion: "1.0.0",
3737
},
3838
{
@@ -42,7 +42,7 @@ func TestCheckLatestVersion(t *testing.T) {
4242
}
4343
for idx, tc := range cases {
4444
t.Run(fmt.Sprintf("check_latest_version_%d", idx), func(t *testing.T) {
45-
version.Version = tc.CurrentVersion
45+
version.BuildVersion = tc.CurrentVersion
4646
t.Logf("check_latest_version test case input %+v", tc)
4747
latest, err := version.CheckLatestVersion(tc.LatestVersion)
4848

0 commit comments

Comments
 (0)