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::IssueRepoRelation; use crate::service::AppService; use crate::service::issues::repo_relations::LinkRepoParams; 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 repository relations for an issue /// /// Returns a paginated list of all repositories linked to the given issue. /// Shows relation type (references, duplicates, blocks, etc.) and link metadata. /// Requires read access to the issue. #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/repos", tag = "Issues", operation_id = "issueListRepoRelations", params(PathParams, QueryParams), responses( (status = 200, description = "Repository relations listed successfully. Returns array of 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_repo_relations( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let relations = service .issue .issue_repo_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 repository to an issue /// /// Creates a relation between the given issue and a repository. /// Requires write access to the issue. /// /// Parameters: /// - repo_id: Repository ID (UUID) to link /// - relation_type: Relation type ("references", "duplicates", "blocks", "depends_on", default: "references") /// /// Returns the created relation. #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/issues/{number}/repos", tag = "Issues", operation_id = "issueLinkRepo", params(PathParams), request_body( content = LinkRepoParams, description = "Link repository parameters", content_type = "application/json" ), responses( (status = 200, description = "Repository 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 repository not found", body = ApiErrorResponse), (status = 409, description = "Repository is already linked to this issue", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn link_repo( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let relation = service .issue .issue_link_repo( &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 repository from an issue /// /// Removes a repository 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}/repos/{relation_id}", tag = "Issues", operation_id = "issueUnlinkRepo", params(RelationPathParams), responses( (status = 200, description = "Repository 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 = "Repository relation not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn unlink_repo( service: web::Data, session: Session, path: web::Path, ) -> Result { service .issue .issue_unlink_repo( &session, &path.workspace_name, path.number, path.relation_id, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Repo unlinked".to_string()))) }