diff --git a/endpoints/api/__init__.py b/endpoints/api/__init__.py index 8da2d4f58..194178578 100644 --- a/endpoints/api/__init__.py +++ b/endpoints/api/__init__.py @@ -349,7 +349,12 @@ def request_error(exception=None, **kwargs): def log_action(kind, user_or_orgname, metadata=None, repo=None, repo_name=None): if not metadata: metadata = {} - + + if repo_name: + repository = model.repository.get_repository(user_or_orgname, repo_name) + else: + repository = repo + oauth_token = get_validated_oauth_token() if oauth_token: metadata['oauth_token_id'] = oauth_token.id diff --git a/endpoints/api/repositorynotification.py b/endpoints/api/repositorynotification.py index 767a7c8c5..cb612803d 100644 --- a/endpoints/api/repositorynotification.py +++ b/endpoints/api/repositorynotification.py @@ -61,16 +61,15 @@ class RepositoryNotificationList(RepositoryParamResource): @validate_json_request('NotificationCreateRequest') def post(self, namespace_name, repository_name): parsed = request.get_json() - repository = model.get_repository(namespace_name, repository_name) method_handler = NotificationMethod.get_method(parsed['method']) try: - method_handler.validate(repository, parsed['config']) + method_handler.validate(namespace_name, repository_name, parsed['config']) except CannotValidateNotificationMethodException as ex: raise request_error(message=ex.message) - new_notification = model.create_repo_notification(repository, + new_notification = model.create_repo_notification(namespace_name, repository_name, parsed['event'], parsed['method'], parsed['config'], @@ -81,7 +80,7 @@ class RepositoryNotificationList(RepositoryParamResource): {'repo': repository_name, 'namespace': namespace_name, 'notification_id': new_notification.uuid, 'event': parsed['event'], 'method': parsed['method']}, - repo=repository) + repo_name=repository_name) return new_notification.to_dict(), 201 @require_repo_admin @@ -117,12 +116,11 @@ class RepositoryNotification(RepositoryParamResource): @disallow_for_app_repositories def delete(self, namespace_name, repository_name, uuid): """ Deletes the specified notification. """ - repository = model.get_repository(namespace_name, repository_name) - deleted = model.delete_repo_notification(repository, uuid) + deleted = model.delete_repo_notification(namespace_name, repository_name, uuid) log_action('delete_repo_notification', namespace_name, {'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid, 'event': deleted.event.name, 'method': deleted.method.name}, - repo=repository) + repo_name=repository_name) return 'No Content', 204 @@ -131,13 +129,12 @@ class RepositoryNotification(RepositoryParamResource): @disallow_for_app_repositories def post(self, namespace_name, repository_name, uuid): """ Resets repository notification to 0 failures. """ - repository = model.get_repository(namespace_name, repository_name) - reset = model.reset_notification_number_of_failures(repository, uuid) + reset = model.reset_notification_number_of_failures(namespace_name, repository_name, uuid) if reset is not None: log_action('reset_repo_notification', namespace_name, {'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid, 'event': reset.event.name, 'method': reset.method.name}, - repo=repository) + repo_name=repository_name) return 'No Content', 204 diff --git a/endpoints/api/repositorynotification_models_interface.py b/endpoints/api/repositorynotification_models_interface.py index fc4e3ee6f..9237cfeda 100644 --- a/endpoints/api/repositorynotification_models_interface.py +++ b/endpoints/api/repositorynotification_models_interface.py @@ -55,13 +55,9 @@ class RepoNotificationInterface(object): """ Interface that represents all data store interactions required by the RepositoryNotification API """ - - @abstractmethod - def get_repository(self, namespace_name, repository_name): - pass @abstractmethod - def create_repo_notification(self, repository, event_name, method_name, method_config, event_config, title=None): + def create_repo_notification(self, namespace_name, repository_name, event_name, method_name, method_config, event_config, title=None): pass @abstractmethod @@ -73,11 +69,11 @@ class RepoNotificationInterface(object): pass @abstractmethod - def delete_repo_notification(self, repository, uuid): + def delete_repo_notification(self, namespace_name, repository_name, uuid): pass @abstractmethod - def reset_notification_number_of_failures(self, repository, uuid): + def reset_notification_number_of_failures(self, namespace_name, repository_name, uuid): pass @abstractmethod diff --git a/endpoints/api/repositorynotification_models_pre_oci.py b/endpoints/api/repositorynotification_models_pre_oci.py index 2dc578fa9..e1a60c5e8 100644 --- a/endpoints/api/repositorynotification_models_pre_oci.py +++ b/endpoints/api/repositorynotification_models_pre_oci.py @@ -9,16 +9,14 @@ from endpoints.notificationhelper import build_notification_data class PreOCIModel(RepoNotificationInterface): - def get_repository(self, namespace_name, repository_name): - return self._notification(model.repository.get_repository(namespace_name, repository_name)) - - def create_repo_notification(self, repository, event_name, method_name, method_config, event_config, title=None): + def create_repo_notification(self, namespace_name, repository_name, event_name, method_name, method_config, event_config, title=None): + repository = model.repository.get_repository(namespace_name, repository_name) return self._notification(model.notification.create_repo_notification(repository, - event_name, - method_name, - method_config, - event_config, - title)) + event_name, + method_name, + method_config, + event_config, + title)) def list_repo_notifications(self, namespace_name, repository_name, event_name=None): return [self._notification(n) @@ -27,13 +25,13 @@ class PreOCIModel(RepoNotificationInterface): def get_repo_notification(self, uuid): return self._notification(model.notification.get_repo_notification(uuid)) - def delete_repo_notification(self, repository, uuid): + def delete_repo_notification(self, namespace_name, repository_name, uuid): return self._notification( - self.model.notification.delete_repo_notification(repository.namespace_user, repository.name, uuid)) + self.model.notification.delete_repo_notification(namespace_name, repository_name, uuid)) - def reset_notification_number_of_failures(self, repository, uuid): + def reset_notification_number_of_failures(self, namespace_name, repository_name, uuid): return self._notification( - model.notification.reset_notification_number_of_failures(repository.namespace_user, repository.name, uuid)) + model.notification.reset_notification_number_of_failures(namespace_name, repository_name, uuid)) def queue_test_notification(self, notification): event_info = NotificationEvent.get_event(notification.event.name) diff --git a/endpoints/notificationmethod.py b/endpoints/notificationmethod.py index 5a6d8e515..f13423b7f 100644 --- a/endpoints/notificationmethod.py +++ b/endpoints/notificationmethod.py @@ -56,7 +56,7 @@ class NotificationMethod(object): """ raise NotImplementedError - def validate(self, repository, config_data): + def validate(self, namespace_name, repository_name, config_data): """ Validates that the notification can be created with the given data. Throws a CannotValidateNotificationMethodException on failure. @@ -88,12 +88,12 @@ class QuayNotificationMethod(NotificationMethod): def method_name(cls): return 'quay_notification' - def validate(self, repository, config_data): - status, err_message, target_users = self.find_targets(repository, config_data) + def validate(self, namespace_name, repository_name, config_data): + status, err_message, target_users = self.find_targets(namespace_name, repository_name, config_data) if err_message: raise CannotValidateNotificationMethodException(err_message) - def find_targets(self, repository, config_data): + def find_targets(self, namespace_name, repository_name, config_data): target_info = config_data['target'] if target_info['kind'] == 'user': @@ -149,12 +149,11 @@ class EmailMethod(NotificationMethod): def method_name(cls): return 'email' - def validate(self, repository, config_data): + def validate(self, namespace_name, repository_name, config_data): email = config_data.get('email', '') if not email: raise CannotValidateNotificationMethodException('Missing e-mail address') - record = model.repository.get_email_authorized_for_repo(_get_namespace_name_from(repository), repository.name, email) if not record or not record.confirmed: @@ -185,7 +184,7 @@ class WebhookMethod(NotificationMethod): def method_name(cls): return 'webhook' - def validate(self, repository, config_data): + def validate(self, namespace_name, repository_name, config_data): url = config_data.get('url', '') if not url: raise CannotValidateNotificationMethodException('Missing webhook URL') @@ -222,7 +221,7 @@ class FlowdockMethod(NotificationMethod): def method_name(cls): return 'flowdock' - def validate(self, repository, config_data): + def validate(self, namespace_name, repository_name, config_data): token = config_data.get('flow_api_token', '') if not token: raise CannotValidateNotificationMethodException('Missing Flowdock API Token') @@ -274,7 +273,7 @@ class HipchatMethod(NotificationMethod): def method_name(cls): return 'hipchat' - def validate(self, repository, config_data): + def validate(self, namespace_name, repository_name, config_data): if not config_data.get('notification_token', ''): raise CannotValidateNotificationMethodException('Missing Hipchat Room Notification Token') @@ -385,7 +384,7 @@ class SlackMethod(NotificationMethod): def method_name(cls): return 'slack' - def validate(self, repository, config_data): + def validate(self, namespace_name, repository_name, config_data): if not config_data.get('url', ''): raise CannotValidateNotificationMethodException('Missing Slack Callback URL')