2014-07-18 02:51:58 +00:00
|
|
|
import logging
|
|
|
|
|
2014-07-29 17:39:26 +00:00
|
|
|
from notificationhelper import build_event_data
|
2014-10-22 23:01:56 +00:00
|
|
|
from util.jinjautil import get_template_env
|
2014-07-29 17:39:26 +00:00
|
|
|
|
2014-10-22 23:01:56 +00:00
|
|
|
template_env = get_template_env("events")
|
2014-07-18 02:51:58 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
class InvalidNotificationEventException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class NotificationEvent(object):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2014-08-19 21:40:36 +00:00
|
|
|
def get_level(self, event_data, notification_data):
|
|
|
|
"""
|
|
|
|
Returns a 'level' representing the severity of the event.
|
2014-10-22 23:01:56 +00:00
|
|
|
Valid values are: 'info', 'warning', 'error', 'primary', 'success'
|
2014-08-19 21:40:36 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
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.
|
|
|
|
"""
|
2014-10-22 23:01:56 +00:00
|
|
|
return template_env.get_template(self.event_name() + '.html').render({
|
|
|
|
'event_data': event_data,
|
|
|
|
'notification_data': notification_data
|
|
|
|
})
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
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-08-19 21:40:36 +00:00
|
|
|
def get_level(self, event_data, notification_data):
|
2014-10-22 23:01:56 +00:00
|
|
|
return 'primary'
|
2014-08-19 21:40:36 +00:00
|
|
|
|
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_sample_data(self, repository):
|
2014-07-29 17:39:26 +00:00
|
|
|
return build_event_data(repository, {
|
2014-08-05 21:45:40 +00:00
|
|
|
'updated_tags': {'latest': 'someimageid', 'foo': 'anotherimage'},
|
2014-07-18 02:51:58 +00:00
|
|
|
'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'
|
2014-08-19 21:40:36 +00:00
|
|
|
|
|
|
|
def get_level(self, event_data, notification_data):
|
|
|
|
return 'info'
|
2014-11-24 21:07:38 +00:00
|
|
|
|
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
|
|
|
'is_manual': False,
|
|
|
|
'build_id': build_uuid,
|
|
|
|
'build_name': 'some-fake-build',
|
|
|
|
'docker_tags': ['latest', 'foo', 'bar'],
|
2015-02-24 20:13:51 +00:00
|
|
|
'trigger_kind': 'GitHub',
|
|
|
|
'trigger_metadata': {
|
|
|
|
"default_branch": "master",
|
|
|
|
"ref": "refs/heads/somebranch",
|
|
|
|
"commit_sha": "42d4a62c53350993ea41069e9f2cfdefb0df097d"
|
|
|
|
}
|
2014-07-29 17:39:26 +00:00
|
|
|
}, subpage='/build?current=%s' % build_uuid)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_summary(self, event_data, notification_data):
|
2014-11-24 21:07:38 +00:00
|
|
|
return 'Build queued for repository %s' % (event_data['repository'])
|
2014-07-18 19:58:18 +00:00
|
|
|
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
class BuildStartEvent(NotificationEvent):
|
|
|
|
@classmethod
|
|
|
|
def event_name(cls):
|
|
|
|
return 'build_start'
|
|
|
|
|
2014-08-19 21:40:36 +00:00
|
|
|
def get_level(self, event_data, notification_data):
|
|
|
|
return 'info'
|
|
|
|
|
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'],
|
2015-02-24 20:13:51 +00:00
|
|
|
'trigger_kind': 'GitHub',
|
|
|
|
'trigger_metadata': {
|
|
|
|
"default_branch": "master",
|
|
|
|
"ref": "refs/heads/somebranch",
|
|
|
|
"commit_sha": "42d4a62c53350993ea41069e9f2cfdefb0df097d"
|
|
|
|
}
|
2014-07-29 17:39:26 +00:00
|
|
|
}, subpage='/build?current=%s' % build_uuid)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
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'])
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
class BuildSuccessEvent(NotificationEvent):
|
|
|
|
@classmethod
|
|
|
|
def event_name(cls):
|
|
|
|
return 'build_success'
|
|
|
|
|
2014-08-19 21:40:36 +00:00
|
|
|
def get_level(self, event_data, notification_data):
|
2014-10-22 23:01:56 +00:00
|
|
|
return 'success'
|
2014-08-19 21:40:36 +00:00
|
|
|
|
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'],
|
2015-02-24 20:13:51 +00:00
|
|
|
'trigger_kind': 'GitHub',
|
|
|
|
'trigger_metadata': {
|
|
|
|
"default_branch": "master",
|
|
|
|
"ref": "refs/heads/somebranch",
|
|
|
|
"commit_sha": "42d4a62c53350993ea41069e9f2cfdefb0df097d"
|
|
|
|
},
|
|
|
|
'image_id': '1245657346'
|
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'])
|
|
|
|
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
class BuildFailureEvent(NotificationEvent):
|
|
|
|
@classmethod
|
|
|
|
def event_name(cls):
|
|
|
|
return 'build_failure'
|
|
|
|
|
2014-08-19 21:40:36 +00:00
|
|
|
def get_level(self, event_data, notification_data):
|
|
|
|
return 'error'
|
|
|
|
|
2014-07-18 19:58:18 +00:00
|
|
|
def get_sample_data(self, repository):
|
2014-08-19 18:33:33 +00:00
|
|
|
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',
|
2015-02-24 20:13:51 +00:00
|
|
|
'error_message': 'This is a fake error message',
|
|
|
|
'trigger_metadata': {
|
|
|
|
"default_branch": "master",
|
|
|
|
"ref": "refs/heads/somebranch",
|
|
|
|
"commit_sha": "42d4a62c53350993ea41069e9f2cfdefb0df097d"
|
|
|
|
}
|
2014-07-29 17:39:26 +00:00
|
|
|
}, subpage='/build?current=%s' % build_uuid)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
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'])
|
|
|
|
|