use axum::Json; use axum::extract::{Path, State}; 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) ) )] pub async fn add_branch_whitelist( State(service): State, Path(p): Path, Json(cmd): Json, ) -> Result, 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) ) )] pub async fn remove_branch_whitelist( State(service): State, Path(p): Path, ) -> Result, 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>) ) )] pub async fn list_branch_whitelists( State(service): State, Path(p): Path, ) -> Result>, 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) ) )] pub async fn add_tag_whitelist( State(service): State, Path(p): Path, Json(cmd): Json, ) -> Result, 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) ) )] pub async fn remove_tag_whitelist( State(service): State, Path(p): Path, ) -> Result, 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>) ) )] pub async fn list_tag_whitelists( State(service): State, Path(p): Path, ) -> Result>, 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, _: AppRequestData) {}