89 lines
3.2 KiB
Python
89 lines
3.2 KiB
Python
from fastapi import APIRouter, HTTPException, Query
|
|
from typing import List, Dict, Any, Optional
|
|
|
|
from app.services.gitea_client import GiteaClient
|
|
from app.services.config_service import ConfigService
|
|
|
|
router = APIRouter()
|
|
gitea_client = GiteaClient()
|
|
config_service = ConfigService()
|
|
|
|
@router.get("/")
|
|
async def list_repositories(limit: int = Query(100, description="Maximum number of repositories to return")):
|
|
"""
|
|
List all available repositories from Gitea.
|
|
|
|
If Gitea integration is not configured, returns an empty list.
|
|
"""
|
|
repositories = gitea_client.list_repositories(limit)
|
|
|
|
# Enhance with linked job information
|
|
for repo in repositories:
|
|
# Create a URL from clone_url
|
|
repo_url = repo.get("clone_url")
|
|
if repo_url:
|
|
# Check if repository is linked to a job
|
|
configs = config_service.list_configs()
|
|
for config in configs:
|
|
if config.get("repository") == repo_url:
|
|
repo["linked_job"] = config.get("job_id")
|
|
repo["config_name"] = config.get("name")
|
|
break
|
|
|
|
return repositories
|
|
|
|
@router.get("/{repository}")
|
|
async def get_repository_info(repository: str):
|
|
"""
|
|
Get information about a specific repository.
|
|
|
|
The repository parameter can be a repository URL or a repository alias.
|
|
If it's a repository URL, we'll get the info directly from Gitea.
|
|
If it's a repository alias, we'll get the info from the configuration and then from Gitea.
|
|
"""
|
|
# First check if it's a repository URL
|
|
repo_info = gitea_client.get_repository_info(repository)
|
|
|
|
if repo_info:
|
|
# Check if repository is linked to a job
|
|
configs = config_service.list_configs()
|
|
for config in configs:
|
|
if config.get("repository") == repository:
|
|
repo_info["linked_job"] = config.get("job_id")
|
|
repo_info["config_name"] = config.get("name")
|
|
repo_info["config"] = config
|
|
break
|
|
|
|
return repo_info
|
|
else:
|
|
# Check if it's a repository alias in our configs
|
|
config = config_service.get_config_by_repository(repository)
|
|
if config:
|
|
repo_url = config.get("repository")
|
|
repo_info = gitea_client.get_repository_info(repo_url)
|
|
|
|
if repo_info:
|
|
repo_info["linked_job"] = config.get("job_id")
|
|
repo_info["config_name"] = config.get("name")
|
|
repo_info["config"] = config
|
|
return repo_info
|
|
|
|
raise HTTPException(status_code=404, detail=f"Repository not found: {repository}")
|
|
|
|
@router.get("/{repository}/branches")
|
|
async def get_repository_branches(repository: str):
|
|
"""
|
|
Get branches for a specific repository.
|
|
|
|
The repository parameter can be a repository URL or a repository alias.
|
|
"""
|
|
# If it's a repository alias, get the actual URL
|
|
config = config_service.get_config_by_repository(repository)
|
|
if config:
|
|
repository = config.get("repository")
|
|
|
|
branches = gitea_client.get_repository_branches(repository)
|
|
if not branches:
|
|
raise HTTPException(status_code=404, detail=f"No branches found for repository: {repository}")
|
|
|
|
return branches |