π OpenHands: AI-Driven Development
fix: filter automation event forwarding by requested types (#15388)
Rohit Malhotraee9e78b7defd
diff --git a/enterprise/server/routes/integration/github.py b/enterprise/server/routes/integration/github.pyindex 12e0d4535f..8940f82dd6 100644--- a/enterprise/server/routes/integration/github.py+++ b/enterprise/server/routes/integration/github.py@@ -85,6 +85,7 @@ async def github_events(provider=ProviderType.GITHUB,payload=payload_data,installation_id=installation_id,+ event_type=x_github_event,)# Existing resolver bot processingdiff --git a/enterprise/server/services/automation_event_service.py b/enterprise/server/services/automation_event_service.pyindex c029705284..dabec3813c 100644--- a/enterprise/server/services/automation_event_service.py+++ b/enterprise/server/services/automation_event_service.py@@ -22,12 +22,16 @@ import asyncioimport hashlibimport hmacimport json+import os+import timefrom dataclasses import dataclassfrom typing import Anyfrom uuid import UUIDimport aiohttp+import jmespathfrom integrations.resolver_org_router import resolve_org_for_repo+from jmespath import exceptions as jmespath_exceptionsfrom server.auth.constants import (AUTOMATION_SERVICE_TIMEOUT,AUTOMATION_SERVICE_URL,@@ -46,7 +50,12 @@ ORG_CLAIM_CACHE_TTL_SECONDS = 3600 # 1 hour for org claims (rarely change)USER_ID_CACHE_TTL_SECONDS = 86400 # 24 hours for user ID mappings (never change)# Short TTL so creating a second team org switches the fallback off promptly.DEFAULT_ORG_FALLBACK_CACHE_TTL_SECONDS = 300-+REQUESTED_EVENT_TYPES_CACHE_TTL_SECONDS = int(+ os.environ.get('AUTOMATION_REQUESTED_EVENT_TYPES_CACHE_TTL_SECONDS', '300')+)+REQUESTED_EVENT_TYPES_REFRESH_EVENT_COUNT = int(+ os.environ.get('AUTOMATION_REQUESTED_EVENT_TYPES_REFRESH_EVENT_COUNT', '1000')+)# Cache key prefixes (provider is appended dynamically)ORG_CLAIM_CACHE_PREFIX = 'automation:org_claim'USER_ID_CACHE_PREFIX = 'automation:idp_to_kc_user'@@ -61,6 +70,24 @@ class OrgContext:git_org: str+@dataclass+class EventDetectionRule:+ """Server-provided rule for detecting an event type from a payload."""++ event_type: str+ jmespath: str+++@dataclass+class RequestedEventTypesCacheEntry:+ """Cached event metadata requested by the automation service for a source."""++ event_types: set[str]+ event_detection_rules: list[EventDetectionRule]+ loaded_at: float+ events_seen: int = 0++class AutomationEventService:"""Service for forwarding webhook events to the automation service.@@ -77,6 +104,8 @@ class AutomationEventService:from server.auth.constants import AUTOMATION_EVENT_FORWARDING_ENABLEDself.token_manager = token_manager+ self._requested_event_types_cache: dict[str, RequestedEventTypesCacheEntry] = {}+ self._requested_event_types_lock = asyncio.Lock()# Fail fast if forwarding is enabled but misconfiguredif AUTOMATION_EVENT_FORWARDING_ENABLED:@@ -96,6 +125,7 @@ class AutomationEventService:provider: ProviderType,payload: dict[str, Any],installation_id: int | str,+ event_type: str | None = None,) -> None:"""Forward a Git provider webhook event to the automation service.@@ -111,6 +141,11 @@ class AutomationEventService:"""org_id: UUID | None = Nonetry:+ if not await self._should_forward_source_event(+ provider, payload, event_type+ ):+ return+# Resolve org context (org_id and git_org name) - uses Redis cacheorg_context = await self._resolve_org_context(provider, payload)if not org_context:@@ -187,6 +222,190 @@ class AutomationEventService:stack_info=True,)+ async def _should_forward_source_event(+ self, provider: ProviderType, payload: dict[str, Any], event_type: str | None+ ) -> bool:+ """Return whether this provider event should be forwarded."""+ source = provider.value+ metadata = await self._get_requested_event_metadata(source)+ if metadata is None:+ logger.warning(+ f'[AutomationEventService] Forwarding {source} event: requested '+ f'event metadata unavailable'+ )+ return True++ detected_event_type = self._detect_source_event_type(source, payload, metadata)+ if detected_event_type is None:+ logger.debug(+ f'[AutomationEventService] Skipping {source} event type '+ f'{event_type}: no automation detection rule matched'+ )+ return False++ if detected_event_type in metadata.event_types:+ return True++ logger.debug(+ f'[AutomationEventService] Skipping {source} event type '+ f'{detected_event_type}: not requested by automation service'+ )+ return False++ def _detect_source_event_type(+ self,+ source: str,+ payload: dict[str, Any],+ metadata: RequestedEventTypesCacheEntry,+ ) -> str | None:+ if not metadata.event_detection_rules:+ logger.warning(+ f'[AutomationEventService] Cannot filter {source} event: '+ f'automation service returned no detection rules'+ )+ return None++ for rule in metadata.event_detection_rules:+ try:+ if jmespath.search(rule.jmespath, payload):+ return rule.event_type+ except jmespath_exceptions.JMESPathError as e:+ logger.warning(+ f'[AutomationEventService] Invalid {source} event detection '+ f'rule for {rule.event_type}: {e}'+ )+ return None+ return None++ async def _get_requested_event_metadata(+ self, source: str+ ) -> RequestedEventTypesCacheEntry | None:+ now = time.monotonic()+ entry = self._requested_event_types_cache.get(source)+ if entry is not None and self._is_requested_event_types_cache_fresh(entry, now):+ entry.events_seen += 1+ return entry++ async with self._requested_event_types_lock:+ entry = self._requested_event_types_cache.get(source)+ now = time.monotonic()+ if entry is not None and self._is_requested_event_types_cache_fresh(+ entry, now+ ):+ entry.events_seen += 1+ return entry++ fetched = await self._fetch_requested_event_metadata(source)+ if fetched is not None:+ fetched.loaded_at = now+ self._requested_event_types_cache[source] = fetched+ return fetched++ if entry is not None:+ return entry+ return None++ @staticmethod+ def _is_requested_event_types_cache_fresh(+ entry: RequestedEventTypesCacheEntry | None, now: float+ ) -> bool:+ if entry is None:+ return False+ return (+ now - entry.loaded_at < REQUESTED_EVENT_TYPES_CACHE_TTL_SECONDS+ and entry.events_seen < REQUESTED_EVENT_TYPES_REFRESH_EVENT_COUNT+ )++ async def _fetch_requested_event_metadata(+ self, source: str+ ) -> RequestedEventTypesCacheEntry | None:+ if not AUTOMATION_SERVICE_URL:+ return None++ base_url = AUTOMATION_SERVICE_URL.rstrip('/')+ url = f'{base_url}/v1/events/{source}/requested-types'+ payload_bytes = source.encode('utf-8')+ headers = {'X-Hub-Signature-256': self._sign_payload(payload_bytes)}++ try:+ async with aiohttp.ClientSession() as session:+ async with session.get(+ url,+ headers=headers,+ timeout=aiohttp.ClientTimeout(total=AUTOMATION_SERVICE_TIMEOUT),+ ) as resp:+ if resp.status >= 400:+ body = await self._read_response_body(resp)+ logger.warning(+ f'[AutomationEventService] Failed to fetch requested '+ f'event types for {source}: {resp.status} {body}'+ )+ return None++ data = await resp.json()+ event_types = data.get('event_types')+ detection_rules = data.get('event_detection_rules')+ if not isinstance(event_types, list) or not isinstance(+ detection_rules, list+ ):+ logger.warning(+ f'[AutomationEventService] Invalid requested event '+ f'metadata response for {source}: {data}'+ )+ return None++ rules = []+ for item in detection_rules:+ if not isinstance(item, dict):+ continue+ rule_event_type = item.get('event_type')+ rule_jmespath = item.get('jmespath')+ if isinstance(rule_event_type, str) and isinstance(+ rule_jmespath, str+ ):+ try:+ jmespath.compile(rule_jmespath)+ except jmespath_exceptions.JMESPathError as e:+ logger.warning(+ f'[AutomationEventService] Invalid {source} '+ f'event detection rule for {rule_event_type}: {e}'+ )+ return None+ rules.append(+ EventDetectionRule(+ event_type=rule_event_type,+ jmespath=rule_jmespath,+ )+ )++ if not rules:+ logger.warning(+ f'[AutomationEventService] Requested event metadata '+ f'for {source} did not include detection rules'+ )+ return None++ requested_event_types = {+ item for item in event_types if isinstance(item, str)+ }+ logger.info(+ f'[AutomationEventService] Refreshed event forwarding '+ f'metadata for {source}: requested_event_types='+ f'{sorted(requested_event_types)}, detection_rule_types='+ f'{[rule.event_type for rule in rules]}'+ )+ return RequestedEventTypesCacheEntry(+ event_types=requested_event_types,+ event_detection_rules=rules,+ loaded_at=0,+ )+ except (aiohttp.ClientError, asyncio.TimeoutError, ValueError) as e:+ logger.warning(+ f'[AutomationEventService] Error fetching requested event metadata '+ f'for {source}: {e}'+ )+ return None+async def _resolve_org_context(self, provider: ProviderType, payload: dict[str, Any]) -> OrgContext | None:diff --git a/enterprise/tests/unit/server/services/test_automation_event_service.py b/enterprise/tests/unit/server/services/test_automation_event_service.pyindex b56381c0d6..17ee3b12ca 100644--- a/enterprise/tests/unit/server/services/test_automation_event_service.py+++ b/enterprise/tests/unit/server/services/test_automation_event_service.py@@ -462,6 +462,200 @@ class TestForwardEvent:assert payload['organization']['git_org'] == 'testuser'assert payload['organization']['openhands_org_id'] == keycloak_id+ @staticmethod+ def _requested_metadata(event_types: set[str]):+ from server.services.automation_event_service import (+ EventDetectionRule,+ RequestedEventTypesCacheEntry,+ )++ return RequestedEventTypesCacheEntry(+ event_types=event_types,+ event_detection_rules=[+ EventDetectionRule(+ event_type='pull_request',+ jmespath="contains(keys(@), 'pull_request')",+ ),+ EventDetectionRule(+ event_type='push',+ jmespath="contains(keys(@), 'ref') && contains(keys(@), 'commits')",+ ),+ ],+ loaded_at=0,+ )++ @pytest.mark.asyncio+ async def test_forward_event_skips_unrequested_github_event(+ self, mock_token_manager, github_org_payload+ ):+ from server.services.automation_event_service import AutomationEventService++ payload = {**github_org_payload, 'pull_request': {'number': 1}}++ with (+ patch.object(+ AutomationEventService,+ '_get_requested_event_metadata',+ new_callable=AsyncMock,+ return_value=self._requested_metadata({'push'}),+ ),+ patch(+ 'server.services.automation_event_service.resolve_org_for_repo',+ new_callable=AsyncMock,+ ) as mock_resolver,+ patch.object(+ AutomationEventService,+ '_send_to_automation_service',+ new_callable=AsyncMock,+ ) as mock_send,+ ):+ service = AutomationEventService(mock_token_manager)+ await service.forward_event(+ provider=ProviderType.GITHUB,+ payload=payload,+ installation_id=99999,+ event_type='pull_request',+ )++ mock_resolver.assert_not_called()+ mock_send.assert_not_called()++ @pytest.mark.asyncio+ async def test_forward_event_allows_requested_github_event(+ self, mock_token_manager, github_org_payload, mock_org_git_claim+ ):+ from server.services.automation_event_service import AutomationEventService++ payload = {**github_org_payload, 'pull_request': {'number': 1}}+ mock_redis = AsyncMock()+ mock_redis.get = AsyncMock(return_value=None)+ mock_redis.setex = AsyncMock()++ with (+ patch.object(+ AutomationEventService,+ '_get_requested_event_metadata',+ new_callable=AsyncMock,+ return_value=self._requested_metadata({'pull_request'}),+ ),+ patch(+ 'server.services.automation_event_service.resolve_org_for_repo',+ new_callable=AsyncMock,+ return_value=mock_org_git_claim.org_id,+ ),+ patch(REDIS_PATCH, return_value=mock_redis),+ patch.object(+ AutomationEventService,+ '_send_to_automation_service',+ new_callable=AsyncMock,+ ) as mock_send,+ ):+ service = AutomationEventService(mock_token_manager)+ await service.forward_event(+ provider=ProviderType.GITHUB,+ payload=payload,+ installation_id=99999,+ event_type='pull_request',+ )++ mock_send.assert_called_once()++ @pytest.mark.asyncio+ async def test_forward_event_uses_detection_rules_not_github_header(+ self, mock_token_manager, github_org_payload, mock_org_git_claim+ ):+ from server.services.automation_event_service import AutomationEventService++ payload = {**github_org_payload, 'pull_request': {'number': 1}}+ mock_redis = AsyncMock()+ mock_redis.get = AsyncMock(return_value=None)+ mock_redis.setex = AsyncMock()++ with (+ patch.object(+ AutomationEventService,+ '_get_requested_event_metadata',+ new_callable=AsyncMock,+ return_value=self._requested_metadata({'pull_request'}),+ ),+ patch(+ 'server.services.automation_event_service.resolve_org_for_repo',+ new_callable=AsyncMock,+ return_value=mock_org_git_claim.org_id,+ ),+ patch(REDIS_PATCH, return_value=mock_redis),+ patch.object(+ AutomationEventService,+ '_send_to_automation_service',+ new_callable=AsyncMock,+ ) as mock_send,+ ):+ service = AutomationEventService(mock_token_manager)+ await service.forward_event(+ provider=ProviderType.GITHUB,+ payload=payload,+ installation_id=99999,+ event_type='pull_request_review_comment',+ )++ mock_send.assert_called_once()++ @pytest.mark.asyncio+ async def test_get_requested_event_metadata_returns_none_without_server_truth(+ self, mock_token_manager+ ):+ from server.services.automation_event_service import AutomationEventService++ with patch.object(+ AutomationEventService,+ '_fetch_requested_event_metadata',+ new_callable=AsyncMock,+ return_value=None,+ ):+ service = AutomationEventService(mock_token_manager)+ metadata = await service._get_requested_event_metadata('github')++ assert metadata is None++ @pytest.mark.asyncio+ async def test_forward_event_allows_when_requested_types_unavailable(+ self, mock_token_manager, github_org_payload, mock_org_git_claim+ ):+ from server.services.automation_event_service import AutomationEventService++ mock_redis = AsyncMock()+ mock_redis.get = AsyncMock(return_value=None)+ mock_redis.setex = AsyncMock()++ with (+ patch.object(+ AutomationEventService,+ '_get_requested_event_metadata',+ new_callable=AsyncMock,+ return_value=None,+ ),+ patch(+ 'server.services.automation_event_service.resolve_org_for_repo',+ new_callable=AsyncMock,+ return_value=mock_org_git_claim.org_id,+ ),+ patch(REDIS_PATCH, return_value=mock_redis),+ patch.object(+ AutomationEventService,+ '_send_to_automation_service',+ new_callable=AsyncMock,+ ) as mock_send,+ ):+ service = AutomationEventService(mock_token_manager)+ await service.forward_event(+ provider=ProviderType.GITHUB,+ payload=github_org_payload,+ installation_id=99999,+ event_type='workflow_job',+ )++ mock_send.assert_called_once()+@pytest.mark.asyncioasync def test_forward_event_no_owner_in_payload(self, mock_token_manager):"""