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::{PrLabel, PrLabelRelation}; use crate::service::AppService; use crate::service::pr::labels::{CreatePrLabelParams, UpdatePrLabelParams}; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct RepoPath { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, } #[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 LabelPath { /// 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, /// Label ID (UUID) pub label_id: uuid::Uuid, } #[derive(Debug, Deserialize, IntoParams)] pub struct LabelIdPath { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, /// Label ID (UUID) pub label_id: uuid::Uuid, } #[derive(Debug, Deserialize, IntoParams)] pub struct QP { pub limit: Option, pub offset: Option, } // ── Repo-level labels ── /// List PR labels in a repository #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/labels", tag = "Pull Requests", operation_id = "prListLabels", params(RepoPath), responses( (status = 200, description = "Labels listed successfully.", body = ApiResponse>), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 404, description = "Repo not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn list_labels( service: web::Data, session: Session, path: web::Path, ) -> Result { let labels = service .pr .pr_labels(&session, &path.workspace_name, &path.repo_name) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(labels))) } /// Create a PR label. Requires Member role. #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/labels", tag = "Pull Requests", operation_id = "prCreateLabel", params(RepoPath), request_body(content = CreatePrLabelParams, description = "Label creation parameters", content_type = "application/json"), responses( (status = 201, description = "Label created.", body = ApiResponse), (status = 400, description = "Invalid parameters", body = ApiErrorResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions", body = ApiErrorResponse), (status = 404, description = "Repo not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn create_label( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let label = service .pr .pr_create_label( &session, &path.workspace_name, &path.repo_name, params.into_inner(), ) .await?; Ok(HttpResponse::Created().json(ApiResponse::new(label))) } /// Update a PR label. Requires Admin role. #[utoipa::path( put, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/labels/{label_id}", tag = "Pull Requests", operation_id = "prUpdateLabel", params(LabelIdPath), request_body(content = UpdatePrLabelParams, description = "Label update parameters", content_type = "application/json"), responses( (status = 200, description = "Label updated.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions (Admin required)", body = ApiErrorResponse), (status = 404, description = "Label not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn update_label( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let label = service .pr .pr_update_label( &session, &path.workspace_name, &path.repo_name, path.label_id, params.into_inner(), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(label))) } /// Delete a PR label. Requires Admin role. #[utoipa::path( delete, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/labels/{label_id}", tag = "Pull Requests", operation_id = "prDeleteLabel", params(LabelIdPath), responses( (status = 200, description = "Label deleted.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions (Admin required)", body = ApiErrorResponse), (status = 404, description = "Label not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn delete_label( service: web::Data, session: Session, path: web::Path, ) -> Result { service .pr .pr_delete_label( &session, &path.workspace_name, &path.repo_name, path.label_id, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Label deleted".to_string()))) } // ── PR-level label relations ── /// List labels assigned to a PR #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/labels", tag = "Pull Requests", operation_id = "prListLabelRelations", params(PrPath, QP), responses( (status = 200, description = "Label relations 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_label_relations( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let rels = service .pr .pr_label_relations( &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(rels))) } /// Assign a label to a PR #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/labels/{label_id}", tag = "Pull Requests", operation_id = "prAssignLabel", params(LabelPath), responses( (status = 200, description = "Label assigned.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions", body = ApiErrorResponse), (status = 404, description = "PR or label not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn assign_label( service: web::Data, session: Session, path: web::Path, ) -> Result { let rel = service .pr .pr_assign_label( &session, &path.workspace_name, &path.repo_name, path.number, path.label_id, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(rel))) } /// Unassign a label from a PR #[utoipa::path( delete, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/labels/{label_id}", tag = "Pull Requests", operation_id = "prUnassignLabel", params(LabelPath), responses( (status = 200, description = "Label unassigned.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions", body = ApiErrorResponse), (status = 404, description = "Label not assigned", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn unassign_label( service: web::Data, session: Session, path: web::Path, ) -> Result { service .pr .pr_unassign_label( &session, &path.workspace_name, &path.repo_name, path.number, path.label_id, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Label unassigned".to_string()))) }