From a975bb214536a34eefe9dc383136bf07b20b26ef Mon Sep 17 00:00:00 2001 From: Shawn Davis Date: Sun, 6 Sep 2020 15:30:51 -0400 Subject: [PATCH] Added support for Twist messaging. --- scripttease/library/overlays/common.py | 30 ++++++++++++++++++++++++++ tests/test_library_overlays_common.py | 13 +++++++++++ 2 files changed, 43 insertions(+) diff --git a/scripttease/library/overlays/common.py b/scripttease/library/overlays/common.py index 4483b40..02ac91e 100644 --- a/scripttease/library/overlays/common.py +++ b/scripttease/library/overlays/common.py @@ -10,6 +10,7 @@ __all__ = ( "python_virtualenv", "run", "slack", + "twist", ) # Functions @@ -70,6 +71,9 @@ def slack(message, url=None, **kwargs): - url (str): The webhook URL. This is required. See documentation. """ + if url is None: + raise ValueError("A url is required to use the slack command.") + kwargs.setdefault("comment", "send a message to slack") a = list() @@ -81,11 +85,37 @@ def slack(message, url=None, **kwargs): return Command(" ".join(a), **kwargs) +def twist(message, title="Notice", url=None, **kwargs): + """Send a message to Twist. + + - message (str): The message to be sent. + - title (str): The message title. + - url (str): The webhook URL. This is required. See documentation. + + """ + if url is None: + raise ValueError("A url is required to use the twist command.") + + kwargs.setdefault("comment", "send a message to twist") + + a = list() + + # curl -X POST -H 'Content-type: application/json' --data '{"content": "This is the message.", "title": "Message Title"}' "https://twist.com/api/v3/integration_incoming/post_data?install_id=116240&install_token=116240_bfb05bde51ecd0f728b4b161bee6fcee" + a.append("curl -X POST -H 'Content-type: application/json' --data") + + data = '{"content": "%s", "title": "%s"}' % (message, title) + a.append("'%s'" % data) + a.append('"%s"' % url) + + return Command(" ".join(a), **kwargs) + + # Mappings COMMON_MAPPINGS = { 'pip': python_pip, 'run': run, 'slack': slack, + 'twist': twist, 'virtualenv': python_virtualenv, } diff --git a/tests/test_library_overlays_common.py b/tests/test_library_overlays_common.py index a22ec85..7aca5f4 100644 --- a/tests/test_library_overlays_common.py +++ b/tests/test_library_overlays_common.py @@ -1,3 +1,4 @@ +import pytest from scripttease.library.overlays.common import * @@ -23,8 +24,20 @@ def test_run(): def test_slack(): + with pytest.raises(ValueError): + slack("This is a test.") + c = slack("This is a test.", url="https://example.slack.com/asdf/1234") s = c.get_statement(suppress_comment=True) assert "curl -X POST -H 'Content-type: application/json' --data" in s assert "This is a test." in s assert "https://example.slack.com/asdf/1234" in s + + +def test_twist(): + with pytest.raises(ValueError): + twist("This is a test.") + + c = twist("This is a test.", url="https://example.twist.com/asdf/1234") + s = c.get_statement(suppress_comment=True) + print(s)