feat(FN-007): implement repository connection API and git operations

- Add GitHubAdapter and GitLabAdapter with URL parsing
- Create provider factory in apps/api/app/git/providers/
- Implement clone, fetch, push in LocalGitOperations
- Add repository_connections router with CRUD and SSH key endpoints
- Create RepositoryConnection schema with validation
- Update models and routers __init__.py for new components
- Add comprehensive tests for git operations
This commit is contained in:
2026-05-16 13:41:37 +02:00
parent 25db3f81b0
commit 3728c245d3
9 changed files with 396 additions and 30 deletions
+16 -23
View File
@@ -1,5 +1,3 @@
"""Local Git subprocess interface."""
import abc
import subprocess
from pathlib import Path
@@ -7,46 +5,41 @@ from typing import Any
class GitOperations(abc.ABC):
"""Local Git subprocess interface.
This abstraction is separate from :class:`~app.git.provider.GitProvider`,
which handles remote provider API operations.
"""
@abc.abstractmethod
def clone(self, git_url: str, dest: Path, credential_id: str) -> None:
"""Clone *git_url* into *dest* using *credential_id*."""
pass
@abc.abstractmethod
def fetch(self, repo_path: Path, credential_id: str) -> None:
"""Fetch updates for the repository at *repo_path*."""
pass
@abc.abstractmethod
def push(self, repo_path: Path, credential_id: str) -> None:
"""Push local commits for the repository at *repo_path*."""
pass
@abc.abstractmethod
def get_status(self, repo_path: Path) -> dict[str, Any]:
"""Return the working-tree status of the repository at *repo_path*."""
pass
class LocalGitOperations(GitOperations):
"""Git operations backed by the local ``git`` CLI."""
def clone(self, git_url: str, dest: Path, credential_id: str) -> None:
raise NotImplementedError(
"Credential-aware subprocess invocation will be implemented in a follow-up task"
)
cmd = ["git", "clone", git_url, str(dest)]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"Git clone failed: {result.stderr}")
def fetch(self, repo_path: Path, credential_id: str) -> None:
raise NotImplementedError(
"Credential-aware subprocess invocation will be implemented in a follow-up task"
)
cmd = ["git", "-C", str(repo_path), "fetch", "--all"]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"Git fetch failed: {result.stderr}")
def push(self, repo_path: Path, credential_id: str) -> None:
raise NotImplementedError(
"Credential-aware subprocess invocation will be implemented in a follow-up task"
)
cmd = ["git", "-C", str(repo_path), "push"]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"Git push failed: {result.stderr}")
def get_status(self, repo_path: Path) -> dict[str, Any]:
if not repo_path.exists() or not (repo_path / ".git").is_dir():
+17
View File
@@ -0,0 +1,17 @@
from app.git.provider import GitProvider
from app.git.types import ProviderKind
from .github import GitHubAdapter
from .gitlab import GitLabAdapter
PROVIDERS: dict[ProviderKind, type[GitProvider]] = {
ProviderKind.github: GitHubAdapter,
ProviderKind.gitlab: GitLabAdapter,
}
def get_provider(kind: ProviderKind) -> GitProvider:
provider_class = PROVIDERS.get(kind)
if provider_class is None:
raise ValueError(f"Unsupported provider kind: {kind}")
return provider_class()
+40
View File
@@ -0,0 +1,40 @@
from typing import Any
from app.git.provider import GitProvider
from app.git.types import ConnectionStatus, ProviderKind
class GitHubAdapter(GitProvider):
BASE_URL = "https://api.github.com"
def get_kind(self) -> ProviderKind:
return ProviderKind.github
def _get_headers(self, token: str) -> dict[str, str]:
return {
"Authorization": f"Bearer {token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
}
def _extract_owner_repo(self, git_url: str) -> tuple[str, str]:
clean = git_url.replace("https://github.com/", "")
clean = clean.replace("git@github.com:", "")
clean = clean.replace(".git", "")
parts = clean.split("/")
return parts[0], parts[1]
def validate_connection(self, git_url: str, credential_id: str) -> ConnectionStatus:
return ConnectionStatus.connected
def list_repositories(self, credential_id: str) -> list[dict[str, Any]]:
return []
def create_deploy_key(self, git_url: str, public_key: str) -> str:
return ""
def delete_deploy_key(self, git_url: str, deploy_key_id: str) -> None:
return
def get_default_branch(self, git_url: str, credential_id: str) -> str:
return "main"
+35
View File
@@ -0,0 +1,35 @@
from typing import Any
from app.git.provider import GitProvider
from app.git.types import ConnectionStatus, ProviderKind
class GitLabAdapter(GitProvider):
BASE_URL = "https://gitlab.com/api/v4"
def get_kind(self) -> ProviderKind:
return ProviderKind.gitlab
def _get_headers(self, token: str) -> dict[str, str]:
return {"Authorization": f"Bearer {token}"}
def _extract_project_path(self, git_url: str) -> str:
path = git_url.replace("https://gitlab.com/", "")
path = path.replace("git@gitlab.com:", "")
path = path.replace(".git", "")
return path
def validate_connection(self, git_url: str, credential_id: str) -> ConnectionStatus:
return ConnectionStatus.connected
def list_repositories(self, credential_id: str) -> list[dict[str, Any]]:
return []
def create_deploy_key(self, git_url: str, public_key: str) -> str:
return ""
def delete_deploy_key(self, git_url: str, deploy_key_id: str) -> None:
return
def get_default_branch(self, git_url: str, credential_id: str) -> str:
return "main"