2015-05-14 20:47:38 +00:00
|
|
|
""" List, create and manage repository events/notifications. """
|
|
|
|
|
2014-07-16 20:30:47 +00:00
|
|
|
import json
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
from flask import request
|
2014-07-16 20:30:47 +00:00
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
from app import notification_queue
|
2014-07-16 20:30:47 +00:00
|
|
|
from endpoints.api import (RepositoryParamResource, nickname, resource, require_repo_admin,
|
2015-07-15 21:25:41 +00:00
|
|
|
log_action, validate_json_request, NotFound, request_error,
|
2014-08-19 23:05:28 +00:00
|
|
|
path_param)
|
2014-07-18 02:51:58 +00:00
|
|
|
from endpoints.notificationevent import NotificationEvent
|
2014-07-28 18:58:12 +00:00
|
|
|
from endpoints.notificationmethod import (NotificationMethod,
|
|
|
|
CannotValidateNotificationMethodException)
|
2014-07-31 17:30:54 +00:00
|
|
|
from endpoints.notificationhelper import build_notification_data
|
2014-07-16 20:30:47 +00:00
|
|
|
from data import model
|
|
|
|
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
def notification_view(note):
|
2014-07-16 20:30:47 +00:00
|
|
|
config = {}
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
config = json.loads(note.config_json)
|
2014-07-16 20:30:47 +00:00
|
|
|
except:
|
|
|
|
config = {}
|
|
|
|
|
2015-11-10 20:08:14 +00:00
|
|
|
event_config = {}
|
|
|
|
try:
|
|
|
|
event_config = json.loads(note.event_config_json)
|
|
|
|
except:
|
|
|
|
event_config = {}
|
|
|
|
|
2014-07-16 20:30:47 +00:00
|
|
|
return {
|
2015-07-15 21:25:41 +00:00
|
|
|
'uuid': note.uuid,
|
|
|
|
'event': note.event.name,
|
|
|
|
'method': note.method.name,
|
2015-08-17 20:30:15 +00:00
|
|
|
'config': config,
|
|
|
|
'title': note.title,
|
2015-11-10 20:08:14 +00:00
|
|
|
'event_config': event_config,
|
2014-07-16 20:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/repository/<repopath:repository>/notification/')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-07-18 02:51:58 +00:00
|
|
|
class RepositoryNotificationList(RepositoryParamResource):
|
2014-07-16 20:30:47 +00:00
|
|
|
""" Resource for dealing with listing and creating notifications on a repository. """
|
|
|
|
schemas = {
|
|
|
|
'NotificationCreateRequest': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information for creating a notification on a repository',
|
|
|
|
'required': [
|
|
|
|
'event',
|
|
|
|
'method',
|
|
|
|
'config'
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'event': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The event on which the notification will respond',
|
|
|
|
},
|
|
|
|
'method': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The method of notification (such as email or web callback)',
|
|
|
|
},
|
|
|
|
'config': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'JSON config information for the specific method of notification'
|
2015-08-17 20:30:15 +00:00
|
|
|
},
|
2015-10-13 22:14:52 +00:00
|
|
|
'eventConfig': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'JSON config information for the specific event of notification',
|
|
|
|
},
|
2015-08-17 20:30:15 +00:00
|
|
|
'title': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The human-readable title of the notification',
|
|
|
|
},
|
2014-07-16 20:30:47 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('createRepoNotification')
|
|
|
|
@validate_json_request('NotificationCreateRequest')
|
|
|
|
def post(self, namespace, repository):
|
|
|
|
""" Create a new notification for the specified repository. """
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
|
|
|
parsed = request.get_json()
|
2014-07-16 20:30:47 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
method_handler = NotificationMethod.get_method(parsed['method'])
|
2014-07-28 18:58:12 +00:00
|
|
|
if not method_handler:
|
|
|
|
raise request_error(message='Unknown method')
|
|
|
|
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
method_handler.validate(repo, parsed['config'])
|
2014-07-28 18:58:12 +00:00
|
|
|
except CannotValidateNotificationMethodException as ex:
|
|
|
|
raise request_error(message=ex.message)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
new_notification = model.notification.create_repo_notification(repo, parsed['event'],
|
2015-08-17 20:30:15 +00:00
|
|
|
parsed['method'], parsed['config'],
|
2015-10-13 22:14:52 +00:00
|
|
|
parsed['eventConfig'],
|
2015-08-17 20:30:15 +00:00
|
|
|
parsed.get('title', None))
|
2014-07-16 20:30:47 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
resp = notification_view(new_notification)
|
2014-11-24 21:07:38 +00:00
|
|
|
log_action('add_repo_notification', namespace,
|
2015-07-15 21:25:41 +00:00
|
|
|
{'repo': repository, 'notification_id': new_notification.uuid,
|
|
|
|
'event': parsed['event'], 'method': parsed['method']},
|
2014-07-16 20:30:47 +00:00
|
|
|
repo=repo)
|
|
|
|
return resp, 201
|
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('listRepoNotifications')
|
|
|
|
def get(self, namespace, repository):
|
|
|
|
""" List the notifications for the specified repository. """
|
2015-07-15 21:25:41 +00:00
|
|
|
notifications = model.notification.list_repo_notifications(namespace, repository)
|
2014-07-16 20:30:47 +00:00
|
|
|
return {
|
|
|
|
'notifications': [notification_view(n) for n in notifications]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/repository/<repopath:repository>/notification/<uuid>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('uuid', 'The UUID of the notification')
|
2014-07-18 02:51:58 +00:00
|
|
|
class RepositoryNotification(RepositoryParamResource):
|
2014-07-16 20:30:47 +00:00
|
|
|
""" Resource for dealing with specific notifications. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('getRepoNotification')
|
|
|
|
def get(self, namespace, repository, uuid):
|
|
|
|
""" Get information for the specified notification. """
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
found = model.notification.get_repo_notification(uuid)
|
2014-07-16 20:30:47 +00:00
|
|
|
except model.InvalidNotificationException:
|
|
|
|
raise NotFound()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
if (found.repository.namespace_user.username != namespace or
|
|
|
|
found.repository.name != repository):
|
2014-10-01 18:23:15 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
return notification_view(found)
|
2014-07-16 20:30:47 +00:00
|
|
|
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('deleteRepoNotification')
|
|
|
|
def delete(self, namespace, repository, uuid):
|
|
|
|
""" Deletes the specified notification. """
|
2015-07-15 21:25:41 +00:00
|
|
|
deleted = model.notification.delete_repo_notification(namespace, repository, uuid)
|
2014-11-24 21:07:38 +00:00
|
|
|
log_action('delete_repo_notification', namespace,
|
2014-07-16 20:30:47 +00:00
|
|
|
{'repo': repository, 'notification_id': uuid,
|
2015-07-15 21:25:41 +00:00
|
|
|
'event': deleted.event.name, 'method': deleted.method.name},
|
|
|
|
repo=model.repository.get_repository(namespace, repository))
|
2014-07-16 20:30:47 +00:00
|
|
|
|
|
|
|
return 'No Content', 204
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/repository/<repopath:repository>/notification/<uuid>/test')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('uuid', 'The UUID of the notification')
|
2014-07-18 02:51:58 +00:00
|
|
|
class TestRepositoryNotification(RepositoryParamResource):
|
|
|
|
""" Resource for queuing a test of a notification. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('testRepoNotification')
|
|
|
|
def post(self, namespace, repository, uuid):
|
|
|
|
""" Queues a test notification for this repository. """
|
|
|
|
try:
|
2015-07-15 21:25:41 +00:00
|
|
|
test_note = model.notification.get_repo_notification(uuid)
|
2014-07-18 02:51:58 +00:00
|
|
|
except model.InvalidNotificationException:
|
|
|
|
raise NotFound()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
if (test_note.repository.namespace_user.username != namespace or
|
|
|
|
test_note.repository.name != repository):
|
2014-10-01 18:23:15 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
event_info = NotificationEvent.get_event(test_note.event.name)
|
2015-11-10 20:08:14 +00:00
|
|
|
sample_data = event_info.get_sample_data(test_note)
|
2015-07-15 21:25:41 +00:00
|
|
|
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))
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
return {}
|