2014-07-18 02:51:58 +00:00
|
|
|
import logging
|
|
|
|
import io
|
|
|
|
import os.path
|
|
|
|
import tarfile
|
|
|
|
import base64
|
|
|
|
|
2014-07-29 17:39:26 +00:00
|
|
|
from notificationhelper import build_event_data
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class InvalidNotificationEventException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NotificationEvent(object):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_summary(self, event_data, notification_data):
|
2014-07-18 02:51:58 +00:00
|
|
|
"""
|
|
|
|
Returns a human readable one-line summary for the given notification data.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_message(self, event_data, notification_data):
|
2014-07-18 02:51:58 +00:00
|
|
|
"""
|
|
|
|
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'
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_summary(self, event_data, notification_data):
|
|
|
|
return 'Repository %s updated' % (event_data['repository'])
|
2014-07-18 02:51:58 +00:00
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_message(self, event_data, notification_data):
|
2014-07-18 15:52:36 +00:00
|
|
|
if not event_data.get('updated_tags', []):
|
2014-07-18 19:58:18 +00:00
|
|
|
html = """
|
|
|
|
Repository <a href="%s">%s</a> has been updated via a push.
|
|
|
|
""" % (event_data['homepage'],
|
|
|
|
event_data['repository'])
|
|
|
|
else:
|
|
|
|
html = """
|
|
|
|
Repository <a href="%s">%s</a> has been updated via a push.
|
|
|
|
<br><br>
|
|
|
|
Tags Updated: %s
|
|
|
|
""" % (event_data['homepage'],
|
|
|
|
event_data['repository'],
|
2014-07-31 17:30:54 +00:00
|
|
|
', '.join(event_data['updated_tags']))
|
2014-07-18 19:58:18 +00:00
|
|
|
|
|
|
|
return html
|
|
|
|
|
|
|
|
def get_sample_data(self, repository):
|
2014-07-29 17:39:26 +00:00
|
|
|
return build_event_data(repository, {
|
2014-07-18 02:51:58 +00:00
|
|
|
'updated_tags': ['latest', 'foo', 'bar'],
|
|
|
|
'pushed_image_count': 10,
|
|
|
|
'pruned_image_count': 3
|
2014-07-29 17:39:26 +00:00
|
|
|
})
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
class BuildQueueEvent(NotificationEvent):
|
|
|
|
@classmethod
|
|
|
|
def event_name(cls):
|
|
|
|
return 'build_queued'
|
|
|
|
|
|
|
|
def get_sample_data(self, repository):
|
|
|
|
build_uuid = 'fake-build-id'
|
2014-07-29 17:39:26 +00:00
|
|
|
|
|
|
|
return build_event_data(repository, {
|
2014-07-18 19:58:18 +00:00
|
|
|
'is_manual': False,
|
|
|
|
'build_id': build_uuid,
|
|
|
|
'build_name': 'some-fake-build',
|
|
|
|
'docker_tags': ['latest', 'foo', 'bar'],
|
|
|
|
'trigger_kind': 'GitHub'
|
2014-07-29 17:39:26 +00:00
|
|
|
}, subpage='/build?current=%s' % build_uuid)
|
2014-07-18 19:58:18 +00:00
|
|
|
|
|
|
|
def get_summary(self, event_data, notification_data):
|
|
|
|
return 'Build queued for repository %s' % (event_data['repository'])
|
|
|
|
|
|
|
|
def get_message(self, event_data, notification_data):
|
|
|
|
is_manual = event_data['is_manual']
|
|
|
|
if is_manual:
|
|
|
|
html = """
|
|
|
|
A <a href="%s">new build</a> has been manually queued to start on repository %s.
|
|
|
|
<br><br>
|
|
|
|
Build ID: %s
|
|
|
|
""" % (event_data['homepage'], event_data['repository'], event_data['build_id'])
|
|
|
|
else:
|
|
|
|
html = """
|
|
|
|
A <a href="%s">new build</a> has been queued via a %s trigger to start on repository %s.
|
|
|
|
<br><br>
|
|
|
|
Build ID: %s
|
2014-07-29 17:39:26 +00:00
|
|
|
""" % (event_data['homepage'], event_data['trigger_kind'],
|
|
|
|
event_data['repository'], event_data['build_id'])
|
2014-07-18 19:58:18 +00:00
|
|
|
|
|
|
|
return html
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
class BuildStartEvent(NotificationEvent):
|
|
|
|
@classmethod
|
|
|
|
def event_name(cls):
|
|
|
|
return 'build_start'
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_sample_data(self, repository):
|
|
|
|
build_uuid = 'fake-build-id'
|
2014-07-29 17:39:26 +00:00
|
|
|
|
|
|
|
return build_event_data(repository, {
|
2014-07-18 19:58:18 +00:00
|
|
|
'build_id': build_uuid,
|
|
|
|
'build_name': 'some-fake-build',
|
|
|
|
'docker_tags': ['latest', 'foo', 'bar'],
|
|
|
|
'trigger_kind': 'GitHub'
|
2014-07-29 17:39:26 +00:00
|
|
|
}, subpage='/build?current=%s' % build_uuid)
|
2014-07-18 19:58:18 +00:00
|
|
|
|
|
|
|
def get_summary(self, event_data, notification_data):
|
|
|
|
return 'Build started for repository %s' % (event_data['repository'])
|
|
|
|
|
|
|
|
def get_message(self, event_data, notification_data):
|
|
|
|
html = """
|
|
|
|
A <a href="%s">new build</a> has started on repository %s.
|
|
|
|
<br><br>
|
|
|
|
Build ID: %s
|
|
|
|
""" % (event_data['homepage'], event_data['repository'], event_data['build_id'])
|
2014-07-18 02:51:58 +00:00
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
return html
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
class BuildSuccessEvent(NotificationEvent):
|
|
|
|
@classmethod
|
|
|
|
def event_name(cls):
|
|
|
|
return 'build_success'
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_sample_data(self, repository):
|
|
|
|
build_uuid = 'fake-build-id'
|
2014-07-29 17:39:26 +00:00
|
|
|
|
|
|
|
return build_event_data(repository, {
|
2014-07-18 19:58:18 +00:00
|
|
|
'build_id': build_uuid,
|
|
|
|
'build_name': 'some-fake-build',
|
|
|
|
'docker_tags': ['latest', 'foo', 'bar'],
|
|
|
|
'trigger_kind': 'GitHub'
|
2014-07-29 17:39:26 +00:00
|
|
|
}, subpage='/build?current=%s' % build_uuid)
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_summary(self, event_data, notification_data):
|
|
|
|
return 'Build succeeded for repository %s' % (event_data['repository'])
|
|
|
|
|
|
|
|
def get_message(self, event_data, notification_data):
|
|
|
|
html = """
|
|
|
|
A <a href="%s">build</a> has finished on repository %s.
|
|
|
|
<br><br>
|
|
|
|
Build ID: %s
|
|
|
|
""" % (event_data['homepage'], event_data['repository'], event_data['build_id'])
|
|
|
|
|
|
|
|
return html
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BuildFailureEvent(NotificationEvent):
|
|
|
|
@classmethod
|
|
|
|
def event_name(cls):
|
|
|
|
return 'build_failure'
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_sample_data(self, repository):
|
2014-07-29 17:39:26 +00:00
|
|
|
return build_event_data(repository, {
|
2014-07-18 19:58:18 +00:00
|
|
|
'build_id': build_uuid,
|
|
|
|
'build_name': 'some-fake-build',
|
|
|
|
'docker_tags': ['latest', 'foo', 'bar'],
|
|
|
|
'trigger_kind': 'GitHub',
|
|
|
|
'error_message': 'This is a fake error message'
|
2014-07-29 17:39:26 +00:00
|
|
|
}, subpage='/build?current=%s' % build_uuid)
|
2014-07-18 19:58:18 +00:00
|
|
|
|
|
|
|
def get_summary(self, event_data, notification_data):
|
|
|
|
return 'Build failure for repository %s' % (event_data['repository'])
|
|
|
|
|
|
|
|
def get_message(self, event_data, notification_data):
|
|
|
|
html = """
|
|
|
|
A <a href="%s">build</a> has failed on repository %s.
|
|
|
|
<br><br>
|
|
|
|
Reason: %s<br>
|
|
|
|
Build ID: %s<br>
|
|
|
|
""" % (event_data['homepage'], event_data['repository'],
|
|
|
|
event_data['error_message'], event_data['build_id'])
|
|
|
|
|
|
|
|
return html
|