diff --git a/endpoints/notificationevent.py b/endpoints/notificationevent.py index dc0491e27..8d720d986 100644 --- a/endpoints/notificationevent.py +++ b/endpoints/notificationevent.py @@ -61,13 +61,24 @@ class NotificationEvent(object): @classmethod def get_event(cls, eventname): - for subc in cls.__subclasses__(): - if subc.event_name() == eventname: - return subc() + found = NotificationEvent._get_event(cls, eventname) + if found is not None: + return found raise InvalidNotificationEventException('Unable to find event: %s' % eventname) + @staticmethod + def _get_event(cls, eventname): + for subc in cls.__subclasses__(): + if subc.event_name() is None: + found = NotificationEvent._get_event(subc, eventname) + if found is not None: + return found + elif subc.event_name() == eventname: + return subc() + + class RepoPushEvent(NotificationEvent): @classmethod def event_name(cls): @@ -140,6 +151,10 @@ class VulnerabilityFoundEvent(NotificationEvent): class BaseBuildEvent(NotificationEvent): + @classmethod + def event_name(cls): + return None + def should_perform(self, event_data, notification_data): event_config = json.loads(notification_data.event_config_json) ref_regex = event_config.get('ref-regex') or None diff --git a/test/test_notifications.py b/test/test_notifications.py index 3da728912..301fcea26 100644 --- a/test/test_notifications.py +++ b/test/test_notifications.py @@ -1,8 +1,17 @@ import unittest -from endpoints.notificationevent import BuildSuccessEvent +from endpoints.notificationevent import BuildSuccessEvent, NotificationEvent from util.morecollections import AttrDict +class TestCreate(unittest.TestCase): + def test_create_notifications(self): + self.assertIsNotNone(NotificationEvent.get_event('repo_push')) + self.assertIsNotNone(NotificationEvent.get_event('build_queued')) + self.assertIsNotNone(NotificationEvent.get_event('build_success')) + self.assertIsNotNone(NotificationEvent.get_event('build_failure')) + self.assertIsNotNone(NotificationEvent.get_event('build_start')) + + class TestShouldPerform(unittest.TestCase): def test_build_nofilter(self): notification_data = AttrDict({