MM-17888 Check plugin Helpers minimum server version comments (#12663)
Этот коммит содержится в:
коммит произвёл
Ben Schumacher
родитель
7cc1f19453
Коммит
7d0d7c304e
22
plugin/checker/internal/version/comments.go
Обычный файл
22
plugin/checker/internal/version/comments.go
Обычный файл
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var versionCommentRE = regexp.MustCompile(`^Minimum server version: (\d+\.\d+(?:\.\d+[\w-]*)?)$`)
|
||||
|
||||
func ExtractMinimumVersionFromComment(s string) string {
|
||||
lines := strings.Split(strings.TrimSpace(s), "\n")
|
||||
if len(lines) > 0 {
|
||||
lastLine := lines[len(lines)-1]
|
||||
if m := versionCommentRE.FindStringSubmatch(lastLine); len(m) >= 1 {
|
||||
return m[1]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
49
plugin/checker/internal/version/comments_test.go
Обычный файл
49
plugin/checker/internal/version/comments_test.go
Обычный файл
@@ -0,0 +1,49 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestExtractVersionFromComment(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
input: "This is a comment.\n\nMinimum server version: 1.2.3-rc1\n",
|
||||
expected: "1.2.3-rc1",
|
||||
},
|
||||
{
|
||||
input: "This is a comment.\n\nMinimum server version: 1.2.3\n",
|
||||
expected: "1.2.3",
|
||||
},
|
||||
{
|
||||
input: "This is a comment.\n\nMinimum server version: 1.2\n",
|
||||
expected: "1.2",
|
||||
},
|
||||
{
|
||||
input: "This is a comment.\n\nMinimum server version: 1\n",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
input: "This is a comment.\n",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
input: "",
|
||||
expected: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%+v", tc), func(t *testing.T) {
|
||||
assert.Equal(t, tc.expected, ExtractMinimumVersionFromComment(tc.input))
|
||||
})
|
||||
}
|
||||
}
|
||||
80
plugin/checker/internal/version/version.go
Обычный файл
80
plugin/checker/internal/version/version.go
Обычный файл
@@ -0,0 +1,80 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type V string
|
||||
|
||||
func (v V) GreaterThanOrEqualTo(other V) bool {
|
||||
return !v.LessThan(other)
|
||||
}
|
||||
|
||||
func (v V) LessThan(other V) bool {
|
||||
leftParts, leftCount := split(v)
|
||||
rightParts, rightCount := split(other)
|
||||
|
||||
var length int
|
||||
if leftCount < rightCount {
|
||||
length = rightCount
|
||||
} else {
|
||||
length = leftCount
|
||||
}
|
||||
|
||||
for i := 0; i < length; i++ {
|
||||
var left, right string
|
||||
|
||||
if i < leftCount {
|
||||
left = leftParts[i]
|
||||
}
|
||||
|
||||
if i < rightCount {
|
||||
right = rightParts[i]
|
||||
}
|
||||
|
||||
if left == right {
|
||||
continue
|
||||
}
|
||||
|
||||
leftInt := parseInt(left)
|
||||
rightInt := parseInt(right)
|
||||
|
||||
isNumericalComparison := leftInt != nil && rightInt != nil
|
||||
|
||||
if isNumericalComparison {
|
||||
return *leftInt < *rightInt
|
||||
}
|
||||
|
||||
return left < right
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func split(v V) ([]string, int) {
|
||||
var chunks []string
|
||||
|
||||
for _, part := range strings.Split(string(v), ".") {
|
||||
chunks = append(chunks, splitNumericalChunks(part)...)
|
||||
}
|
||||
|
||||
return chunks, len(chunks)
|
||||
}
|
||||
|
||||
var numericalOrAlphaRE = regexp.MustCompile(`(\d+|\D+)`)
|
||||
|
||||
func splitNumericalChunks(s string) []string {
|
||||
return numericalOrAlphaRE.FindAllString(s, -1)
|
||||
}
|
||||
|
||||
func parseInt(s string) *int64 {
|
||||
if n, err := strconv.ParseInt(s, 10, 64); err == nil {
|
||||
return &n
|
||||
}
|
||||
return nil
|
||||
}
|
||||
58
plugin/checker/internal/version/version_test.go
Обычный файл
58
plugin/checker/internal/version/version_test.go
Обычный файл
@@ -0,0 +1,58 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package version
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestVersionComparison(t *testing.T) {
|
||||
testCases := []struct {
|
||||
a, b V
|
||||
}{
|
||||
{
|
||||
a: V("1.2"),
|
||||
b: V("1.10"),
|
||||
},
|
||||
{
|
||||
a: V("1.2.1"),
|
||||
b: V("1.2.3"),
|
||||
},
|
||||
{
|
||||
a: V("1.2"),
|
||||
b: V("1.2.3"),
|
||||
},
|
||||
{
|
||||
a: V("1.2.1"),
|
||||
b: V("1.2.3"),
|
||||
},
|
||||
{
|
||||
a: V("1.1"),
|
||||
b: V("1.2.3"),
|
||||
},
|
||||
{
|
||||
a: V("1.2.3"),
|
||||
b: V("1.3"),
|
||||
},
|
||||
{
|
||||
a: V("1.2.1-rc2"),
|
||||
b: V("1.2.1-rc10"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(fmt.Sprintf("%+v", tc), func(t *testing.T) {
|
||||
assert.True(t, tc.a.LessThan(tc.b))
|
||||
assert.False(t, tc.b.LessThan(tc.a))
|
||||
|
||||
assert.True(t, tc.b.GreaterThanOrEqualTo(tc.a))
|
||||
assert.False(t, tc.a.GreaterThanOrEqualTo(tc.b))
|
||||
})
|
||||
}
|
||||
|
||||
assert.True(t, V("1.2").GreaterThanOrEqualTo("1.2"))
|
||||
}
|
||||
Ссылка в новой задаче
Block a user