Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
ef6d6e9
Add new black-check and black-format Make targets which verify code
Kami Feb 17, 2021
8496bb2
Reformat all the code using black tool.
stackstorm-neptr Feb 17, 2021
3277415
Update black config, update .flake8 config so we ignore rules which
Kami Feb 17, 2021
4c52638
Fix typo.
Kami Feb 17, 2021
705d585
Add pre-commit config which runs various lint tools on the modified /
Kami Feb 17, 2021
0720c0a
Update pre-commit config to also run trialing whitespace and check yaml
Kami Feb 17, 2021
e794527
Also run trailing whitespace + yaml checks as part of Make targets and
Kami Feb 17, 2021
969793f
Remove trailing whitespace from all the files.
stackstorm-neptr Feb 17, 2021
e4cdc05
Add .git-blame-ignore-rev file.
Kami Feb 17, 2021
6051539
Revert "Remove trailing whitespace from all the files."
Kami Feb 18, 2021
33d9efd
Exclude test fixture files from trailing whitespace hook since it breaks
Kami Feb 18, 2021
223a7ad
Remove trailing whitespace.
stackstorm-neptr Feb 18, 2021
eba4abe
Also don't re-format config files.
Kami Feb 18, 2021
514bd27
Revert "Remove trailing whitespace."
Kami Feb 18, 2021
56101b8
Make sure sample config doesn't contain trailing whitespace.
Kami Feb 18, 2021
100fbdb
Remove trailing whitespace.
stackstorm-neptr Feb 18, 2021
549bcd0
Update black config so we don't try to reformat submodule.
Kami Feb 18, 2021
0015767
Update Makefile.
Kami Feb 18, 2021
efa4611
Fix lint.
Kami Feb 18, 2021
9823328
Merge branch 'master' of github.com:StackStorm/st2 into reformat_code…
Kami Mar 6, 2021
083b103
Fix typo.
Kami Mar 6, 2021
370aa87
Add changelog entry.
Kami Mar 6, 2021
5d07a5c
Fix typo.
Kami Mar 6, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
58 changes: 57 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,62 @@ schemasgen: requirements .schemasgen
. $(VIRTUALENV_DIR)/bin/activate; pylint -j $(PYLINT_CONCURRENCY) -E --rcfile=./lint-configs/python/.pylintrc --load-plugins=pylint_plugins.api_models tools/*.py || exit 1;
. $(VIRTUALENV_DIR)/bin/activate; pylint -j $(PYLINT_CONCURRENCY) -E --rcfile=./lint-configs/python/.pylintrc pylint_plugins/*.py || exit 1;

# Black task which checks if the code comforts to black code style
.PHONY: black-check
black: requirements .black-check

.PHONY: .black-check
.black-check:
@echo
@echo "================== black-check ===================="
@echo
# st2 components
@for component in $(COMPONENTS); do\
echo "==========================================================="; \
echo "Running black on" $$component; \
echo "==========================================================="; \
. $(VIRTUALENV_DIR)/bin/activate ; black --check --config pyproject.toml $$component/ || exit 1; \
done
# runner modules and packages
@for component in $(COMPONENTS_RUNNERS); do\
echo "==========================================================="; \
echo "Running black on" $$component; \
echo "==========================================================="; \
. $(VIRTUALENV_DIR)/bin/activate ; black --check --config pyproject.toml $$component/ || exit 1; \
done
. $(VIRTUALENV_DIR)/bin/activate; black --check --config pyproject.toml contrib/ || exit 1;
. $(VIRTUALENV_DIR)/bin/activate; black --check --config pyproject.toml scripts/*.py || exit 1;
. $(VIRTUALENV_DIR)/bin/activate; black --check --config pyproject.toml tools/*.py || exit 1;
. $(VIRTUALENV_DIR)/bin/activate; black --check --config pyproject.toml pylint_plugins/*.py || exit 1;

# Black task which reformats the code using black
.PHONY: black-format
black: requirements .black-format

.PHONY: .black-format
.black-format:
@echo
@echo "================== black ===================="
@echo
# st2 components
@for component in $(COMPONENTS); do\
echo "==========================================================="; \
echo "Running black on" $$component; \
echo "==========================================================="; \
. $(VIRTUALENV_DIR)/bin/activate ; black --config pyproject.toml $$component/ || exit 1; \
done
# runner modules and packages
@for component in $(COMPONENTS_RUNNERS); do\
echo "==========================================================="; \
echo "Running black on" $$component; \
echo "==========================================================="; \
. $(VIRTUALENV_DIR)/bin/activate ; black --config pyproject.toml $$component/ || exit 1; \
done
. $(VIRTUALENV_DIR)/bin/activate; black --config pyproject.toml contrib/ || exit 1;
. $(VIRTUALENV_DIR)/bin/activate; black --config pyproject.toml scripts/*.py || exit 1;
. $(VIRTUALENV_DIR)/bin/activate; black --config pyproject.toml tools/*.py || exit 1;
. $(VIRTUALENV_DIR)/bin/activate; black --config pyproject.toml pylint_plugins/*.py || exit 1;

.PHONY: lint-api-spec
lint-api-spec: requirements .lint-api-spec

Expand Down Expand Up @@ -979,7 +1035,7 @@ debs:
ci: ci-checks ci-unit ci-integration ci-packs-tests

.PHONY: ci-checks
ci-checks: .generated-files-check .pylint .flake8 check-requirements check-sdist-requirements .st2client-dependencies-check .st2common-circular-dependencies-check circle-lint-api-spec .rst-check .st2client-install-check check-python-packages
ci-checks: .generated-files-check .black-check .pylint .flake8 check-requirements check-sdist-requirements .st2client-dependencies-check .st2common-circular-dependencies-check circle-lint-api-spec .rst-check .st2client-install-check check-python-packages

.PHONY: .rst-check
.rst-check:
Expand Down
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
Loading