use actix_web::{HttpResponse, web}; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::models::issues::IssuePrRelation; use crate::service::AppService; use crate::service::issues::pr_relations::LinkPrParams; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct PathParams { /// Workspace name (unique identifier) pub workspace_name: String, /// Issue number (unique within the workspace) pub number: i64, } #[derive(Debug, Deserialize, IntoParams)] pub struct QueryParams { /// Maximum number of relations to return (default: 50, max: 100) pub limit: Option, /// Number of relations to skip for pagination (default: 0) pub offset: Option, } /// List pull request relations for an issue /// /// Returns a paginated list of all pull requests linked to the given issue. /// Shows relation type (closes, references, depends_on, etc.) and link metadata. /// Requires read access to the issue. #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/prs", tag = "Issues", operation_id = "issueListPrRelations", params(PathParams, QueryParams), responses( (status = 200, description = "PR relations listed successfully. Returns array of PR relation objects.", body = ApiResponse>), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions to view this issue", body = ApiErrorResponse), (status = 404, description = "Issue not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn list_pr_relations( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let relations = service .issue .issue_pr_relations( &session, &path.workspace_name, path.number, query.limit.unwrap_or(50), query.offset.unwrap_or(0), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(relations))) } /// Link a pull request to an issue /// /// Creates a relation between the given issue and a pull request. /// Commonly used to mark a PR as closing or referencing an issue. /// Requires write access to the issue. /// /// Parameters: /// - pull_request_id: Pull request ID (UUID) to link /// - relation_type: Relation type ("closes", "references", "depends_on", default: "references") /// /// Returns the created relation. #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/prs", tag = "Issues", operation_id = "issueLinkPr", params(PathParams), request_body( content = LinkPrParams, description = "Link pull request parameters", content_type = "application/json" ), responses( (status = 200, description = "Pull request linked successfully. Returns the created relation.", body = ApiResponse), (status = 400, description = "Invalid parameters: invalid relation type", body = ApiErrorResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions to edit this issue", body = ApiErrorResponse), (status = 404, description = "Issue or pull request not found", body = ApiErrorResponse), (status = 409, description = "Pull request is already linked to this issue", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn link_pr( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let relation = service .issue .issue_link_pr( &session, &path.workspace_name, path.number, params.into_inner(), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(relation))) } #[derive(Debug, Deserialize, IntoParams)] pub struct RelationPathParams { /// Workspace name (unique identifier) pub workspace_name: String, /// Issue number (unique within the workspace) pub number: i64, /// Relation ID (UUID) pub relation_id: uuid::Uuid, } /// Unlink a pull request from an issue /// /// Removes a pull request relation from the given issue. /// Requires write access to the issue. /// /// Returns success message on completion. #[utoipa::path( delete, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/prs/{relation_id}", tag = "Issues", operation_id = "issueUnlinkPr", params(RelationPathParams), responses( (status = 200, description = "Pull request unlinked successfully.", body = ApiResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions to edit this issue", body = ApiErrorResponse), (status = 404, description = "PR relation not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn unlink_pr( service: web::Data, session: Session, path: web::Path, ) -> Result { service .issue .issue_unlink_pr( &session, &path.workspace_name, path.number, path.relation_id, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("PR unlinked".to_string()))) }