25
25
from sentry_plugins .base import CorePluginMixin
26
26
from sentry .utils import json
27
27
from sentry .integrations import FeatureDescription , IntegrationFeatures
28
+ from string import Formatter
28
29
29
30
import sentry_mattermost
30
31
31
32
32
33
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 )
37
35
38
36
39
37
def get_tags (event ):
@@ -46,36 +44,38 @@ def get_tags(event):
46
44
47
45
48
46
class PayloadFactory :
49
- @classmethod
50
- def render_text (cls , params ):
51
- template = "__{project}__\n __[{title}]({link})__ \n {culprit}\n "
52
- return template .format (** params )
53
47
54
48
@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
71
68
72
69
if plugin .get_option ('include_rules' , project ):
73
- params ["rules" ] = get_rules (rules , group , project )
70
+ params ["rules" ] = get_rules (rules , event . group , project )
74
71
75
72
if plugin .get_option ('include_tags' , project ):
76
73
params ["tags" ] = get_tags (event )
77
74
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 )
79
79
80
80
payload = {
81
81
"username" : "Sentry" ,
@@ -122,6 +122,14 @@ def get_config(self, project, **kwargs):
122
122
"required" : True ,
123
123
"help" : "Your custom mattermost webhook URL." ,
124
124
},
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
+ },
125
133
{
126
134
"name" : "include_rules" ,
127
135
"label" : "Include Rules" ,
@@ -140,11 +148,13 @@ def get_config(self, project, **kwargs):
140
148
def is_configured (self , project ):
141
149
return bool (self .get_option ("webhook" , project ))
142
150
143
- def notify_users (self , group , event , triggering_rules , fail_silently = False , ** kwargs ):
151
+ def notify_users (self , group , event , triggering_rules , ** kwargs ):
144
152
project = event .group .project
145
153
if not self .is_configured (project ):
146
154
return
147
155
148
156
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 )
150
160
return request (webhook , payload )
0 commit comments