|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import logging |
| 4 | +from git import Repo |
| 5 | +from fastapi import FastAPI, HTTPException |
| 6 | +from fastapi.middleware.cors import CORSMiddleware |
| 7 | +from typing import Dict, Union |
| 8 | +from pydantic import BaseModel |
| 9 | +from cli.git_analysis import ( |
| 10 | + analyze_commit_frequency, |
| 11 | + analyze_contributor_activity, |
| 12 | + analyze_commit_frequency_by_weekday, |
| 13 | + analyze_commit_frequency_by_hour, |
| 14 | + analyze_average_commit_size, |
| 15 | + analyze_file_change_frequency, |
| 16 | +) |
| 17 | + |
| 18 | +# Configure logging |
| 19 | +logging.basicConfig(level=logging.INFO) |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +app = FastAPI(title="Git Analysis API") |
| 23 | + |
| 24 | +# Enable CORS |
| 25 | +app.add_middleware( |
| 26 | + CORSMiddleware, |
| 27 | + allow_origins=["chrome-extension://*"], # Allow requests from any Chrome extension |
| 28 | + allow_credentials=True, |
| 29 | + allow_methods=["*"], |
| 30 | + allow_headers=["*"], |
| 31 | +) |
| 32 | + |
| 33 | +# Store repo paths temporarily |
| 34 | +repo_cache: Dict[str, str] = {} |
| 35 | + |
| 36 | + |
| 37 | +class CloneRequest(BaseModel): |
| 38 | + url: str |
| 39 | + |
| 40 | + |
| 41 | +@app.post("/clone") |
| 42 | +async def clone_repository(request: CloneRequest) -> Dict[str, str]: |
| 43 | + """Clone a git repository and return a temporary ID to reference it.""" |
| 44 | + try: |
| 45 | + # Create a temporary directory |
| 46 | + temp_dir = tempfile.mkdtemp() |
| 47 | + logger.info(f"Created temp directory: {temp_dir}") |
| 48 | + |
| 49 | + # Clone the repository |
| 50 | + logger.info(f"Cloning repository from {request.url}") |
| 51 | + repo = Repo.clone_from(request.url, temp_dir) |
| 52 | + |
| 53 | + # Generate a simple ID (you might want to use UUID in production) |
| 54 | + repo_id = str(hash(request.url)) |
| 55 | + |
| 56 | + # Store the mapping |
| 57 | + repo_cache[repo_id] = temp_dir |
| 58 | + logger.info(f"Successfully cloned repository. ID: {repo_id}") |
| 59 | + |
| 60 | + return {"repo_id": repo_id} |
| 61 | + except Exception as e: |
| 62 | + logger.error(f"Error cloning repository: {str(e)}", exc_info=True) |
| 63 | + raise HTTPException(status_code=500, detail=str(e)) |
| 64 | + |
| 65 | + |
| 66 | +@app.get("/analyze/{analysis_type}") |
| 67 | +async def analyze_repo( |
| 68 | + analysis_type: str, repo_id: str |
| 69 | +) -> Dict[str, Union[int, float]]: |
| 70 | + """Analyze a git repository using the specified analysis type.""" |
| 71 | + try: |
| 72 | + # Get repo path from cache |
| 73 | + repo_path = repo_cache.get(repo_id) |
| 74 | + if not repo_path: |
| 75 | + logger.error(f"Repository not found for ID: {repo_id}") |
| 76 | + raise HTTPException( |
| 77 | + status_code=404, detail="Repository not found. Please clone it first." |
| 78 | + ) |
| 79 | + |
| 80 | + logger.info(f"Analyzing repository at {repo_path}") |
| 81 | + |
| 82 | + analysis_functions = { |
| 83 | + "commit-frequency": analyze_commit_frequency, |
| 84 | + "contributor-activity": analyze_contributor_activity, |
| 85 | + "commit-frequency-by-weekday": analyze_commit_frequency_by_weekday, |
| 86 | + "commit-frequency-by-hour": analyze_commit_frequency_by_hour, |
| 87 | + "average-commit-size": analyze_average_commit_size, |
| 88 | + "file-change-frequency": analyze_file_change_frequency, |
| 89 | + } |
| 90 | + |
| 91 | + if analysis_type not in analysis_functions: |
| 92 | + logger.error(f"Invalid analysis type: {analysis_type}") |
| 93 | + raise HTTPException( |
| 94 | + status_code=400, |
| 95 | + detail=f"Invalid analysis type. Must be one of: {', '.join(analysis_functions.keys())}", |
| 96 | + ) # Execute analysis |
| 97 | + logger.info(f"Running {analysis_type} analysis") |
| 98 | + results = analysis_functions[analysis_type](repo_path) |
| 99 | + logger.info(f"Analysis complete: {results}") |
| 100 | + |
| 101 | + # Cast the dictionary to the expected type |
| 102 | + if not isinstance(results, dict): |
| 103 | + raise HTTPException( |
| 104 | + status_code=500, detail="Analysis returned invalid type" |
| 105 | + ) |
| 106 | + return results |
| 107 | + except Exception as e: |
| 108 | + logger.error(f"Error during analysis: {str(e)}", exc_info=True) |
| 109 | + raise HTTPException(status_code=500, detail=str(e)) |
0 commit comments