19
19
# THE SOFTWARE.
20
20
21
21
import urllib2
22
+ import logging
22
23
23
24
from sentry import tagstore
24
25
from sentry .plugins .bases import notify
27
28
from sentry .integrations import FeatureDescription , IntegrationFeatures
28
29
from string import Formatter
29
30
30
- import sentry_mattermost
31
+ logger = logging . getLogger ( "sentry.integrations. sentry_mattermost.plugin" )
31
32
32
33
33
34
def get_rules (rules , group , project ):
@@ -49,8 +50,14 @@ class PayloadFactory:
49
50
def create (cls , plugin , event , template , rules ):
50
51
project = event .group .project
51
52
53
+ if not template :
54
+ # In some cases like after updating the plugin, template config variable can be
55
+ # None, in that case we need a fallback.
56
+ template = "__[{project@get_full_name}]({project@get_absolute_url})__\n __[{group@title}]({group@get_absolute_url})__\n {group@culprit}\n {rules}\n {tags}"
57
+
52
58
names = [fn for _ , fn , _ , _ in Formatter ().parse (template )
53
59
if fn not in {None , "rules" , "tags" }]
60
+
54
61
params = {"rules" : "" , "tags" : "" }
55
62
for name in names :
56
63
getter = None
@@ -72,7 +79,6 @@ def create(cls, plugin, event, template, rules):
72
79
if plugin .get_option ('include_tags' , project ):
73
80
params ["tags" ] = get_tags (event )
74
81
75
-
76
82
# \n is not correctly interpreted from the text field of sentry
77
83
template = template .replace ("\\ n" , "\n " )
78
84
text = template .format (** params )
@@ -88,7 +94,8 @@ def create(cls, plugin, event, template, rules):
88
94
def request (url , payload ):
89
95
data = "payload=" + json .dumps (payload )
90
96
# Prevent servers from rejecting webhook calls by adding a existing user agent
91
- req = urllib2 .Request (url , data , headers = {'User-Agent' : "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" })
97
+ req = urllib2 .Request (url , data , headers = {
98
+ 'User-Agent' : "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0" })
92
99
response = urllib2 .urlopen (req )
93
100
return response .read ()
94
101
@@ -97,7 +104,7 @@ class Mattermost(CorePluginMixin, notify.NotificationPlugin):
97
104
title = 'Mattermost'
98
105
slug = 'mattermost'
99
106
description = 'Enables notifications for Mattermost Open Source Chat'
100
- version = sentry_mattermost . VERSION
107
+ version = '0.0.5'
101
108
author = 'Andre Freitas <[email protected] >, Guillaume Lastecoueres<[email protected] >'
102
109
author_url = 'https://github.com/Biekos/sentry-mattermost'
103
110
required_field = "webhook"
@@ -143,18 +150,38 @@ def get_config(self, project, **kwargs):
143
150
"type" : "bool" ,
144
151
"required" : False ,
145
152
"help" : "Include tags with notifications."
153
+ },
154
+ {
155
+ "name" : "debug" ,
156
+ "label" : "Debug mode" ,
157
+ "type" : "bool" ,
158
+ "required" : False ,
159
+ "help" : "Enable logging" ,
146
160
}]
147
161
148
162
def is_configured (self , project ):
149
163
return bool (self .get_option ("webhook" , project ))
150
164
151
165
def notify_users (self , group , event , triggering_rules , ** kwargs ):
166
+
152
167
project = event .group .project
168
+ debug_mode = self .get_option ('debug' , project )
153
169
if not self .is_configured (project ):
154
170
return
155
171
156
172
webhook = self .get_option ('webhook' , project )
173
+ if debug_mode :
174
+ logger .info ("DEBUG:webhook used: {}" .format (webhook ))
157
175
template = self .get_option ('template' , project )
176
+ if debug_mode :
177
+ logger .info ("DEBUG:template used: {}" .format (template ))
158
178
payload = PayloadFactory .create (
159
179
self , event , template , triggering_rules )
160
- return request (webhook , payload )
180
+ if debug_mode :
181
+ logger .info ("DEBUG:payload: {}" .format (payload ))
182
+
183
+ res = request (webhook , payload )
184
+
185
+ if debug_mode :
186
+ logger .info ("DEBUG:request executed: {}" .format (res ))
187
+ return res
0 commit comments