Files
gitks/api/pr/labels.rs
T
zhenyi d98e4d59e3 feat(api): implement pull request assignees and check runs endpoints
- Add PR assignees API with list, assign, and unassign operations
- Add PR check runs API with create, update, list, and delete operations
- Implement workspace finding by ID method in core service
- Update .gitignore to include .env* files while preserving .env.example
- Reorder imports in multiple API files for consistency
- Format function calls with proper line breaks across PR-related APIs
- Add wiki revision comparison endpoint with proper schema definitions
- Integrate new API modules into main application setup
- Add health check, readiness probe, and OpenAPI endpoints to main server
- Configure session management and dependency injection in main application
2026-06-07 23:01:05 +08:00

296 lines
10 KiB
Rust

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<i64>,
pub offset: Option<i64>,
}
// ── 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<Vec<PrLabel>>),
(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<AppService>,
session: Session,
path: web::Path<RepoPath>,
) -> Result<HttpResponse, AppError> {
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<PrLabel>),
(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<AppService>,
session: Session,
path: web::Path<RepoPath>,
params: web::Json<CreatePrLabelParams>,
) -> Result<HttpResponse, AppError> {
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<PrLabel>),
(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<AppService>,
session: Session,
path: web::Path<LabelIdPath>,
params: web::Json<UpdatePrLabelParams>,
) -> Result<HttpResponse, AppError> {
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<String>),
(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<AppService>,
session: Session,
path: web::Path<LabelIdPath>,
) -> Result<HttpResponse, AppError> {
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<Vec<PrLabelRelation>>),
(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<AppService>,
session: Session,
path: web::Path<PrPath>,
query: web::Query<QP>,
) -> Result<HttpResponse, AppError> {
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<PrLabelRelation>),
(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<AppService>,
session: Session,
path: web::Path<LabelPath>,
) -> Result<HttpResponse, AppError> {
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<String>),
(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<AppService>,
session: Session,
path: web::Path<LabelPath>,
) -> Result<HttpResponse, AppError> {
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())))
}