# HG changeset patch # User Daniel Dourvaris # Date 2016-07-12 09:14:40 # Node ID b1411a81e7c47f7ae01f11dc8f31c02f7f1984f1 # Parent 367d4e886a56bca92dc3676666388df4e0cd8c13 integrations: fix bug where deleting a repo did not delete integrations diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -1330,6 +1330,8 @@ class Repository(Base, BaseModel): cascade="all, delete, delete-orphan") ui = relationship('RepoRhodeCodeUi', cascade="all") settings = relationship('RepoRhodeCodeSetting', cascade="all") + integrations = relationship('Integration', + cascade="all, delete, delete-orphan") def __unicode__(self): return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id, @@ -3495,6 +3497,11 @@ class Integration(Base, BaseModel): nullable=True, unique=None, default=None) repo = relationship('Repository', lazy='joined') + def __init__(self, **kw): + settings = kw.pop('settings', {}) + self.settings = settings + super(Integration, self).__init__(**kw) + @hybrid_property def settings(self): data = json.loads(self.settings_json or '{}') diff --git a/rhodecode/model/integration.py b/rhodecode/model/integration.py --- a/rhodecode/model/integration.py +++ b/rhodecode/model/integration.py @@ -42,6 +42,7 @@ from rhodecode.model import BaseModel from rhodecode.model.db import Integration, User from rhodecode.model.meta import Session from rhodecode.integrations import integration_type_registry +from rhodecode.integrations.types.base import IntegrationTypeBase log = logging.getLogger(__name__) @@ -60,11 +61,24 @@ class IntegrationModel(BaseModel): raise Exception('integration must be int, long or Instance' ' of Integration got %s' % type(integration)) + def create(self, IntegrationType, enabled, name, settings, repo=None): + """ Create an IntegrationType integration """ + integration = Integration( + integration_type=IntegrationType.key, + settings={}, + repo=repo, + enabled=enabled, + name=name + ) + self.sa.add(integration) + self.sa.commit() + return integration + def delete(self, integration): try: integration = self.__get_integration(integration) if integration: - Session().delete(integration) + self.sa.delete(integration) return True except Exception: log.error(traceback.format_exc()) diff --git a/rhodecode/tests/integrations/test_integration.py b/rhodecode/tests/integrations/test_integration.py new file mode 100644 --- /dev/null +++ b/rhodecode/tests/integrations/test_integration.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2010-2016 RhodeCode GmbH +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License, version 3 +# (only), as published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# This program is dual-licensed. If you wish to learn more about the +# RhodeCode Enterprise Edition, including its added features, Support services, +# and proprietary license terms, please see https://rhodecode.com/licenses/ + +import pytest +import requests +from mock import Mock, patch + +from rhodecode import events +from rhodecode.model.db import Session, Integration +from rhodecode.model.integration import IntegrationModel +from rhodecode.integrations.types.base import IntegrationTypeBase + + +class TestIntegrationType(IntegrationTypeBase): + """ Test integration type class """ + + key = 'test-integration' + display_name = 'Test integration type' + + def __init__(self, settings): + super(IntegrationTypeBase, self).__init__(settings) + self.sent_events = [] # for testing + + def send_event(self, event): + self.sent_events.append(event) + + +@pytest.fixture +def repo_integration_stub(request, repo_stub): + settings = {'test_key': 'test_value'} + integration = IntegrationModel().create( + TestIntegrationType, settings=settings, repo=repo_stub, enabled=True, + name='test repo integration') + + @request.addfinalizer + def cleanup(): + IntegrationModel().delete(integration) + + return integration + + +@pytest.fixture +def global_integration_stub(request): + settings = {'test_key': 'test_value'} + integration = IntegrationModel().create( + TestIntegrationType, settings=settings, enabled=True, + name='test global integration') + + @request.addfinalizer + def cleanup(): + IntegrationModel().delete(integration) + + return integration + + +def test_delete_repo_with_integration_deletes_integration(repo_integration): + Session().delete(repo_integration.repo) + Session().commit() + Session().expire_all() + assert Integration.get(repo_integration.integration_id) is None +