diff --git a/Cargo.lock b/Cargo.lock index e86650d..c9864f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -914,6 +914,8 @@ dependencies = [ "mockall", "mockall_double", "prost", + "serde", + "serde_json", "tokio", "tonic", "tonic-build", @@ -2016,9 +2018,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.97" +version = "1.0.113" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdf3bf93142acad5821c99197022e170842cdbc1c30482b98750c688c640842a" +checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79" dependencies = [ "itoa", "ryu", diff --git a/crates/flux-releaser/Cargo.toml b/crates/flux-releaser/Cargo.toml index c381d48..2f3303d 100644 --- a/crates/flux-releaser/Cargo.toml +++ b/crates/flux-releaser/Cargo.toml @@ -18,6 +18,8 @@ async-trait = "0.1.77" mockall_double = "0.3.1" aws-config = { version = "1.1.5", features = ["behavior-version-latest"] } aws-sdk-s3 = { version = "1.15.0", features = ["behavior-version-latest"] } +serde = { version = "1.0.196", features = ["derive"] } +serde_json = "1.0.113" [build-dependencies] tonic-build = "0.11.0" diff --git a/crates/flux-releaser/src/services.rs b/crates/flux-releaser/src/services.rs index d77da38..5338f9c 100644 --- a/crates/flux-releaser/src/services.rs +++ b/crates/flux-releaser/src/services.rs @@ -1,2 +1,3 @@ +mod domain_events; mod file_store; pub mod release_manager; diff --git a/crates/flux-releaser/src/services/domain_events.rs b/crates/flux-releaser/src/services/domain_events.rs new file mode 100644 index 0000000..c53ea93 --- /dev/null +++ b/crates/flux-releaser/src/services/domain_events.rs @@ -0,0 +1,24 @@ +use std::{path::PathBuf, sync::Arc}; + +use super::release_manager::models::ArtifactID; + +pub mod extensions; + +#[derive(Clone)] +pub struct DomainEvents {} + +#[cfg(test)] +use mockall::{automock, mock, predicate::*}; + +#[cfg_attr(test, automock)] +impl DomainEvents { + pub fn new() -> Self { + Self {} + } + + pub async fn publish_event(&self, event: &str) -> anyhow::Result<()> { + tracing::trace!("publish events: {}", event); + + Ok(()) + } +} diff --git a/crates/flux-releaser/src/services/domain_events/extensions.rs b/crates/flux-releaser/src/services/domain_events/extensions.rs new file mode 100644 index 0000000..164f3ad --- /dev/null +++ b/crates/flux-releaser/src/services/domain_events/extensions.rs @@ -0,0 +1,14 @@ +use crate::app::SharedApp; + +#[mockall_double::double] +use super::DomainEvents; + +pub trait DomainEventsExt { + fn domain_events(&self) -> DomainEvents; +} + +impl DomainEventsExt for SharedApp { + fn domain_events(&self) -> DomainEvents { + DomainEvents::new() + } +} diff --git a/crates/flux-releaser/src/services/release_manager.rs b/crates/flux-releaser/src/services/release_manager.rs index 6ee650b..c98e930 100644 --- a/crates/flux-releaser/src/services/release_manager.rs +++ b/crates/flux-releaser/src/services/release_manager.rs @@ -1,15 +1,24 @@ +use serde::Serialize; + #[mockall_double::double] use crate::services::file_store::FileStore; +#[mockall_double::double] +use super::domain_events::DomainEvents; + use self::models::{ArtifactID, CommitArtifact}; pub struct ReleaseManager { file_store: FileStore, + domain_events: DomainEvents, } impl ReleaseManager { - pub fn new(file_store: FileStore) -> Self { - Self { file_store } + pub fn new(file_store: FileStore, domain_events: DomainEvents) -> Self { + Self { + file_store, + domain_events, + } } pub async fn commit_artifact(&self, request: CommitArtifact) -> anyhow::Result { @@ -20,17 +29,27 @@ impl ReleaseManager { .upload_files(artifact_id.clone(), Vec::new()) .await?; - // publish domain event that it has been published + self.domain_events + .publish_event(&serde_json::to_string(&CommittedArtifactEvent { + artifact_id: artifact_id.to_string(), + })?) + .await?; Ok(artifact_id) } } +#[derive(Serialize)] +pub struct CommittedArtifactEvent { + artifact_id: String, +} + pub mod extensions; pub mod models; #[cfg(test)] mod test { + use crate::services::domain_events::MockDomainEvents; use crate::services::file_store::MockFileStore; use super::*; @@ -43,7 +62,13 @@ mod test { .times(1) .returning(|_, _| Ok(())); - let releaser_manager = ReleaseManager::new(file_store); + let mut domain_events = MockDomainEvents::default(); + domain_events + .expect_publish_event() + .times(1) + .returning(|_| Ok(())); + + let releaser_manager = ReleaseManager::new(file_store, domain_events); releaser_manager .commit_artifact(CommitArtifact { diff --git a/crates/flux-releaser/src/services/release_manager/extensions.rs b/crates/flux-releaser/src/services/release_manager/extensions.rs index f96900c..0030be5 100644 --- a/crates/flux-releaser/src/services/release_manager/extensions.rs +++ b/crates/flux-releaser/src/services/release_manager/extensions.rs @@ -1,4 +1,7 @@ -use crate::{app::SharedApp, services::file_store::extensions::FileStoreExt}; +use crate::{ + app::SharedApp, + services::{domain_events::extensions::DomainEventsExt, file_store::extensions::FileStoreExt}, +}; use super::ReleaseManager; @@ -8,6 +11,6 @@ pub trait ReleaseManagerExt { impl ReleaseManagerExt for SharedApp { fn release_manager(&self) -> ReleaseManager { - ReleaseManager::new(self.file_store()) + ReleaseManager::new(self.file_store(), self.domain_events()) } }