Files
OpenHands/enterprise/server/auth/gitlab_sync.py
2026-01-08 22:44:18 +00:00

37 lines
1.3 KiB
Python

import asyncio
from pydantic import SecretStr
from openhands.core.logger import openhands_logger as logger
from openhands.server.types import AppMode
def schedule_gitlab_repo_sync(
user_id: str, keycloak_access_token: SecretStr | None = None
) -> None:
"""Schedule a background sync of GitLab repositories and webhook tracking.
Because the outer call is already a background task, we instruct the service
to store repository data synchronously (store_in_background=False) to avoid
nested background tasks while still keeping the overall operation async.
"""
async def _run():
try:
# Lazy import to avoid circular dependency:
# middleware -> gitlab_sync -> integrations.gitlab.gitlab_service
# -> openhands.integrations.gitlab.gitlab_service -> get_impl
# -> integrations.gitlab.gitlab_service (circular)
from integrations.gitlab.gitlab_service import SaaSGitLabService
service = SaaSGitLabService(
external_auth_id=user_id, external_auth_token=keycloak_access_token
)
await service.get_all_repositories(
'pushed', AppMode.SAAS, store_in_background=False
)
except Exception:
logger.warning('gitlab_repo_sync_failed', exc_info=True)
asyncio.create_task(_run())