use crate::models::common::DEFAULT_REVISION; use actix_web::{HttpResponse, web}; use futures_util::StreamExt; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::ApiResponse; use crate::error::AppError; use crate::service::AppService; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct PathParams { pub workspace_name: String, pub repo_name: String, } #[derive(Debug, Deserialize, IntoParams)] pub struct RevisionPathParams { pub workspace_name: String, pub repo_name: String, pub revision: String, } #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/commit-diff/{revision}", tag = "Git", operation_id = "gitCommitDiff", params(RevisionPathParams), responses(( status = 200, body = ApiResponse )), security(("session_cookie" = [])) )] pub async fn git_commit_diff( service: web::Data, session: Session, path: web::Path, ) -> Result { let r = service .repo .git_get_commit_diff( &session, &path.workspace_name, &path.repo_name, &path.revision, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(r))) } #[derive(Debug, Deserialize, IntoParams)] pub struct PatchQuery { pub old: String, pub new: String, } #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/patch", tag = "Git", operation_id = "gitPatch", params(PathParams, PatchQuery), responses((status = 200, content_type = "text/plain")), security(("session_cookie" = [])) )] pub async fn git_patch( service: web::Data, session: Session, path: web::Path, q: web::Query, ) -> Result { let r = service .repo .git_get_patch( &session, &path.workspace_name, &path.repo_name, &q.old, &q.new, ) .await?; Ok(HttpResponse::Ok().content_type("text/plain").body(r)) } #[derive(Debug, Deserialize, IntoParams)] pub struct DiffQuery { pub old: String, pub new: String, } #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/raw-diff", tag = "Git", operation_id = "gitRawDiff", params(PathParams, DiffQuery), responses((status = 200, content_type = "text/plain")), security(("session_cookie" = [])) )] pub async fn git_raw_diff( service: web::Data, session: Session, path: web::Path, q: web::Query, ) -> Result { let r = service .repo .git_raw_diff( &session, &path.workspace_name, &path.repo_name, &q.old, &q.new, ) .await?; Ok(HttpResponse::Ok().content_type("text/plain").body(r)) } #[derive(Debug, Deserialize, IntoParams)] pub struct ChangedPathsQuery { pub old: String, pub new: String, } #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/changed-paths", tag = "Git", operation_id = "gitChangedPaths", params(PathParams, ChangedPathsQuery), responses(( status = 200, body = ApiResponse )), security(("session_cookie" = [])) )] pub async fn git_changed_paths( service: web::Data, session: Session, path: web::Path, q: web::Query, ) -> Result { let r = service .repo .git_find_changed_paths( &session, &path.workspace_name, &path.repo_name, &q.old, &q.new, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(r))) } #[derive(Debug, Deserialize, IntoParams)] pub struct StreamBlameQuery { pub path: String, pub revision: Option, } #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/stream-blame", tag = "Git", operation_id = "gitStreamBlame", params(PathParams, StreamBlameQuery), responses(( status = 200, body = ApiResponse> )), security(("session_cookie" = [])) )] pub async fn git_stream_blame( service: web::Data, session: Session, path: web::Path, q: web::Query, ) -> Result { let mut stream = service .repo .git_stream_blame( &session, &path.workspace_name, &path.repo_name, &q.path, q.revision.as_deref().unwrap_or(DEFAULT_REVISION), ) .await?; let mut hunks = Vec::new(); while let Some(Ok(hunk)) = stream.next().await { hunks.push(hunk); } Ok(HttpResponse::Ok().json(ApiResponse::new(hunks))) } #[derive(Debug, Deserialize, utoipa::ToSchema)] pub struct ResolveConflictsBody { pub target_branch: String, pub source_revision: String, pub message: Option, } #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/git/resolve-conflicts", tag = "Git", operation_id = "gitResolveConflicts", params(PathParams), request_body(content = ResolveConflictsBody), responses(( status = 200, body = ApiResponse )), security(("session_cookie" = [])) )] pub async fn git_resolve_conflicts( service: web::Data, session: Session, path: web::Path, body: web::Json, ) -> Result { let r = service .repo .git_resolve_merge_conflicts( &session, &path.workspace_name, &path.repo_name, &body.target_branch, &body.source_revision, body.message.as_deref().unwrap_or(""), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(r))) }