2014-07-18 02:51:58 +00:00
|
|
|
import json
|
2017-05-19 15:36:55 +00:00
|
|
|
import logging
|
2014-08-27 02:09:56 +00:00
|
|
|
import re
|
2014-07-18 02:51:58 +00:00
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
import requests
|
|
|
|
from flask_mail import Message
|
|
|
|
|
2015-08-19 17:53:34 +00:00
|
|
|
from app import mail, app, OVERRIDE_CONFIG_DIRECTORY
|
2014-07-18 02:51:58 +00:00
|
|
|
from data import model
|
2015-08-19 17:53:34 +00:00
|
|
|
from util.config.validator import SSL_FILENAMES
|
2015-07-29 18:21:29 +00:00
|
|
|
from workers.queueworker import JobException
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-05-19 15:36:55 +00:00
|
|
|
METHOD_TIMEOUT = app.config.get('NOTIFICATION_SEND_TIMEOUT', 10) # Seconds
|
2016-11-08 23:20:38 +00:00
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
class InvalidNotificationMethodException(Exception):
|
|
|
|
pass
|
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
class CannotValidateNotificationMethodException(Exception):
|
|
|
|
pass
|
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
|
2014-09-22 16:52:57 +00:00
|
|
|
class NotificationMethodPerformException(JobException):
|
|
|
|
pass
|
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
|
2017-07-14 14:32:38 +00:00
|
|
|
def _get_namespace_name_from(repository):
|
|
|
|
# TODO Charlie 2017-07-14: This is hack for a bug in production
|
|
|
|
# because in some places have started calling this method with
|
|
|
|
# pre oci models and in some we have started calling with non pre oci models. We should
|
|
|
|
# remove this when we have switched over to database interfaces.
|
|
|
|
if hasattr(repository, 'namespace_name'):
|
|
|
|
namespace_name = repository.namespace_name
|
|
|
|
else:
|
|
|
|
namespace_name = repository.namespace_user.username
|
|
|
|
return namespace_name
|
|
|
|
|
|
|
|
|
2015-08-19 17:53:34 +00:00
|
|
|
SSLClientCert = None
|
|
|
|
if app.config['PREFERRED_URL_SCHEME'] == 'https':
|
|
|
|
# TODO(jschorr): move this into the config provider library
|
|
|
|
SSLClientCert = [OVERRIDE_CONFIG_DIRECTORY + f for f in SSL_FILENAMES]
|
|
|
|
|
2016-09-29 00:17:14 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
class NotificationMethod(object):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def method_name(cls):
|
|
|
|
"""
|
|
|
|
Particular method implemented by subclasses.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def validate(self, namespace_name, repository_name, config_data):
|
2014-07-28 18:58:12 +00:00
|
|
|
"""
|
|
|
|
Validates that the notification can be created with the given data. Throws
|
|
|
|
a CannotValidateNotificationMethodException on failure.
|
|
|
|
"""
|
2014-11-24 21:07:38 +00:00
|
|
|
raise NotImplementedError
|
2014-07-28 18:58:12 +00:00
|
|
|
|
2017-05-18 21:52:50 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def perform(self, notification_obj, event_handler, notification_data):
|
2014-07-18 02:51:58 +00:00
|
|
|
"""
|
|
|
|
Performs the notification method.
|
|
|
|
|
2017-07-12 12:50:47 +00:00
|
|
|
notification_obj: The noticication namedtuple.
|
2014-07-18 02:51:58 +00:00
|
|
|
event_handler: The NotificationEvent handler.
|
|
|
|
notification_data: The dict of notification data placed in the queue.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_method(cls, methodname):
|
|
|
|
for subc in cls.__subclasses__():
|
|
|
|
if subc.method_name() == methodname:
|
|
|
|
return subc()
|
|
|
|
|
|
|
|
raise InvalidNotificationMethodException('Unable to find method: %s' % methodname)
|
|
|
|
|
|
|
|
|
|
|
|
class QuayNotificationMethod(NotificationMethod):
|
|
|
|
@classmethod
|
|
|
|
def method_name(cls):
|
|
|
|
return 'quay_notification'
|
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def validate(self, namespace_name, repository_name, config_data):
|
|
|
|
status, err_message, target_users = self.find_targets(namespace_name, repository_name, config_data)
|
2014-07-28 18:58:12 +00:00
|
|
|
if err_message:
|
2017-05-19 15:36:55 +00:00
|
|
|
raise CannotValidateNotificationMethodException(err_message)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def find_targets(self, namespace_name, repository_name, config_data):
|
2014-07-18 18:12:20 +00:00
|
|
|
target_info = config_data['target']
|
|
|
|
|
|
|
|
if target_info['kind'] == 'user':
|
2015-07-15 21:25:41 +00:00
|
|
|
target = model.user.get_nonrobot_user(target_info['name'])
|
2014-07-18 18:12:20 +00:00
|
|
|
if not target:
|
|
|
|
# Just to be safe.
|
2014-07-28 18:58:12 +00:00
|
|
|
return (True, 'Unknown user %s' % target_info['name'], [])
|
2014-07-18 18:12:20 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
return (True, None, [target])
|
2014-07-22 17:39:41 +00:00
|
|
|
elif target_info['kind'] == 'org':
|
2015-07-15 21:25:41 +00:00
|
|
|
target = model.organization.get_organization(target_info['name'])
|
2014-07-22 17:39:41 +00:00
|
|
|
if not target:
|
|
|
|
# Just to be safe.
|
2014-07-28 18:58:12 +00:00
|
|
|
return (True, 'Unknown organization %s' % target_info['name'], None)
|
2014-07-22 17:39:41 +00:00
|
|
|
|
|
|
|
# Only repositories under the organization can cause notifications to that org.
|
2017-07-14 14:32:38 +00:00
|
|
|
if target_info['name'] != _get_namespace_name_from(repository):
|
2014-07-28 18:58:12 +00:00
|
|
|
return (False, 'Organization name must match repository namespace')
|
2014-07-22 17:39:41 +00:00
|
|
|
|
2014-07-28 18:58:12 +00:00
|
|
|
return (True, None, [target])
|
2014-07-18 18:12:20 +00:00
|
|
|
elif target_info['kind'] == 'team':
|
|
|
|
# Lookup the team.
|
2015-07-15 21:25:41 +00:00
|
|
|
org_team = None
|
2014-07-18 18:12:20 +00:00
|
|
|
try:
|
2017-07-14 14:32:38 +00:00
|
|
|
org_team = model.team.get_organization_team(_get_namespace_name_from(repository), target_info['name'])
|
2014-07-18 18:12:20 +00:00
|
|
|
except model.InvalidTeamException:
|
|
|
|
# Probably deleted.
|
2014-07-28 18:58:12 +00:00
|
|
|
return (True, 'Unknown team %s' % target_info['name'], None)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-18 18:12:20 +00:00
|
|
|
# Lookup the team's members
|
2015-07-15 21:25:41 +00:00
|
|
|
return (True, None, model.organization.get_organization_team_members(org_team.id))
|
2014-07-28 18:58:12 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def perform(self, notification_obj, event_handler, notification_data):
|
|
|
|
repository = notification_obj.repository
|
2014-07-28 18:58:12 +00:00
|
|
|
if not repository:
|
|
|
|
# Probably deleted.
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-07-28 18:58:12 +00:00
|
|
|
|
|
|
|
# Lookup the target user or team to which we'll send the notification.
|
2017-07-12 12:50:47 +00:00
|
|
|
config_data = notification_obj.method_config_dict
|
2014-07-28 18:58:12 +00:00
|
|
|
status, err_message, target_users = self.find_targets(repository, config_data)
|
|
|
|
if not status:
|
2014-09-22 16:52:57 +00:00
|
|
|
raise NotificationMethodPerformException(err_message)
|
2014-07-18 18:12:20 +00:00
|
|
|
|
|
|
|
# For each of the target users, create a notification.
|
2014-07-28 18:58:12 +00:00
|
|
|
for target_user in set(target_users or []):
|
2015-07-15 21:25:41 +00:00
|
|
|
model.notification.create_notification(event_handler.event_name(), target_user,
|
|
|
|
metadata=notification_data['event_data'])
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EmailMethod(NotificationMethod):
|
|
|
|
@classmethod
|
|
|
|
def method_name(cls):
|
|
|
|
return 'email'
|
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def validate(self, namespace_name, repository_name, config_data):
|
2014-07-28 18:58:12 +00:00
|
|
|
email = config_data.get('email', '')
|
|
|
|
if not email:
|
|
|
|
raise CannotValidateNotificationMethodException('Missing e-mail address')
|
|
|
|
|
2017-07-14 14:32:38 +00:00
|
|
|
record = model.repository.get_email_authorized_for_repo(_get_namespace_name_from(repository),
|
2015-07-15 21:25:41 +00:00
|
|
|
repository.name, email)
|
2014-07-28 18:58:12 +00:00
|
|
|
if not record or not record.confirmed:
|
|
|
|
raise CannotValidateNotificationMethodException('The specified e-mail address '
|
|
|
|
'is not authorized to receive '
|
|
|
|
'notifications for this repository')
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def perform(self, notification_obj, event_handler, notification_data):
|
2017-07-12 12:50:47 +00:00
|
|
|
config_data = notification_obj.method_config_dict
|
2014-07-18 02:51:58 +00:00
|
|
|
email = config_data.get('email', '')
|
|
|
|
if not email:
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-07-18 02:51:58 +00:00
|
|
|
|
2017-05-10 18:24:27 +00:00
|
|
|
with app.app_context():
|
|
|
|
msg = Message(event_handler.get_summary(notification_data['event_data'], notification_data),
|
|
|
|
recipients=[email])
|
|
|
|
msg.html = event_handler.get_message(notification_data['event_data'], notification_data)
|
|
|
|
|
|
|
|
try:
|
2017-05-19 15:36:55 +00:00
|
|
|
mail.send(msg)
|
2017-05-10 18:24:27 +00:00
|
|
|
except Exception as ex:
|
|
|
|
logger.exception('Email was unable to be sent: %s' % ex.message)
|
|
|
|
raise NotificationMethodPerformException(ex.message)
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebhookMethod(NotificationMethod):
|
|
|
|
@classmethod
|
|
|
|
def method_name(cls):
|
|
|
|
return 'webhook'
|
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def validate(self, namespace_name, repository_name, config_data):
|
2014-07-28 18:58:12 +00:00
|
|
|
url = config_data.get('url', '')
|
|
|
|
if not url:
|
|
|
|
raise CannotValidateNotificationMethodException('Missing webhook URL')
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def perform(self, notification_obj, event_handler, notification_data):
|
2017-07-12 12:50:47 +00:00
|
|
|
config_data = notification_obj.method_config_dict
|
2014-07-18 02:51:58 +00:00
|
|
|
url = config_data.get('url', '')
|
|
|
|
if not url:
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
payload = notification_data['event_data']
|
|
|
|
headers = {'Content-type': 'application/json'}
|
|
|
|
|
|
|
|
try:
|
2016-11-08 23:20:38 +00:00
|
|
|
resp = requests.post(url, data=json.dumps(payload), headers=headers, cert=SSLClientCert,
|
|
|
|
timeout=METHOD_TIMEOUT)
|
2017-05-19 15:36:55 +00:00
|
|
|
if resp.status_code / 100 != 2:
|
2014-09-22 16:52:57 +00:00
|
|
|
error_message = '%s response for webhook to url: %s' % (resp.status_code, url)
|
|
|
|
logger.error(error_message)
|
|
|
|
logger.error(resp.content)
|
|
|
|
raise NotificationMethodPerformException(error_message)
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
except requests.exceptions.RequestException as ex:
|
|
|
|
logger.exception('Webhook was unable to be sent: %s' % ex.message)
|
2014-09-22 16:52:57 +00:00
|
|
|
raise NotificationMethodPerformException(ex.message)
|
2014-08-19 18:33:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FlowdockMethod(NotificationMethod):
|
|
|
|
""" Method for sending notifications to Flowdock via the Team Inbox API:
|
2014-11-24 21:07:38 +00:00
|
|
|
https://www.flowdock.com/api/team-inbox
|
2014-08-19 18:33:33 +00:00
|
|
|
"""
|
2017-05-19 15:36:55 +00:00
|
|
|
|
2014-08-19 18:33:33 +00:00
|
|
|
@classmethod
|
|
|
|
def method_name(cls):
|
|
|
|
return 'flowdock'
|
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def validate(self, namespace_name, repository_name, config_data):
|
2014-08-19 18:33:33 +00:00
|
|
|
token = config_data.get('flow_api_token', '')
|
|
|
|
if not token:
|
|
|
|
raise CannotValidateNotificationMethodException('Missing Flowdock API Token')
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def perform(self, notification_obj, event_handler, notification_data):
|
2017-07-12 12:50:47 +00:00
|
|
|
config_data = notification_obj.method_config_dict
|
2014-08-19 18:33:33 +00:00
|
|
|
token = config_data.get('flow_api_token', '')
|
|
|
|
if not token:
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-08-19 18:33:33 +00:00
|
|
|
|
2017-07-14 14:32:38 +00:00
|
|
|
owner = model.user.get_user_or_org(_get_namespace_name_from(notification_obj.repository))
|
2014-08-19 18:33:33 +00:00
|
|
|
if not owner:
|
|
|
|
# Something went wrong.
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-08-19 18:33:33 +00:00
|
|
|
|
|
|
|
url = 'https://api.flowdock.com/v1/messages/team_inbox/%s' % token
|
|
|
|
headers = {'Content-type': 'application/json'}
|
|
|
|
payload = {
|
|
|
|
'source': 'Quay',
|
2017-05-19 15:36:55 +00:00
|
|
|
'from_address': 'support@quay.io',
|
2014-08-19 18:33:33 +00:00
|
|
|
'subject': event_handler.get_summary(notification_data['event_data'], notification_data),
|
|
|
|
'content': event_handler.get_message(notification_data['event_data'], notification_data),
|
|
|
|
'from_name': owner.username,
|
2017-07-14 14:32:38 +00:00
|
|
|
'project': (_get_namespace_name_from(notification_obj.repository)+ ' ' +
|
2015-07-15 21:25:41 +00:00
|
|
|
notification_obj.repository.name),
|
2014-08-19 18:33:33 +00:00
|
|
|
'tags': ['#' + event_handler.event_name()],
|
|
|
|
'link': notification_data['event_data']['homepage']
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
2016-11-08 23:20:38 +00:00
|
|
|
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
|
2017-05-19 15:36:55 +00:00
|
|
|
if resp.status_code / 100 != 2:
|
2014-09-22 16:52:57 +00:00
|
|
|
error_message = '%s response for flowdock to url: %s' % (resp.status_code, url)
|
|
|
|
logger.error(error_message)
|
2014-08-19 18:33:33 +00:00
|
|
|
logger.error(resp.content)
|
2014-09-22 16:52:57 +00:00
|
|
|
raise NotificationMethodPerformException(error_message)
|
2014-08-19 18:33:33 +00:00
|
|
|
|
|
|
|
except requests.exceptions.RequestException as ex:
|
|
|
|
logger.exception('Flowdock method was unable to be sent: %s' % ex.message)
|
2014-09-22 16:52:57 +00:00
|
|
|
raise NotificationMethodPerformException(ex.message)
|
2014-08-19 21:40:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HipchatMethod(NotificationMethod):
|
|
|
|
""" Method for sending notifications to Hipchat via the API:
|
|
|
|
https://www.hipchat.com/docs/apiv2/method/send_room_notification
|
|
|
|
"""
|
2017-05-19 15:36:55 +00:00
|
|
|
|
2014-08-19 21:40:36 +00:00
|
|
|
@classmethod
|
|
|
|
def method_name(cls):
|
|
|
|
return 'hipchat'
|
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def validate(self, namespace_name, repository_name, config_data):
|
2014-08-19 21:40:36 +00:00
|
|
|
if not config_data.get('notification_token', ''):
|
|
|
|
raise CannotValidateNotificationMethodException('Missing Hipchat Room Notification Token')
|
|
|
|
|
|
|
|
if not config_data.get('room_id', ''):
|
|
|
|
raise CannotValidateNotificationMethodException('Missing Hipchat Room ID')
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def perform(self, notification_obj, event_handler, notification_data):
|
2017-07-12 12:50:47 +00:00
|
|
|
config_data = notification_obj.method_config_dict
|
2014-08-19 21:40:36 +00:00
|
|
|
token = config_data.get('notification_token', '')
|
|
|
|
room_id = config_data.get('room_id', '')
|
|
|
|
|
|
|
|
if not token or not room_id:
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-08-19 21:40:36 +00:00
|
|
|
|
2017-07-14 14:32:38 +00:00
|
|
|
owner = model.user.get_user_or_org(_get_namespace_name_from(notification_obj.repository))
|
2014-08-19 21:40:36 +00:00
|
|
|
if not owner:
|
|
|
|
# Something went wrong.
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-08-19 21:40:36 +00:00
|
|
|
|
|
|
|
url = 'https://api.hipchat.com/v2/room/%s/notification?auth_token=%s' % (room_id, token)
|
|
|
|
|
|
|
|
level = event_handler.get_level(notification_data['event_data'], notification_data)
|
|
|
|
color = {
|
|
|
|
'info': 'gray',
|
|
|
|
'warning': 'yellow',
|
|
|
|
'error': 'red',
|
2014-10-22 23:01:56 +00:00
|
|
|
'success': 'green',
|
2014-08-19 21:40:36 +00:00
|
|
|
'primary': 'purple'
|
|
|
|
}.get(level, 'gray')
|
|
|
|
|
|
|
|
headers = {'Content-type': 'application/json'}
|
|
|
|
payload = {
|
|
|
|
'color': color,
|
|
|
|
'message': event_handler.get_message(notification_data['event_data'], notification_data),
|
|
|
|
'notify': level == 'error',
|
2014-11-24 21:07:38 +00:00
|
|
|
'message_format': 'html',
|
2014-08-19 21:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
2016-11-08 23:20:38 +00:00
|
|
|
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
|
2017-05-19 15:36:55 +00:00
|
|
|
if resp.status_code / 100 != 2:
|
2014-09-22 16:52:57 +00:00
|
|
|
error_message = '%s response for hipchat to url: %s' % (resp.status_code, url)
|
|
|
|
logger.error(error_message)
|
2014-08-19 21:40:36 +00:00
|
|
|
logger.error(resp.content)
|
2014-09-22 16:52:57 +00:00
|
|
|
raise NotificationMethodPerformException(error_message)
|
2014-08-19 21:40:36 +00:00
|
|
|
|
|
|
|
except requests.exceptions.RequestException as ex:
|
|
|
|
logger.exception('Hipchat method was unable to be sent: %s' % ex.message)
|
2014-09-22 16:52:57 +00:00
|
|
|
raise NotificationMethodPerformException(ex.message)
|
2014-08-27 02:09:56 +00:00
|
|
|
|
|
|
|
|
2014-10-22 23:01:56 +00:00
|
|
|
from HTMLParser import HTMLParser
|
|
|
|
|
2017-05-19 15:36:55 +00:00
|
|
|
|
2014-10-22 23:01:56 +00:00
|
|
|
class SlackAdjuster(HTMLParser):
|
|
|
|
def __init__(self):
|
|
|
|
self.reset()
|
|
|
|
self.result = []
|
|
|
|
|
|
|
|
def handle_data(self, d):
|
|
|
|
self.result.append(d)
|
|
|
|
|
|
|
|
def get_attr(self, attrs, name):
|
|
|
|
for attr in attrs:
|
|
|
|
if attr[0] == name:
|
|
|
|
return attr[1]
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
if tag == 'a':
|
2017-05-19 15:36:55 +00:00
|
|
|
self.result.append('<%s|' % (self.get_attr(attrs, 'href'),))
|
2014-10-22 23:01:56 +00:00
|
|
|
|
|
|
|
if tag == 'i':
|
|
|
|
self.result.append('_')
|
|
|
|
|
|
|
|
if tag == 'b' or tag == 'strong':
|
|
|
|
self.result.append('*')
|
|
|
|
|
|
|
|
if tag == 'img':
|
2015-06-17 03:16:36 +00:00
|
|
|
self.result.append('')
|
2014-10-22 23:01:56 +00:00
|
|
|
|
|
|
|
def handle_endtag(self, tag):
|
|
|
|
if tag == 'a':
|
|
|
|
self.result.append('>')
|
|
|
|
|
|
|
|
if tag == 'b' or tag == 'strong':
|
|
|
|
self.result.append('*')
|
|
|
|
|
|
|
|
if tag == 'i':
|
|
|
|
self.result.append('_')
|
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return ''.join(self.result)
|
|
|
|
|
2017-05-19 15:36:55 +00:00
|
|
|
|
2014-10-22 23:01:56 +00:00
|
|
|
def adjust_tags(html):
|
|
|
|
s = SlackAdjuster()
|
|
|
|
s.feed(html)
|
|
|
|
return s.get_data()
|
|
|
|
|
|
|
|
|
2014-08-27 02:09:56 +00:00
|
|
|
class SlackMethod(NotificationMethod):
|
|
|
|
""" Method for sending notifications to Slack via the API:
|
|
|
|
https://api.slack.com/docs/attachments
|
|
|
|
"""
|
|
|
|
@classmethod
|
|
|
|
def method_name(cls):
|
|
|
|
return 'slack'
|
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def validate(self, namespace_name, repository_name, config_data):
|
2014-12-16 12:34:43 +00:00
|
|
|
if not config_data.get('url', ''):
|
|
|
|
raise CannotValidateNotificationMethodException('Missing Slack Callback URL')
|
2014-08-27 02:09:56 +00:00
|
|
|
|
2014-10-22 23:01:56 +00:00
|
|
|
def format_for_slack(self, message):
|
2014-08-27 02:09:56 +00:00
|
|
|
message = message.replace('\n', '')
|
|
|
|
message = re.sub(r'\s+', ' ', message)
|
|
|
|
message = message.replace('<br>', '\n')
|
2014-10-22 23:01:56 +00:00
|
|
|
return adjust_tags(message)
|
2014-08-27 02:09:56 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def perform(self, notification_obj, event_handler, notification_data):
|
2017-07-12 12:50:47 +00:00
|
|
|
config_data = notification_obj.method_config_dict
|
2014-12-16 12:34:43 +00:00
|
|
|
url = config_data.get('url', '')
|
|
|
|
if not url:
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-08-27 02:09:56 +00:00
|
|
|
|
2017-07-14 14:32:38 +00:00
|
|
|
owner = model.user.get_user_or_org(_get_namespace_name_from(notification_obj.repository))
|
2014-08-27 02:09:56 +00:00
|
|
|
if not owner:
|
|
|
|
# Something went wrong.
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-08-27 02:09:56 +00:00
|
|
|
|
|
|
|
level = event_handler.get_level(notification_data['event_data'], notification_data)
|
|
|
|
color = {
|
|
|
|
'info': '#ffffff',
|
|
|
|
'warning': 'warning',
|
|
|
|
'error': 'danger',
|
2014-10-22 23:01:56 +00:00
|
|
|
'success': 'good',
|
2014-08-27 02:09:56 +00:00
|
|
|
'primary': 'good'
|
|
|
|
}.get(level, '#ffffff')
|
|
|
|
|
|
|
|
summary = event_handler.get_summary(notification_data['event_data'], notification_data)
|
|
|
|
message = event_handler.get_message(notification_data['event_data'], notification_data)
|
|
|
|
|
|
|
|
headers = {'Content-type': 'application/json'}
|
|
|
|
payload = {
|
|
|
|
'text': summary,
|
|
|
|
'username': 'quayiobot',
|
|
|
|
'attachments': [
|
|
|
|
{
|
|
|
|
'fallback': summary,
|
2014-10-22 23:01:56 +00:00
|
|
|
'text': self.format_for_slack(message),
|
|
|
|
'color': color,
|
|
|
|
'mrkdwn_in': ["text"]
|
2014-08-27 02:09:56 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
2016-11-08 23:20:38 +00:00
|
|
|
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
|
2017-05-19 15:36:55 +00:00
|
|
|
if resp.status_code / 100 != 2:
|
2014-09-22 16:52:57 +00:00
|
|
|
error_message = '%s response for Slack to url: %s' % (resp.status_code, url)
|
|
|
|
logger.error(error_message)
|
2014-08-27 02:09:56 +00:00
|
|
|
logger.error(resp.content)
|
2014-09-22 16:52:57 +00:00
|
|
|
raise NotificationMethodPerformException(error_message)
|
2014-08-27 02:09:56 +00:00
|
|
|
|
|
|
|
except requests.exceptions.RequestException as ex:
|
2014-12-16 12:34:43 +00:00
|
|
|
logger.exception('Slack method was unable to be sent: %s', ex.message)
|
2014-09-22 16:52:57 +00:00
|
|
|
raise NotificationMethodPerformException(ex.message)
|