style(workers/notificationworker.py): formatted file

[TESTING -> locally]

Issue: https://www.pivotaltracker.com/story/show/b144646649n

- [ ] It works!
- [ ] Comments provide sufficient explanations for the next contributor
- [ ] Tests cover changes and corner cases
- [ ] Follows Quay syntax patterns and format
This commit is contained in:
Charlton Austin 2017-05-19 11:36:55 -04:00
parent b40ad361db
commit 2282af2619

View file

@ -1,9 +1,8 @@
import logging
import json import json
import logging
import re import re
import requests import requests
from flask_mail import Message from flask_mail import Message
from app import mail, app, OVERRIDE_CONFIG_DIRECTORY from app import mail, app, OVERRIDE_CONFIG_DIRECTORY
@ -11,10 +10,9 @@ from data import model
from util.config.validator import SSL_FILENAMES from util.config.validator import SSL_FILENAMES
from workers.queueworker import JobException from workers.queueworker import JobException
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
METHOD_TIMEOUT = app.config.get('NOTIFICATION_SEND_TIMEOUT', 10) # Seconds METHOD_TIMEOUT = app.config.get('NOTIFICATION_SEND_TIMEOUT', 10) # Seconds
class InvalidNotificationMethodException(Exception): class InvalidNotificationMethodException(Exception):
@ -80,7 +78,7 @@ class QuayNotificationMethod(NotificationMethod):
def validate(self, repository, config_data): def validate(self, repository, config_data):
status, err_message, target_users = self.find_targets(repository, config_data) status, err_message, target_users = self.find_targets(repository, config_data)
if err_message: if err_message:
raise CannotValidateNotificationMethodException(err_message) raise CannotValidateNotificationMethodException(err_message)
def find_targets(self, repository, config_data): def find_targets(self, repository, config_data):
target_info = config_data['target'] target_info = config_data['target']
@ -152,7 +150,6 @@ class EmailMethod(NotificationMethod):
'is not authorized to receive ' 'is not authorized to receive '
'notifications for this repository') 'notifications for this repository')
def perform(self, notification_obj, event_handler, notification_data): def perform(self, notification_obj, event_handler, notification_data):
config_data = json.loads(notification_obj.config_json) config_data = json.loads(notification_obj.config_json)
email = config_data.get('email', '') email = config_data.get('email', '')
@ -165,7 +162,7 @@ class EmailMethod(NotificationMethod):
msg.html = event_handler.get_message(notification_data['event_data'], notification_data) msg.html = event_handler.get_message(notification_data['event_data'], notification_data)
try: try:
mail.send(msg) mail.send(msg)
except Exception as ex: except Exception as ex:
logger.exception('Email was unable to be sent: %s' % ex.message) logger.exception('Email was unable to be sent: %s' % ex.message)
raise NotificationMethodPerformException(ex.message) raise NotificationMethodPerformException(ex.message)
@ -193,7 +190,7 @@ class WebhookMethod(NotificationMethod):
try: try:
resp = requests.post(url, data=json.dumps(payload), headers=headers, cert=SSLClientCert, resp = requests.post(url, data=json.dumps(payload), headers=headers, cert=SSLClientCert,
timeout=METHOD_TIMEOUT) timeout=METHOD_TIMEOUT)
if resp.status_code/100 != 2: if resp.status_code / 100 != 2:
error_message = '%s response for webhook to url: %s' % (resp.status_code, url) error_message = '%s response for webhook to url: %s' % (resp.status_code, url)
logger.error(error_message) logger.error(error_message)
logger.error(resp.content) logger.error(resp.content)
@ -208,6 +205,7 @@ class FlowdockMethod(NotificationMethod):
""" Method for sending notifications to Flowdock via the Team Inbox API: """ Method for sending notifications to Flowdock via the Team Inbox API:
https://www.flowdock.com/api/team-inbox https://www.flowdock.com/api/team-inbox
""" """
@classmethod @classmethod
def method_name(cls): def method_name(cls):
return 'flowdock' return 'flowdock'
@ -232,7 +230,7 @@ class FlowdockMethod(NotificationMethod):
headers = {'Content-type': 'application/json'} headers = {'Content-type': 'application/json'}
payload = { payload = {
'source': 'Quay', 'source': 'Quay',
'from_address': 'support@quay.io', 'from_address': 'support@quay.io',
'subject': event_handler.get_summary(notification_data['event_data'], notification_data), 'subject': event_handler.get_summary(notification_data['event_data'], notification_data),
'content': event_handler.get_message(notification_data['event_data'], notification_data), 'content': event_handler.get_message(notification_data['event_data'], notification_data),
'from_name': owner.username, 'from_name': owner.username,
@ -244,7 +242,7 @@ class FlowdockMethod(NotificationMethod):
try: try:
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT) resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
if resp.status_code/100 != 2: if resp.status_code / 100 != 2:
error_message = '%s response for flowdock to url: %s' % (resp.status_code, url) error_message = '%s response for flowdock to url: %s' % (resp.status_code, url)
logger.error(error_message) logger.error(error_message)
logger.error(resp.content) logger.error(resp.content)
@ -259,6 +257,7 @@ class HipchatMethod(NotificationMethod):
""" Method for sending notifications to Hipchat via the API: """ Method for sending notifications to Hipchat via the API:
https://www.hipchat.com/docs/apiv2/method/send_room_notification https://www.hipchat.com/docs/apiv2/method/send_room_notification
""" """
@classmethod @classmethod
def method_name(cls): def method_name(cls):
return 'hipchat' return 'hipchat'
@ -305,7 +304,7 @@ class HipchatMethod(NotificationMethod):
try: try:
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT) resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
if resp.status_code/100 != 2: if resp.status_code / 100 != 2:
error_message = '%s response for hipchat to url: %s' % (resp.status_code, url) error_message = '%s response for hipchat to url: %s' % (resp.status_code, url)
logger.error(error_message) logger.error(error_message)
logger.error(resp.content) logger.error(resp.content)
@ -318,6 +317,7 @@ class HipchatMethod(NotificationMethod):
from HTMLParser import HTMLParser from HTMLParser import HTMLParser
class SlackAdjuster(HTMLParser): class SlackAdjuster(HTMLParser):
def __init__(self): def __init__(self):
self.reset() self.reset()
@ -335,7 +335,7 @@ class SlackAdjuster(HTMLParser):
def handle_starttag(self, tag, attrs): def handle_starttag(self, tag, attrs):
if tag == 'a': if tag == 'a':
self.result.append('<%s|' % (self.get_attr(attrs, 'href'), )) self.result.append('<%s|' % (self.get_attr(attrs, 'href'),))
if tag == 'i': if tag == 'i':
self.result.append('_') self.result.append('_')
@ -359,6 +359,7 @@ class SlackAdjuster(HTMLParser):
def get_data(self): def get_data(self):
return ''.join(self.result) return ''.join(self.result)
def adjust_tags(html): def adjust_tags(html):
s = SlackAdjuster() s = SlackAdjuster()
s.feed(html) s.feed(html)
@ -423,7 +424,7 @@ class SlackMethod(NotificationMethod):
try: try:
resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT) resp = requests.post(url, data=json.dumps(payload), headers=headers, timeout=METHOD_TIMEOUT)
if resp.status_code/100 != 2: if resp.status_code / 100 != 2:
error_message = '%s response for Slack to url: %s' % (resp.status_code, url) error_message = '%s response for Slack to url: %s' % (resp.status_code, url)
logger.error(error_message) logger.error(error_message)
logger.error(resp.content) logger.error(resp.content)