Skip to content
This repository has been archived by the owner on Mar 19, 2019. It is now read-only.

Commit

Permalink
Ensure Event Handlers are not invoked for empty matches #454 (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
kipz authored and cdupuis committed Mar 21, 2017
1 parent 060c575 commit 682b73d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed

- Allow construction of Message without body
- Ensure Event Handlers are not invoked if there are no PE matches
https://github.com/atomist/rug/issues/454

## [0.17.1] - 2017-03-20

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class JavaScriptEventHandler(jsc: JavaScriptContext,
val root = SimpleContainerGraphNode("root", targetNode)
ctx.pathExpressionEngine.ee.evaluate(root, pathExpression, ctx.typeRegistry, None) match {
case Right(Nil) => None
case Right(matches) =>
case Right(matches) if matches.nonEmpty =>
val cm = jsContextMatch(
jsSafeCommittingProxy.wrapOne(targetNode, ctx.typeRegistry),
jsSafeCommittingProxy.wrap(matches, ctx.typeRegistry),
Expand All @@ -66,6 +66,7 @@ class JavaScriptEventHandler(jsc: JavaScriptContext,
case plan: ScriptObjectMirror => ConstructPlan(plan)
case other => throw new InvalidHandlerResultException(s"$name EventHandler returned an invalid response ($other) when handling $pathExpressionStr")
}
case Right(matches) if matches.isEmpty => None
case Left(failure) =>
throw new RugRuntimeException(pathExpressionStr,
s"Error evaluating path expression $pathExpression: [$failure]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ object JavaScriptEventHandlerTest {
|}
|export let handler = new SimpleHandler();
""".stripMargin)

val eventHandlerWithEmptyMatches = StringFileArtifact(atomistConfig.handlersRoot + "/Handler.ts",
s"""
|import {HandleEvent, Plan, Message} from '@atomist/rug/operations/Handlers'
|import {TreeNode, Match, PathExpression} from '@atomist/rug/tree/PathExpression'
|import {EventHandler, Tags} from '@atomist/rug/operations/Decorators'
|
|@EventHandler("BuildHandler", "Handles a Build event", new PathExpression<TreeNode,TreeNode>("/nomatch"))
|@Tags("github", "build")
|class SimpleHandler implements HandleEvent<TreeNode,TreeNode> {
| handle(event: Match<TreeNode, TreeNode>): Message{
| return new Message("woot").withCorrelationId("dude").withNode(event.root());
| }
|}
|export let handler = new SimpleHandler();
""".stripMargin)

}


Expand Down Expand Up @@ -221,6 +238,18 @@ class JavaScriptEventHandlerTest extends FlatSpec with Matchers with DiagrammedA
assert(msg.treeNode.nonEmpty)
assert(msg.correlationId.nonEmpty)
}

it should "it should not invoke the actual handler if there matches are empty" in {
val rugArchive = TypeScriptBuilder.compileWithModel(SimpleFileBasedArtifactSource(JavaScriptEventHandlerTest.eventHandlerWithEmptyMatches))
val finder = new JavaScriptEventHandlerFinder()
val handlers = finder.find(new JavaScriptContext(rugArchive))
handlers.size should be(1)
val handler = handlers.head
handler.rootNodeName should be("nomatch")
handler.pathExpression should not be null
val actualPlan = handler.handle(LocalRugContext(TestTreeMaterializer), SysEvent)
assert(actualPlan.isEmpty)
}
}

object SysEvent extends SystemEvent ("blah", "issue", 0l)
Expand Down

0 comments on commit 682b73d

Please sign in to comment.