Don't use repository object anywhere in endpoints/api/repositorynotification

Also adds support for audit logging with repo name only
This commit is contained in:
Evan Cordell 2017-07-17 17:55:00 -04:00
parent 047722b295
commit 9dad44e93d
5 changed files with 36 additions and 41 deletions

View file

@ -349,7 +349,12 @@ def request_error(exception=None, **kwargs):
def log_action(kind, user_or_orgname, metadata=None, repo=None, repo_name=None): def log_action(kind, user_or_orgname, metadata=None, repo=None, repo_name=None):
if not metadata: if not metadata:
metadata = {} metadata = {}
if repo_name:
repository = model.repository.get_repository(user_or_orgname, repo_name)
else:
repository = repo
oauth_token = get_validated_oauth_token() oauth_token = get_validated_oauth_token()
if oauth_token: if oauth_token:
metadata['oauth_token_id'] = oauth_token.id metadata['oauth_token_id'] = oauth_token.id

View file

@ -61,16 +61,15 @@ class RepositoryNotificationList(RepositoryParamResource):
@validate_json_request('NotificationCreateRequest') @validate_json_request('NotificationCreateRequest')
def post(self, namespace_name, repository_name): def post(self, namespace_name, repository_name):
parsed = request.get_json() parsed = request.get_json()
repository = model.get_repository(namespace_name, repository_name)
method_handler = NotificationMethod.get_method(parsed['method']) method_handler = NotificationMethod.get_method(parsed['method'])
try: try:
method_handler.validate(repository, parsed['config']) method_handler.validate(namespace_name, repository_name, parsed['config'])
except CannotValidateNotificationMethodException as ex: except CannotValidateNotificationMethodException as ex:
raise request_error(message=ex.message) 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['event'],
parsed['method'], parsed['method'],
parsed['config'], parsed['config'],
@ -81,7 +80,7 @@ class RepositoryNotificationList(RepositoryParamResource):
{'repo': repository_name, 'namespace': namespace_name, {'repo': repository_name, 'namespace': namespace_name,
'notification_id': new_notification.uuid, 'notification_id': new_notification.uuid,
'event': parsed['event'], 'method': parsed['method']}, 'event': parsed['event'], 'method': parsed['method']},
repo=repository) repo_name=repository_name)
return new_notification.to_dict(), 201 return new_notification.to_dict(), 201
@require_repo_admin @require_repo_admin
@ -117,12 +116,11 @@ class RepositoryNotification(RepositoryParamResource):
@disallow_for_app_repositories @disallow_for_app_repositories
def delete(self, namespace_name, repository_name, uuid): def delete(self, namespace_name, repository_name, uuid):
""" Deletes the specified notification. """ """ Deletes the specified notification. """
repository = model.get_repository(namespace_name, repository_name) deleted = model.delete_repo_notification(namespace_name, repository_name, uuid)
deleted = model.delete_repo_notification(repository, uuid)
log_action('delete_repo_notification', namespace_name, log_action('delete_repo_notification', namespace_name,
{'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid, {'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid,
'event': deleted.event.name, 'method': deleted.method.name}, 'event': deleted.event.name, 'method': deleted.method.name},
repo=repository) repo_name=repository_name)
return 'No Content', 204 return 'No Content', 204
@ -131,13 +129,12 @@ class RepositoryNotification(RepositoryParamResource):
@disallow_for_app_repositories @disallow_for_app_repositories
def post(self, namespace_name, repository_name, uuid): def post(self, namespace_name, repository_name, uuid):
""" Resets repository notification to 0 failures. """ """ Resets repository notification to 0 failures. """
repository = model.get_repository(namespace_name, repository_name) reset = model.reset_notification_number_of_failures(namespace_name, repository_name, uuid)
reset = model.reset_notification_number_of_failures(repository, uuid)
if reset is not None: if reset is not None:
log_action('reset_repo_notification', namespace_name, log_action('reset_repo_notification', namespace_name,
{'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid, {'repo': repository_name, 'namespace': namespace_name, 'notification_id': uuid,
'event': reset.event.name, 'method': reset.method.name}, 'event': reset.event.name, 'method': reset.method.name},
repo=repository) repo_name=repository_name)
return 'No Content', 204 return 'No Content', 204

View file

@ -55,13 +55,9 @@ class RepoNotificationInterface(object):
""" """
Interface that represents all data store interactions required by the RepositoryNotification API Interface that represents all data store interactions required by the RepositoryNotification API
""" """
@abstractmethod
def get_repository(self, namespace_name, repository_name):
pass
@abstractmethod @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 pass
@abstractmethod @abstractmethod
@ -73,11 +69,11 @@ class RepoNotificationInterface(object):
pass pass
@abstractmethod @abstractmethod
def delete_repo_notification(self, repository, uuid): def delete_repo_notification(self, namespace_name, repository_name, uuid):
pass pass
@abstractmethod @abstractmethod
def reset_notification_number_of_failures(self, repository, uuid): def reset_notification_number_of_failures(self, namespace_name, repository_name, uuid):
pass pass
@abstractmethod @abstractmethod

