108 lines
2.8 KiB
Python
108 lines
2.8 KiB
Python
import logging
|
|
import io
|
|
import os.path
|
|
import tarfile
|
|
import base64
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class InvalidNotificationEventException(Exception):
|
|
pass
|
|
|
|
class NotificationEvent(object):
|
|
def __init__(self):
|
|
pass
|
|
|
|
def get_summary(self, notification_data):
|
|
"""
|
|
Returns a human readable one-line summary for the given notification data.
|
|
"""
|
|
raise NotImplementedError
|
|
|
|
def get_message(self, notification_data):
|
|
"""
|
|
Returns a human readable HTML message for the given notification data.
|
|
"""
|
|
raise NotImplementedError
|
|
|
|
def get_sample_data(self, repository=None):
|
|
"""
|
|
Returns sample data for testing the raising of this notification, with an optional
|
|
repository.
|
|
"""
|
|
raise NotImplementedError
|
|
|
|
@classmethod
|
|
def event_name(cls):
|
|
"""
|
|
Particular event implemented by subclasses.
|
|
"""
|
|
raise NotImplementedError
|
|
|
|
@classmethod
|
|
def get_event(cls, eventname):
|
|
for subc in cls.__subclasses__():
|
|
if subc.event_name() == eventname:
|
|
return subc()
|
|
|
|
raise InvalidNotificationEventException('Unable to find event: %s' % eventname)
|
|
|
|
|
|
class RepoPushEvent(NotificationEvent):
|
|
@classmethod
|
|
def event_name(cls):
|
|
return 'repo_push'
|
|
|
|
def get_summary(self, notification_data):
|
|
return 'Repository %s updated' % (event_data['repository'])
|
|
|
|
def get_message(self, notification_data):
|
|
event_data = notification_data['event_data']
|
|
if not event_data['tags']:
|
|
return '%s images pushed for repository %s (%s)' % (event_data['pushed_image_count'],
|
|
event_data['repository'], event_data['homepage'])
|
|
|
|
return 'Tags %s updated for repository %s (%s)' % (event_data['updated_tags'],
|
|
event_data['repository'], event_data['homepage'])
|
|
|
|
def get_sample_data(self, repository=None):
|
|
repo_string = '%s/%s' % (repository.namespace, repository.name)
|
|
event_data = {
|
|
'repository': repo_string,
|
|
'namespace': repository.namespace,
|
|
'name': repository.name,
|
|
'docker_url': 'quay.io/%s' % repo_string,
|
|
'homepage': 'https://quay.io/repository/%s' % repo_string,
|
|
'visibility': repository.visibility.name,
|
|
'updated_tags': ['latest', 'foo', 'bar'],
|
|
'pushed_image_count': 10,
|
|
'pruned_image_count': 3
|
|
}
|
|
return event_data
|
|
|
|
|
|
class BuildStartEvent(NotificationEvent):
|
|
@classmethod
|
|
def event_name(cls):
|
|
return 'build_start'
|
|
|
|
def get_sample_data(repository=None):
|
|
pass
|
|
|
|
|
|
class BuildSuccessEvent(NotificationEvent):
|
|
@classmethod
|
|
def event_name(cls):
|
|
return 'build_success'
|
|
|
|
def get_sample_data(repository=None):
|
|
pass
|
|
|
|
|
|
class BuildFailureEvent(NotificationEvent):
|
|
@classmethod
|
|
def event_name(cls):
|
|
return 'build_failure'
|
|
|
|
def get_sample_data(repository=None):
|
|
pass
|