use export::http::{Json, Path, State}; use export::rest::{AppResponseData, NoData}; use serde::Deserialize; use utoipa::IntoParams; use uuid::Uuid; use ::session::Session; use crate::errors::{ApiError, GitApiError}; use crate::handler; use crate::service::GitService; use crate::service::whitelists::{ AddBranchWhitelistCmd, AddTagWhitelistCmd, BranchWhitelistDto, TagWhitelistDto, }; #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct BranchPolicyPath { pub space_name: String, pub repo_name: String, pub policy_id: Uuid, } #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct BranchWhitelistPath { pub space_name: String, pub repo_name: String, pub policy_id: Uuid, pub whitelist_id: Uuid, } #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct TagPolicyPath { pub space_name: String, pub repo_name: String, pub policy_id: Uuid, } #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct TagWhitelistPath { pub space_name: String, pub repo_name: String, pub policy_id: Uuid, pub user_id: i64, } #[utoipa::path( post, path = "/repos/{space_name}/{repo_name}/branch-protection/{policy_id}/whitelists", tag = "branch_protection", description = "为分支保护策略添加用户或群组白名单。", params(BranchPolicyPath), request_body = AddBranchWhitelistCmd, responses( (status = 200, description = "add branch whitelist", body = AppResponseData) ) )] pub async fn add_branch_whitelist( State(service): State, session: Session, Path(p): Path, Json(cmd): Json, ) -> Result, ApiError> { let (ctx, _) = handler::require_repo_admin( &service, &session, p.space_name.clone(), p.repo_name.clone(), ) .await?; let out = service .add_branch_whitelist(ctx.model.id, p.policy_id, cmd) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( delete, path = "/repos/{space_name}/{repo_name}/branch-protection/{policy_id}/whitelists/{whitelist_id}", tag = "branch_protection", description = "删除指定分支保护策略下的白名单条目。", params(BranchWhitelistPath), responses( (status = 200, description = "remove branch whitelist", body = AppResponseData) ) )] pub async fn remove_branch_whitelist( State(service): State, session: Session, Path(p): Path, ) -> Result, ApiError> { let (ctx, _) = handler::require_repo_admin( &service, &session, p.space_name.clone(), p.repo_name.clone(), ) .await?; service .remove_branch_whitelist(ctx.model.id, p.policy_id, p.whitelist_id) .await .map_err(GitApiError::from)?; Ok(AppResponseData::empty()) } #[utoipa::path( get, path = "/repos/{space_name}/{repo_name}/branch-protection/{policy_id}/whitelists", tag = "branch_protection", description = "列出分支保护策略关联的用户与群组白名单。", params(BranchPolicyPath), responses( (status = 200, description = "list branch whitelists", body = AppResponseData>) ) )] pub async fn list_branch_whitelists( State(service): State, session: Session, Path(p): Path, ) -> Result>, ApiError> { let (ctx, _) = handler::require_repo_admin( &service, &session, p.space_name.clone(), p.repo_name.clone(), ) .await?; let out = service .list_branch_whitelists(ctx.model.id, p.policy_id) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( post, path = "/repos/{space_name}/{repo_name}/tag-protection/{policy_id}/whitelists", tag = "tag_protection", description = "为标签保护策略添加用户白名单。", params(TagPolicyPath), request_body = AddTagWhitelistCmd, responses( (status = 200, description = "add tag whitelist", body = AppResponseData) ) )] pub async fn add_tag_whitelist( State(service): State, session: Session, Path(p): Path, Json(cmd): Json, ) -> Result, ApiError> { let (ctx, _) = handler::require_repo_admin( &service, &session, p.space_name.clone(), p.repo_name.clone(), ) .await?; let out = service .add_tag_whitelist(ctx.model.id, p.policy_id, cmd) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( delete, path = "/repos/{space_name}/{repo_name}/tag-protection/{policy_id}/whitelists/{user_id}", tag = "tag_protection", description = "移除标签保护策略中的指定用户白名单。", params(TagWhitelistPath), responses( (status = 200, description = "remove tag whitelist", body = AppResponseData) ) )] pub async fn remove_tag_whitelist( State(service): State, session: Session, Path(p): Path, ) -> Result, ApiError> { let (ctx, _) = handler::require_repo_admin( &service, &session, p.space_name.clone(), p.repo_name.clone(), ) .await?; service .remove_tag_whitelist(ctx.model.id, p.policy_id, p.user_id) .await .map_err(GitApiError::from)?; Ok(AppResponseData::empty()) } #[utoipa::path( get, path = "/repos/{space_name}/{repo_name}/tag-protection/{policy_id}/whitelists", tag = "tag_protection", description = "列出标签保护策略关联的用户白名单。", params(TagPolicyPath), responses( (status = 200, description = "list tag whitelists", body = AppResponseData>) ) )] pub async fn list_tag_whitelists( State(service): State, session: Session, Path(p): Path, ) -> Result>, ApiError> { let (ctx, _) = handler::require_repo_admin( &service, &session, p.space_name.clone(), p.repo_name.clone(), ) .await?; let out = service .list_tag_whitelists(ctx.model.id, p.policy_id) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) }