Spaces:
Build error
Build error
| 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 | |
| async def validate_provider_token( | |
| token: SecretStr, base_domain: str | None = None | |
| ) -> ProviderType | None: | |
| """ | |
| Determine whether a token is for GitHub or GitLab by attempting to get user info | |
| from both services. | |
| Args: | |
| token: The token to check | |
| Returns: | |
| 'github' if it's a GitHub token | |
| 'gitlab' if it's a GitLab token | |
| None if the token is invalid for both services | |
| """ | |
| # Try GitHub first | |
| try: | |
| github_service = GitHubService(token=token, base_domain=base_domain) | |
| await github_service.verify_access() | |
| return ProviderType.GITHUB | |
| 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 as e: | |
| logger.debug( | |
| f'Failed to validate GitLab token: {e} \n {traceback.format_exc()}' | |
| ) | |
| return None | |