951a522
use axum::Json;
use axum::extract::{Path, State};
use export::rest::AppResponseData;
use jwt::Session;
use crate::errors::{ApiError, GitApiError};
use crate::handler::require_writer;
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> {
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))
}