Compare commits

..

2 commits

Author SHA1 Message Date
022e6bcbb0 WIP: feat: support sending messages to a notification webhook
All checks were successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
2024-07-19 07:59:57 +01:00
f50cb40cf0 WIP: feat: configure a webhook for receiving notifications 2024-07-19 07:59:57 +01:00
12 changed files with 61 additions and 27 deletions

View file

@ -41,7 +41,7 @@ type Result<T> = core::result::Result<T, Error>;
)] )]
pub struct ServerConfig { pub struct ServerConfig {
http: Http, http: Http,
webhook: Webhook, webhook: InboundWebhook,
notifications: Notification, notifications: Notification,
storage: ServerStorage, storage: ServerStorage,
pub forge: BTreeMap<String, ForgeConfig>, pub forge: BTreeMap<String, ForgeConfig>,
@ -69,7 +69,7 @@ impl ServerConfig {
&self.notifications &self.notifications
} }
pub const fn webhook(&self) -> &Webhook { pub const fn webhook(&self) -> &InboundWebhook {
&self.webhook &self.webhook
} }
@ -118,10 +118,10 @@ impl Http {
serde::Deserialize, serde::Deserialize,
derive_more::Constructor, derive_more::Constructor,
)] )]
pub struct Webhook { pub struct InboundWebhook {
url: String, url: String,
} }
impl Webhook { impl InboundWebhook {
pub fn url(&self, forge_alias: &ForgeAlias, repo_alias: &RepoAlias) -> WebhookUrl { pub fn url(&self, forge_alias: &ForgeAlias, repo_alias: &RepoAlias) -> WebhookUrl {
let base_url = &self.url; let base_url = &self.url;
WebhookUrl(format!("{base_url}/{forge_alias}/{repo_alias}")) WebhookUrl(format!("{base_url}/{forge_alias}/{repo_alias}"))
@ -189,7 +189,7 @@ pub enum NotificationType {
)] )]
pub struct Notification { pub struct Notification {
r#type: NotificationType, r#type: NotificationType,
webhook: Option<Webhook>, webhook: Option<OutboundWebhook>,
} }
impl Notification { impl Notification {
pub const fn none() -> Self { pub const fn none() -> Self {
@ -198,7 +198,7 @@ impl Notification {
webhook: None, webhook: None,
} }
} }
pub const fn webhook(webhook: Webhook) -> Self { pub const fn webhook(webhook: OutboundWebhook) -> Self {
Self { Self {
r#type: NotificationType::Webhook, r#type: NotificationType::Webhook,
webhook: Some(webhook), webhook: Some(webhook),
@ -214,3 +214,19 @@ impl Default for Notification {
Self::none() Self::none()
} }
} }
#[derive(
Clone,
Debug,
derive_more::From,
PartialEq,
Eq,
PartialOrd,
Ord,
derive_more::AsRef,
serde::Deserialize,
derive_more::Constructor,
)]
pub struct OutboundWebhook {
url: String,
}

View file

@ -7,9 +7,9 @@ use std::collections::BTreeMap;
use std::path::PathBuf; use std::path::PathBuf;
use crate::server::Http; use crate::server::Http;
use crate::server::InboundWebhook;
use crate::server::ServerConfig; use crate::server::ServerConfig;
use crate::server::ServerStorage; use crate::server::ServerStorage;
use crate::server::Webhook;
use crate::webhook::push::Branch; use crate::webhook::push::Branch;
mod url; mod url;
@ -695,7 +695,7 @@ mod push {
} }
mod given { mod given {
use crate::server::Notification; use crate::server::{Notification, OutboundWebhook};
use super::*; use super::*;
use rand::Rng as _; use rand::Rng as _;
@ -719,7 +719,7 @@ mod given {
pub fn a_server_config() -> ServerConfig { pub fn a_server_config() -> ServerConfig {
ServerConfig::new( ServerConfig::new(
an_http(), an_http(),
a_webhook(), an_inbound_webhook(),
a_notification_config(), a_notification_config(),
a_server_storage(), a_server_storage(),
some_forge_configs(), some_forge_configs(),
@ -741,14 +741,17 @@ mod given {
pub fn a_port() -> u16 { pub fn a_port() -> u16 {
rand::thread_rng().gen() rand::thread_rng().gen()
} }
pub fn a_webhook() -> Webhook { pub fn an_inbound_webhook() -> InboundWebhook {
Webhook::new(a_name()) InboundWebhook::new(a_name())
}
pub fn an_outbound_webhook() -> OutboundWebhook {
OutboundWebhook::new(a_name())
} }
pub fn a_server_storage() -> ServerStorage { pub fn a_server_storage() -> ServerStorage {
ServerStorage::new(a_name().into()) ServerStorage::new(a_name().into())
} }
pub fn a_notification_config() -> Notification { pub fn a_notification_config() -> Notification {
Notification::webhook(a_webhook()) Notification::webhook(an_outbound_webhook())
} }
pub fn some_forge_configs() -> BTreeMap<String, ForgeConfig> { pub fn some_forge_configs() -> BTreeMap<String, ForgeConfig> {
[(a_name(), a_forge_config())].into() [(a_name(), a_forge_config())].into()

View file

@ -703,7 +703,7 @@ mod forgejo {
forge_alias: &config::ForgeAlias, forge_alias: &config::ForgeAlias,
repo_alias: &config::RepoAlias, repo_alias: &config::RepoAlias,
) -> git_next_config::server::WebhookUrl { ) -> git_next_config::server::WebhookUrl {
config::server::Webhook::new(a_name()).url(forge_alias, repo_alias) config::server::InboundWebhook::new(a_name()).url(forge_alias, repo_alias)
} }
pub fn any_webhook_url() -> git_next_config::server::WebhookUrl { pub fn any_webhook_url() -> git_next_config::server::WebhookUrl {

View file

@ -510,7 +510,7 @@ mod github {
use std::path::PathBuf; use std::path::PathBuf;
use git_next_config::{server::Webhook, WebhookId}; use git_next_config::{server::InboundWebhook, WebhookId};
use kxio::network::{MockNetwork, StatusCode}; use kxio::network::{MockNetwork, StatusCode};
use rand::RngCore; use rand::RngCore;
use serde_json::json; use serde_json::json;
@ -648,7 +648,7 @@ mod github {
forge_alias: &ForgeAlias, forge_alias: &ForgeAlias,
repo_alias: &RepoAlias, repo_alias: &RepoAlias,
) -> git_next_config::server::WebhookUrl { ) -> git_next_config::server::WebhookUrl {
Webhook::new(a_name()).url(forge_alias, repo_alias) InboundWebhook::new(a_name()).url(forge_alias, repo_alias)
} }
pub fn any_webhook_url() -> git_next_config::server::WebhookUrl { pub fn any_webhook_url() -> git_next_config::server::WebhookUrl {
given::a_webhook_url(&given::a_forge_alias(), &given::a_repo_alias()) given::a_webhook_url(&given::a_forge_alias(), &given::a_repo_alias())

View file

@ -37,7 +37,7 @@ pub struct RepoActor {
generation: git::Generation, generation: git::Generation,
message_token: messages::MessageToken, message_token: messages::MessageToken,
repo_details: git::RepoDetails, repo_details: git::RepoDetails,
webhook: config::server::Webhook, webhook: config::server::InboundWebhook,
webhook_id: Option<config::WebhookId>, // INFO: if [None] then no webhook is configured webhook_id: Option<config::WebhookId>, // INFO: if [None] then no webhook is configured
webhook_auth: Option<config::WebhookAuth>, // INFO: if [None] then no webhook is configured webhook_auth: Option<config::WebhookAuth>, // INFO: if [None] then no webhook is configured
last_main_commit: Option<git::Commit>, last_main_commit: Option<git::Commit>,
@ -53,7 +53,7 @@ impl RepoActor {
pub fn new( pub fn new(
repo_details: git::RepoDetails, repo_details: git::RepoDetails,
forge: Box<dyn git::ForgeLike>, forge: Box<dyn git::ForgeLike>,
webhook: config::server::Webhook, webhook: config::server::InboundWebhook,
generation: git::Generation, generation: git::Generation,
net: Network, net: Network,
repository_factory: Box<dyn git::repository::RepositoryFactory>, repository_factory: Box<dyn git::repository::RepositoryFactory>,

View file

@ -57,7 +57,7 @@ pub fn a_webhook_url(
forge_alias: &ForgeAlias, forge_alias: &ForgeAlias,
repo_alias: &RepoAlias, repo_alias: &RepoAlias,
) -> git_next_config::server::WebhookUrl { ) -> git_next_config::server::WebhookUrl {
config::server::Webhook::new(a_name()).url(forge_alias, repo_alias) config::server::InboundWebhook::new(a_name()).url(forge_alias, repo_alias)
} }
pub fn a_name() -> String { pub fn a_name() -> String {
@ -179,8 +179,8 @@ pub fn a_message_token() -> MessageToken {
MessageToken::default() MessageToken::default()
} }
pub fn a_webhook(url: &WebhookUrl) -> Webhook { pub fn a_webhook(url: &WebhookUrl) -> InboundWebhook {
Webhook::new(url.clone().into()) InboundWebhook::new(url.clone().into())
} }
pub fn a_forge() -> Box<MockForgeLike> { pub fn a_forge() -> Box<MockForgeLike> {

View file

@ -7,7 +7,7 @@ use actor::{
}; };
use assert2::let_assert; use assert2::let_assert;
use config::{ use config::{
server::{Webhook, WebhookUrl}, server::{InboundWebhook, WebhookUrl},
webhook::forge_notification::Body, webhook::forge_notification::Body,
BranchName, ForgeAlias, ForgeConfig, ForgeNotification, ForgeType, GitDir, RegisteredWebhook, BranchName, ForgeAlias, ForgeConfig, ForgeNotification, ForgeType, GitDir, RegisteredWebhook,
RepoAlias, RepoBranches, RepoConfig, ServerRepoConfig, WebhookAuth, WebhookId, RepoAlias, RepoBranches, RepoConfig, ServerRepoConfig, WebhookAuth, WebhookId,
@ -82,7 +82,7 @@ pub struct RepoActorView {
pub generation: git::Generation, pub generation: git::Generation,
pub message_token: MessageToken, pub message_token: MessageToken,
pub repo_details: git::RepoDetails, pub repo_details: git::RepoDetails,
pub webhook: config::server::Webhook, pub webhook: config::server::InboundWebhook,
pub webhook_id: Option<config::WebhookId>, // INFO: if [None] then no webhook is configured pub webhook_id: Option<config::WebhookId>, // INFO: if [None] then no webhook is configured
pub webhook_auth: Option<config::WebhookAuth>, // INFO: if [None] then no webhook is configured pub webhook_auth: Option<config::WebhookAuth>, // INFO: if [None] then no webhook is configured
pub last_main_commit: Option<git::Commit>, pub last_main_commit: Option<git::Commit>,

View file

@ -7,7 +7,7 @@ pub mod messages;
use actix::prelude::*; use actix::prelude::*;
use git_next_config as config; use git_next_config as config;
use git_next_config::server::{ServerConfig, ServerStorage, Webhook}; use git_next_config::server::{InboundWebhook, ServerConfig, ServerStorage};
use git_next_config::{ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig}; use git_next_config::{ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig};
use git_next_git::{Generation, RepoDetails}; use git_next_git::{Generation, RepoDetails};
use git_next_repo_actor::{messages::CloneRepo, RepoActor}; use git_next_repo_actor::{messages::CloneRepo, RepoActor};
@ -104,7 +104,7 @@ impl Server {
forge_config: &ForgeConfig, forge_config: &ForgeConfig,
forge_name: ForgeAlias, forge_name: ForgeAlias,
server_storage: &ServerStorage, server_storage: &ServerStorage,
webhook: &Webhook, webhook: &InboundWebhook,
) -> Vec<(ForgeAlias, RepoAlias, RepoActor)> { ) -> Vec<(ForgeAlias, RepoAlias, RepoActor)> {
let span = let span =
tracing::info_span!("create_forge_repos", name = %forge_name, config = %forge_config); tracing::info_span!("create_forge_repos", name = %forge_name, config = %forge_config);
@ -129,7 +129,7 @@ impl Server {
forge_name: ForgeAlias, forge_name: ForgeAlias,
forge_config: ForgeConfig, forge_config: ForgeConfig,
server_storage: &ServerStorage, server_storage: &ServerStorage,
webhook: &Webhook, webhook: &InboundWebhook,
) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeAlias, RepoAlias, RepoActor) { ) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeAlias, RepoAlias, RepoActor) {
let server_storage = server_storage.clone(); let server_storage = server_storage.clone();
let webhook = webhook.clone(); let webhook = webhook.clone();

View file

@ -1,7 +1,7 @@
// //
use crate::{tests::given, ReceiveServerConfig, Server}; use crate::{tests::given, ReceiveServerConfig, Server};
use actix::prelude::*; use actix::prelude::*;
use git_next_config::server::{Http, Notification, ServerConfig, ServerStorage, Webhook}; use git_next_config::server::{Http, InboundWebhook, Notification, ServerConfig, ServerStorage};
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
@ -21,7 +21,7 @@ async fn when_webhook_url_has_trailing_slash_should_not_send() {
// collaborators // collaborators
let http = Http::new("0.0.0.0".to_string(), 80); let http = Http::new("0.0.0.0".to_string(), 80);
let webhook = Webhook::new("http://localhost/".to_string()); // With trailing slash let webhook = InboundWebhook::new("http://localhost/".to_string()); // With trailing slash
let notifications = Notification::none(); let notifications = Notification::none();
let server_storage = ServerStorage::new((fs.base()).to_path_buf()); let server_storage = ServerStorage::new((fs.base()).to_path_buf());
let repos = BTreeMap::default(); let repos = BTreeMap::default();

View file

@ -1 +1,2 @@
mod notify_user;
mod shutdown_webhook; mod shutdown_webhook;

View file

@ -0,0 +1,12 @@
//
use actix::prelude::*;
use crate::{messages::NotifyUser, WebhookActor};
impl Handler<NotifyUser> for WebhookActor {
type Result = ();
fn handle(&mut self, _msg: NotifyUser, _ctx: &mut Self::Context) -> Self::Result {
// TODO: (#95) should notify user
}
}

View file

@ -2,3 +2,5 @@
use git_next_actor_macros::message; use git_next_actor_macros::message;
message!(ShutdownWebhook: "Request to shutdown the Webhook actor"); message!(ShutdownWebhook: "Request to shutdown the Webhook actor");
message!(NotifyUser: String: "Request to send the message payload to the notification webhook");