Merge pull request #2788 from coreos-inc/fix-notifications

Fix notification system to use the new tuple correctly
This commit is contained in:
josephschorr 2017-07-14 00:26:16 +03:00 committed by GitHub
commit 2f750bfc87
5 changed files with 31 additions and 72 deletions

View file

@ -15,6 +15,8 @@ from endpoints.notificationmethod import (NotificationMethod,
CannotValidateNotificationMethodException)
from endpoints.notificationhelper import build_notification_data
from data import model
from workers.notificationworker.models_pre_oci import notification
logger = logging.getLogger(__name__)
def notification_view(note):
@ -191,7 +193,11 @@ class TestRepositoryNotification(RepositoryParamResource):
raise NotFound()
event_info = NotificationEvent.get_event(test_note.event.name)
sample_data = event_info.get_sample_data(test_note)
# TODO(jschorr): Stop depending on the worker module's data interface and instead only depend
# on the notification's data interface (to be added).
sample_data = event_info.get_sample_data(notification(test_note))
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))

View file

@ -92,13 +92,7 @@ class RepoPushEvent(NotificationEvent):
return 'Repository %s updated' % (event_data['repository'])
def get_sample_data(self, notification):
# TODO(jzelinskie): remove when more endpoints have been converted to using
# interfaces
repo = AttrDict({
'namespace_name': notification.repository.namespace_user.username,
'name': notification.repository.name,
})
return build_event_data(repo, {
return build_event_data(notification.repository, {
'updated_tags': {'latest': 'someimageid', 'foo': 'anotherimage'},
'pruned_image_count': 3
})
@ -133,16 +127,8 @@ class VulnerabilityFoundEvent(NotificationEvent):
def get_sample_data(self, notification):
event_config = notification.event_config_dict
# TODO(jzelinskie): remove when more endpoints have been converted to using
# interfaces
repo = AttrDict({
'namespace_name': notification.repository.namespace_user.username,
'name': notification.repository.name,
})
level = event_config.get(VulnerabilityFoundEvent.CONFIG_LEVEL, 'Critical')
return build_event_data(repo, {
return build_event_data(notification.repository, {
'tags': ['latest', 'prod', 'foo', 'bar', 'baz'],
'image': 'some-image-id',
'vulnerability': {
@ -229,14 +215,7 @@ class BuildQueueEvent(BaseBuildEvent):
def get_sample_data(self, notification):
build_uuid = 'fake-build-id'
# TODO(jzelinskie): remove when more endpoints have been converted to using
# interfaces
repo = AttrDict({
'namespace_name': notification.repository.namespace_user.username,
'name': notification.repository.name,
})
return build_event_data(repo, {
return build_event_data(notification.repository, {
'is_manual': False,
'build_id': build_uuid,
'build_name': 'some-fake-build',
@ -274,14 +253,7 @@ class BuildStartEvent(BaseBuildEvent):
def get_sample_data(self, notification):
build_uuid = 'fake-build-id'
# TODO(jzelinskie): remove when more endpoints have been converted to using
# interfaces
repo = AttrDict({
'namespace_name': notification.repository.namespace_user.username,
'name': notification.repository.name,
})
return build_event_data(repo, {
return build_event_data(notification.repository, {
'build_id': build_uuid,
'build_name': 'some-fake-build',
'docker_tags': ['latest', 'foo', 'bar'],
@ -308,14 +280,7 @@ class BuildSuccessEvent(BaseBuildEvent):
def get_sample_data(self, notification):
build_uuid = 'fake-build-id'
# TODO(jzelinskie): remove when more endpoints have been converted to using
# interfaces
repo = AttrDict({
'namespace_name': notification.repository.namespace_user.username,
'name': notification.repository.name,
})
return build_event_data(repo, {
return build_event_data(notification.repository, {
'build_id': build_uuid,
'build_name': 'some-fake-build',
'docker_tags': ['latest', 'foo', 'bar'],
@ -343,14 +308,7 @@ class BuildFailureEvent(BaseBuildEvent):
def get_sample_data(self, notification):
build_uuid = 'fake-build-id'
# TODO(jzelinskie): remove when more endpoints have been converted to using
# interfaces
repo = AttrDict({
'namespace_name': notification.repository.namespace_user.username,
'name': notification.repository.name,
})
return build_event_data(repo, {
return build_event_data(notification.repository, {
'build_id': build_uuid,
'build_name': 'some-fake-build',
'docker_tags': ['latest', 'foo', 'bar'],
@ -389,14 +347,7 @@ class BuildCancelledEvent(BaseBuildEvent):
def get_sample_data(self, notification):
build_uuid = 'fake-build-id'
# TODO(jzelinskie): remove when more endpoints have been converted to using
# interfaces
repo = AttrDict({
'namespace_name': notification.repository.namespace_user.username,
'name': notification.repository.name,
})
return build_event_data(repo, {
return build_event_data(notification.repository, {
'build_id': build_uuid,
'build_name': 'some-fake-build',
'docker_tags': ['latest', 'foo', 'bar'],

View file

@ -98,7 +98,7 @@ class QuayNotificationMethod(NotificationMethod):
return (True, 'Unknown organization %s' % target_info['name'], None)
# Only repositories under the organization can cause notifications to that org.
if target_info['name'] != repository.namespace_user.username:
if target_info['name'] != repository.namespace_name:
return (False, 'Organization name must match repository namespace')
return (True, None, [target])
@ -106,8 +106,7 @@ class QuayNotificationMethod(NotificationMethod):
# Lookup the team.
org_team = None
try:
org_team = model.team.get_organization_team(repository.namespace_user.username,
target_info['name'])
org_team = model.team.get_organization_team(repository.namespace_name, target_info['name'])
except model.InvalidTeamException:
# Probably deleted.
return (True, 'Unknown team %s' % target_info['name'], None)
@ -143,7 +142,7 @@ class EmailMethod(NotificationMethod):
if not email:
raise CannotValidateNotificationMethodException('Missing e-mail address')
record = model.repository.get_email_authorized_for_repo(repository.namespace_user.username,
record = model.repository.get_email_authorized_for_repo(repository.namespace_name,
repository.name, email)
if not record or not record.confirmed:
raise CannotValidateNotificationMethodException('The specified e-mail address '
@ -221,7 +220,7 @@ class FlowdockMethod(NotificationMethod):
if not token:
return
owner = model.user.get_user_or_org(notification_obj.repository.namespace_user.username)
owner = model.user.get_user_or_org(notification_obj.repository.namespace_name)
if not owner:
# Something went wrong.
return
@ -234,7 +233,7 @@ class FlowdockMethod(NotificationMethod):
'subject': event_handler.get_summary(notification_data['event_data'], notification_data),
'content': event_handler.get_message(notification_data['event_data'], notification_data),
'from_name': owner.username,
'project': (notification_obj.repository.namespace_user.username + ' ' +
'project': (notification_obj.repository.namespace_name + ' ' +
notification_obj.repository.name),
'tags': ['#' + event_handler.event_name()],
'link': notification_data['event_data']['homepage']
@ -277,7 +276,7 @@ class HipchatMethod(NotificationMethod):
if not token or not room_id:
return
owner = model.user.get_user_or_org(notification_obj.repository.namespace_user.username)
owner = model.user.get_user_or_org(notification_obj.repository.namespace_name)
if not owner:
# Something went wrong.
return
@ -389,7 +388,7 @@ class SlackMethod(NotificationMethod):
if not url:
return
owner = model.user.get_user_or_org(notification_obj.repository.namespace_user.username)
owner = model.user.get_user_or_org(notification_obj.repository.namespace_name)
if not owner:
# Something went wrong.
return

View file

@ -9,7 +9,7 @@ def test_all_notifications(app):
# Create a test notification.
test_notification = AttrDict({
'repository': AttrDict({
'namespace_user': AttrDict(dict(username='foo')),
'namespace_name': AttrDict(dict(username='foo')),
'name': 'bar',
}),
'event_config_dict': {

View file

@ -4,6 +4,14 @@ from data import model
from workers.notificationworker.models_interface import (
NotificationWorkerDataInterface, Notification, Repository)
def notification(notification_row):
""" Converts the given notification row into a notification tuple. """
return Notification(uuid=notification_row.uuid, event_name=notification_row.event.name,
method_name=notification_row.method.name,
event_config_dict=json.loads(notification_row.event_config_json),
method_config_dict=json.loads(notification_row.config_json),
repository=Repository(notification_row.repository.namespace_user.username,
notification_row.repository.name))
class PreOCIModel(NotificationWorkerDataInterface):
def get_enabled_notification(self, notification_uuid):
@ -12,12 +20,7 @@ class PreOCIModel(NotificationWorkerDataInterface):
except model.InvalidNotificationException:
return None
return Notification(uuid=notification_uuid, event_name=notification_row.event.name,
method_name=notification_row.method.name,
event_config_dict=json.loads(notification_row.event_config_json),
method_config_dict=json.loads(notification_row.config_json),
repository=Repository(notification_row.repository.namespace_user.username,
notification_row.repository.name))
return notification(notification_row)
def reset_number_of_failures_to_zero(self, notification):
model.notification.reset_notification_number_of_failures(