feat(server/webhook): implement unregister webhook

Closes kemitix/git-next#17
This commit is contained in:
Paul Campbell 2024-04-13 20:59:57 +01:00
parent df2d9d684c
commit e7060800eb
2 changed files with 33 additions and 11 deletions

View file

@ -39,9 +39,8 @@ impl Actor for RepoActor {
match self.webhook_id.take() { match self.webhook_id.take() {
Some(webhook_id) => { Some(webhook_id) => {
let repo_details = self.details.clone(); let repo_details = self.details.clone();
let addr = ctx.address();
let net = self.net.clone(); let net = self.net.clone();
webhook::unregister(webhook_id, repo_details, addr, net) webhook::unregister(webhook_id, repo_details, net)
.into_actor(self) .into_actor(self)
.wait(ctx); .wait(ctx);
Running::Continue Running::Continue

View file

@ -1,7 +1,8 @@
use actix::prelude::*; 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::{ use crate::server::actors::{
repo::{RepoActor, ValidateRepo}, repo::{RepoActor, ValidateRepo},
@ -22,21 +23,43 @@ impl Deref for WebhookId {
&self.0 &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( pub async fn unregister(
_webhook_id: WebhookId, webhook_id: WebhookId,
_repo_details: crate::server::config::RepoDetails, repo_details: crate::server::config::RepoDetails,
_addr: actix::prelude::Addr<super::RepoActor>, net: network::Network,
_net: kxio::network::Network,
) { ) {
// TODO: (#17) unregister webhook info!(?webhook_id, "unregister webhook");
// on success - stop the actor 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( pub async fn register(
_repo_details: crate::server::config::RepoDetails, _repo_details: crate::server::config::RepoDetails,
addr: actix::prelude::Addr<super::RepoActor>, addr: actix::prelude::Addr<super::RepoActor>,
_net: kxio::network::Network, _net: network::Network,
) { ) {
// TODO: (#15) register webhook - on success send webhook id to RepoActor // TODO: (#15) register webhook - on success send webhook id to RepoActor
tokio::time::sleep(std::time::Duration::from_secs(10)).await; tokio::time::sleep(std::time::Duration::from_secs(10)).await;