Răsfoiți Sursa

fix(webhooks): extract an empty JSONPath collection as an empty list

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 1 zi în urmă
părinte
comite
d5ede804a4

+ 26 - 7
service/internal/webhooks/jsonpath.go

@@ -61,25 +61,44 @@ func (m *JSONMatcher) ExtractValue(pathExpr string) (string, error) {
 	return string(jsonBytes), nil
 }
 
+// HasValue reports whether the path matches at least one value.
+func (m *JSONMatcher) HasValue(pathExpr string) (bool, error) {
+	_, nodes, err := m.selectNodes(pathExpr)
+	if err != nil {
+		return false, err
+	}
+	return len(nodes) > 0, 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.
+// and a list for a path that can match several (wildcards, descendants). A
+// singular path that matches nothing is an error, which callers use to tell
+// that it is absent. A path that can match several nodes returns a list even
+// when it is empty, so that "$.commits[*].id" on a push with no commits is "[]".
 func (m *JSONMatcher) selectValue(pathExpr string) (any, error) {
-	path, err := jsonpath.Parse(withRootSelector(pathExpr))
+	path, nodes, err := m.selectNodes(pathExpr)
 	if err != nil {
 		return nil, err
 	}
 
-	nodes := path.Select(m.payload)
+	if path.Query().Singular() == nil {
+		return append([]any{}, nodes...), nil
+	}
+
 	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 nodes[0], nil
+}
+
+func (m *JSONMatcher) selectNodes(pathExpr string) (*jsonpath.Path, []any, error) {
+	path, err := jsonpath.Parse(withRootSelector(pathExpr))
+	if err != nil {
+		return nil, nil, err
 	}
 
-	return []any(nodes), nil
+	return path, path.Select(m.payload), nil
 }
 
 // Earlier versions of OliveTin accepted paths without the leading "$", such as

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

@@ -78,3 +78,39 @@ func TestMatchPath(t *testing.T) {
 	_, err = matcher.MatchPath("$.missing", "x")
 	assert.Error(t, err)
 }
+
+func TestEmptyCollectionIsExtractedAndMatchedAsAnEmptyList(t *testing.T) {
+	matcher, err := NewJSONMatcher([]byte(`{"commits": []}`))
+	require.NoError(t, err)
+
+	value, err := matcher.ExtractValue("$.commits[*].id")
+	require.NoError(t, err)
+	assert.Equal(t, "[]", value)
+
+	matched, err := matcher.MatchPath("$.commits[*].id", "[]")
+	require.NoError(t, err)
+	assert.True(t, matched)
+
+	found, err := matcher.HasValue("$.commits[*].id")
+	require.NoError(t, err)
+	assert.False(t, found, "an empty collection has no value for an existence check")
+}
+
+func TestHasValue(t *testing.T) {
+	matcher := newTestJSONMatcher(t)
+
+	cases := map[string]bool{
+		"$.repository.name":   true,
+		"$.repository.topics": true,
+		"$.commits[*].id":     true,
+		"$.missing":           false,
+		"$.commits[5]":        false,
+	}
+	for path, expected := range cases {
+		t.Run(path, func(t *testing.T) {
+			found, err := matcher.HasValue(path)
+			require.NoError(t, err)
+			assert.Equal(t, expected, found)
+		})
+	}
+}