Skip to content

πŸ™Œ OpenHands: AI-Driven Development

feat: PLTF-2956 parallelize GitHub GraphQL queries in get_suggested_tasks (#14821)

aivong-openhands4eecdb43023a
diff --git a/openhands/app_server/integrations/github/service/features.py b/openhands/app_server/integrations/github/service/features.py
index cae45213a3..1995fd8586 100644
--- a/openhands/app_server/integrations/github/service/features.py
+++ b/openhands/app_server/integrations/github/service/features.py
@@ -1,3 +1,6 @@
+import asyncio
+from typing import Any
+
from openhands.app_server.integrations.github.queries import (
suggested_task_issue_graphql_query,
suggested_task_pr_graphql_query,
@@ -23,95 +26,113 @@ class GitHubFeaturesMixin(GitHubMixinBase):
- PRs authored by the user.
- Issues assigned to the user.
- Note: Queries are split to avoid timeout issues.
+ Note: Queries are run concurrently for better performance.
"""
# Get user info to use in queries
user = await self.get_user()
login = user.login
tasks: list[SuggestedTask] = []
- variables = {'login': login}
-
- try:
- pr_response = await self.execute_graphql_query(
- suggested_task_pr_graphql_query, variables
- )
- pr_data = pr_response['data']['user']
-
- # Process pull requests
- for pr in pr_data['pullRequests']['nodes']:
- repo_name = pr['repository']['nameWithOwner']
-
- # Start with default task type
- task_type = TaskType.OPEN_PR
-
- # Check for specific states
- if pr['mergeable'] == 'CONFLICTING':
- task_type = TaskType.MERGE_CONFLICTS
- elif (
- pr['commits']['nodes']
- and pr['commits']['nodes'][0]['commit']['statusCheckRollup']
- and pr['commits']['nodes'][0]['commit']['statusCheckRollup'][
- 'state'
- ]
- == 'FAILURE'
- ):
- task_type = TaskType.FAILING_CHECKS
- elif any(
- review['state'] in ['CHANGES_REQUESTED', 'COMMENTED']
- for review in pr['reviews']['nodes']
- ):
- task_type = TaskType.UNRESOLVED_COMMENTS
-
- # Only add the task if it's not OPEN_PR
- if task_type != TaskType.OPEN_PR:
- tasks.append(
- SuggestedTask(
- git_provider=ProviderType.GITHUB,
- task_type=task_type,
- repo=repo_name,
- issue_number=pr['number'],
- title=pr['title'],
- )
- )
-
- except Exception as e:
+ variables: dict[str, str] = {'login': login}
+
+ # Execute both GraphQL queries concurrently
+ results: tuple[Any, Any] = await asyncio.gather(
+ self.execute_graphql_query(suggested_task_pr_graphql_query, variables),
+ self.execute_graphql_query(suggested_task_issue_graphql_query, variables),
+ return_exceptions=True,
+ )
+ pr_response: Any = results[0]
+ issue_response: Any = results[1]
+
+ # Process PR response
+ if isinstance(pr_response, Exception):
logger.info(
- f'Error fetching suggested task for PRs: {e}',
+ f'Error fetching suggested task for PRs: {pr_response}',
extra={
'signal': 'github_suggested_tasks',
'user_id': self.external_auth_id,
},
)
-
- try:
- # Execute issue query
- issue_response = await self.execute_graphql_query(
- suggested_task_issue_graphql_query, variables
- )
- issue_data = issue_response['data']['user']
-
- # Process issues
- for issue in issue_data['issues']['nodes']:
- repo_name = issue['repository']['nameWithOwner']
- tasks.append(
- SuggestedTask(
- git_provider=ProviderType.GITHUB,
- task_type=TaskType.OPEN_ISSUE,
- repo=repo_name,
- issue_number=issue['number'],
- title=issue['title'],
- )
+ else:
+ try:
+ pr_data = pr_response['data']['user']
+
+ # Process pull requests
+ for pr in pr_data['pullRequests']['nodes']:
+ repo_name = pr['repository']['nameWithOwner']
+
+ # Start with default task type
+ task_type = TaskType.OPEN_PR
+
+ # Check for specific states
+ if pr['mergeable'] == 'CONFLICTING':
+ task_type = TaskType.MERGE_CONFLICTS
+ elif (
+ pr['commits']['nodes']
+ and pr['commits']['nodes'][0]['commit']['statusCheckRollup']
+ and pr['commits']['nodes'][0]['commit']['statusCheckRollup'][
+ 'state'
+ ]
+ == 'FAILURE'
+ ):
+ task_type = TaskType.FAILING_CHECKS
+ elif any(
+ review['state'] in ['CHANGES_REQUESTED', 'COMMENTED']
+ for review in pr['reviews']['nodes']
+ ):
+ task_type = TaskType.UNRESOLVED_COMMENTS
+
+ # Only add the task if it's not OPEN_PR
+ if task_type != TaskType.OPEN_PR:
+ tasks.append(
+ SuggestedTask(
+ git_provider=ProviderType.GITHUB,
+ task_type=task_type,
+ repo=repo_name,
+ issue_number=pr['number'],
+ title=pr['title'],
+ )
+ )
+ except Exception as e:
+ logger.info(
+ f'Error processing PR response: {e}',
+ extra={
+ 'signal': 'github_suggested_tasks',
+ 'user_id': self.external_auth_id,
+ },
)
- return tasks
-
- except Exception as e:
+ # Process issue response
+ if isinstance(issue_response, Exception):
logger.info(
- f'Error fetching suggested task for issues: {e}',
+ f'Error fetching suggested task for issues: {issue_response}',
extra={
'signal': 'github_suggested_tasks',
'user_id': self.external_auth_id,
},
)
+ else:
+ try:
+ issue_data = issue_response['data']['user']
+
+ # Process issues
+ for issue in issue_data['issues']['nodes']:
+ repo_name = issue['repository']['nameWithOwner']
+ tasks.append(
+ SuggestedTask(
+ git_provider=ProviderType.GITHUB,
+ task_type=TaskType.OPEN_ISSUE,
+ repo=repo_name,
+ issue_number=issue['number'],
+ title=issue['title'],
+ )
+ )
+ except Exception as e:
+ logger.info(
+ f'Error processing issue response: {e}',
+ extra={
+ 'signal': 'github_suggested_tasks',
+ 'user_id': self.external_auth_id,
+ },
+ )
return tasks
diff --git a/tests/unit/integrations/github/test_suggested_tasks.py b/tests/unit/integrations/github/test_suggested_tasks.py
index 08d1db5756..200cda8a16 100644
--- a/tests/unit/integrations/github/test_suggested_tasks.py
+++ b/tests/unit/integrations/github/test_suggested_tasks.py
@@ -1,8 +1,12 @@
-from unittest.mock import AsyncMock
+from unittest.mock import AsyncMock, call
import pytest
from openhands.app_server.integrations.github.github_service import GitHubService
+from openhands.app_server.integrations.github.queries import (
+ suggested_task_issue_graphql_query,
+ suggested_task_pr_graphql_query,
+)
from openhands.app_server.integrations.service_types import TaskType, User
@@ -16,8 +20,8 @@ async def test_get_suggested_tasks():
name='Test User',
)
- # Mock GraphQL response
- mock_graphql_response = {
+ # Mock PR GraphQL response
+ mock_pr_graphql_response = {
'data': {
'user': {
'pullRequests': {
@@ -66,6 +70,14 @@ async def test_get_suggested_tasks():
},
]
},
+ }
+ }
+ }
+
+ # Mock issue GraphQL response
+ mock_issue_graphql_response = {
+ 'data': {
+ 'user': {
'issues': {
'nodes': [
{
@@ -87,11 +99,21 @@ async def test_get_suggested_tasks():
# Create service instance with mocked methods
service = GitHubService()
service.get_user = AsyncMock(return_value=mock_user)
- service.execute_graphql_query = AsyncMock(return_value=mock_graphql_response)
+ service.execute_graphql_query = AsyncMock(
+ side_effect=[mock_pr_graphql_response, mock_issue_graphql_response]
+ )
# Call the function
tasks = await service.get_suggested_tasks()
+ # Verify both GraphQL queries were called
+ assert service.execute_graphql_query.call_count == 2
+ expected_calls = [
+ call(suggested_task_pr_graphql_query, {'login': 'test-user'}),
+ call(suggested_task_issue_graphql_query, {'login': 'test-user'}),
+ ]
+ service.execute_graphql_query.assert_has_calls(expected_calls)
+
# Verify the results
assert len(tasks) == 5 # Should have 5 tasks total
@@ -122,3 +144,98 @@ async def test_get_suggested_tasks():
commented_pr = next(t for t in tasks if t.task_type == TaskType.UNRESOLVED_COMMENTS)
assert commented_pr.issue_number == 4
assert commented_pr.title == 'PR with comments'
+
+
+async def test_get_suggested_tasks_pr_query_fails():
+ """Test that issues are still returned when PR query fails."""
+ mock_user = User(
+ id='1',
+ login='test-user',
+ avatar_url='https://example.com/avatar.jpg',
+ name='Test User',
+ )
+
+ # Mock issue response only
+ mock_issue_graphql_response = {
+ 'data': {
+ 'user': {
+ 'issues': {
+ 'nodes': [
+ {
+ 'number': 1,
+ 'title': 'Assigned issue',
+ 'repository': {'nameWithOwner': 'test-org/repo'},
+ },
+ ]
+ },
+ }
+ }
+ }
+
+ service = GitHubService()
+ service.get_user = AsyncMock(return_value=mock_user)
+ service.execute_graphql_query = AsyncMock(
+ side_effect=[
+ Exception('PR query failed'), # PR query fails
+ mock_issue_graphql_response, # Issue query succeeds
+ ]
+ )
+
+ # Call the function - should not raise despite PR query failure
+ tasks = await service.get_suggested_tasks()
+
+ # Verify we still get the issue task
+ assert len(tasks) == 1
+ assert tasks[0].task_type == TaskType.OPEN_ISSUE
+ assert tasks[0].issue_number == 1
+
+
+async def test_get_suggested_tasks_issue_query_fails():
+ """Test that PRs are still returned when issue query fails."""
+ mock_user = User(
+ id='1',
+ login='test-user',
+ avatar_url='https://example.com/avatar.jpg',
+ name='Test User',
+ )
+
+ # Mock PR response only
+ mock_pr_graphql_response = {
+ 'data': {
+ 'user': {
+ 'pullRequests': {
+ 'nodes': [
+ {
+ 'number': 1,
+ 'title': 'PR with conflicts',
+ 'repository': {'nameWithOwner': 'test-org/repo'},
+ 'mergeable': 'CONFLICTING',
+ 'commits': {
+ 'nodes': [{'commit': {'statusCheckRollup': None}}]
+ },
+ 'reviews': {'nodes': []},
+ },
+ ]
+ },
+ }
+ }
+ }
+
+ service = GitHubService()
+ service.get_user = AsyncMock(return_value=mock_user)
+ service.execute_graphql_query = AsyncMock(
+ side_effect=[
+ mock_pr_graphql_response, # PR query succeeds
+ Exception('Issue query failed'), # Issue query fails
+ ]
+ )
+
+ # Call the function - should not raise despite issue query failure
+ tasks = await service.get_suggested_tasks()
+
+ # Verify we still get the PR task
+ assert len(tasks) == 1
+ assert tasks[0].task_type == TaskType.MERGE_CONFLICTS
+ assert tasks[0].issue_number == 1