Skip to content

Commit

Permalink
Reformat all the code using black tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
stackstorm-neptr authored and Kami committed Feb 17, 2021
1 parent ef6d6e9 commit 8496bb2
Show file tree
Hide file tree
Showing 937 changed files with 54,139 additions and 42,097 deletions.
43 changes: 21 additions & 22 deletions contrib/chatops/actions/format_execution_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,50 @@

class FormatResultAction(Action):
def __init__(self, config=None, action_service=None):
super(FormatResultAction, self).__init__(config=config, action_service=action_service)
api_url = os.environ.get('ST2_ACTION_API_URL', None)
token = os.environ.get('ST2_ACTION_AUTH_TOKEN', None)
super(FormatResultAction, self).__init__(
config=config, action_service=action_service
)
api_url = os.environ.get("ST2_ACTION_API_URL", None)
token = os.environ.get("ST2_ACTION_AUTH_TOKEN", None)
self.client = Client(api_url=api_url, token=token)
self.jinja = jinja_utils.get_jinja_environment(allow_undefined=True)
self.jinja.tests['in'] = lambda item, list: item in list
self.jinja.tests["in"] = lambda item, list: item in list

path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(path, 'templates/default.j2'), 'r') as f:
with open(os.path.join(path, "templates/default.j2"), "r") as f:
self.default_template = f.read()

def run(self, execution_id):
execution = self._get_execution(execution_id)
context = {
'six': six,
'execution': execution
}
context = {"six": six, "execution": execution}
template = self.default_template
result = {"enabled": True}

alias_id = execution['context'].get('action_alias_ref', {}).get('id', None)
alias_id = execution["context"].get("action_alias_ref", {}).get("id", None)
if alias_id:
alias = self.client.managers['ActionAlias'].get_by_id(alias_id)
alias = self.client.managers["ActionAlias"].get_by_id(alias_id)

context.update({
'alias': alias
})
context.update({"alias": alias})

result_params = getattr(alias, 'result', None)
result_params = getattr(alias, "result", None)
if result_params:
if not result_params.get('enabled', True):
if not result_params.get("enabled", True):
result["enabled"] = False
else:
if 'format' in alias.result:
template = alias.result['format']
if 'extra' in alias.result:
result['extra'] = jinja_utils.render_values(alias.result['extra'], context)
if "format" in alias.result:
template = alias.result["format"]
if "extra" in alias.result:
result["extra"] = jinja_utils.render_values(
alias.result["extra"], context
)

result['message'] = self.jinja.from_string(template).render(context)
result["message"] = self.jinja.from_string(template).render(context)

return result

def _get_execution(self, execution_id):
if not execution_id:
raise ValueError('Invalid execution_id provided.')
raise ValueError("Invalid execution_id provided.")
execution = self.client.liveactions.get_by_id(id=execution_id)
if not execution:
return None
Expand Down
17 changes: 5 additions & 12 deletions contrib/chatops/actions/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,16 @@
class MatchAction(Action):
def __init__(self, config=None):
super(MatchAction, self).__init__(config=config)
api_url = os.environ.get('ST2_ACTION_API_URL', None)
token = os.environ.get('ST2_ACTION_AUTH_TOKEN', None)
api_url = os.environ.get("ST2_ACTION_API_URL", None)
token = os.environ.get("ST2_ACTION_AUTH_TOKEN", None)
self.client = Client(api_url=api_url, token=token)

def run(self, text):
alias_match = ActionAliasMatch()
alias_match.command = text
matches = self.client.managers['ActionAlias'].match(alias_match)
return {
'alias': _format_match(matches[0]),
'representation': matches[1]
}
matches = self.client.managers["ActionAlias"].match(alias_match)
return {"alias": _format_match(matches[0]), "representation": matches[1]}


def _format_match(match):
return {
'name': match.name,
'pack': match.pack,
'action_ref': match.action_ref
}
return {"name": match.name, "pack": match.pack, "action_ref": match.action_ref}
27 changes: 14 additions & 13 deletions contrib/chatops/actions/match_and_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@
from st2common.runners.base_action import Action
from st2client.models.action_alias import ActionAliasMatch
from st2client.models.aliasexecution import ActionAliasExecution
from st2client.commands.action import (LIVEACTION_STATUS_REQUESTED,
LIVEACTION_STATUS_SCHEDULED,
LIVEACTION_STATUS_RUNNING,
LIVEACTION_STATUS_CANCELING)
from st2client.commands.action import (
LIVEACTION_STATUS_REQUESTED,
LIVEACTION_STATUS_SCHEDULED,
LIVEACTION_STATUS_RUNNING,
LIVEACTION_STATUS_CANCELING,
)
from st2client.client import Client


