From b706f59cfd73db9318fc1041cee7ab64713ab293 Mon Sep 17 00:00:00 2001 From: baii <33405932+littlebai3618@users.noreply.github.com> Date: Tue, 3 Jun 2025 23:57:04 +0800 Subject: [PATCH] fix: can't add gitlab personal access token and add more debug log in validate_provider_token (#8782) --- openhands/integrations/gitlab/gitlab_service.py | 6 +++++- openhands/integrations/provider.py | 1 + openhands/integrations/service_types.py | 1 + openhands/integrations/utils.py | 15 +++++++++++---- openhands/runtime/base.py | 4 ++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/openhands/integrations/gitlab/gitlab_service.py b/openhands/integrations/gitlab/gitlab_service.py index 59cb168a8e..9ad745c003 100644 --- a/openhands/integrations/gitlab/gitlab_service.py +++ b/openhands/integrations/gitlab/gitlab_service.py @@ -167,10 +167,14 @@ class GitLabService(BaseGitService, GitService): url = f'{self.BASE_URL}/user' response, _ = await self._make_request(url) + # Use a default avatar URL if not provided + # In some self-hosted GitLab instances, the avatar_url field may be returned as None. + avatar_url = response.get('avatar_url') or '' + return User( id=response.get('id'), username=response.get('username'), - avatar_url=response.get('avatar_url'), + avatar_url=avatar_url, name=response.get('name'), email=response.get('email'), company=response.get('organization'), diff --git a/openhands/integrations/provider.py b/openhands/integrations/provider.py index ad89b042b3..6e60730526 100644 --- a/openhands/integrations/provider.py +++ b/openhands/integrations/provider.py @@ -130,6 +130,7 @@ class ProviderHandler: external_auth_token=self.external_auth_token, token=token.token, external_token_manager=self.external_token_manager, + base_domain=token.host, ) async def get_user(self) -> User: diff --git a/openhands/integrations/service_types.py b/openhands/integrations/service_types.py index a22f98764e..3e7dea4d79 100644 --- a/openhands/integrations/service_types.py +++ b/openhands/integrations/service_types.py @@ -184,6 +184,7 @@ class GitService(Protocol): external_auth_id: str | None = None, external_auth_token: SecretStr | None = None, external_token_manager: bool = False, + base_domain: str | None = None, ) -> None: """Initialize the service with authentication details""" ... diff --git a/openhands/integrations/utils.py b/openhands/integrations/utils.py index 303fd9cb9b..16f26acd05 100644 --- a/openhands/integrations/utils.py +++ b/openhands/integrations/utils.py @@ -1,5 +1,8 @@ +import traceback + from pydantic import SecretStr +from openhands.core.logger import openhands_logger as logger from openhands.integrations.github.github_service import GitHubService from openhands.integrations.gitlab.gitlab_service import GitLabService from openhands.integrations.provider import ProviderType @@ -25,15 +28,19 @@ async def validate_provider_token( github_service = GitHubService(token=token, base_domain=base_domain) await github_service.verify_access() return ProviderType.GITHUB - except Exception: - pass + except Exception as e: + logger.debug( + f'Failed to validate Github token: {e} \n {traceback.format_exc()}' + ) # Try GitLab next try: gitlab_service = GitLabService(token=token, base_domain=base_domain) await gitlab_service.get_user() return ProviderType.GITLAB - except Exception: - pass + except Exception as e: + logger.debug( + f'Failed to validate GitLab token: {e} \n {traceback.format_exc()}' + ) return None diff --git a/openhands/runtime/base.py b/openhands/runtime/base.py index d82a9786eb..421c428252 100644 --- a/openhands/runtime/base.py +++ b/openhands/runtime/base.py @@ -398,6 +398,10 @@ class Runtime(FileEditRuntimeMixin): domain = provider_domains[provider] + # If git_provider_tokens is provided, use the host from the token if available + if git_provider_tokens and provider in git_provider_tokens: + domain = git_provider_tokens[provider].host or domain + # Try to use token if available, otherwise use public URL if git_provider_tokens and provider in git_provider_tokens: git_token = git_provider_tokens[provider].token