use crate::{ApiResponse, error::ApiError}; use actix_web::{HttpResponse, Result, web}; use service::AppService; use service::git::diff::SideBySideDiffQuery; use service::pull_request::ReviewCommentListQuery; use session::Session; use super::ListQuery; #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls", params( ("namespace" = String, Path, description = "Project namespace"), ("repo" = String, Path, description = "Repository name"), ("status" = Option, Query), ("page" = Option, Query), ("per_page" = Option, Query), ), responses( (status = 200, description = "List pull requests", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pull_request_list( service: web::Data, session: Session, path: web::Path<(String, String)>, query: web::Query, ) -> Result { let (namespace, repo) = path.into_inner(); let resp = service .pull_request_list( namespace, repo, query.status.clone(), Some(query.page.unwrap_or(1)), Some(query.per_page.unwrap_or(20)), &session, ) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/{number}", params( ("namespace" = String, Path), ("repo" = String, Path), ("number" = i64, Path), ), responses( (status = 200, description = "Get pull request", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pull_request_get( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, number) = path.into_inner(); let resp = service .pull_request_get(namespace, repo, number, &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( post, path = "/api/repo_pr/{namespace}/{repo}/pulls", params( ("namespace" = String, Path), ("repo" = String, Path), ), request_body = service::pull_request::PullRequestCreateRequest, responses( (status = 200, description = "Create pull request", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pull_request_create( service: web::Data, session: Session, path: web::Path<(String, String)>, body: web::Json, ) -> Result { let (namespace, repo) = path.into_inner(); let resp = service .pull_request_create(namespace, repo, body.into_inner(), &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( patch, path = "/api/repo_pr/{namespace}/{repo}/pulls/{number}", params( ("namespace" = String, Path), ("repo" = String, Path), ("number" = i64, Path), ), request_body = service::pull_request::PullRequestUpdateRequest, responses( (status = 200, description = "Update pull request", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pull_request_update( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, body: web::Json, ) -> Result { let (namespace, repo, number) = path.into_inner(); let resp = service .pull_request_update(namespace, repo, number, body.into_inner(), &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( post, path = "/api/repo_pr/{namespace}/{repo}/pulls/{number}/close", params( ("namespace" = String, Path), ("repo" = String, Path), ("number" = i64, Path), ), responses( (status = 200, description = "Close pull request", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pull_request_close( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, number) = path.into_inner(); let resp = service .pull_request_close(namespace, repo, number, &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( post, path = "/api/repo_pr/{namespace}/{repo}/pulls/{number}/reopen", params( ("namespace" = String, Path), ("repo" = String, Path), ("number" = i64, Path), ), responses( (status = 200, description = "Reopen pull request", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pull_request_reopen( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, number) = path.into_inner(); let resp = service .pull_request_reopen(namespace, repo, number, &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( delete, path = "/api/repo_pr/{namespace}/{repo}/pulls/{number}", params( ("namespace" = String, Path), ("repo" = String, Path), ("number" = i64, Path), ), responses( (status = 200, description = "Delete pull request"), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pull_request_delete( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, number) = path.into_inner(); service .pull_request_delete(namespace, repo, number, &session) .await?; Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/summary", params( ("namespace" = String, Path), ("repo" = String, Path), ), responses( (status = 200, description = "Get pull request summary", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pull_request_summary( service: web::Data, session: Session, path: web::Path<(String, String)>, ) -> Result { let (namespace, repo) = path.into_inner(); let resp = service .pull_request_summary(namespace, repo, &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/reviews", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), responses( (status = 200, description = "List pull request reviews", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn review_list( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .review_list(namespace, repo, pr_number, &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( post, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/reviews", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), request_body = service::pull_request::ReviewSubmitRequest, responses( (status = 200, description = "Submit pull request review", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn review_submit( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, body: web::Json, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .review_submit(namespace, repo, pr_number, body.into_inner(), &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( patch, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/reviews", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), request_body = service::pull_request::ReviewUpdateRequest, responses( (status = 200, description = "Update pull request review", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn review_update( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, body: web::Json, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .review_update(namespace, repo, pr_number, body.into_inner(), &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( delete, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/reviews/{reviewer_id}", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ("reviewer_id" = String, Path, description = "Reviewer UUID"), ), responses( (status = 200, description = "Delete pull request review"), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn review_delete( service: web::Data, session: Session, path: web::Path<(String, String, i64, String)>, ) -> Result { let (namespace, repo, pr_number, reviewer_id) = path.into_inner(); let reviewer_uuid = uuid::Uuid::parse_str(&reviewer_id) .map_err(|_| service::error::AppError::BadRequest("Invalid reviewer ID".to_string()))?; service .review_delete(namespace, repo, pr_number, reviewer_uuid, &session) .await?; Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ("path" = Option, Query, description = "Filter by file path"), ("resolved" = Option, Query, description = "Filter by resolved status"), ("file_only" = Option, Query, description = "Only inline comments (true) or only general comments (false)"), ), responses( (status = 200, description = "List pull request review comments", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn review_comment_list( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, query: web::Query, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .review_comment_list(namespace, repo, pr_number, query.into_inner(), &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( post, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), request_body = service::pull_request::ReviewCommentCreateRequest, responses( (status = 200, description = "Create pull request review comment", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn review_comment_create( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, body: web::Json, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .review_comment_create(namespace, repo, pr_number, body.into_inner(), &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( patch, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments/{comment_id}", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ("comment_id" = i64, Path), ), request_body = service::pull_request::ReviewCommentUpdateRequest, responses( (status = 200, description = "Update pull request review comment", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn review_comment_update( service: web::Data, session: Session, path: web::Path<(String, String, i64, i64)>, body: web::Json, ) -> Result { let (namespace, repo, pr_number, comment_id) = path.into_inner(); let resp = service .review_comment_update( namespace, repo, pr_number, comment_id, body.into_inner(), &session, ) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( delete, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/comments/{comment_id}", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ("comment_id" = i64, Path), ), responses( (status = 200, description = "Delete pull request review comment"), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn review_comment_delete( service: web::Data, session: Session, path: web::Path<(String, String, i64, i64)>, ) -> Result { let (namespace, repo, pr_number, comment_id) = path.into_inner(); service .review_comment_delete(namespace, repo, pr_number, comment_id, &session) .await?; Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/merge", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), responses( (status = 200, description = "Get merge analysis", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn merge_analysis( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .merge_analysis(namespace, repo, pr_number, &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/conflicts", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), responses( (status = 200, description = "Check merge conflicts", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn merge_conflict_check( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .merge_conflict_check(namespace, repo, pr_number, &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( post, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/merge", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), request_body = service::pull_request::MergeRequest, responses( (status = 200, description = "Execute merge", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), (status = 409, description = "Conflict"), ), tag = "PullRequest" )] pub async fn merge_execute( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, body: web::Json, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .merge_execute(namespace, repo, pr_number, body.into_inner(), &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( post, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/merge/abort", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), responses( (status = 200, description = "Abort merge"), (status = 401, description = "Unauthorized"), (status = 403, description = "Forbidden"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn merge_abort( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); service .merge_abort(namespace, repo, pr_number, &session) .await?; Ok(ApiResponse::ok(serde_json::json!({ "success": true })).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/merge/in_progress", params( ("namespace" = String, Path), ("repo" = String, Path), ("pr_number" = i64, Path), ), responses( (status = 200, description = "Check if merge is in progress"), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn merge_is_in_progress( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .merge_is_in_progress(namespace, repo, pr_number, &session) .await?; Ok(ApiResponse::ok(serde_json::json!({ "in_progress": resp })).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/diff/side-by-side", params( ("namespace" = String, Path, description = "Project namespace"), ("repo" = String, Path, description = "Repository name"), ("pr_number" = i64, Path, description = "Pull request number"), ), responses( (status = 200, description = "Side-by-side diff for a pull request", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pr_diff_side_by_side( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, query: web::Query, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .pr_diff_side_by_side(namespace, repo, pr_number, query.into_inner(), &session) .await?; Ok(ApiResponse::ok(resp).to_response()) } #[utoipa::path( get, path = "/api/repo_pr/{namespace}/{repo}/pulls/{pr_number}/commits", params( ("namespace" = String, Path, description = "Project namespace"), ("repo" = String, Path, description = "Repository name"), ("pr_number" = i64, Path, description = "Pull request number"), ), responses( (status = 200, description = "List commits in a pull request", body = ApiResponse), (status = 401, description = "Unauthorized"), (status = 404, description = "Not found"), ), tag = "PullRequest" )] pub async fn pr_commits_list( service: web::Data, session: Session, path: web::Path<(String, String, i64)>, ) -> Result { let (namespace, repo, pr_number) = path.into_inner(); let resp = service .pr_commits_list(namespace, repo, pr_number, &session) .await?; Ok(ApiResponse::ok(resp).to_response()) }