2024-05-22 08:41:30 +01:00
|
|
|
//
|
2024-04-13 16:16:09 +01:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
2024-05-29 19:22:05 +01:00
|
|
|
use derive_more::Constructor;
|
|
|
|
use git_next_config::{ForgeAlias, RepoAlias};
|
2024-05-22 08:41:30 +01:00
|
|
|
use git_next_repo_actor::webhook::WebhookMessage;
|
2024-05-04 12:37:35 +01:00
|
|
|
use tracing::{debug, info};
|
2024-04-13 16:16:09 +01:00
|
|
|
|
|
|
|
pub struct WebhookRouter {
|
2024-05-04 12:37:35 +01:00
|
|
|
span: tracing::Span,
|
2024-05-29 19:22:05 +01:00
|
|
|
recipients: HashMap<ForgeAlias, HashMap<RepoAlias, Recipient<WebhookMessage>>>,
|
2024-04-13 16:16:09 +01:00
|
|
|
}
|
|
|
|
impl WebhookRouter {
|
|
|
|
pub fn new() -> Self {
|
2024-05-04 12:37:35 +01:00
|
|
|
let span = tracing::info_span!("WebhookRouter");
|
|
|
|
Self {
|
|
|
|
span,
|
2024-05-29 19:22:05 +01:00
|
|
|
recipients: Default::default(),
|
2024-05-04 12:37:35 +01:00
|
|
|
}
|
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-05-29 19:22:05 +01:00
|
|
|
let forge_alias = msg.forge_alias();
|
2024-05-20 20:41:01 +01:00
|
|
|
let repo_alias = msg.repo_alias();
|
2024-05-29 19:22:05 +01:00
|
|
|
debug!(forge = %forge_alias, repo = %repo_alias, "Router...");
|
|
|
|
let Some(forge_repos) = self.recipients.get(forge_alias) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let Some(recipient) = forge_repos.get(repo_alias) else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
info!(repo = %repo_alias, "Sending to Recipient");
|
|
|
|
recipient.do_send(msg);
|
2024-04-13 16:16:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 19:22:05 +01:00
|
|
|
#[derive(Message, Constructor)]
|
2024-04-13 16:16:09 +01:00
|
|
|
#[rtype(result = "()")]
|
2024-05-29 19:22:05 +01:00
|
|
|
pub struct AddWebhookRecipient {
|
|
|
|
pub forge_alias: ForgeAlias,
|
|
|
|
pub repo_alias: RepoAlias,
|
|
|
|
pub recipient: Recipient<WebhookMessage>,
|
|
|
|
}
|
2024-04-13 16:16:09 +01:00
|
|
|
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();
|
2024-05-29 19:22:05 +01:00
|
|
|
info!(forge = %msg.forge_alias, repo = %msg.repo_alias, "Register Recipient");
|
|
|
|
if !self.recipients.contains_key(&msg.forge_alias) {
|
|
|
|
self.recipients
|
|
|
|
.insert(msg.forge_alias.clone(), HashMap::new());
|
|
|
|
}
|
|
|
|
self.recipients
|
|
|
|
.get_mut(&msg.forge_alias)
|
|
|
|
.map(|repos| repos.insert(msg.repo_alias, msg.recipient));
|
2024-04-13 16:16:09 +01:00
|
|
|
}
|
|
|
|
}
|