From bf3e941d7f00233e4f29c7d9a6ceaef9f80fe6a1 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Thu, 13 Jul 2017 22:43:26 +0300 Subject: [PATCH] Fix notification system to use the new tuple correctly --- endpoints/api/repositorynotification.py | 5 +- endpoints/notificationevent.py | 63 +++----------------- endpoints/notificationmethod.py | 15 +++-- endpoints/test/test_notificationevent.py | 2 +- workers/notificationworker/models_pre_oci.py | 15 +++-- 5 files changed, 28 insertions(+), 72 deletions(-) diff --git a/endpoints/api/repositorynotification.py b/endpoints/api/repositorynotification.py index ab81d2c5b..9a812050c 100644 --- a/endpoints/api/repositorynotification.py +++ b/endpoints/api/repositorynotification.py @@ -15,6 +15,8 @@ from endpoints.notificationmethod import (NotificationMethod, CannotValidateNotificationMethodException) from endpoints.notificationhelper import build_notification_data from data import model +from workers.notificationworker.models_pre_oci import notification + logger = logging.getLogger(__name__) def notification_view(note): @@ -191,7 +193,8 @@ class TestRepositoryNotification(RepositoryParamResource): raise NotFound() event_info = NotificationEvent.get_event(test_note.event.name) - sample_data = event_info.get_sample_data(test_note) + sample_data = event_info.get_sample_data(notification(test_note)) + notification_data = build_notification_data(test_note, sample_data) notification_queue.put([test_note.repository.namespace_user.username, repository, test_note.event.name], json.dumps(notification_data)) diff --git a/endpoints/notificationevent.py b/endpoints/notificationevent.py index 1da827843..750e84caf 100644 --- a/endpoints/notificationevent.py +++ b/endpoints/notificationevent.py @@ -92,13 +92,7 @@ class RepoPushEvent(NotificationEvent): return 'Repository %s updated' % (event_data['repository']) def get_sample_data(self, notification): - # TODO(jzelinskie): remove when more endpoints have been converted to using - # interfaces - repo = AttrDict({ - 'namespace_name': notification.repository.namespace_user.username, - 'name': notification.repository.name, - }) - return build_event_data(repo, { + return build_event_data(notification.repository, { 'updated_tags': {'latest': 'someimageid', 'foo': 'anotherimage'}, 'pruned_image_count': 3 }) @@ -133,16 +127,8 @@ class VulnerabilityFoundEvent(NotificationEvent): def get_sample_data(self, notification): event_config = notification.event_config_dict - - # TODO(jzelinskie): remove when more endpoints have been converted to using - # interfaces - repo = AttrDict({ - 'namespace_name': notification.repository.namespace_user.username, - 'name': notification.repository.name, - }) - level = event_config.get(VulnerabilityFoundEvent.CONFIG_LEVEL, 'Critical') - return build_event_data(repo, { + return build_event_data(notification.repository, { 'tags': ['latest', 'prod', 'foo', 'bar', 'baz'], 'image': 'some-image-id', 'vulnerability': { @@ -229,14 +215,7 @@ class BuildQueueEvent(BaseBuildEvent): def get_sample_data(self, notification): build_uuid = 'fake-build-id' - - # TODO(jzelinskie): remove when more endpoints have been converted to using - # interfaces - repo = AttrDict({ - 'namespace_name': notification.repository.namespace_user.username, - 'name': notification.repository.name, - }) - return build_event_data(repo, { + return build_event_data(notification.repository, { 'is_manual': False, 'build_id': build_uuid, 'build_name': 'some-fake-build', @@ -274,14 +253,7 @@ class BuildStartEvent(BaseBuildEvent): def get_sample_data(self, notification): build_uuid = 'fake-build-id' - - # TODO(jzelinskie): remove when more endpoints have been converted to using - # interfaces - repo = AttrDict({ - 'namespace_name': notification.repository.namespace_user.username, - 'name': notification.repository.name, - }) - return build_event_data(repo, { + return build_event_data(notification.repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', 'docker_tags': ['latest', 'foo', 'bar'], @@ -308,14 +280,7 @@ class BuildSuccessEvent(BaseBuildEvent): def get_sample_data(self, notification): build_uuid = 'fake-build-id' - - # TODO(jzelinskie): remove when more endpoints have been converted to using - # interfaces - repo = AttrDict({ - 'namespace_name': notification.repository.namespace_user.username, - 'name': notification.repository.name, - }) - return build_event_data(repo, { + return build_event_data(notification.repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', 'docker_tags': ['latest', 'foo', 'bar'], @@ -343,14 +308,7 @@ class BuildFailureEvent(BaseBuildEvent): def get_sample_data(self, notification): build_uuid = 'fake-build-id' - - # TODO(jzelinskie): remove when more endpoints have been converted to using - # interfaces - repo = AttrDict({ - 'namespace_name': notification.repository.namespace_user.username, - 'name': notification.repository.name, - }) - return build_event_data(repo, { + return build_event_data(notification.repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', 'docker_tags': ['latest', 'foo', 'bar'], @@ -389,14 +347,7 @@ class BuildCancelledEvent(BaseBuildEvent): def get_sample_data(self, notification): build_uuid = 'fake-build-id' - - # TODO(jzelinskie): remove when more endpoints have been converted to using - # interfaces - repo = AttrDict({ - 'namespace_name': notification.repository.namespace_user.username, - 'name': notification.repository.name, - }) - return build_event_data(repo, { + return build_event_data(notification.repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', 'docker_tags': ['latest', 'foo', 'bar'], diff --git a/endpoints/notificationmethod.py b/endpoints/notificationmethod.py index 0656f24ec..71962dfed 100644 --- a/endpoints/notificationmethod.py +++ b/endpoints/notificationmethod.py @@ -98,7 +98,7 @@ class QuayNotificationMethod(NotificationMethod): return (True, 'Unknown organization %s' % target_info['name'], None) # Only repositories under the organization can cause notifications to that org. - if target_info['name'] != repository.namespace_user.username: + if target_info['name'] != repository.namespace_name: return (False, 'Organization name must match repository namespace') return (True, None, [target]) @@ -106,8 +106,7 @@ class QuayNotificationMethod(NotificationMethod): # Lookup the team. org_team = None try: - org_team = model.team.get_organization_team(repository.namespace_user.username, - target_info['name']) + org_team = model.team.get_organization_team(repository.namespace_name, target_info['name']) except model.InvalidTeamException: # Probably deleted. return (True, 'Unknown team %s' % target_info['name'], None) @@ -143,7 +142,7 @@ class EmailMethod(NotificationMethod): if not email: raise CannotValidateNotificationMethodException('Missing e-mail address') - record = model.repository.get_email_authorized_for_repo(repository.namespace_user.username, + record = model.repository.get_email_authorized_for_repo(repository.namespace_name, repository.name, email) if not record or not record.confirmed: raise CannotValidateNotificationMethodException('The specified e-mail address ' @@ -221,7 +220,7 @@ class FlowdockMethod(NotificationMethod): if not token: return - owner = model.user.get_user_or_org(notification_obj.repository.namespace_user.username) + owner = model.user.get_user_or_org(notification_obj.repository.namespace_name) if not owner: # Something went wrong. return @@ -234,7 +233,7 @@ class FlowdockMethod(NotificationMethod): '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, - 'project': (notification_obj.repository.namespace_user.username + ' ' + + 'project': (notification_obj.repository.namespace_name + ' ' + notification_obj.repository.name), 'tags': ['#' + event_handler.event_name()], 'link': notification_data['event_data']['homepage'] @@ -277,7 +276,7 @@ class HipchatMethod(NotificationMethod): if not token or not room_id: return - owner = model.user.get_user_or_org(notification_obj.repository.namespace_user.username) + owner = model.user.get_user_or_org(notification_obj.repository.namespace_name) if not owner: # Something went wrong. return @@ -389,7 +388,7 @@ class SlackMethod(NotificationMethod): if not url: return - owner = model.user.get_user_or_org(notification_obj.repository.namespace_user.username) + owner = model.user.get_user_or_org(notification_obj.repository.namespace_name) if not owner: # Something went wrong. return diff --git a/endpoints/test/test_notificationevent.py b/endpoints/test/test_notificationevent.py index e6a4d15a8..fd4d81c12 100644 --- a/endpoints/test/test_notificationevent.py +++ b/endpoints/test/test_notificationevent.py @@ -9,7 +9,7 @@ def test_all_notifications(app): # Create a test notification. test_notification = AttrDict({ 'repository': AttrDict({ - 'namespace_user': AttrDict(dict(username='foo')), + 'namespace_name': AttrDict(dict(username='foo')), 'name': 'bar', }), 'event_config_dict': { diff --git a/workers/notificationworker/models_pre_oci.py b/workers/notificationworker/models_pre_oci.py index da0a6c4b3..32389df87 100644 --- a/workers/notificationworker/models_pre_oci.py +++ b/workers/notificationworker/models_pre_oci.py @@ -4,6 +4,14 @@ from data import model from workers.notificationworker.models_interface import ( NotificationWorkerDataInterface, Notification, Repository) +def notification(notification_row): + """ Converts the given notification row into a notification tuple. """ + return Notification(uuid=notification_row.uuid, event_name=notification_row.event.name, + method_name=notification_row.method.name, + event_config_dict=json.loads(notification_row.event_config_json), + method_config_dict=json.loads(notification_row.config_json), + repository=Repository(notification_row.repository.namespace_user.username, + notification_row.repository.name)) class PreOCIModel(NotificationWorkerDataInterface): def get_enabled_notification(self, notification_uuid): @@ -12,12 +20,7 @@ class PreOCIModel(NotificationWorkerDataInterface): except model.InvalidNotificationException: return None - return Notification(uuid=notification_uuid, event_name=notification_row.event.name, - method_name=notification_row.method.name, - event_config_dict=json.loads(notification_row.event_config_json), - method_config_dict=json.loads(notification_row.config_json), - repository=Repository(notification_row.repository.namespace_user.username, - notification_row.repository.name)) + return notification(notification_row) def reset_number_of_failures_to_zero(self, notification): model.notification.reset_notification_number_of_failures(