Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added abort support to `Cog.Command.GenCommand` * Fixed `Cog.Pipeline.ExecutionStage`'s handling of command aborts * Added `operable:abort-when` w/tests * Catch error when updating pipeline state to `finished`
- Loading branch information
Kevin Smith
committed
Feb 2, 2017
1 parent
1177bb8
commit 4e48734
Showing
10 changed files
with
180 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule Cog.Commands.AbortWhen do | ||
use Cog.Command.GenCommand.Base, | ||
bundle: Cog.Util.Misc.embedded_bundle, | ||
name: "abort-when" | ||
|
||
@description "Aborts pipeline when argument evaluates to truthy" | ||
|
||
@arguments "value" | ||
|
||
@default_message "Pipeline aborted" | ||
|
||
option "message", short: "m", type: "string", required: false, | ||
description: "Message sent when pipeline is aborted" | ||
|
||
# Allow any user to run | ||
rule "when command is #{Cog.Util.Misc.embedded_bundle}:abort-when allow" | ||
|
||
def handle_message(req, state) do | ||
case eval_args(req.args) do | ||
true -> | ||
{:abort, req.reply_to, Map.get(req.options, "message", @default_message), state} | ||
false -> | ||
{:reply, req.reply_to, req.cog_env, state} | ||
end | ||
end | ||
|
||
defp eval_args([n|_]) when n > 0, do: true | ||
defp eval_args([true|_]), do: true | ||
defp eval_args(_), do: false | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
~attachment title=$message color="yellow"~ | ||
|
||
**Command:** `~$invocation~` | ||
**Calling Environment:** `~$cog_env_text~` | ||
**Pipeline Id:** ~$pipeline_id~ | ||
|
||
~end~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Integration.AbortTest do | ||
|
||
use Cog.AdapterCase, provider: "test" | ||
|
||
@moduletag integration: :general | ||
@moduletag :command | ||
@moduletag :abort | ||
|
||
setup do | ||
user = user("vanstee", first_name: "Patrick", last_name: "Van Stee") | ||
|> with_chat_handle_for("test") | ||
|
||
{:ok, %{user: user}} | ||
end | ||
|
||
test "abort-when aborts a pipeline", %{user: user} do | ||
response = send_message(user, "@bot: operable:seed '[{\"foo\": 1}]' | operable:abort-when $foo") | ||
assert String.starts_with?(response, "Command: operable:abort-when $foo\nCalling Environment: {\n \"foo\": 1\n}\n") | ||
[response] = send_message(user, "@bot: operable:seed '[{\"foo\": 0}]' | operable:abort-when $foo") | ||
assert response == %{foo: 0} | ||
end | ||
|
||
test "abort-when uses custom abort message", %{user: user} do | ||
response = send_message(user, "@bot: operable:seed '[{\"foo\": 1}]' | operable:abort-when -m \"PIPELINE ABORT\" $foo") | ||
assert String.starts_with?(response, "Command: operable:abort-when -m \"PIPELINE ABORT\" $foo\nCalling Environment: {\n \"foo\": 1\n}\n") | ||
end | ||
|
||
test "abort-when works in middle of pipeline", %{user: user} do | ||
json = Poison.encode!([%{foo: 0}, %{foo: 0}, %{foo: 1}, %{foo: 0}]) | ||
response = send_message(user, "@bot: operable:seed '#{json}' | operable:abort-when $foo | operable:echo foo is $foo") | ||
assert String.starts_with?(response, "Command: operable:abort-when $foo\nCalling Environment: {\n \"foo\": 1\n}\n") | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters