Skip to content

Commit cc01ce1

Browse files
committed
Global: Add template as a parameter
Summary: * Add a template parameter that allow to customise the message representation. * Fix an issue with tags. * Fix an issue with rules. * Complete the documentation.
1 parent a6b5220 commit cc01ce1

File tree

3 files changed

+57
-29
lines changed

3 files changed

+57
-29
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ Configure Mattermost:
2323
- Create an Incoming Webhook
2424
- Enable override usernames and profile picture icons in System Console Integrations
2525

26+
# Templating
27+
28+
You can change the template per project. It supports markdown.
29+
30+
To configure the variable you need to write `{myvariable}`
31+
32+
Variable can design any attribute/function of the `group` class in https://github.com/getsentry/sentry/blob/master/src/sentry/models/group.py#L247
33+
34+
By doing `{group@message}` for example.
35+
36+
Or any attribute/function of the `project` class in https://github.com/getsentry/sentry/blob/master/src/sentry/models/project.py#L78
37+
38+
By doing `{project@status}` for example.
39+
40+
There are two specific variable `{rules}` and `{tags}` which when you want to include rules or tags will be replace by their content.
41+
42+
The default value is:
43+
`"__[{project@get_full_name}]({project@get_absolute_url})__\n__[{group@title}]({group@get_absolute_url})__\n{group@culprit}\n{rules}\n{tags}"`
2644

2745
# Contributing
2846
We use Docker to setup a development stack. Make sure you have the latest

sentry_mattermost/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
VERSION = __import__('pkg_resources') \
33
.get_distribution(__name__).version
44
except Exception as e:
5-
VERSION = '0.0.2'
5+
VERSION = '0.0.3'

sentry_mattermost/plugin.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@
2525
from sentry_plugins.base import CorePluginMixin
2626
from sentry.utils import json
2727
from sentry.integrations import FeatureDescription, IntegrationFeatures
28+
from string import Formatter
2829

2930
import sentry_mattermost
3031

3132

3233
def get_rules(rules, group, project):
33-
rules_list = []
34-
for rule in rules:
35-
rules_list.append(rule.label.encode('utf-8'))
36-
return ', '.join('%s' % r for r in rules_list)
34+
return ', '.join(rules)
3735

3836

3937
def get_tags(event):
@@ -46,36 +44,38 @@ def get_tags(event):
4644

4745

4846
class PayloadFactory:
49-
@classmethod
50-
def render_text(cls, params):
51-
template = "__{project}__\n__[{title}]({link})__ \n{culprit}\n"
52-
return template.format(**params)
5347

5448
@classmethod
55-
def create(cls, plugin, event, rules):
56-
group = event.group
57-
project = group.project
58-
59-
if group.culprit:
60-
culprit = group.culprit.encode("utf-8")
61-
else:
62-
culprit = None
63-
project_name = project.get_full_name().encode("utf-8")
64-
65-
params = {
66-
"title": group.message_short.encode('utf-8'),
67-
"link": group.get_absolute_url(),
68-
"culprit": culprit,
69-
"project": project_name
70-
}
49+
def create(cls, plugin, event, template, rules):
50+
project = event.group.project
51+
52+
names = [fn for _, fn, _, _ in Formatter().parse(template)
53+
if fn not in {None, "rules", "tags"}]
54+
params = {"rules": "", "tags": ""}
55+
for name in names:
56+
getter = None
57+
particules = name.split("@")
58+
for particule in particules:
59+
if not getter:
60+
getter = event.__getattribute__(particule)
61+
else:
62+
getter = getter.__getattribute__(particule)
63+
64+
if callable(getter):
65+
params[name] = getter()
66+
else:
67+
params[name] = getter
7168

7269
if plugin.get_option('include_rules', project):
73-
params["rules"] = get_rules(rules, group, project)
70+
params["rules"] = get_rules(rules, event.group, project)
7471

7572
if plugin.get_option('include_tags', project):
7673
params["tags"] = get_tags(event)
7774

78-
text = cls.render_text(params)
75+
76+
# \n is not correctly interpreted from the text field of sentry
77+
template = template.replace("\\n", "\n")
78+
text = template.format(**params)
7979

8080
payload = {
8181
"username": "Sentry",
@@ -122,6 +122,14 @@ def get_config(self, project, **kwargs):
122122
"required": True,
123123
"help": "Your custom mattermost webhook URL.",
124124
},
125+
{
126+
"name": "template",
127+
"label": "Template",
128+
"type": "string",
129+
"required": True,
130+
"default": "__[{project@get_full_name}]({project@get_absolute_url})__\n__[{group@title}]({group@get_absolute_url})__\n{group@culprit}\n{rules}\n{tags}",
131+
"help": "You can define the template that the plugin will post to mattermost. More info: https://github.com/Biekos/sentry-mattermost",
132+
},
125133
{
126134
"name": "include_rules",
127135
"label": "Include Rules",
@@ -140,11 +148,13 @@ def get_config(self, project, **kwargs):
140148
def is_configured(self, project):
141149
return bool(self.get_option("webhook", project))
142150

143-
def notify_users(self, group, event, triggering_rules, fail_silently=False, **kwargs):
151+
def notify_users(self, group, event, triggering_rules, **kwargs):
144152
project = event.group.project
145153
if not self.is_configured(project):
146154
return
147155

148156
webhook = self.get_option('webhook', project)
149-
payload = PayloadFactory.create(self, event, triggering_rules)
157+
template = self.get_option('template', project)
158+
payload = PayloadFactory.create(
159+
self, event, template, triggering_rules)
150160
return request(webhook, payload)

0 commit comments

Comments
 (0)