mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 04:10:20 +08:00
feat: fix incorrect Casdoor version in system info page (#1631)
* 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 --------- Co-authored-by: hsluoyz <hsluoyz@qq.com>
This commit is contained in:
@ -15,12 +15,13 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
)
|
||||
@ -46,33 +47,52 @@ func GetMemoryUsage() (uint64, uint64, error) {
|
||||
return m.TotalAlloc, virtualMem.Total, nil
|
||||
}
|
||||
|
||||
// get github repo release version
|
||||
func GetGitRepoVersion() (string, error) {
|
||||
pwd, err := os.Getwd()
|
||||
// get github current commit and repo release version
|
||||
func GetGitRepoVersion() (string, string, error) {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
rootPath := path.Dir(path.Dir(filename))
|
||||
r, err := git.PlainOpen(rootPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
ref, err := r.Head()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
tags, err := r.Tags()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
tagMap := make(map[plumbing.Hash]string)
|
||||
err = tags.ForEach(func(t *plumbing.Reference) error {
|
||||
// This technique should work for both lightweight and annotated tags.
|
||||
revHash, err := r.ResolveRevision(plumbing.Revision(t.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tagMap[*revHash] = t.Name().Short()
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
fileInfos, err := ioutil.ReadDir(pwd + "/.git/refs/heads")
|
||||
for _, v := range fileInfos {
|
||||
if v.Name() == "master" {
|
||||
if v.ModTime().String() == fileDate {
|
||||
return version, nil
|
||||
} else {
|
||||
fileDate = v.ModTime().String()
|
||||
break
|
||||
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
|
||||
|
||||
releaseVersion := ""
|
||||
// iterates over the commits
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
tag, ok := tagMap[c.Hash]
|
||||
if ok {
|
||||
if releaseVersion == "" {
|
||||
releaseVersion = tag
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadFile(pwd + "/.git/refs/heads/master")
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// Convert to full length
|
||||
temp := string(content)
|
||||
version = strings.ReplaceAll(temp, "\n", "")
|
||||
|
||||
return version, nil
|
||||
return ref.Hash().String(), releaseVersion, nil
|
||||
}
|
||||
|
@ -12,11 +12,19 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !skipCi
|
||||
// +build !skipCi
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"path"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@ -31,3 +39,48 @@ func TestGetMemoryUsage(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
t.Log(used, total)
|
||||
}
|
||||
|
||||
func TestGetGitRepoVersion(t *testing.T) {
|
||||
commit, version, err := GetGitRepoVersion()
|
||||
assert.Nil(t, err)
|
||||
t.Log(commit, version)
|
||||
}
|
||||
|
||||
func TestGetVersion(t *testing.T) {
|
||||
_, filename, _, _ := runtime.Caller(0)
|
||||
rootPath := path.Dir(path.Dir(filename))
|
||||
r, err := git.PlainOpen(rootPath)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
tags, err := r.Tags()
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
tagMap := make(map[plumbing.Hash]string)
|
||||
err = tags.ForEach(func(t *plumbing.Reference) error {
|
||||
// This technique should work for both lightweight and annotated tags.
|
||||
revHash, err := r.ResolveRevision(plumbing.Revision(t.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tagMap[*revHash] = t.Name().Short()
|
||||
return nil
|
||||
})
|
||||
|
||||
testHash := plumbing.NewHash("16b1d0e1f001c1162a263ed50c1a892c947d5783")
|
||||
cIter, err := r.Log(&git.LogOptions{From: testHash})
|
||||
|
||||
releaseVersion := ""
|
||||
// iterates over the commits
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
tag, ok := tagMap[c.Hash]
|
||||
if ok {
|
||||
if releaseVersion == "" {
|
||||
releaseVersion = tag
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
assert.Equal(t, "v1.260.0", releaseVersion)
|
||||
}
|
||||
|
Reference in New Issue
Block a user