class ExecuteActionAliasAction(Action):
def __init__(self, config=None):
super(ExecuteActionAliasAction, self).__init__(config=config)
api_url = os.environ.get('ST2_ACTION_API_URL', None)
token = os.environ.get('ST2_ACTION_AUTH_TOKEN', None)
api_url = os.environ.get("ST2_ACTION_API_URL", None)
token = os.environ.get("ST2_ACTION_AUTH_TOKEN", None)
self.client = Client(api_url=api_url, token=token)

def run(self, text, source_channel=None, user=None):
alias_match = ActionAliasMatch()
alias_match.command = text
alias, representation = self.client.managers['ActionAlias'].match(
alias_match)
alias, representation = self.client.managers["ActionAlias"].match(alias_match)

execution = ActionAliasExecution()
execution.name = alias.name
Expand All @@ -48,20 +49,20 @@ def run(self, text, source_channel=None, user=None):
execution.notification_route = None
execution.user = user

action_exec_mgr = self.client.managers['ActionAliasExecution']
action_exec_mgr = self.client.managers["ActionAliasExecution"]

execution = action_exec_mgr.create(execution)
self._wait_execution_to_finish(execution.execution['id'])
return execution.execution['id']
self._wait_execution_to_finish(execution.execution["id"])
return execution.execution["id"]

def _wait_execution_to_finish(self, execution_id):
pending_statuses = [
LIVEACTION_STATUS_REQUESTED,
LIVEACTION_STATUS_SCHEDULED,
LIVEACTION_STATUS_RUNNING,
LIVEACTION_STATUS_CANCELING
LIVEACTION_STATUS_CANCELING,
]
action_exec_mgr = self.client.managers['LiveAction']
action_exec_mgr = self.client.managers["LiveAction"]
execution = action_exec_mgr.get_by_id(execution_id)
while execution.status in pending_statuses:
time.sleep(1)
Expand Down
28 changes: 12 additions & 16 deletions contrib/chatops/tests/test_format_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,57 +20,53 @@

from format_execution_result import FormatResultAction

__all__ = [
'FormatResultActionTestCase'
]
__all__ = ["FormatResultActionTestCase"]


class FormatResultActionTestCase(BaseActionTestCase):
action_cls = FormatResultAction

def test_rendering_works_remote_shell_cmd(self):
remote_shell_cmd_execution_model = json.loads(
self.get_fixture_content('remote_cmd_execution.json')
self.get_fixture_content("remote_cmd_execution.json")
)

action = self.get_action_instance()
action._get_execution = mock.MagicMock(
return_value=remote_shell_cmd_execution_model
)
result = action.run(execution_id='57967f9355fc8c19a96d9e4f')
result = action.run(execution_id="57967f9355fc8c19a96d9e4f")
self.assertTrue(result)
self.assertIn('web_url', result['message'])
self.assertIn('Took 2s to complete', result['message'])
self.assertIn("web_url", result["message"])
self.assertIn("Took 2s to complete", result["message"])

def test_rendering_local_shell_cmd(self):
local_shell_cmd_execution_model = json.loads(
self.get_fixture_content('local_cmd_execution.json')
self.get_fixture_content("local_cmd_execution.json")
)

action = self.get_action_instance()
action._get_execution = mock.MagicMock(
return_value=local_shell_cmd_execution_model
)
self.assertTrue(action.run(execution_id='5799522f55fc8c2d33ac03e0'))
self.assertTrue(action.run(execution_id="5799522f55fc8c2d33ac03e0"))

def test_rendering_http_request(self):
http_execution_model = json.loads(
self.get_fixture_content('http_execution.json')
self.get_fixture_content("http_execution.json")
)

action = self.get_action_instance()
action._get_execution = mock.MagicMock(
return_value=http_execution_model
)
self.assertTrue(action.run(execution_id='579955f055fc8c2d33ac03e3'))
action._get_execution = mock.MagicMock(return_value=http_execution_model)
self.assertTrue(action.run(execution_id="579955f055fc8c2d33ac03e3"))

def test_rendering_python_action(self):
python_action_execution_model = json.loads(
self.get_fixture_content('python_action_execution.json')
self.get_fixture_content("python_action_execution.json")
)

action = self.get_action_instance()
action._get_execution = mock.MagicMock(
return_value=python_action_execution_model
)
self.assertTrue(action.run(execution_id='5799572a55fc8c2d33ac03ec'))
self.assertTrue(action.run(execution_id="5799572a55fc8c2d33ac03ec"))
8 changes: 3 additions & 5 deletions contrib/core/actions/generate_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@

