Added support for Slack messaging.

development
Shawn Davis 4 years ago
parent 87dc71d926
commit 7df4edb7dc
  1. 20
      scripttease/library/overlays/common.py
  2. 8
      tests/test_library_overlays_common.py

@ -9,6 +9,7 @@ __all__ = (
"python_pip", "python_pip",
"python_virtualenv", "python_virtualenv",
"run", "run",
"slack",
) )
# Functions # Functions
@ -62,10 +63,29 @@ def run(statement, **kwargs):
return Command(statement, **kwargs) return Command(statement, **kwargs)
def slack(message, url=None, **kwargs):
"""Send a message to Slack.
- message (str): The message to be sent.
- url (str): The webhook URL. This is required. See documentation.
"""
kwargs.setdefault("comment", "send a message to slack")
a = list()
a.append("curl -X POST -H 'Content-type: application/json' --data")
a.append("'" + '{"text": "%s"}' % message + "'")
a.append(url)
return Command(" ".join(a), **kwargs)
# Mappings # Mappings
COMMON_MAPPINGS = { COMMON_MAPPINGS = {
'pip': python_pip, 'pip': python_pip,
'run': run, 'run': run,
'slack': slack,
'virtualenv': python_virtualenv, 'virtualenv': python_virtualenv,
} }

@ -20,3 +20,11 @@ def test_python_virtual_env():
def test_run(): def test_run():
c = run("ls -ls") c = run("ls -ls")
assert "ls -ls" in c.get_statement() assert "ls -ls" in c.get_statement()
def test_slack():
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

Loading…
Cancel
Save