Skip to content
Clone
ba6fe0f
222 lines 6.5 KB Rust
use export::http::{Json, Path, State};
use export::rest::{AppResponseData, NoData};
use serde::Deserialize;
use utoipa::IntoParams;
use uuid::Uuid;
use token::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<BranchWhitelistDto>)
)
)]
pub async fn add_branch_whitelist(
State(service): State<GitService>,
session: Session,
Path(p): Path<BranchPolicyPath>,
Json(cmd): Json<AddBranchWhitelistCmd>,
) -> Result<AppResponseData<BranchWhitelistDto>, 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<NoData>)
)
)]
pub async fn remove_branch_whitelist(
State(service): State<GitService>,
session: Session,
Path(p): Path<BranchWhitelistPath>,
) -> Result<AppResponseData<NoData>, 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<Vec<BranchWhitelistDto>>)
)
)]
pub async fn list_branch_whitelists(
State(service): State<GitService>,
session: Session,
Path(p): Path<BranchPolicyPath>,
) -> Result<AppResponseData<Vec<BranchWhitelistDto>>, 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<TagWhitelistDto>)
)
)]
pub async fn add_tag_whitelist(
State(service): State<GitService>,
session: Session,
Path(p): Path<TagPolicyPath>,
Json(cmd): Json<AddTagWhitelistCmd>,
) -> Result<AppResponseData<TagWhitelistDto>, 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<export::rest::NoData>)
)
)]
pub async fn remove_tag_whitelist(
State(service): State<GitService>,
session: Session,
Path(p): Path<TagWhitelistPath>,
) -> Result<AppResponseData<NoData>, 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<Vec<TagWhitelistDto>>)
)
)]
pub async fn list_tag_whitelists(
State(service): State<GitService>,
session: Session,
Path(p): Path<TagPolicyPath>,
) -> Result<AppResponseData<Vec<TagWhitelistDto>>, 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))
}