From e7060800eb959787c879559856335e53c0c74664 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 13 Apr 2024 20:59:57 +0100 Subject: [PATCH] feat(server/webhook): implement unregister webhook Closes kemitix/git-next#17 --- src/server/actors/repo/mod.rs | 3 +-- src/server/actors/repo/webhook.rs | 41 ++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/server/actors/repo/mod.rs b/src/server/actors/repo/mod.rs index 6a6d0a1..5514dfe 100644 --- a/src/server/actors/repo/mod.rs +++ b/src/server/actors/repo/mod.rs @@ -39,9 +39,8 @@ impl Actor for RepoActor { match self.webhook_id.take() { Some(webhook_id) => { let repo_details = self.details.clone(); - let addr = ctx.address(); let net = self.net.clone(); - webhook::unregister(webhook_id, repo_details, addr, net) + webhook::unregister(webhook_id, repo_details, net) .into_actor(self) .wait(ctx); Running::Continue diff --git a/src/server/actors/repo/webhook.rs b/src/server/actors/repo/webhook.rs index 005ae49..6c62d16 100644 --- a/src/server/actors/repo/webhook.rs +++ b/src/server/actors/repo/webhook.rs @@ -1,7 +1,8 @@ use actix::prelude::*; -use tracing::info; +use kxio::network; +use tracing::{info, warn}; -use std::ops::Deref; +use std::{fmt::Display, ops::Deref}; use crate::server::actors::{ repo::{RepoActor, ValidateRepo}, @@ -22,21 +23,43 @@ impl Deref for WebhookId { &self.0 } } +impl Display for WebhookId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} pub async fn unregister( - _webhook_id: WebhookId, - _repo_details: crate::server::config::RepoDetails, - _addr: actix::prelude::Addr, - _net: kxio::network::Network, + webhook_id: WebhookId, + repo_details: crate::server::config::RepoDetails, + net: network::Network, ) { - // TODO: (#17) unregister webhook - // on success - stop the actor + info!(?webhook_id, "unregister webhook"); + let hostname = &repo_details.forge.hostname; + let path = repo_details.repo; + let url = network::NetUrl::new(format!( + "https://{hostname}/api/v1/repos/{path}/hooks/{webhook_id}" + )); + let request = network::NetRequest::new( + network::RequestMethod::Delete, + url, + network::NetRequestHeaders::new(), + network::RequestBody::None, + network::ResponseType::Json, + None, + network::NetRequestLogging::None, + ); + let result = net.delete(request).await; + match result { + Ok(_) => info!(?webhook_id, "unregistered webhook"), + Err(_) => warn!(?webhook_id, "Failed to unregister webhook"), + } } pub async fn register( _repo_details: crate::server::config::RepoDetails, addr: actix::prelude::Addr, - _net: kxio::network::Network, + _net: network::Network, ) { // TODO: (#15) register webhook - on success send webhook id to RepoActor tokio::time::sleep(std::time::Duration::from_secs(10)).await;