Skip to content
Clone
4dc6d37
151 lines 4.8 KB Rust
use axum::extract::{Path, State};
use axum::Json;
use export::rest::{AppRequestData, AppResponseData, NoData};
use serde::Deserialize;
use utoipa::IntoParams;
use uuid::Uuid;
use crate::errors::{ApiError, GitApiError};
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: Uuid,
}
#[utoipa::path(
post,
path = "/repos/{space_name}/{repo_name}/branch-protection/{policy_id}/whitelists",
tag = "branch_protection",
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>,
Path(p): Path<BranchPolicyPath>,
Json(cmd): Json<AddBranchWhitelistCmd>,
) -> Result<AppResponseData<BranchWhitelistDto>, ApiError> {
let out = service.add_branch_whitelist(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",
params(BranchWhitelistPath),
responses(
(status = 200, description = "remove branch whitelist", body = AppResponseData<NoData>)
)
)]
pub async fn remove_branch_whitelist(
State(service): State<GitService>,
Path(p): Path<BranchWhitelistPath>,
) -> Result<AppResponseData<NoData>, ApiError> {
service.remove_branch_whitelist(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",
params(BranchPolicyPath),
responses(
(status = 200, description = "list branch whitelists", body = AppResponseData<Vec<BranchWhitelistDto>>)
)
)]
pub async fn list_branch_whitelists(
State(service): State<GitService>,
Path(p): Path<BranchPolicyPath>,
) -> Result<AppResponseData<Vec<BranchWhitelistDto>>, ApiError> {
let out = service.list_branch_whitelists(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",
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>,
Path(p): Path<TagPolicyPath>,
Json(cmd): Json<AddTagWhitelistCmd>,
) -> Result<AppResponseData<TagWhitelistDto>, ApiError> {
let out = service.add_tag_whitelist(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",
params(TagWhitelistPath),
responses(
(status = 200, description = "remove tag whitelist", body = AppResponseData<export::rest::NoData>)
)
)]
pub async fn remove_tag_whitelist(
State(service): State<GitService>,
Path(p): Path<TagWhitelistPath>,
) -> Result<AppResponseData<NoData>, ApiError> {
service.remove_tag_whitelist(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",
params(TagPolicyPath),
responses(
(status = 200, description = "list tag whitelists", body = AppResponseData<Vec<TagWhitelistDto>>)
)
)]
pub async fn list_tag_whitelists(
State(service): State<GitService>,
Path(p): Path<TagPolicyPath>,
) -> Result<AppResponseData<Vec<TagWhitelistDto>>, ApiError> {
let out = service.list_tag_whitelists(p.policy_id).await.map_err(GitApiError::from)?;
Ok(AppResponseData::ok(out))
}
#[allow(dead_code)]
fn _typecheck(_: AppRequestData<AddBranchWhitelistCmd>, _: AppRequestData<AddTagWhitelistCmd>) {}