import { apiClient } from "./client"; import type { CommitDetail, CommitHistoryResponse, CommitResponse, GitRepository, GitRepositoryCreate, GitStatus, MergeResponse, URLParseResult, } from "../types/git-repository"; export type { CommitDetail, CommitHistoryEntry, CommitHistoryResponse, CommitResponse, GitRepository, GitRepositoryCreate, GitStatus, MergeResponse, URLParseResult, } from "../types/git-repository"; export interface Branch { name: string; is_default: boolean; last_commit: string | null; } export interface BranchesResponse { branches: Branch[]; default_branch: string; } export async function parseGitUrl(url: string): Promise { const response = await apiClient.post("/projects/repositories/parse-url", { url, }); return response.data; } export async function listRepositories( projectId: string, ): Promise { const response = await apiClient.get(`/projects/${projectId}/repositories`); return response.data; } export async function listRepositoryBranches( projectId: string, repoId: string, ): Promise { const response = await apiClient.get( `/projects/${projectId}/repositories/${repoId}/branches`, ); return response.data; } export async function createRepository( projectId: string, data: GitRepositoryCreate, ): Promise { const response = await apiClient.post( `/projects/${projectId}/repositories`, data, ); return response.data; } export async function deleteRepository( projectId: string, repoId: string, ): Promise { await apiClient.delete(`/projects/${projectId}/repositories/${repoId}`); } export async function getRepositoryHistory( projectId: string, repoId: string, branch?: string, limit?: number, ): Promise { const searchParams = new URLSearchParams(); if (branch) searchParams.set("branch", branch); if (limit) searchParams.set("limit", String(limit)); const queryString = searchParams.toString(); const params = queryString ? `?${queryString}` : ""; const response = await apiClient.get( `/projects/${projectId}/repositories/${repoId}/history${params}`, ); return response.data; } export async function getCommitDetail( projectId: string, repoId: string, commitHash: string, ): Promise { const response = await apiClient.get( `/projects/${projectId}/repositories/${repoId}/commits/${commitHash}`, ); return response.data; } export async function getRepositoryStatus( projectId: string, repoId: string, ): Promise { const response = await apiClient.get( `/projects/${projectId}/repositories/${repoId}/status`, ); return response.data; } export async function createBranch( projectId: string, repoId: string, name: string, baseBranch: string = "HEAD", ): Promise<{ message: string; branch: string }> { const response = await apiClient.post( `/projects/${projectId}/repositories/${repoId}/branches`, { name, base_branch: baseBranch }, ); return response.data; } export async function deleteBranch( projectId: string, repoId: string, branchName: string, force: boolean = false, ): Promise<{ message: string }> { const response = await apiClient.delete( `/projects/${projectId}/repositories/${repoId}/branches/${branchName}?force=${force}`, ); return response.data; } export async function checkoutBranch( projectId: string, repoId: string, branch: string, ): Promise<{ message: string; branch: string }> { const response = await apiClient.post( `/projects/${projectId}/repositories/${repoId}/checkout`, { branch }, ); return response.data; } export async function commitChanges( projectId: string, repoId: string, message: string, files?: string[], ): Promise { const response = await apiClient.post( `/projects/${projectId}/repositories/${repoId}/commit`, { message, files }, ); return response.data; } export async function fetchRepository( projectId: string, repoId: string, ): Promise<{ message: string }> { const response = await apiClient.post( `/projects/${projectId}/repositories/${repoId}/fetch`, ); return response.data; } export async function pullRepository( projectId: string, repoId: string, branch?: string, ): Promise<{ message: string }> { const params = branch ? `?branch=${branch}` : ""; const response = await apiClient.post( `/projects/${projectId}/repositories/${repoId}/pull${params}`, ); return response.data; } export async function pushRepository( projectId: string, repoId: string, branch?: string, ): Promise<{ message: string }> { const params = branch ? `?branch=${branch}` : ""; const response = await apiClient.post( `/projects/${projectId}/repositories/${repoId}/push${params}`, ); return response.data; } export async function mergeBranches( projectId: string, repoId: string, sourceBranch: string, targetBranch?: string, message?: string, ): Promise { const response = await apiClient.post( `/projects/${projectId}/repositories/${repoId}/merge`, { source_branch: sourceBranch, target_branch: targetBranch, message }, ); return response.data; }