from st2common.runners.base_action import Action

__all__ = [
'GenerateUUID'
]
__all__ = ["GenerateUUID"]


class GenerateUUID(Action):
def run(self, uuid_type):
if uuid_type == 'uuid1':
if uuid_type == "uuid1":
return str(uuid.uuid1())
elif uuid_type == 'uuid4':
elif uuid_type == "uuid4":
return str(uuid.uuid4())
else:
raise ValueError("Unknown uuid_type. Only uuid1 and uuid4 are supported")
13 changes: 7 additions & 6 deletions contrib/core/actions/inject_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

from st2common.runners.base_action import Action

__all__ = [
'InjectTriggerAction'
]
__all__ = ["InjectTriggerAction"]


class InjectTriggerAction(Action):
Expand All @@ -34,8 +32,11 @@ def run(self, trigger, payload=None, trace_tag=None):
# results in a TriggerInstanceDB database object creation or not. The object is created
# inside rulesengine service and could fail due to the user providing an invalid trigger
# reference or similar.
self.logger.debug('Injecting trigger "%s" with payload="%s"' % (trigger, str(payload)))
result = client.webhooks.post_generic_webhook(trigger=trigger, payload=payload,
trace_tag=trace_tag)
self.logger.debug(
'Injecting trigger "%s" with payload="%s"' % (trigger, str(payload))
)
result = client.webhooks.post_generic_webhook(
trigger=trigger, payload=payload, trace_tag=trace_tag
)

return result
4 changes: 1 addition & 3 deletions contrib/core/actions/pause.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

from st2common.runners.base_action import Action

__all__ = [
'PauseAction'
]
__all__ = ["PauseAction"]


class PauseAction(Action):
Expand Down
26 changes: 11 additions & 15 deletions contrib/core/tests/test_action_inject_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,46 @@
class InjectTriggerActionTestCase(BaseActionTestCase):
action_cls = InjectTriggerAction

@mock.patch('st2common.services.datastore.BaseDatastoreService.get_api_client')
@mock.patch("st2common.services.datastore.BaseDatastoreService.get_api_client")
def test_inject_trigger_only_trigger_no_payload(self, mock_get_api_client):
mock_api_client = mock.Mock()
mock_get_api_client.return_value = mock_api_client

action = self.get_action_instance()

action.run(trigger='dummy_pack.trigger1')
action.run(trigger="dummy_pack.trigger1")
mock_api_client.webhooks.post_generic_webhook.assert_called_with(
trigger='dummy_pack.trigger1',
payload={},
trace_tag=None
trigger="dummy_pack.trigger1", payload={}, trace_tag=None
)

mock_api_client.webhooks.post_generic_webhook.reset()

@mock.patch('st2common.services.datastore.BaseDatastoreService.get_api_client')
@mock.patch("st2common.services.datastore.BaseDatastoreService.get_api_client")
def test_inject_trigger_trigger_and_payload(self, mock_get_api_client):
mock_api_client = mock.Mock()
mock_get_api_client.return_value = mock_api_client

action = self.get_action_instance()

action.run(trigger='dummy_pack.trigger2', payload={'foo': 'bar'})
action.run(trigger="dummy_pack.trigger2", payload={"foo": "bar"})

mock_api_client.webhooks.post_generic_webhook.assert_called_with(
trigger='dummy_pack.trigger2',
payload={'foo': 'bar'},
trace_tag=None
trigger="dummy_pack.trigger2", payload={"foo": "bar"}, trace_tag=None
)

mock_api_client.webhooks.post_generic_webhook.reset()

@mock.patch('st2common.services.datastore.BaseDatastoreService.get_api_client')
@mock.patch("st2common.services.datastore.BaseDatastoreService.get_api_client")
def test_inject_trigger_trigger_payload_trace_tag(self, mock_get_api_client):
mock_api_client = mock.Mock()
mock_get_api_client.return_value = mock_api_client

action = self.get_action_instance()

action.run(trigger='dummy_pack.trigger3', payload={'foo': 'bar'}, trace_tag='Tag1')
action.run(
trigger="dummy_pack.trigger3", payload={"foo": "bar"}, trace_tag="Tag1"
)

mock_api_client.webhooks.post_generic_webhook.assert_called_with(
trigger='dummy_pack.trigger3',
payload={'foo': 'bar'},
trace_tag='Tag1'
trigger="dummy_pack.trigger3", payload={"foo": "bar"}, trace_tag="Tag1"
)
Loading

0 comments on commit 8496bb2

Please sign in to comment.