| 1 | use { |
| 2 | crate::{ |
| 3 | cli::HookCommand, |
| 4 | context::Ctx, |
| 5 | ui::{self, prompt}, |
| 6 | }, |
| 7 | anyhow::Result, |
| 8 | }; |
| 9 | |
| 10 | pub async fn run(ctx: &Ctx, command: HookCommand) -> Result<()> { |
| 11 | ctx.require_token()?; |
| 12 | |
| 13 | match command { |
| 14 | HookCommand::List { repo } => list(ctx, repo).await, |
| 15 | HookCommand::Create { url, events, repo } => create(ctx, url, events, repo).await, |
| 16 | HookCommand::Delete { id, repo, yes } => delete(ctx, id, repo, yes).await, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | async fn list(ctx: &Ctx, repo: Option<String>) -> Result<()> { |
| 21 | let slug = ctx.resolve_repo(repo)?; |
| 22 | let hooks = ctx.client.hooks(&slug.owner, &slug.name).await?; |
| 23 | ui::emit(ctx.json, &hooks, |hooks| ui::render::hooks(hooks)) |
| 24 | } |
| 25 | |
| 26 | async fn create(ctx: &Ctx, url: String, events: Vec<String>, repo: Option<String>) -> Result<()> { |
| 27 | let slug = ctx.resolve_repo(repo)?; |
| 28 | let hook = ctx |
| 29 | .client |
| 30 | .create_hook(&slug.owner, &slug.name, &url, &events) |
| 31 | .await?; |
| 32 | |
| 33 | ui::emit(ctx.json, &hook, |hook| { |
| 34 | ui::success(format!("Created webhook #{}.", hook.id)); |
| 35 | if let Some(secret) = &hook.secret { |
| 36 | println!( |
| 37 | " {} {}", |
| 38 | ui::bold("Secret (shown once):"), |
| 39 | ui::highlight(secret) |
| 40 | ); |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | async fn delete(ctx: &Ctx, id: i64, repo: Option<String>, yes: bool) -> Result<()> { |
| 46 | let slug = ctx.resolve_repo(repo)?; |
| 47 | |
| 48 | if !yes && !prompt::confirm(&format!("Delete webhook #{id} on {slug}?"), false)? { |
| 49 | ui::note("Cancelled."); |
| 50 | return Ok(()); |
| 51 | } |
| 52 | |
| 53 | ctx.client.delete_hook(&slug.owner, &slug.name, id).await?; |
| 54 | ui::success(format!("Deleted webhook #{id}.")); |
| 55 | Ok(()) |
| 56 | } |