fix(endpoints/notificationmethod.py): deals with pre oci and the previous data model

Issue: NA

- [ ] 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-07-14 10:32:38 -04:00
parent 2f750bfc87
commit 69a0a10ab4

View file

@ -27,6 +27,18 @@ class NotificationMethodPerformException(JobException):
pass pass
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
SSLClientCert = None SSLClientCert = None
if app.config['PREFERRED_URL_SCHEME'] == 'https': if app.config['PREFERRED_URL_SCHEME'] == 'https':
# TODO(jschorr): move this into the config provider library # TODO(jschorr): move this into the config provider library
@ -98,7 +110,7 @@ class QuayNotificationMethod(NotificationMethod):
return (True, 'Unknown organization %s' % target_info['name'], None) return (True, 'Unknown organization %s' % target_info['name'], None)
# Only repositories under the organization can cause notifications to that org. # Only repositories under the organization can cause notifications to that org.
if target_info['name'] != repository.namespace_name: if target_info['name'] != _get_namespace_name_from(repository):
return (False, 'Organization name must match repository namespace') return (False, 'Organization name must match repository namespace')
return (True, None, [target]) return (True, None, [target])
@ -106,7 +118,7 @@ class QuayNotificationMethod(NotificationMethod):
# Lookup the team. # Lookup the team.
org_team = None org_team = None
try: try:
org_team = model.team.get_organization_team(repository.namespace_name, target_info['name']) org_team = model.team.get_organization_team(_get_namespace_name_from(repository), target_info['name'])
except model.InvalidTeamException: except model.InvalidTeamException:
# Probably deleted. # Probably deleted.
return (True, 'Unknown team %s' % target_info['name'], None) return (True, 'Unknown team %s' % target_info['name'], None)
@ -142,7 +154,8 @@ class EmailMethod(NotificationMethod):
if not email: if not email:
raise CannotValidateNotificationMethodException('Missing e-mail address') raise CannotValidateNotificationMethodException('Missing e-mail address')
record = model.repository.get_email_authorized_for_repo(repository.namespace_name,
record = model.repository.get_email_authorized_for_repo(_get_namespace_name_from(repository),
repository.name, email) repository.name, email)
if not record or not record.confirmed: if not record or not record.confirmed:
raise CannotValidateNotificationMethodException('The specified e-mail address ' raise CannotValidateNotificationMethodException('The specified e-mail address '
@ -220,7 +233,7 @@ class FlowdockMethod(NotificationMethod):
if not token: if not token:
return return
owner = model.user.get_user_or_org(notification_obj.repository.namespace_name) owner = model.user.get_user_or_org(_get_namespace_name_from(notification_obj.repository))
if not owner: if not owner:
# Something went wrong. # Something went wrong.
return return
@ -233,7 +246,7 @@ class FlowdockMethod(NotificationMethod):
'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,
'project': (notification_obj.repository.namespace_name + ' ' + 'project': (_get_namespace_name_from(notification_obj.repository)+ ' ' +
notification_obj.repository.name), notification_obj.repository.name),
'tags': ['#' + event_handler.event_name()], 'tags': ['#' + event_handler.event_name()],
'link': notification_data['event_data']['homepage'] 'link': notification_data['event_data']['homepage']
@ -276,7 +289,7 @@ class HipchatMethod(NotificationMethod):
if not token or not room_id: if not token or not room_id:
return return
owner = model.user.get_user_or_org(notification_obj.repository.namespace_name) owner = model.user.get_user_or_org(_get_namespace_name_from(notification_obj.repository))
if not owner: if not owner:
# Something went wrong. # Something went wrong.
return return
@ -388,7 +401,7 @@ class SlackMethod(NotificationMethod):
if not url: if not url:
return return
owner = model.user.get_user_or_org(notification_obj.repository.namespace_name) owner = model.user.get_user_or_org(_get_namespace_name_from(notification_obj.repository))
if not owner: if not owner:
# Something went wrong. # Something went wrong.
return return