2024-04-13 16:16:09 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
2024-05-04 12:37:35 +01:00
|
|
|
use tracing::{debug, info};
|
2024-04-13 16:16:09 +01:00
|
|
|
|
2024-05-11 18:57:18 +01:00
|
|
|
use crate::{actors::webhook::message::WebhookMessage, config::RepoAlias};
|
2024-04-13 16:16:09 +01:00
|
|
|
|
|
|
|
pub struct WebhookRouter {
|
2024-05-04 12:37:35 +01:00
|
|
|
span: tracing::Span,
|
2024-04-13 16:16:09 +01:00
|
|
|
repos: HashMap<RepoAlias, Recipient<WebhookMessage>>,
|
|
|
|
}
|
|
|
|
impl WebhookRouter {
|
|
|
|
pub fn new() -> Self {
|
2024-05-04 12:37:35 +01:00
|
|
|
let span = tracing::info_span!("WebhookRouter");
|
|
|
|
Self {
|
|
|
|
span,
|
|
|
|
repos: Default::default(),
|
|
|
|
}
|
2024-04-13 16:16:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Actor for WebhookRouter {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Handler<WebhookMessage> for WebhookRouter {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: WebhookMessage, _ctx: &mut Self::Context) -> Self::Result {
|
2024-05-04 12:37:35 +01:00
|
|
|
let _gaurd = self.span.enter();
|
2024-04-13 16:16:09 +01:00
|
|
|
let repo_alias = RepoAlias(msg.path().clone());
|
2024-05-04 12:37:35 +01:00
|
|
|
debug!(repo = %repo_alias, "Router...");
|
2024-04-13 16:16:09 +01:00
|
|
|
if let Some(recipient) = self.repos.get(&repo_alias) {
|
2024-05-09 20:39:04 +01:00
|
|
|
info!(repo = %repo_alias, "Sending to Recipient");
|
2024-04-13 16:16:09 +01:00
|
|
|
recipient.do_send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
|
|
|
#[rtype(result = "()")]
|
|
|
|
pub struct AddWebhookRecipient(pub RepoAlias, pub Recipient<WebhookMessage>);
|
|
|
|
impl Handler<AddWebhookRecipient> for WebhookRouter {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: AddWebhookRecipient, _ctx: &mut Self::Context) -> Self::Result {
|
2024-05-04 12:37:35 +01:00
|
|
|
let _gaurd = self.span.enter();
|
|
|
|
info!(repo = %msg.0, "Register Recipient");
|
2024-04-13 16:16:09 +01:00
|
|
|
self.repos.insert(msg.0, msg.1);
|
|
|
|
}
|
|
|
|
}
|