Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: Forgejo/Gitea clone failure when hosted under a subpath (#14930)

Vasco Schiavo0bbaffad76ad
diff --git a/openhands/app_server/integrations/provider.py b/openhands/app_server/integrations/provider.py
index 079999becb..7c64c01afc 100644
--- a/openhands/app_server/integrations/provider.py
+++ b/openhands/app_server/integrations/provider.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import os
+import re
from collections.abc import Mapping
from types import MappingProxyType
from typing import cast
@@ -543,9 +544,10 @@ class ProviderHandler:
if domain:
domain = domain.strip()
domain = domain.replace('https://', '').replace('http://', '')
- # Remove any trailing path like /api/v3 or /api/v4
- if '/' in domain:
- domain = domain.split('/')[0]
+ # Strip a trailing API path (e.g. /api/v3) but preserve any subpath
+ # that is part of the repo URL (e.g. Forgejo under myserver/forgejo).
+ domain = re.sub(r'/api/(?:v\d+|graphql)/?$', '', domain)
+ domain = domain.rstrip('/')
# Try to use token if available, otherwise use public URL
if self.provider_tokens and provider in self.provider_tokens:
diff --git a/tests/unit/integrations/test_provider_immutability.py b/tests/unit/integrations/test_provider_immutability.py
index 9f95100c33..4ba3b8c414 100644
--- a/tests/unit/integrations/test_provider_immutability.py
+++ b/tests/unit/integrations/test_provider_immutability.py
@@ -255,6 +255,54 @@ async def test_azure_devops_pat_git_url_uses_basic_auth():
)
+async def test_forgejo_subpath_preserved_in_git_url():
+ """A Forgejo/Gitea host under a subpath keeps the subpath in the clone URL."""
+ tokens = MappingProxyType(
+ {
+ ProviderType.FORGEJO: ProviderToken(
+ token=SecretStr('forgejo-token'),
+ host='https://myserver/forgejo',
+ )
+ }
+ )
+ handler = ProviderHandler(provider_tokens=tokens)
+
+ with patch.object(handler, 'verify_repo_provider') as mock_verify:
+ mock_verify.return_value.git_provider = ProviderType.FORGEJO
+ mock_verify.return_value.full_name = 'username/reponame'
+
+ remote_url = await handler.get_authenticated_git_url('username/reponame')
+
+ assert remote_url == (
+ 'https://forgejo-token@myserver/forgejo/username/reponame.git'
+ )
+
+
+async def test_forgejo_host_api_suffix_stripped():
+ """A host entered with a trailing API path still has that suffix stripped."""
+ tokens = MappingProxyType(
+ {
+ ProviderType.FORGEJO: ProviderToken(
+ token=SecretStr('forgejo-token'),
+ host='https://myserver/forgejo/api/v1',
+ )
+ }
+ )
+ handler = ProviderHandler(provider_tokens=tokens)
+
+ with patch.object(handler, 'verify_repo_provider') as mock_verify:
+ mock_verify.return_value.git_provider = ProviderType.FORGEJO
+ mock_verify.return_value.full_name = 'username/reponame'
+
+ remote_url = await handler.get_authenticated_git_url('username/reponame')
+
+ assert remote_url == (
+ 'https://forgejo-token@myserver/forgejo/username/reponame.git'
+ )
+
+
@pytest.mark.asyncio
async def test_get_github_organizations_delegates_to_service():
"""Test that get_github_organizations calls get_organizations_from_installations on the GitHub service."""