Files
appks/api/pr/create.rs
T
zhenyi 3a22c4265d feat(api): add pull request and wiki API endpoints with OpenAPI generator
- Add gen_openapi binary for generating OpenAPI specification
- Implement comprehensive pull request API endpoints including core operations
- Add pull request reviews, check runs, labels, assignees, and events APIs
- Include pull request status and merge strategy management endpoints
- Add wiki page CRUD operations with revision history and comparison
- Update OpenAPI documentation with Pull Requests and Wiki tags
- Modify workspace find function visibility for external access
- Integrate new API modules into main OpenAPI router configuration
2026-06-07 19:58:02 +08:00

77 lines
2.9 KiB
Rust

use actix_web::{web, HttpResponse};
use serde::Deserialize;
use utoipa::IntoParams;
use crate::api::response::{ApiResponse, ApiErrorResponse};
use crate::error::AppError;
use crate::models::prs::PullRequest;
use crate::service::pr::core::CreatePrParams;
use crate::service::AppService;
use crate::session::Session;
#[derive(Debug, Deserialize, IntoParams)]
pub struct PathParams {
/// Workspace name (unique identifier)
pub workspace_name: String,
/// Repository name (unique within the workspace)
pub repo_name: String,
}
/// Create a pull request
///
/// Creates a new pull request proposing changes from a source branch to a target branch.
/// Requires at least Member role in the repository.
///
/// Parameters:
/// - title: PR title (required)
/// - body: PR description in markdown (optional)
/// - source_repo_id: Source repository ID (supports cross-repo PRs from forks)
/// - source_branch: Source branch name (must exist)
/// - target_branch: Target branch name (must exist in the repo)
/// - head_commit_sha: Head commit SHA from the source branch
/// - base_commit_sha: Base commit SHA for diff calculation (optional)
/// - draft: Whether this is a draft PR (optional, defaults to false)
///
/// Effects:
/// - PR is created with auto-incrementing number
/// - PR status tracking is initialized
/// - Author is automatically subscribed
/// - Repository stats are updated
///
/// Returns the created PR with full metadata.
#[utoipa::path(
post,
path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs",
tag = "Pull Requests",
operation_id = "prCreate",
params(PathParams),
request_body(
content = CreatePrParams,
description = "PR creation parameters",
content_type = "application/json"
),
responses(
(status = 201, description = "PR created successfully. Returns the newly created PR with full metadata.", body = ApiResponse<PullRequest>),
(status = 400, description = "Invalid parameters: empty title, non-existent branch/commit, or invalid fork relationship", body = ApiErrorResponse),
(status = 401, description = "Authentication required or session expired", body = ApiErrorResponse),
(status = 403, description = "Insufficient permissions (requires Member role or higher)", body = ApiErrorResponse),
(status = 404, description = "Repository or workspace not found", body = ApiErrorResponse),
(status = 500, description = "Internal server error", body = ApiErrorResponse),
),
security(
("session_cookie" = [])
)
)]
pub async fn create(
service: web::Data<AppService>,
session: Session,
path: web::Path<PathParams>,
params: web::Json<CreatePrParams>,
) -> Result<HttpResponse, AppError> {
let pr = service
.pr
.pr_create(&session, &path.workspace_name, &path.repo_name, params.into_inner())
.await?;
Ok(HttpResponse::Created().json(ApiResponse::new(pr)))
}