Skip to content
Clone
5f84089
52 lines 1.5 KB Rust
use ::session::Session;
use export::http::{Json, Path, State};
use export::rest::AppResponseData;
use crate::errors::{ApiError, GitApiError};
use crate::handler;
use crate::service::GitService;
use crate::service::hooks::{HookEventKind, ScriptOutputDto};
#[derive(Debug, Clone, serde::Deserialize, utoipa::IntoParams)]
pub struct HookScriptPath {
pub space_name: String,
pub repo_name: String,
pub script: HookEventKind,
}
#[derive(Debug, Clone, serde::Deserialize, utoipa::ToSchema)]
pub struct HookExecBody {
#[serde(default)]
pub args: Vec<String>,
}
#[utoipa::path(
post,
path = "/repos/{space_name}/{repo_name}/hooks/{script}",
tag = "hooks",
description = "以受控参数执行仓库 pre-receive 或 post-receive 脚本并回传结果。",
params(HookScriptPath),
request_body = HookExecBody,
responses(
(status = 200, description = "execute hook script", body = AppResponseData<ScriptOutputDto>)
)
)]
pub async fn execute_script(
State(service): State<GitService>,
session: Session,
Path(path): Path<HookScriptPath>,
Json(body): Json<HookExecBody>,
) -> Result<AppResponseData<ScriptOutputDto>, ApiError> {
handler::require_writer(
&service,
&session,
path.space_name.clone(),
path.repo_name.clone(),
)
.await?;
let out = service
.hook_execute_script(path.space_name, path.repo_name, path.script, body.args)
.await
.map_err(GitApiError::from)?;
Ok(AppResponseData::ok(out))
}