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

Ensure Event Handlers are not invoked for empty matches #454 #456

Merged
merged 1 commit into from
Mar 21, 2017
Merged
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
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