/
basuev
/
gverse
Обзор
Документация
Войти
/
basuev
/
gverse
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/api/webhooks.rs
177 строк
5 KB
basuev
initial commit: gverse v0.1.0
27 май 2026, 08:52
27 май 2026, 08:52
98b24dd
Код
Авторство
О чём код?
use crate::api::split_repo; use crate::client::Client; use crate::config::Config; use crate::error::{Error, Result}; use crate::output::{Format, print_value}; use clap::Subcommand; use reqwest::Method; use serde_json::{Map, Value}; #[derive(Subcommand, Debug)] pub enum WebhooksCmd { List { repo: String, #[arg(long)] page: Option<u32>, #[arg(long)] limit: Option<u32>, }, View { repo: String, id: u64, }, Create { repo: String, #[arg(long)] url: String, #[arg(long, default_value = "json")] content_type: String, #[arg(long, value_delimiter = ',')] events: Vec<String>, #[arg(long)] secret: Option<String>, #[arg(long)] active: bool, }, Edit { repo: String, id: u64, #[arg(long)] url: Option<String>, #[arg(long)] content_type: Option<String>, #[arg(long, value_delimiter = ',')] events: Option<Vec<String>>, #[arg(long)] secret: Option<String>, #[arg(long)] active: Option<bool>, }, Delete { repo: String, id: u64, #[arg(long)] yes: bool, }, } pub async fn run(cmd: WebhooksCmd, cfg: &Config, profile: Option<&str>, fmt: Format) -> Result<()> { let p = cfg.active_profile(profile)?; let client = Client::new(p)?; match cmd { WebhooksCmd::List { repo, page, limit } => { let (owner, name) = split_repo(&repo)?; let mut q: Vec<(&str, String)> = Vec::new(); if let Some(v) = page { q.push(("page", v.to_string())); } if let Some(v) = limit { q.push(("limit", v.to_string())); } let v = client .get_value(&format!("/repos/{owner}/{name}/hooks"), &q) .await?; print_value(fmt, &v); Ok(()) } WebhooksCmd::View { repo, id } => { let (owner, name) = split_repo(&repo)?; let v = client .get_value(&format!("/repos/{owner}/{name}/hooks/{id}"), &[]) .await?; print_value(fmt, &v); Ok(()) } WebhooksCmd::Create { repo, url, content_type, events, secret, active, } => { let (owner, name) = split_repo(&repo)?; let mut config = Map::new(); config.insert("url".into(), Value::String(url)); config.insert("content_type".into(), Value::String(content_type)); if let Some(s) = secret { config.insert("secret".into(), Value::String(s)); } let mut payload = Map::new(); payload.insert("type".into(), Value::String("gitea".into())); payload.insert("config".into(), Value::Object(config)); payload.insert("active".into(), Value::Bool(active)); if !events.is_empty() { payload.insert( "events".into(), Value::Array(events.into_iter().map(Value::String).collect()), ); } let v = client .send_json( Method::POST, &format!("/repos/{owner}/{name}/hooks"), &Value::Object(payload), ) .await?; print_value(fmt, &v); Ok(()) } WebhooksCmd::Edit { repo, id, url, content_type, events, secret, active, } => { let (owner, name) = split_repo(&repo)?; let mut payload = Map::new(); let mut config = Map::new(); if let Some(u) = url { config.insert("url".into(), Value::String(u)); } if let Some(c) = content_type { config.insert("content_type".into(), Value::String(c)); } if let Some(s) = secret { config.insert("secret".into(), Value::String(s)); } if !config.is_empty() { payload.insert("config".into(), Value::Object(config)); } if let Some(e) = events { payload.insert( "events".into(), Value::Array(e.into_iter().map(Value::String).collect()), ); } if let Some(a) = active { payload.insert("active".into(), Value::Bool(a)); } let v = client .send_json( Method::PATCH, &format!("/repos/{owner}/{name}/hooks/{id}"), &Value::Object(payload), ) .await?; print_value(fmt, &v); Ok(()) } WebhooksCmd::Delete { repo, id, yes } => { let (owner, name) = split_repo(&repo)?; if !yes { return Err(Error::Invalid(format!( "refusing to delete webhook {id} without --yes" ))); } client .send_empty(Method::DELETE, &format!("/repos/{owner}/{name}/hooks/{id}")) .await?; eprintln!("deleted webhook {id}"); Ok(()) } } }