Feature Request - Standard way to handle Text formatting with Templates
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 6.5k
- Forks
- 787
- PR merge metrics
- No merged PRs in 30d
Description
Should we have a story in ST2 for handling formatting of text for use in workflows using Jinga2 templates over and above action aliases?
My thoughts on this come from us requiring a scalable manner (i.e. a single action instead of many actions) to handle formatting of troubleshooting information that can be sent to our ticket system while at the same time being able to keep data that's returned by actions as structured data so that we can reuse them (e.g. DNS troubleshooting via ChatOps action alias and also supporting a workflow triggered via an webhook into a ticket).
This something that I expect many StackStorm users would need to so carry out as they use the system more and would decrease the complexity of workflows plus most importantly supply a standardized template location. This would then allow additional sharing of templates and workflows that use them via st2contrib and thus decrease code duplication as currently each pack (be it on st2contrib or private) would need to have it's own formatting action.
The required amount of python action is very small (the following gets inspiration from format_execution_result.py), however it's just very much linked to the pack as it gets the template from <pack>/actions/templates.
import sys
import os
from st2actions.runners.pythonrunner import Action
from st2common.util import jinja as jinja_utils
class FormatText(Action):
def run(self, template_name, format_data):
results = {}
self.jinja = jinja_utils.get_jinja_environment()
path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(path, 'templates', template_name), 'r') as f:
template = f.read()
results['text'] = self.jinja.from_string(template).render(format_data)
return results
If this is a good idea for others, I'm happy to do (or help out) with a PR for this as out of the box support for this will help us going forward, the real question really where it should the action live.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading format_execution_result.py, the Python action entry point using st2actions.runners.pythonrunner.Action, and the Jinja environment from st2common.util.jinja. Compare the existing /actions/templates arrangement with the requested standardized location; done would require an agreed design for reusable text-formatting support and its placement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100