use actix_web::{HttpResponse, web}; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::models::prs::PrCheckRun; use crate::service::AppService; use crate::service::pr::check_runs::{CreateCheckRunParams, UpdateCheckRunParams}; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct PrPath { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, /// PR number (unique within the repository) pub number: i64, } #[derive(Debug, Deserialize, IntoParams)] pub struct CheckRunPath { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, /// PR number (unique within the repository) pub number: i64, /// Check run ID (UUID) pub check_run_id: uuid::Uuid, } #[derive(Debug, Deserialize, IntoParams)] pub struct QP { pub limit: Option, pub offset: Option, } /// List check runs for a PR #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/check-runs", tag = "Pull Requests", operation_id = "prListCheckRuns", params(PrPath, QP), responses( (status = 200, description = "Check runs listed.", body = ApiResponse>), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 404, description = "PR not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn list_check_runs( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let runs = service .pr .pr_check_runs( &session, &path.workspace_name, &path.repo_name, path.number, query.limit.unwrap_or(50), query.offset.unwrap_or(0), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(runs))) } /// Create a check run. Requires Member role. #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/check-runs", tag = "Pull Requests", operation_id = "prCreateCheckRun", params(PrPath), request_body(content = CreateCheckRunParams, description = "Check run parameters", content_type = "application/json"), responses( (status = 201, description = "Check run created.", body = ApiResponse), (status = 400, description = "Invalid status", body = ApiErrorResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions", body = ApiErrorResponse), (status = 404, description = "PR not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn create_check_run( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let run = service .pr .pr_create_check_run( &session, &path.workspace_name, &path.repo_name, path.number, params.into_inner(), ) .await?; Ok(HttpResponse::Created().json(ApiResponse::new(run))) } /// Update a check run. Requires Member role. #[utoipa::path( put, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/check-runs/{check_run_id}", tag = "Pull Requests", operation_id = "prUpdateCheckRun", params(CheckRunPath), request_body(content = UpdateCheckRunParams, description = "Check run update parameters", content_type = "application/json"), responses( (status = 200, description = "Check run updated.", body = ApiResponse), (status = 400, description = "Invalid status", body = ApiErrorResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions", body = ApiErrorResponse), (status = 404, description = "Check run not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn update_check_run( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let run = service .pr .pr_update_check_run( &session, &path.workspace_name, &path.repo_name, path.number, path.check_run_id, params.into_inner(), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(run))) } /// Delete a check run. Requires Admin role. #[utoipa::path( delete, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/check-runs/{check_run_id}", tag = "Pull Requests", operation_id = "prDeleteCheckRun", params(CheckRunPath), responses( (status = 200, description = "Check run deleted.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions (Admin required)", body = ApiErrorResponse), (status = 404, description = "Check run not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn delete_check_run( service: web::Data, session: Session, path: web::Path, ) -> Result { service .pr .pr_delete_check_run( &session, &path.workspace_name, &path.repo_name, path.number, path.check_run_id, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Check run deleted".to_string()))) }