mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 20:50:19 +08:00
fix: add aheadCnt in sysinfo (#1632)
* feat: add sync module to bi-sync mysql * feat: fix the delay problem * feat: fix go mod * feat: fix the varchar(100) parse error * fix: fix go.mod space inconsistency * fix: fix go.mod space inconsistency * fix: use sql builder instead of concatenation * fix: remove serverId * fix: fix file is not `gofumpt`-ed (gofumpt) error * feat: add mysql bi-sync * feat: fix some data inconsistency problems * feat: add function atuo get server uuid * fix: encapsulate the struct to optimize the code * fix: fix incorrect Casdoor version in system info page * fix: fix incorrect root path * Update sysytem_test.go * feat: add aheadCnt means that the commit is ahead of version several times --------- Co-authored-by: hsluoyz <hsluoyz@qq.com>
This commit is contained in:
@ -26,6 +26,7 @@ type SystemInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GitRepoInfo struct {
|
type GitRepoInfo struct {
|
||||||
|
AheadCnt int `json:"ahead_cnt"`
|
||||||
Commit string `json:"commit"`
|
Commit string `json:"commit"`
|
||||||
Version string `json:"version"`
|
Version string `json:"version"`
|
||||||
}
|
}
|
||||||
@ -76,13 +77,14 @@ func (c *ApiController) GetSystemInfo() {
|
|||||||
// @Success 200 {string} local latest version hash of casdoor
|
// @Success 200 {string} local latest version hash of casdoor
|
||||||
// @router /get-release [get]
|
// @router /get-release [get]
|
||||||
func (c *ApiController) GitRepoVersion() {
|
func (c *ApiController) GitRepoVersion() {
|
||||||
commit, version, err := util.GetGitRepoVersion()
|
aheadCnt, commit, version, err := util.GetGitRepoVersion()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ResponseError(err.Error())
|
c.ResponseError(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Data["json"] = GitRepoInfo{
|
c.Data["json"] = GitRepoInfo{
|
||||||
|
AheadCnt: aheadCnt,
|
||||||
Commit: commit,
|
Commit: commit,
|
||||||
Version: version,
|
Version: version,
|
||||||
}
|
}
|
||||||
|
@ -48,20 +48,20 @@ func GetMemoryUsage() (uint64, uint64, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get github current commit and repo release version
|
// get github current commit and repo release version
|
||||||
func GetGitRepoVersion() (string, string, error) {
|
func GetGitRepoVersion() (int, string, string, error) {
|
||||||
_, filename, _, _ := runtime.Caller(0)
|
_, filename, _, _ := runtime.Caller(0)
|
||||||
rootPath := path.Dir(path.Dir(filename))
|
rootPath := path.Dir(path.Dir(filename))
|
||||||
r, err := git.PlainOpen(rootPath)
|
r, err := git.PlainOpen(rootPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return -1, "", "", err
|
||||||
}
|
}
|
||||||
ref, err := r.Head()
|
ref, err := r.Head()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return -1, "", "", err
|
||||||
}
|
}
|
||||||
tags, err := r.Tags()
|
tags, err := r.Tags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return -1, "", "", err
|
||||||
}
|
}
|
||||||
tagMap := make(map[plumbing.Hash]string)
|
tagMap := make(map[plumbing.Hash]string)
|
||||||
err = tags.ForEach(func(t *plumbing.Reference) error {
|
err = tags.ForEach(func(t *plumbing.Reference) error {
|
||||||
@ -74,11 +74,12 @@ func GetGitRepoVersion() (string, string, error) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return -1, "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
|
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
|
||||||
|
|
||||||
|
aheadCnt := 0
|
||||||
releaseVersion := ""
|
releaseVersion := ""
|
||||||
// iterates over the commits
|
// iterates over the commits
|
||||||
err = cIter.ForEach(func(c *object.Commit) error {
|
err = cIter.ForEach(func(c *object.Commit) error {
|
||||||
@ -88,11 +89,14 @@ func GetGitRepoVersion() (string, string, error) {
|
|||||||
releaseVersion = tag
|
releaseVersion = tag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if releaseVersion == "" {
|
||||||
|
aheadCnt++
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return -1, "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ref.Hash().String(), releaseVersion, nil
|
return aheadCnt, ref.Hash().String(), releaseVersion, nil
|
||||||
}
|
}
|
||||||
|
@ -41,15 +41,15 @@ func TestGetMemoryUsage(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGetGitRepoVersion(t *testing.T) {
|
func TestGetGitRepoVersion(t *testing.T) {
|
||||||
commit, version, err := GetGitRepoVersion()
|
aheadCnt, commit, version, err := GetGitRepoVersion()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
t.Log(commit, version)
|
t.Log(aheadCnt, commit, version)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetVersion(t *testing.T) {
|
func TestGetVersion(t *testing.T) {
|
||||||
_, filename, _, _ := runtime.Caller(0)
|
_, filename, _, _ := runtime.Caller(0)
|
||||||
rootPath := path.Dir(path.Dir(filename))
|
root := path.Dir(path.Dir(filename))
|
||||||
r, err := git.PlainOpen(rootPath)
|
r, err := git.PlainOpen(root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
}
|
}
|
||||||
@ -68,9 +68,10 @@ func TestGetVersion(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
testHash := plumbing.NewHash("16b1d0e1f001c1162a263ed50c1a892c947d5783")
|
testHash := plumbing.NewHash("f8bc87eb4e5ba3256424cf14aafe0549f812f1cf")
|
||||||
cIter, err := r.Log(&git.LogOptions{From: testHash})
|
cIter, err := r.Log(&git.LogOptions{From: testHash})
|
||||||
|
|
||||||
|
aheadCnt := 0
|
||||||
releaseVersion := ""
|
releaseVersion := ""
|
||||||
// iterates over the commits
|
// iterates over the commits
|
||||||
err = cIter.ForEach(func(c *object.Commit) error {
|
err = cIter.ForEach(func(c *object.Commit) error {
|
||||||
@ -80,7 +81,12 @@ func TestGetVersion(t *testing.T) {
|
|||||||
releaseVersion = tag
|
releaseVersion = tag
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if releaseVersion == "" {
|
||||||
|
aheadCnt++
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
assert.Equal(t, "v1.260.0", releaseVersion)
|
|
||||||
|
assert.Equal(t, 3, aheadCnt)
|
||||||
|
assert.Equal(t, "v1.257.0", releaseVersion)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user