View file

@ -9,16 +9,14 @@ from endpoints.notificationhelper import build_notification_data
class PreOCIModel(RepoNotificationInterface): class PreOCIModel(RepoNotificationInterface):
def get_repository(self, namespace_name, repository_name): def create_repo_notification(self, namespace_name, repository_name, event_name, method_name, method_config, event_config, title=None):
return self._notification(model.repository.get_repository(namespace_name, repository_name)) repository = model.repository.get_repository(namespace_name, repository_name)
def create_repo_notification(self, repository, event_name, method_name, method_config, event_config, title=None):
return self._notification(model.notification.create_repo_notification(repository, return self._notification(model.notification.create_repo_notification(repository,
event_name, event_name,
method_name, method_name,
method_config, method_config,
event_config, event_config,
title)) title))
def list_repo_notifications(self, namespace_name, repository_name, event_name=None): def list_repo_notifications(self, namespace_name, repository_name, event_name=None):
return [self._notification(n) return [self._notification(n)
@ -27,13 +25,13 @@ class PreOCIModel(RepoNotificationInterface):
def get_repo_notification(self, uuid): def get_repo_notification(self, uuid):
return self._notification(model.notification.get_repo_notification(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( 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( 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): def queue_test_notification(self, notification):
event_info = NotificationEvent.get_event(notification.event.name) event_info = NotificationEvent.get_event(notification.event.name)

View file

@ -56,7 +56,7 @@ class NotificationMethod(object):
""" """
raise NotImplementedError 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 Validates that the notification can be created with the given data. Throws
a CannotValidateNotificationMethodException on failure. a CannotValidateNotificationMethodException on failure.
@ -88,12 +88,12 @@ class QuayNotificationMethod(NotificationMethod):
def method_name(cls): def method_name(cls):
return 'quay_notification' return 'quay_notification'
def validate(self, repository, config_data): def validate(self, namespace_name, repository_name, config_data):
status, err_message, target_users = self.find_targets(repository, config_data) status, err_message, target_users = self.find_targets(namespace_name, repository_name, 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, namespace_name, repository_name, config_data):
target_info = config_data['target'] target_info = config_data['target']
if target_info['kind'] == 'user': if target_info['kind'] == 'user':
@ -149,12 +149,11 @@ class EmailMethod(NotificationMethod):
def method_name(cls): def method_name(cls):
return 'email' return 'email'
def validate(self, repository, config_data): def validate(self, namespace_name, repository_name, config_data):
email = config_data.get('email', '') email = config_data.get('email', '')
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(_get_namespace_name_from(repository), 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:
@ -185,7 +184,7 @@ class WebhookMethod(NotificationMethod):
def method_name(cls): def method_name(cls):
return 'webhook' return 'webhook'
def validate(self, repository, config_data): def validate(self, namespace_name, repository_name, config_data):
url = config_data.get('url', '') url = config_data.get('url', '')
if not url: if not url:
raise CannotValidateNotificationMethodException('Missing webhook URL') raise CannotValidateNotificationMethodException('Missing webhook URL')
@ -222,7 +221,7 @@ class FlowdockMethod(NotificationMethod):
def method_name(cls): def method_name(cls):
return 'flowdock' return 'flowdock'
def validate(self, repository, config_data): def validate(self, namespace_name, repository_name, config_data):
token = config_data.get('flow_api_token', '') token = config_data.get('flow_api_token', '')
if not token: if not token:
raise CannotValidateNotificationMethodException('Missing Flowdock API Token') raise CannotValidateNotificationMethodException('Missing Flowdock API Token')
@ -274,7 +273,7 @@ class HipchatMethod(NotificationMethod):
def method_name(cls): def method_name(cls):
return 'hipchat' return 'hipchat'
def validate(self, repository, config_data): def validate(self, namespace_name, repository_name, config_data):
if not config_data.get('notification_token', ''): if not config_data.get('notification_token', ''):
raise CannotValidateNotificationMethodException('Missing Hipchat Room Notification Token') raise CannotValidateNotificationMethodException('Missing Hipchat Room Notification Token')
@ -385,7 +384,7 @@ class SlackMethod(NotificationMethod):
def method_name(cls): def method_name(cls):
return 'slack' return 'slack'
def validate(self, repository, config_data): def validate(self, namespace_name, repository_name, config_data):
if not config_data.get('url', ''): if not config_data.get('url', ''):
raise CannotValidateNotificationMethodException('Missing Slack Callback URL') raise CannotValidateNotificationMethodException('Missing Slack Callback URL')