π OpenHands: AI-Driven Development
feat(backend): route gitlab resolver conversations to claimed org workspaces (#13755)
Hiep Led058323a8725
diff --git a/enterprise/integrations/github/github_view.py b/enterprise/integrations/github/github_view.pyindex cf1a68378c..ba439c0d9a 100644--- a/enterprise/integrations/github/github_view.py+++ b/enterprise/integrations/github/github_view.py@@ -342,7 +342,7 @@ class GithubIssue(ResolverViewInterface):'full_repo_name': self.full_repo_name,'installation_id': self.installation_id,},- send_summary_instruction=self.send_summary_instruction,+ should_request_summary=self.send_summary_instruction,)@@ -496,7 +496,7 @@ class GithubInlinePRComment(GithubPRComment):'comment_id': self.comment_id,},inline_pr_comment=True,- send_summary_instruction=self.send_summary_instruction,+ should_request_summary=self.send_summary_instruction,)diff --git a/enterprise/integrations/gitlab/gitlab_view.py b/enterprise/integrations/gitlab/gitlab_view.pyindex 4b71940640..59222484a6 100644--- a/enterprise/integrations/gitlab/gitlab_view.py+++ b/enterprise/integrations/gitlab/gitlab_view.py@@ -3,6 +3,7 @@ from uuid import UUID, uuid4from integrations.models import Messagefrom integrations.resolver_context import ResolverUserContext+from integrations.resolver_org_router import resolve_org_for_repofrom integrations.types import ResolverViewInterface, UserDatafrom integrations.utils import (ENABLE_V1_GITLAB_RESOLVER,@@ -14,6 +15,7 @@ from integrations.utils import (from jinja2 import Environmentfrom server.auth.token_manager import TokenManagerfrom server.config import get_config+from storage.saas_conversation_store import SaasConversationStorefrom storage.saas_secrets_store import SaasSecretsStorefrom openhands.agent_server.models import SendMessageRequest@@ -29,15 +31,13 @@ from openhands.integrations.gitlab.gitlab_service import GitLabServiceImplfrom openhands.integrations.provider import PROVIDER_TOKEN_TYPE, ProviderTypefrom openhands.integrations.service_types import Commentfrom openhands.sdk import TextContent-from openhands.server.services.conversation_service import (- initialize_conversation,- start_conversation,-)+from openhands.server.services.conversation_service import start_conversationfrom openhands.server.user_auth.user_auth import UserAuthfrom openhands.storage.data_models.conversation_metadata import (ConversationMetadata,ConversationTrigger,)+from openhands.utils.conversation_summary import get_default_conversation_titleOH_LABEL, INLINE_OH_LABEL = get_oh_labels(HOST)CONFIDENTIAL_NOTE = 'confidential_note'@@ -118,6 +118,14 @@ class GitlabIssue(ResolverViewInterface):async def initialize_new_conversation(self) -> ConversationMetadata:# v1_enabled is already set at construction time in the factory method# This is the source of truth for the conversation type++ # Resolve target org based on claimed git organizations+ self.resolved_org_id = await resolve_org_for_repo(+ provider='gitlab',+ full_repo_name=self.full_repo_name,+ keycloak_user_id=self.user_info.keycloak_user_id,+ )+if self.v1_enabled:# Create dummy conversation metadata# Don't save to conversation store@@ -128,16 +136,28 @@ class GitlabIssue(ResolverViewInterface):selected_repository=self.full_repo_name,)- conversation_metadata: ConversationMetadata = await initialize_conversation( # type: ignore[assignment]+ # Create the conversation store with resolver org routing+ # (bypasses initialize_conversation to avoid threading enterprise-only+ # resolver_org_id through the generic OSS interface)+ store = await SaasConversationStore.get_resolver_instance(+ get_config(),+ self.user_info.keycloak_user_id,+ self.resolved_org_id,+ )++ conversation_id = uuid4().hex+ conversation_metadata = ConversationMetadata(+ trigger=ConversationTrigger.RESOLVER,+ conversation_id=conversation_id,+ title=get_default_conversation_title(conversation_id),user_id=self.user_info.keycloak_user_id,- conversation_id=None,selected_repository=self.full_repo_name,selected_branch=self._get_branch_name(),- conversation_trigger=ConversationTrigger.RESOLVER,git_provider=ProviderType.GITLAB,)+ await store.save_metadata(conversation_metadata)- self.conversation_id = conversation_metadata.conversation_id+ self.conversation_id = conversation_idreturn conversation_metadataasync def create_new_conversation(@@ -228,7 +248,10 @@ class GitlabIssue(ResolverViewInterface):)# Set up the GitLab user context for the V1 system- gitlab_user_context = ResolverUserContext(saas_user_auth=saas_user_auth)+ gitlab_user_context = ResolverUserContext(+ saas_user_auth=saas_user_auth,+ resolver_org_id=self.resolved_org_id,+ )setattr(injector_state, USER_CONTEXT_ATTR, gitlab_user_context)async with get_app_conversation_service(@@ -260,7 +283,7 @@ class GitlabIssue(ResolverViewInterface):'is_mr': self.is_mr,'discussion_id': getattr(self, 'discussion_id', None),},- send_summary_instruction=self.send_summary_instruction,+ should_request_summary=self.send_summary_instruction,)diff --git a/enterprise/tests/unit/test_gitlab_view.py b/enterprise/tests/unit/test_gitlab_view.pynew file mode 100644index 0000000000..0ed803dda0--- /dev/null+++ b/enterprise/tests/unit/test_gitlab_view.py@@ -0,0 +1,126 @@+"""Tests for GitLab resolver org routing logic.++Tests that the GitLab resolver correctly resolves the target organization+and passes resolver_org_id through V0 and V1 conversation paths.+"""++from unittest import TestCase+from unittest.mock import AsyncMock, MagicMock, patch+from uuid import UUID++import pytest+from integrations.gitlab.gitlab_view import GitlabIssue+from integrations.models import Message, SourceType+from integrations.types import UserData+++class TestGitlabOrgRouting(TestCase):+ """Test org routing for GitLab resolver conversations."""++ def setUp(self):+ self.user_data = UserData(+ user_id=123, username='testuser', keycloak_user_id='test-keycloak-id'+ )+ self.raw_payload = Message(+ source=SourceType.GITLAB,+ message={+ 'payload': {+ 'object_kind': 'issue',+ 'object_attributes': {'action': 'open', 'iid': 42},+ }+ },+ )+ self.resolved_org_id = UUID('aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa')++ def _create_gitlab_issue(self):+ return GitlabIssue(+ user_info=self.user_data,+ full_repo_name='ClaimedOrg/repo',+ issue_number=42,+ project_id=100,+ installation_id='install-123',+ conversation_id='',+ should_extract=True,+ send_summary_instruction=False,+ is_public_repo=True,+ raw_payload=self.raw_payload,+ title='',+ description='',+ previous_comments=[],+ is_mr=False,+ v1_enabled=False,+ )++ @pytest.mark.asyncio+ @patch(+ 'integrations.gitlab.gitlab_view.SaasConversationStore.get_resolver_instance'+ )+ @patch('integrations.gitlab.gitlab_view.resolve_org_for_repo')+ async def test_v0_passes_resolver_org_id_to_get_resolver_instance(+ self, mock_resolve_org, mock_get_resolver+ ):+ """V0 path creates store via get_resolver_instance with resolver_org_id."""+ # Arrange+ mock_resolve_org.return_value = self.resolved_org_id+ mock_store = MagicMock()+ mock_store.save_metadata = AsyncMock()+ mock_get_resolver.return_value = mock_store++ gitlab_issue = self._create_gitlab_issue()++ # Act+ await gitlab_issue.initialize_new_conversation()++ # Assert+ mock_resolve_org.assert_called_once_with(+ provider='gitlab',+ full_repo_name='ClaimedOrg/repo',+ keycloak_user_id='test-keycloak-id',+ )+ # get_resolver_instance(config, user_id, resolver_org_id)+ args, _ = mock_get_resolver.call_args+ assert args[1] == 'test-keycloak-id'+ assert args[2] == self.resolved_org_id++ @pytest.mark.asyncio+ @patch('integrations.gitlab.gitlab_view.get_app_conversation_service')+ @patch('integrations.gitlab.gitlab_view.resolve_org_for_repo')+ async def test_v1_passes_resolver_org_id_to_resolver_user_context(+ self, mock_resolve_org, mock_get_service+ ):+ """V1 path passes resolved org_id to ResolverUserContext."""+ # Arrange+ mock_resolve_org.return_value = self.resolved_org_id++ gitlab_issue = self._create_gitlab_issue()+ gitlab_issue.v1_enabled = True++ # Initialize to set resolved_org_id+ await gitlab_issue.initialize_new_conversation()++ # Assert+ assert gitlab_issue.resolved_org_id == self.resolved_org_id++ @pytest.mark.asyncio+ @patch(+ 'integrations.gitlab.gitlab_view.SaasConversationStore.get_resolver_instance'+ )+ @patch('integrations.gitlab.gitlab_view.resolve_org_for_repo')+ async def test_no_claim_passes_none_resolver_org_id(+ self, mock_resolve_org, mock_get_resolver+ ):+ """When no claim exists, resolver_org_id is None (falls back to personal workspace)."""+ # Arrange+ mock_resolve_org.return_value = None+ mock_store = MagicMock()+ mock_store.save_metadata = AsyncMock()+ mock_get_resolver.return_value = mock_store++ gitlab_issue = self._create_gitlab_issue()++ # Act+ await gitlab_issue.initialize_new_conversation()++ # Assert+ args, _ = mock_get_resolver.call_args+ assert args[2] is None