Просмотр исходного кода

refactor: use golang.org/x/mod/semver and theory/jsonpath

Masterminds/semver v1 was only used to compare versions in the update check,
which golang.org/x/mod/semver does with a module already in go.sum.

PaesslerAG/jsonpath is replaced by theory/jsonpath, which implements RFC 9535.
Webhook matching and extraction keep the same results: a missing path is still
an error, single-node paths still return the value rather than a list, and
paths without a leading "$" are still accepted. Bracket notation for keys with
spaces, such as $['key with space'], now works.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 2 дней назад
Родитель
Сommit
b6d81f56a0

+ 2 - 5
service/go.mod

@@ -9,10 +9,8 @@ exclude github.com/alecthomas/go-check-sumtype v0.5.0
 
 require (
 	connectrpc.com/connect v1.21.1
-	github.com/Masterminds/semver v1.5.0
 	github.com/MicahParks/jwkset v0.12.0
 	github.com/MicahParks/keyfunc/v3 v3.9.2
-	github.com/PaesslerAG/jsonpath v0.1.1
 	github.com/alexedwards/argon2id v1.0.0
 	github.com/bufbuild/buf v1.75.0
 	github.com/expr-lang/expr v1.18.1
@@ -31,7 +29,9 @@ require (
 	github.com/robfig/cron/v3 v3.0.1
 	github.com/sirupsen/logrus v1.10.2
 	github.com/stretchr/testify v1.12.1
+	github.com/theory/jsonpath v0.12.3
 	go.akshayshah.org/connectproto v0.6.0
+	golang.org/x/mod v0.43.0
 	golang.org/x/oauth2 v0.38.0
 	golang.org/x/sys v0.50.0
 	google.golang.org/protobuf v1.37.0-rc.1
@@ -79,7 +79,6 @@ require (
 	github.com/Microsoft/go-winio v0.6.4 // indirect
 	github.com/MirrexOne/unqueryvet v1.5.4 // indirect
 	github.com/OpenPeeDeeP/depguard/v2 v2.3.0 // indirect
-	github.com/PaesslerAG/gval v1.2.4 // indirect
 	github.com/alecthomas/chroma/v2 v2.27.0 // indirect
 	github.com/alecthomas/go-check-sumtype v0.5.1-0.20260828200218-ae6904d28606 // indirect
 	github.com/alexkohler/nakedret/v2 v2.0.6 // indirect
@@ -247,7 +246,6 @@ require (
 	github.com/sashamelentyev/interfacebloat v1.1.0 // indirect
 	github.com/sashamelentyev/usestdlibvars v1.29.0 // indirect
 	github.com/securego/gosec/v2 v2.30.0 // indirect
-	github.com/shopspring/decimal v1.6.0 // indirect
 	github.com/sivchari/containedctx v1.0.3 // indirect
 	github.com/sonatard/noctx v0.5.1 // indirect
 	github.com/sourcegraph/go-diff v0.10.0 // indirect
@@ -297,7 +295,6 @@ require (
 	golang.org/x/crypto v0.59.0 // indirect
 	golang.org/x/exp v0.0.0-20261124000051-d6646c509b6d // indirect
 	golang.org/x/exp/typeparams v0.0.0-20261124000051-d6646c509b6d // indirect
-	golang.org/x/mod v0.43.0 // indirect
 	golang.org/x/net v0.63.0 // indirect
 	golang.org/x/sync v0.25.0 // indirect
 	golang.org/x/term v0.48.0 // indirect

+ 2 - 11
service/go.sum

@@ -74,8 +74,6 @@ github.com/ClickHouse/clickhouse-go-linter v1.2.1 h1:zGEKIyd5YL08ieWG/LOUmlau2Dx
 github.com/ClickHouse/clickhouse-go-linter v1.2.1/go.mod h1:pLorS7ffPTfuUV9M0SJgfHA/h/WQPQUk2FWG9x74cQ4=
 github.com/Djarvur/go-err113 v0.1.1 h1:eHfopDqXRwAi+YmCUas75ZE0+hoBHJ2GQNLYRSxao4g=
 github.com/Djarvur/go-err113 v0.1.1/go.mod h1:IaWJdYFLg76t2ihfflPZnM1LIQszWOsFDh2hhhAVF6k=
-github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
-github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
 github.com/Masterminds/semver/v3 v3.5.0 h1:kQceYJfbupGfZOKZQg0kou0DgAKhzDg2NZPAwZ/2OOE=
 github.com/Masterminds/semver/v3 v3.5.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
 github.com/MicahParks/jwkset v0.12.0 h1:d/S70TKDneLrBhn9XvyKaKDp/UnDkU1kacVLHytECA4=
@@ -88,12 +86,6 @@ github.com/MirrexOne/unqueryvet v1.5.4 h1:38QOxShO7JmMWT+eCdDMbcUgGCOeJphVkzzRgy
 github.com/MirrexOne/unqueryvet v1.5.4/go.mod h1:fs9Zq6eh1LRIhsDIsxf9PONVUjYdFHdtkHIgZdJnyPU=
 github.com/OpenPeeDeeP/depguard/v2 v2.3.0 h1:AzcyrJSVScO5DztG8NB5XSKNeuxqgFsLQf+sTXEbF8w=
 github.com/OpenPeeDeeP/depguard/v2 v2.3.0/go.mod h1:s0C87mbhDHS0xxt0K9kkxb+w+W1mdvgUyajfFs4AcfI=
-github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
-github.com/PaesslerAG/gval v1.2.4 h1:rhX7MpjJlcxYwL2eTTYIOBUyEKZ+A96T9vQySWkVUiU=
-github.com/PaesslerAG/gval v1.2.4/go.mod h1:XRFLwvmkTEdYziLdaCeCa5ImcGVrfQbeNUbVR+C6xac=
-github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
-github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk=
-github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY=
 github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
 github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
 github.com/alecthomas/chroma/v2 v2.27.0 h1:FodwmyOBgJULFYmDqibcp9pvfDLWdtPRh9v/r5BXYZs=
@@ -529,9 +521,6 @@ github.com/securego/gosec/v2 v2.30.0 h1:67aF6LHa7SAL6QEc4es857GrXTT50gUqmrl0XPht
 github.com/securego/gosec/v2 v2.30.0/go.mod h1:qQn/kd9spDGy4yCVezLnzPkUuHPhm6jiIcBANad01Sw=
 github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
 github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
-github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
-github.com/shopspring/decimal v1.6.0 h1:DcTwjCj4tJPLuGZYsR9W0hyX9pQwMIkdo26Ig1T9xX4=
-github.com/shopspring/decimal v1.6.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
 github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo=
 github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q=
 github.com/sivchari/containedctx v1.0.3 h1:x+etemjbsh2fB5ewm5FeLNi5bUjK0V8n0RB+Wwfd0XE=
@@ -573,6 +562,8 @@ github.com/tetafro/godot v1.5.6 h1:IEkrFCwXaYHlOn4mGzGS3F3dkP6m9t0jpwqBFPIkKiA=
 github.com/tetafro/godot v1.5.6/go.mod h1:eOkMrVQurDui411nBY2FA05EYH01r14LuWY/NrVDVcU=
 github.com/tetratelabs/wazero v1.12.0 h1:DuWcpNu/FzgEXgGBDp8J1Spc+CWOvvtvVyjKlaZopYU=
 github.com/tetratelabs/wazero v1.12.0/go.mod h1:LvKtzl2RqO4gyF27BiXU+nKAjcV8f38U+kP/q2vgxh0=
+github.com/theory/jsonpath v0.12.3 h1:hRYcJGqnIetJJKJNS5yLLW0LDtY1kzo8S0zHAOTxUQs=
+github.com/theory/jsonpath v0.12.3/go.mod h1:fYTXa8TVFAnyGzDL5JyaFlfaHzKMm+2XfwK3rbEzTC4=
 github.com/tidwall/btree v1.8.2 h1:mTY5iM4UTh2e80TG7RHDQQmnAY3xGNQA/DbU0olkhkA=
 github.com/tidwall/btree v1.8.2/go.mod h1:jBbTdUWhSZClZWoDg54VnvV7/54modSOzDN7VXftj1A=
 github.com/timakin/bodyclose v0.0.0-20261107224203-8bb60b19bea4 h1:kAmyvrO1ePXQxa1gD3Mq27AtapMDmOJdDd5WW/gCY6s=

+ 17 - 6
service/internal/updatecheck/updateCheck.go

@@ -3,7 +3,6 @@ package updatecheck
 import (
 	"context"
 	"encoding/json"
-	"github.com/Masterminds/semver"
 	config "github.com/OliveTin/OliveTin/internal/config"
 	"github.com/OliveTin/OliveTin/internal/installationinfo"
 	"github.com/robfig/cron/v3"
@@ -11,7 +10,10 @@ import (
 	"io"
 	"net/http"
 	"os"
+	"strings"
 	"time"
+
+	"golang.org/x/mod/semver"
 )
 
 type versionMapType struct {
@@ -69,22 +71,31 @@ func parseVersion(input []byte) string {
 }
 
 func parseIfVersionIsLater(currentString string, latestString string) string {
-	currentVersion, errCurrent := semver.NewVersion(currentString)
-	latestVersion, errLatest := semver.NewVersion(latestString)
+	currentVersion := withSemverPrefix(currentString)
+	latestVersion := withSemverPrefix(latestString)
 
-	if errCurrent != nil || errLatest != nil {
-		log.Warnf("Version parse failure: %v %v", errCurrent, errLatest)
+	if !semver.IsValid(currentVersion) || !semver.IsValid(latestVersion) {
+		log.Warnf("Version parse failure: current=%q latest=%q", currentString, latestString)
 
 		return "version-parse-failure"
 	}
 
-	if latestVersion.GreaterThan(currentVersion) {
+	if semver.Compare(latestVersion, currentVersion) > 0 {
 		return latestString
 	}
 
 	return "none"
 }
 
+// OliveTin versions are published without the "v" prefix that semver comparison requires.
+func withSemverPrefix(version string) string {
+	if strings.HasPrefix(version, "v") {
+		return version
+	}
+
+	return "v" + version
+}
+
 func doRequest() string {
 	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
 

+ 4 - 0
service/internal/updatecheck/updateCheck_test.go

@@ -11,4 +11,8 @@ func TestVersionLater(t *testing.T) {
 	assert.Equal(t, "none", parseIfVersionIsLater("2.0.0", "1.10.1"))
 	assert.Equal(t, "version-parse-failure", parseIfVersionIsLater("1.0.0", "1.2.3.4"))
 	assert.Equal(t, "version-parse-failure", parseIfVersionIsLater("asdf", "foobar"))
+	assert.Equal(t, "3000.23.0", parseIfVersionIsLater("3000.22.0", "3000.23.0"))
+	assert.Equal(t, "none", parseIfVersionIsLater("3000.10.0", "3000.9.0"))
+	assert.Equal(t, "v1.1.0", parseIfVersionIsLater("v1.0.0", "v1.1.0"))
+	assert.Equal(t, "version-parse-failure", parseIfVersionIsLater("dev", "3000.23.0"))
 }

+ 35 - 3
service/internal/webhooks/jsonpath.go

@@ -3,8 +3,9 @@ package webhooks
 import (
 	"encoding/json"
 	"fmt"
+	"strings"
 
-	"github.com/PaesslerAG/jsonpath"
+	"github.com/theory/jsonpath"
 )
 
 type JSONMatcher struct {
@@ -20,7 +21,7 @@ func NewJSONMatcher(payload []byte) (*JSONMatcher, error) {
 }
 
 func (m *JSONMatcher) MatchPath(pathExpr string, expectedValue string) (bool, error) {
-	value, err := jsonpath.Get(pathExpr, m.payload)
+	value, err := m.selectValue(pathExpr)
 	if err != nil {
 		return false, err
 	}
@@ -41,7 +42,7 @@ func (m *JSONMatcher) MatchPath(pathExpr string, expectedValue string) (bool, er
 }
 
 func (m *JSONMatcher) ExtractValue(pathExpr string) (string, error) {
-	value, err := jsonpath.Get(pathExpr, m.payload)
+	value, err := m.selectValue(pathExpr)
 	if err != nil {
 		return "", err
 	}
@@ -60,6 +61,37 @@ func (m *JSONMatcher) ExtractValue(pathExpr string) (string, error) {
 	return string(jsonBytes), nil
 }
 
+// selectValue returns the single value for a path that can only match one node,
+// and a list for a path that can match several (wildcards, descendants). A path
+// that matches nothing is an error, which callers use to tell that it is absent.
+func (m *JSONMatcher) selectValue(pathExpr string) (any, error) {
+	path, err := jsonpath.Parse(withRootSelector(pathExpr))
+	if err != nil {
+		return nil, err
+	}
+
+	nodes := path.Select(m.payload)
+	if len(nodes) == 0 {
+		return nil, fmt.Errorf("no value found at JSONPath %q", pathExpr)
+	}
+
+	if path.Query().Singular() != nil {
+		return nodes[0], nil
+	}
+
+	return []any(nodes), nil
+}
+
+// Earlier versions of OliveTin accepted paths without the leading "$", such as
+// "repository.name", so keep accepting them.
+func withRootSelector(pathExpr string) string {
+	if strings.HasPrefix(pathExpr, "$") {
+		return pathExpr
+	}
+
+	return "$." + pathExpr
+}
+
 func (m *JSONMatcher) GetPayload() any {
 	return m.payload
 }

+ 80 - 0
service/internal/webhooks/jsonpath_test.go

@@ -0,0 +1,80 @@
+package webhooks
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+const jsonPathTestPayload = `{
+	"repository": {"name": "OliveTin", "private": false, "stars": 5, "topics": null},
+	"commits": [{"id": "a1"}, {"id": "b2"}],
+	"ref": "refs/heads/main",
+	"key with space": "spaced"
+}`
+
+func newTestJSONMatcher(t *testing.T) *JSONMatcher {
+	t.Helper()
+	matcher, err := NewJSONMatcher([]byte(jsonPathTestPayload))
+	require.NoError(t, err)
+	return matcher
+}
+
+func TestExtractValue(t *testing.T) {
+	tests := []struct {
+		path     string
+		expected string
+	}{
+		{"$.repository.name", "OliveTin"},
+		{"$.ref", "refs/heads/main"},
+		{"$.repository.private", "false"},
+		{"$.repository.stars", "5"},
+		{"$.repository.topics", "null"},
+		{"$.commits[0].id", "a1"},
+		{"$.commits[*].id", `["a1","b2"]`},
+		{"$..id", `["a1","b2"]`},
+		{"$['key with space']", "spaced"},
+		{"repository.name", "OliveTin"},
+	}
+
+	matcher := newTestJSONMatcher(t)
+
+	for _, tt := range tests {
+		t.Run(tt.path, func(t *testing.T) {
+			value, err := matcher.ExtractValue(tt.path)
+			require.NoError(t, err)
+			assert.Equal(t, tt.expected, value)
+		})
+	}
+}
+
+func TestExtractValueErrorsWhenPathIsAbsentOrInvalid(t *testing.T) {
+	matcher := newTestJSONMatcher(t)
+
+	for _, path := range []string{"$.missing", "$.repository.missing", "$.commits[5]", "$.ref.deeper", "", "$["} {
+		t.Run(path, func(t *testing.T) {
+			_, err := matcher.ExtractValue(path)
+			assert.Error(t, err)
+		})
+	}
+}
+
+func TestMatchPath(t *testing.T) {
+	matcher := newTestJSONMatcher(t)
+
+	matches, err := matcher.MatchPath("$.repository.name", "OliveTin")
+	require.NoError(t, err)
+	assert.True(t, matches)
+
+	matches, err = matcher.MatchPath("$.repository.private", "false")
+	require.NoError(t, err)
+	assert.True(t, matches)
+
+	matches, err = matcher.MatchPath("$.repository.name", "Other")
+	require.NoError(t, err)
+	assert.False(t, matches)
+
+	_, err = matcher.MatchPath("$.missing", "x")
+	assert.Error(t, err)
+}