jsonpath.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package webhooks
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/theory/jsonpath"
  7. )
  8. type JSONMatcher struct {
  9. payload any
  10. }
  11. func NewJSONMatcher(payload []byte) (*JSONMatcher, error) {
  12. var data any
  13. if err := json.Unmarshal(payload, &data); err != nil {
  14. return nil, err
  15. }
  16. return &JSONMatcher{payload: data}, nil
  17. }
  18. func (m *JSONMatcher) MatchPath(pathExpr string, expectedValue string) (bool, error) {
  19. value, err := m.selectValue(pathExpr)
  20. if err != nil {
  21. return false, err
  22. }
  23. // For string values, compare directly without marshaling
  24. if strValue, ok := value.(string); ok {
  25. return strValue == expectedValue, nil
  26. }
  27. // For non-string values, marshal to JSON for consistent string representation
  28. jsonBytes, err := json.Marshal(value)
  29. if err != nil {
  30. return false, fmt.Errorf("failed to marshal extracted value: %w", err)
  31. }
  32. valueStr := string(jsonBytes)
  33. return valueStr == expectedValue, nil
  34. }
  35. func (m *JSONMatcher) ExtractValue(pathExpr string) (string, error) {
  36. value, err := m.selectValue(pathExpr)
  37. if err != nil {
  38. return "", err
  39. }
  40. // For string values, return directly without marshaling to avoid adding JSON quotes
  41. if strValue, ok := value.(string); ok {
  42. return strValue, nil
  43. }
  44. // For non-string values, marshal to JSON for consistent string representation
  45. jsonBytes, err := json.Marshal(value)
  46. if err != nil {
  47. return "", fmt.Errorf("failed to marshal extracted value: %w", err)
  48. }
  49. return string(jsonBytes), nil
  50. }
  51. // HasValue reports whether the path matches at least one value.
  52. func (m *JSONMatcher) HasValue(pathExpr string) (bool, error) {
  53. _, nodes, err := m.selectNodes(pathExpr)
  54. if err != nil {
  55. return false, err
  56. }
  57. return len(nodes) > 0, nil
  58. }
  59. // selectValue returns the single value for a path that can only match one node,
  60. // and a list for a path that can match several (wildcards, descendants). A
  61. // singular path that matches nothing is an error, which callers use to tell
  62. // that it is absent. A path that can match several nodes returns a list even
  63. // when it is empty, so that "$.commits[*].id" on a push with no commits is "[]".
  64. func (m *JSONMatcher) selectValue(pathExpr string) (any, error) {
  65. path, nodes, err := m.selectNodes(pathExpr)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if path.Query().Singular() == nil {
  70. return append([]any{}, nodes...), nil
  71. }
  72. if len(nodes) == 0 {
  73. return nil, fmt.Errorf("no value found at JSONPath %q", pathExpr)
  74. }
  75. return nodes[0], nil
  76. }
  77. func (m *JSONMatcher) selectNodes(pathExpr string) (*jsonpath.Path, []any, error) {
  78. path, err := jsonpath.Parse(withRootSelector(pathExpr))
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. return path, path.Select(m.payload), nil
  83. }
  84. // Earlier versions of OliveTin accepted paths without the leading "$", such as
  85. // "repository.name", so keep accepting them.
  86. func withRootSelector(pathExpr string) string {
  87. if strings.HasPrefix(pathExpr, "$") {
  88. return pathExpr
  89. }
  90. return "$." + pathExpr
  91. }
  92. func (m *JSONMatcher) GetPayload() any {
  93. return m.payload
  94. }