2024-04-13 16:16:09 +01:00
|
|
|
use actix::prelude::*;
|
2024-04-13 20:59:57 +01:00
|
|
|
use kxio::network;
|
|
|
|
use tracing::{info, warn};
|
2024-04-13 16:16:09 +01:00
|
|
|
|
2024-04-13 20:59:57 +01:00
|
|
|
use std::{fmt::Display, ops::Deref};
|
2024-04-09 19:30:05 +01:00
|
|
|
|
2024-04-13 16:16:09 +01:00
|
|
|
use crate::server::actors::{
|
|
|
|
repo::{RepoActor, ValidateRepo},
|
|
|
|
webhook::WebhookMessage,
|
|
|
|
};
|
2024-04-11 18:30:36 +01:00
|
|
|
|
2024-04-09 19:30:05 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub struct WebhookId(String);
|
|
|
|
impl WebhookId {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub const fn new(id: String) -> Self {
|
|
|
|
Self(id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Deref for WebhookId {
|
|
|
|
type Target = String;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
2024-04-13 20:59:57 +01:00
|
|
|
impl Display for WebhookId {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 19:30:05 +01:00
|
|
|
|
|
|
|
pub async fn unregister(
|
2024-04-13 20:59:57 +01:00
|
|
|
webhook_id: WebhookId,
|
|
|
|
repo_details: crate::server::config::RepoDetails,
|
|
|
|
net: network::Network,
|
2024-04-09 19:30:05 +01:00
|
|
|
) {
|
2024-04-13 20:59:57 +01:00
|
|
|
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"),
|
|
|
|
}
|
2024-04-09 19:30:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn register(
|
|
|
|
_repo_details: crate::server::config::RepoDetails,
|
2024-04-11 18:30:36 +01:00
|
|
|
addr: actix::prelude::Addr<super::RepoActor>,
|
2024-04-13 20:59:57 +01:00
|
|
|
_net: network::Network,
|
2024-04-09 19:30:05 +01:00
|
|
|
) {
|
|
|
|
// TODO: (#15) register webhook - on success send webhook id to RepoActor
|
2024-04-11 18:30:36 +01:00
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
|
|
|
addr.do_send(ValidateRepo);
|
2024-04-09 19:30:05 +01:00
|
|
|
}
|
2024-04-13 16:16:09 +01:00
|
|
|
|
|
|
|
impl Handler<WebhookMessage> for RepoActor {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: WebhookMessage, _ctx: &mut Self::Context) -> Self::Result {
|
|
|
|
let id = msg.id();
|
|
|
|
let body = msg.body();
|
|
|
|
info!(?id, ?body, "RepoActor received message");
|
|
|
|
// TODO: (#43) do something with the message
|
|
|
|
}
|
|
|
|
}
|