fix gitea workflow path and runner label
Build docker and publish / build (20.15.1) (push) Failing after 8m21s
Build docker and publish / build (20.15.1) (push) Failing after 8m21s
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package apiversion
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const DefaultThreshold = "1.0.0"
|
||||
|
||||
type Version struct {
|
||||
Major int
|
||||
Minor int
|
||||
Patch int
|
||||
}
|
||||
|
||||
var versionPattern = regexp.MustCompile(`^v?(\d+)\.(\d+)\.(\d+)$`)
|
||||
|
||||
func Parse(header string) (Version, bool) {
|
||||
normalized := strings.TrimSpace(header)
|
||||
if normalized == "" {
|
||||
return Version{}, false
|
||||
}
|
||||
|
||||
matches := versionPattern.FindStringSubmatch(normalized)
|
||||
if len(matches) != 4 {
|
||||
return Version{}, false
|
||||
}
|
||||
|
||||
major, err := strconv.Atoi(matches[1])
|
||||
if err != nil {
|
||||
return Version{}, false
|
||||
}
|
||||
minor, err := strconv.Atoi(matches[2])
|
||||
if err != nil {
|
||||
return Version{}, false
|
||||
}
|
||||
patch, err := strconv.Atoi(matches[3])
|
||||
if err != nil {
|
||||
return Version{}, false
|
||||
}
|
||||
|
||||
return Version{Major: major, Minor: minor, Patch: patch}, true
|
||||
}
|
||||
|
||||
func UseLatest(header string, threshold string) bool {
|
||||
currentVersion, ok := Parse(header)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
thresholdVersion, ok := Parse(strings.TrimSpace(threshold))
|
||||
if !ok {
|
||||
thresholdVersion, _ = Parse(DefaultThreshold)
|
||||
}
|
||||
|
||||
return compare(currentVersion, thresholdVersion) > 0
|
||||
}
|
||||
|
||||
func compare(left Version, right Version) int {
|
||||
if left.Major != right.Major {
|
||||
if left.Major > right.Major {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
if left.Minor != right.Minor {
|
||||
if left.Minor > right.Minor {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
if left.Patch != right.Patch {
|
||||
if left.Patch > right.Patch {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package apiversion
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
valid bool
|
||||
version Version
|
||||
}{
|
||||
{name: "empty", raw: "", valid: false},
|
||||
{name: "invalid text", raw: "abc", valid: false},
|
||||
{name: "missing patch", raw: "1.0", valid: false},
|
||||
{name: "exact", raw: "1.0.0", valid: true, version: Version{Major: 1, Minor: 0, Patch: 0}},
|
||||
{name: "with prefix", raw: "v1.2.3", valid: true, version: Version{Major: 1, Minor: 2, Patch: 3}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
version, ok := Parse(tt.raw)
|
||||
if ok != tt.valid {
|
||||
t.Fatalf("expected valid=%v, got %v", tt.valid, ok)
|
||||
}
|
||||
if tt.valid && version != tt.version {
|
||||
t.Fatalf("expected version=%+v, got %+v", tt.version, version)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUseLatest(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
header string
|
||||
threshold string
|
||||
expect bool
|
||||
}{
|
||||
{name: "missing header", header: "", threshold: "1.0.0", expect: false},
|
||||
{name: "invalid header", header: "invalid", threshold: "1.0.0", expect: false},
|
||||
{name: "equal threshold", header: "1.0.0", threshold: "1.0.0", expect: false},
|
||||
{name: "greater threshold", header: "1.0.1", threshold: "1.0.0", expect: true},
|
||||
{name: "greater with v prefix", header: "v1.2.3", threshold: "1.0.0", expect: true},
|
||||
{name: "less than threshold", header: "0.9.9", threshold: "1.0.0", expect: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
result := UseLatest(tt.header, tt.threshold)
|
||||
if result != tt.expect {
|
||||
t.Fatalf("expected %v, got %v", tt.expect, result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+12
-10
@@ -3,14 +3,16 @@ package constant
|
||||
type CtxKey string
|
||||
|
||||
const (
|
||||
CtxKeyUser CtxKey = "user"
|
||||
CtxKeySessionID CtxKey = "sessionId"
|
||||
CtxKeyRequestHost CtxKey = "requestHost"
|
||||
CtxKeyPlatform CtxKey = "platform"
|
||||
CtxKeyPayment CtxKey = "payment"
|
||||
CtxLoginType CtxKey = "loginType"
|
||||
LoginType CtxKey = "loginType"
|
||||
CtxKeyIdentifier CtxKey = "identifier"
|
||||
CtxKeyDeviceID CtxKey = "deviceId"
|
||||
CtxKeyIncludeExpired CtxKey = "includeExpired"
|
||||
CtxKeyUser CtxKey = "user"
|
||||
CtxKeySessionID CtxKey = "sessionId"
|
||||
CtxKeyRequestHost CtxKey = "requestHost"
|
||||
CtxKeyPlatform CtxKey = "platform"
|
||||
CtxKeyPayment CtxKey = "payment"
|
||||
CtxLoginType CtxKey = "loginType"
|
||||
LoginType CtxKey = "loginType"
|
||||
CtxKeyIdentifier CtxKey = "identifier"
|
||||
CtxKeyDeviceID CtxKey = "deviceId"
|
||||
CtxKeyIncludeExpired CtxKey = "includeExpired"
|
||||
CtxKeyAPIVersionUseLatest CtxKey = "apiVersionUseLatest"
|
||||
CtxKeyAPIHeaderRaw CtxKey = "apiHeaderRaw"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user