Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(pipelineTriggers): handle invalid constraint regexes (#1209) (#9) #10

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -129,7 +130,19 @@ public static boolean isJsonPathConstraintInPayload(final Map constraints, final
}

private static boolean matches(String us, String other) {
return Pattern.compile(us).asPredicate().test(other);
Pattern p;
try {
p = Pattern.compile(us);
} catch (PatternSyntaxException ex) {
log.error(
"Invalid regex pattern for constraint, will never match any payload: \""
+ us
+ "\": "
+ ex.getMessage(),
ex);
return false;
}
return p.asPredicate().test(other);
}

private static boolean anyMatch(String us, List<String> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class ArtifactMatcherSpec extends Specification {
"one": "o"
]

def invalidConstraint = [
"one": "[one,two"
]

def constraintsOR = [
"one": ["uno", "one"]
]
Expand Down Expand Up @@ -160,6 +164,14 @@ class ArtifactMatcherSpec extends Specification {
!result
}

def "no match when constraint invalid"() {
when:
boolean result = ArtifactMatcher.isConstraintInPayload(invalidConstraint, matchPayload)

then:
!result
}

def "matches when payload value is in a list of constraint strings"() {
when:
boolean result = ArtifactMatcher.isConstraintInPayload(constraintsOR, matchPayload)
Expand Down Expand Up @@ -224,6 +236,14 @@ class ArtifactMatcherSpec extends Specification {
!result
}

def "no match when constraint not valid using Jsonpath"() {
when:
boolean result = ArtifactMatcher.isJsonPathConstraintInPayload(invalidConstraint, matchPayload)

then:
!result
}

def "matches when payload value is in a list of constraint strings using Jsonpath"() {
when:
boolean result = ArtifactMatcher.isJsonPathConstraintInPayload(constraintsOR, matchPayload)
Expand Down
Loading