2017-07-17 21:53:08 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from app import notification_queue
|
|
|
|
from data import model
|
2017-07-17 21:56:32 +00:00
|
|
|
from data.model import InvalidNotificationException
|
2017-07-17 21:53:08 +00:00
|
|
|
from endpoints.api.repositorynotification_models_interface import RepoNotificationInterface, RepositoryNotification
|
|
|
|
from endpoints.notificationevent import NotificationEvent
|
|
|
|
from endpoints.notificationhelper import build_notification_data
|
|
|
|
|
|
|
|
|
2017-07-17 21:56:32 +00:00
|
|
|
class RepoNotificationPreOCIModel(RepoNotificationInterface):
|
2017-07-17 21:53:08 +00:00
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
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)
|
2017-07-17 21:53:08 +00:00
|
|
|
return self._notification(model.notification.create_repo_notification(repository,
|
2017-07-17 21:55:00 +00:00
|
|
|
event_name,
|
|
|
|
method_name,
|
|
|
|
method_config,
|
|
|
|
event_config,
|
|
|
|
title))
|
2017-07-17 21:53:08 +00:00
|
|
|
|
|
|
|
def list_repo_notifications(self, namespace_name, repository_name, event_name=None):
|
|
|
|
return [self._notification(n)
|
|
|
|
for n in model.notification.list_repo_notifications(namespace_name, repository_name, event_name)]
|
|
|
|
|
|
|
|
def get_repo_notification(self, uuid):
|
2017-07-17 21:56:32 +00:00
|
|
|
try:
|
|
|
|
found = model.notification.get_repo_notification(uuid)
|
|
|
|
except InvalidNotificationException:
|
|
|
|
return None
|
|
|
|
return self._notification(found)
|
2017-07-17 21:53:08 +00:00
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def delete_repo_notification(self, namespace_name, repository_name, uuid):
|
2017-07-17 21:56:32 +00:00
|
|
|
try:
|
|
|
|
found = model.notification.delete_repo_notification(namespace_name, repository_name, uuid)
|
|
|
|
except InvalidNotificationException:
|
|
|
|
return None
|
|
|
|
return self._notification(found)
|
2017-07-17 21:53:08 +00:00
|
|
|
|
2017-07-17 21:55:00 +00:00
|
|
|
def reset_notification_number_of_failures(self, namespace_name, repository_name, uuid):
|
2017-07-17 21:53:08 +00:00
|
|
|
return self._notification(
|
2017-07-17 21:55:00 +00:00
|
|
|
model.notification.reset_notification_number_of_failures(namespace_name, repository_name, uuid))
|
2017-07-17 21:53:08 +00:00
|
|
|
|
2017-07-17 21:56:32 +00:00
|
|
|
def queue_test_notification(self, uuid):
|
|
|
|
try:
|
|
|
|
notification = model.notification.get_repo_notification(uuid)
|
|
|
|
except InvalidNotificationException:
|
|
|
|
return None
|
|
|
|
|
2017-07-17 21:53:08 +00:00
|
|
|
event_info = NotificationEvent.get_event(notification.event.name)
|
|
|
|
sample_data = event_info.get_sample_data(notification)
|
|
|
|
notification_data = build_notification_data(notification, sample_data)
|
2017-07-17 21:56:32 +00:00
|
|
|
notification_queue.put([notification.repository.namespace_user.username, notification.uuid,
|
2017-07-17 21:53:08 +00:00
|
|
|
notification.event.name], json.dumps(notification_data))
|
2017-07-17 21:56:32 +00:00
|
|
|
return self._notification(notification)
|
2017-07-17 21:53:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _notification(self, notification):
|
2017-07-17 21:56:32 +00:00
|
|
|
if not notification:
|
|
|
|
return None
|
2017-07-17 21:53:08 +00:00
|
|
|
return RepositoryNotification(uuid=notification.uuid,
|
|
|
|
title=notification.title,
|
|
|
|
event_name=notification.event.name,
|
|
|
|
method_name=notification.method.name,
|
|
|
|
config_json=notification.config_json,
|
|
|
|
event_config_json=notification.event_config_json,
|
|
|
|
number_of_failures=notification.number_of_failures)
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-17 21:56:32 +00:00
|
|
|
pre_oci_model = RepoNotificationPreOCIModel()
|