From b1e5bf3788c23f23cd5714743051792c9045ba49 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 14 Jun 2024 09:26:27 +0100 Subject: [PATCH] WIP: add more tests to repo-actor crate --- bacon.toml | 6 +- crates/cli/src/main.rs | 5 +- crates/config/src/branch_name.rs | 2 +- .../src/{forge_name.rs => forge_alias.rs} | 2 +- crates/config/src/git_dir.rs | 7 +- crates/config/src/host_name.rs | 2 +- crates/config/src/lib.rs | 4 +- crates/config/src/newtype.rs | 57 +- crates/config/src/repo_branches.rs | 12 +- crates/config/src/repo_config.rs | 14 +- crates/config/src/repo_config_source.rs | 4 +- crates/config/src/server.rs | 17 +- crates/config/src/tests.rs | 83 +- crates/config/src/webhook/auth.rs | 6 +- crates/config/src/webhook/id.rs | 6 +- crates/config/src/webhook/tests.rs | 4 +- crates/forge-forgejo/src/lib.rs | 2 +- crates/forge/src/mock_forge.rs | 15 +- crates/forge/src/tests.rs | 2 +- crates/git/src/commit.rs | 21 +- crates/git/src/repo_details.rs | 6 + crates/git/src/repository/mock.rs | 47 +- crates/git/src/repository/mod.rs | 31 +- crates/git/src/repository/open/mod.rs | 24 +- crates/git/src/repository/open/omock.rs | 71 +- crates/git/src/repository/open/otest.rs | 91 +- crates/git/src/repository/open/tests.rs | 16 +- crates/git/src/repository/test.rs | 33 +- crates/git/src/repository/tests.rs | 146 +-- crates/git/src/tests.rs | 32 +- crates/git/src/validation/tests.rs | 32 +- crates/repo-actor/Cargo.toml | 7 + crates/repo-actor/src/branch.rs | 84 +- .../src/handlers/advance_to_main.rs | 52 + crates/repo-actor/src/handlers/clone.rs | 37 + .../src/handlers/load_config_from_repo.rs | 31 + .../repo-actor/src/handlers/loaded_config.rs | 20 + crates/repo-actor/src/handlers/mod.rs | 11 + .../src/handlers/start_monitoring.rs | 83 ++ crates/repo-actor/src/handlers/test.rs | 18 + .../repo-actor/src/handlers/validate_repo.rs | 98 ++ .../webhook_message.rs} | 7 +- .../src/handlers/webhook_registered.rs | 17 + crates/repo-actor/src/lib.rs | 315 +---- crates/repo-actor/src/load.rs | 97 +- crates/repo-actor/src/messages.rs | 89 ++ crates/repo-actor/src/status.rs | 33 - crates/repo-actor/src/tests.rs | 1040 ++++++++++++++++- crates/server/src/actors/server.rs | 55 +- crates/server/src/config/tests.rs | 2 +- crates/server/src/lib.rs | 17 +- 51 files changed, 2108 insertions(+), 805 deletions(-) rename crates/config/src/{forge_name.rs => forge_alias.rs} (69%) create mode 100644 crates/repo-actor/src/handlers/advance_to_main.rs create mode 100644 crates/repo-actor/src/handlers/clone.rs create mode 100644 crates/repo-actor/src/handlers/load_config_from_repo.rs create mode 100644 crates/repo-actor/src/handlers/loaded_config.rs create mode 100644 crates/repo-actor/src/handlers/mod.rs create mode 100644 crates/repo-actor/src/handlers/start_monitoring.rs create mode 100644 crates/repo-actor/src/handlers/test.rs create mode 100644 crates/repo-actor/src/handlers/validate_repo.rs rename crates/repo-actor/src/{webhook.rs => handlers/webhook_message.rs} (95%) create mode 100644 crates/repo-actor/src/handlers/webhook_registered.rs create mode 100644 crates/repo-actor/src/messages.rs delete mode 100644 crates/repo-actor/src/status.rs diff --git a/bacon.toml b/bacon.toml index 63dafe8..3d30b8b 100644 --- a/bacon.toml +++ b/bacon.toml @@ -4,7 +4,7 @@ # Complete help on configuration: https://dystroy.org/bacon/config/ default_job = "check" -reverse = true +# reverse = true [jobs.check] command = ["cargo", "check", "--color", "always"] @@ -48,7 +48,7 @@ need_stdout = false [jobs.doc-open] command = ["cargo", "doc", "--color", "always", "--no-deps", "--open"] need_stdout = false -on_success = "back" # so that we don't open the browser at each change +on_success = "back" # so that we don't open the browser at each change # You can run your application and have the result displayed in bacon, # *if* it makes sense for this crate. You can run an example the same @@ -57,7 +57,7 @@ on_success = "back" # so that we don't open the browser at each change # If you want to pass options to your program, a `--` separator # will be needed. [jobs.run] -command = [ "cargo", "run", "--color", "always" ] +command = ["cargo", "run", "--color", "always"] need_stdout = true allow_warnings = true diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index f8eb9da..0954f93 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -8,6 +8,9 @@ use std::path::PathBuf; use clap::Parser; use kxio::{fs, network::Network}; +/// How long to wait between rechecking the status of a repo. +const SLEEP_DURATION: std::time::Duration = std::time::Duration::from_secs(10); + #[derive(Parser, Debug)] #[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())] struct Commands { @@ -42,7 +45,7 @@ async fn main() { git_next_server::init(fs); } Server::Start => { - git_next_server::start(fs, net, repo).await; + git_next_server::start(fs, net, repo, SLEEP_DURATION).await; } }, } diff --git a/crates/config/src/branch_name.rs b/crates/config/src/branch_name.rs index 136f460..1eb81cf 100644 --- a/crates/config/src/branch_name.rs +++ b/crates/config/src/branch_name.rs @@ -1,2 +1,2 @@ // The name of a Branch -crate::newtype!(BranchName is a String); +crate::newtype!(BranchName is a String, derive_more::Display, Default); diff --git a/crates/config/src/forge_name.rs b/crates/config/src/forge_alias.rs similarity index 69% rename from crates/config/src/forge_name.rs rename to crates/config/src/forge_alias.rs index 1cb9892..e53790e 100644 --- a/crates/config/src/forge_name.rs +++ b/crates/config/src/forge_alias.rs @@ -1,5 +1,5 @@ // The name of a Forge to connect to -crate::newtype!(ForgeAlias is a String); +crate::newtype!(ForgeAlias is a String, derive_more::Display, Default); impl From<&ForgeAlias> for std::path::PathBuf { fn from(value: &ForgeAlias) -> Self { Self::from(&value.0) diff --git a/crates/config/src/git_dir.rs b/crates/config/src/git_dir.rs index 9716c01..b89222e 100644 --- a/crates/config/src/git_dir.rs +++ b/crates/config/src/git_dir.rs @@ -1,7 +1,7 @@ // use std::path::PathBuf; -crate::newtype!(GitDir is a PathBuf, without Display); +crate::newtype!(GitDir is a PathBuf, Default); impl GitDir { pub const fn pathbuf(&self) -> &PathBuf { &self.0 @@ -22,8 +22,3 @@ impl From<&GitDir> for PathBuf { value.to_path_buf() } } -impl From for PathBuf { - fn from(value: GitDir) -> Self { - value.0 - } -} diff --git a/crates/config/src/host_name.rs b/crates/config/src/host_name.rs index ca69be4..f38a0e5 100644 --- a/crates/config/src/host_name.rs +++ b/crates/config/src/host_name.rs @@ -1,2 +1,2 @@ // The hostname of a forge -crate::newtype!(Hostname is a String); +crate::newtype!(Hostname is a String, derive_more::Display, Default); diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index 260dee2..ab2cd4e 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -3,9 +3,9 @@ mod api_token; mod branch_name; pub mod common; +mod forge_alias; mod forge_config; mod forge_details; -mod forge_name; mod forge_type; pub mod git_dir; mod host_name; @@ -26,9 +26,9 @@ mod tests; pub use api_token::ApiToken; pub use branch_name::BranchName; +pub use forge_alias::ForgeAlias; pub use forge_config::ForgeConfig; pub use forge_details::ForgeDetails; -pub use forge_name::ForgeAlias; pub use forge_type::ForgeType; pub use git_dir::GitDir; pub use host_name::Hostname; diff --git a/crates/config/src/newtype.rs b/crates/config/src/newtype.rs index df2ef6c..9536d22 100644 --- a/crates/config/src/newtype.rs +++ b/crates/config/src/newtype.rs @@ -1,35 +1,10 @@ // #[macro_export] macro_rules! newtype { - ($name:ident is a $type:ty, without Display) => { - #[derive( - Clone, - Default, - Debug, - derive_more::From, - PartialEq, - Eq, - PartialOrd, - Ord, - Hash, - derive_more::AsRef, - derive_more::Deref, - serde::Deserialize, - serde::Serialize, - )] - pub struct $name($type); - impl $name { - pub fn new(value: impl Into<$type>) -> Self { - Self(value.into()) - } - pub fn unwrap(self) -> $type { - self.0 - } - } - }; - ($name:ident is a $type:ty) => { + ($name:ident) => { #[derive( Clone, + Copy, Default, Debug, derive_more::Display, @@ -40,18 +15,42 @@ macro_rules! newtype { Ord, Hash, derive_more::AsRef, + )] + pub struct $name; + impl $name { + pub fn new() -> Self { + Self + } + } + }; + ($name:ident is a $type:ty $(, $derive:ty)*) => { + #[derive( + Clone, + Debug, + derive_more::From, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + derive_more::AsRef, derive_more::Deref, - serde::Deserialize, - serde::Serialize, + $($derive),* )] pub struct $name($type); impl $name { pub fn new(value: impl Into<$type>) -> Self { Self(value.into()) } + #[allow(clippy::missing_const_for_fn)] pub fn unwrap(self) -> $type { self.0 } } + impl From<$name> for $type { + fn from(value: $name) -> $type { + value.unwrap() + } + } }; } diff --git a/crates/config/src/repo_branches.rs b/crates/config/src/repo_branches.rs index 637a2dd..7851370 100644 --- a/crates/config/src/repo_branches.rs +++ b/crates/config/src/repo_branches.rs @@ -2,7 +2,17 @@ use crate::BranchName; /// Mapped from `.git-next.toml` file at `branches` #[derive( - Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Constructor, derive_more::Display, + Clone, + Hash, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + serde::Deserialize, + serde::Serialize, + derive_more::Constructor, + derive_more::Display, )] #[display("{},{},{}", main, next, dev)] pub struct RepoBranches { diff --git a/crates/config/src/repo_config.rs b/crates/config/src/repo_config.rs index 6a649e7..d7661ca 100644 --- a/crates/config/src/repo_config.rs +++ b/crates/config/src/repo_config.rs @@ -5,7 +5,17 @@ use crate::RepoConfigSource; /// Is also derived from the optional parameters in `git-next-server.toml` at /// `forge.{forge}.repos.{repo}.(main|next|dev)` #[derive( - Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Constructor, derive_more::Display, + Clone, + Hash, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + serde::Deserialize, + serde::Serialize, + derive_more::Constructor, + derive_more::Display, )] #[display("{}", branches)] pub struct RepoConfig { @@ -13,7 +23,7 @@ pub struct RepoConfig { source: RepoConfigSource, } impl RepoConfig { - pub fn load(toml: &str) -> Result { + pub fn parse(toml: &str) -> Result { toml::from_str(format!("source = \"Repo\"\n{}", toml).as_str()) } diff --git a/crates/config/src/repo_config_source.rs b/crates/config/src/repo_config_source.rs index 67263d8..475539f 100644 --- a/crates/config/src/repo_config_source.rs +++ b/crates/config/src/repo_config_source.rs @@ -1,4 +1,6 @@ -#[derive(Copy, Clone, Debug, PartialEq, Eq, serde::Deserialize)] +#[derive( + Copy, Hash, Clone, Debug, PartialEq, Eq, serde::Deserialize, PartialOrd, Ord, serde::Serialize, +)] pub enum RepoConfigSource { Repo, Server, diff --git a/crates/config/src/server.rs b/crates/config/src/server.rs index 068a2c3..a523b2a 100644 --- a/crates/config/src/server.rs +++ b/crates/config/src/server.rs @@ -1,6 +1,5 @@ // use actix::prelude::*; -use derive_more::Constructor; use std::{ collections::HashMap, @@ -12,7 +11,7 @@ use std::{ use kxio::fs::FileSystem; use tracing::info; -use crate::{ForgeAlias, ForgeConfig, RepoAlias}; +use crate::{newtype, ForgeAlias, ForgeConfig, RepoAlias}; #[derive(Debug, thiserror::Error)] pub enum Error { @@ -96,18 +95,8 @@ impl Webhook { } } -/// The URL for the webhook where forges should send their updates -#[derive( - Clone, - Debug, - PartialEq, - Eq, - serde::Serialize, - serde::Deserialize, - derive_more::AsRef, - Constructor, -)] -pub struct WebhookUrl(String); +// The RL for the webhook where forges should send their updates +newtype!(WebhookUrl is a String, serde::Serialize); /// The directory to store server data, such as cloned repos #[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, derive_more::Constructor)] diff --git a/crates/config/src/tests.rs b/crates/config/src/tests.rs index 62e0ded..5035233 100644 --- a/crates/config/src/tests.rs +++ b/crates/config/src/tests.rs @@ -1,18 +1,22 @@ // +use super::*; + +use assert2::let_assert; +use secrecy::ExposeSecret; +use std::collections::BTreeMap; +use std::path::PathBuf; + +use crate::server::Http; +use crate::server::ServerConfig; +use crate::server::ServerStorage; +use crate::server::Webhook; +use crate::webhook::push::Branch; type Result = core::result::Result>; type TestResult = Result<()>; mod server_repo_config { - use std::path::PathBuf; - - use assert2::let_assert; - - use crate::{ - tests::given, BranchName, GitDir, RepoBranches, RepoConfig, RepoConfigSource, RepoPath, - }; - - use super::super::server_repo_config::*; + use super::*; #[test] fn should_not_return_repo_config_when_no_branches() { @@ -91,9 +95,6 @@ mod server_repo_config { } } mod repo_config { - use crate::{RepoBranches, RepoConfigSource}; - - use super::super::repo_config::*; use super::*; #[test] @@ -110,7 +111,7 @@ mod repo_config { "# ); - let rc = RepoConfig::load(toml.as_str())?; + let rc = RepoConfig::parse(toml.as_str())?; assert_eq!( rc, @@ -144,13 +145,7 @@ mod repo_config { } } mod forge_config { - use std::collections::BTreeMap; - - use secrecy::ExposeSecret; - - use crate::{ - tests::given, ForgeConfig, ForgeType, Hostname, RepoAlias, ServerRepoConfig, User, - }; + use super::*; #[test] fn should_return_repos() { @@ -246,14 +241,9 @@ mod forge_config { } } mod forge_details { - use std::collections::BTreeMap; - + use super::*; use secrecy::ExposeSecret; - use crate::{ - tests::given, ApiToken, ForgeAlias, ForgeConfig, ForgeDetails, ForgeType, Hostname, User, - }; - #[test] fn should_return_forge_alias() { let forge_type = ForgeType::MockForge; @@ -364,10 +354,9 @@ mod forge_details { } } mod forge_name { + use super::*; use std::path::PathBuf; - use crate::{tests::given, ForgeAlias}; - #[test] fn should_convert_to_pathbuf() { let name = given::a_name(); @@ -379,7 +368,7 @@ mod forge_name { } } mod forge_type { - use crate::ForgeType; + use super::*; #[test] fn should_display_as_string() { @@ -387,10 +376,9 @@ mod forge_type { } } mod gitdir { + use super::*; use std::path::PathBuf; - use crate::{tests::given, GitDir}; - #[test] fn should_return_pathbuf() { let pathbuf = PathBuf::default().join(given::a_name()); @@ -439,7 +427,7 @@ mod gitdir { } } mod repo_branches { - use crate::{tests::given, BranchName, RepoBranches}; + use super::*; #[test] fn should_return_main() { @@ -470,15 +458,10 @@ mod repo_branches { } } mod server { + use super::*; mod load { - use assert2::let_assert; - use pretty_assertions::assert_eq; - - use crate::{ - server::ServerConfig, - tests::{given, TestResult}, - }; + use super::*; #[test] fn load_should_parse_server_config() -> TestResult { @@ -559,7 +542,7 @@ token = "{forge_token}" {repos} "# ); - eprintln!("{file_contents}"); + println!("{file_contents}"); fs.file_write( &fs.base().join("git-next-server.toml"), file_contents.as_str(), @@ -569,8 +552,7 @@ token = "{forge_token}" } } mod registered_webhook { - - use crate::{tests::given, RegisteredWebhook, WebhookAuth}; + use super::*; #[test] fn should_return_id() { @@ -590,11 +572,11 @@ mod registered_webhook { } } mod webhook { + use super::*; mod message { + use super::*; use std::collections::HashMap; - use crate::{tests::given, WebhookMessage}; - #[test] fn should_return_forge_alias() { let forge_alias = given::a_forge_alias(); @@ -645,7 +627,8 @@ mod webhook { } } mod push { - use crate::{tests::given, webhook::push::Branch, BranchName}; + + use super::*; #[test] fn should_return_main_branch() { @@ -717,18 +700,14 @@ mod push { } } mod given { + + use super::*; + use rand::Rng as _; use std::{ collections::{BTreeMap, HashMap}, path::{Path, PathBuf}, }; - use rand::Rng as _; - - use crate::{ - server::{Http, ServerConfig, ServerStorage, Webhook}, - ForgeAlias, ForgeConfig, ForgeType, RepoAlias, RepoBranches, ServerRepoConfig, WebhookId, - }; - pub fn a_name() -> String { use rand::Rng; use std::iter; diff --git a/crates/config/src/webhook/auth.rs b/crates/config/src/webhook/auth.rs index 2e4e1ff..d61d593 100644 --- a/crates/config/src/webhook/auth.rs +++ b/crates/config/src/webhook/auth.rs @@ -1,7 +1,7 @@ -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Deref, derive_more::Display)] -pub struct WebhookAuth(ulid::Ulid); +// +crate::newtype!(WebhookAuth is a ulid::Ulid, derive_more::Display); impl WebhookAuth { - pub fn new(authorisation: &str) -> Result { + pub fn try_new(authorisation: &str) -> Result { use std::str::FromStr as _; let id = ulid::Ulid::from_str(authorisation)?; tracing::info!("Parse auth token: {}", id); diff --git a/crates/config/src/webhook/id.rs b/crates/config/src/webhook/id.rs index ed7fb32..66fca94 100644 --- a/crates/config/src/webhook/id.rs +++ b/crates/config/src/webhook/id.rs @@ -1,4 +1,2 @@ -use derive_more::{Constructor, Deref, Display}; - -#[derive(Clone, Debug, PartialEq, Eq, Constructor, Deref, Display)] -pub struct WebhookId(String); +// +crate::newtype!(WebhookId is a String, derive_more::Display); diff --git a/crates/config/src/webhook/tests.rs b/crates/config/src/webhook/tests.rs index c7e1df4..0a597ee 100644 --- a/crates/config/src/webhook/tests.rs +++ b/crates/config/src/webhook/tests.rs @@ -5,7 +5,7 @@ mod auth { fn bytes() -> Result<(), Box> { let ulid = ulid::Ulid::new(); - let wa = WebhookAuth::new(ulid.to_string().as_str())?; + let wa = WebhookAuth::try_new(ulid.to_string().as_str())?; assert_eq!(ulid.to_bytes(), wa.to_bytes()); @@ -16,7 +16,7 @@ mod auth { fn string() -> Result<(), Box> { let ulid = ulid::Ulid::new(); - let wa = WebhookAuth::new(ulid.to_string().as_str())?; + let wa = WebhookAuth::try_new(ulid.to_string().as_str())?; assert_eq!(ulid.to_string(), wa.to_string()); diff --git a/crates/forge-forgejo/src/lib.rs b/crates/forge-forgejo/src/lib.rs index 9fa6a3e..29a6f73 100644 --- a/crates/forge-forgejo/src/lib.rs +++ b/crates/forge-forgejo/src/lib.rs @@ -36,7 +36,7 @@ impl git::ForgeLike for ForgeJo { tracing::info!(?authorization, %expected, "is message authorised?"); authorization .and_then(|header| header.strip_prefix("Basic ").map(|v| v.to_owned())) - .and_then(|value| config::WebhookAuth::new(value.as_str()).ok()) + .and_then(|value| config::WebhookAuth::try_new(value.as_str()).ok()) .map(|auth| &auth == expected) .unwrap_or(false) } diff --git a/crates/forge/src/mock_forge.rs b/crates/forge/src/mock_forge.rs index b0136e5..e32463d 100644 --- a/crates/forge/src/mock_forge.rs +++ b/crates/forge/src/mock_forge.rs @@ -19,38 +19,41 @@ impl git::ForgeLike for MockForge { _msg: &config::WebhookMessage, _expected: &config::WebhookAuth, ) -> bool { - todo!() + todo!("MockForge::is_message_authorised") } fn parse_webhook_body( &self, _body: &config::webhook::message::Body, ) -> git::forge::webhook::Result { - todo!() + todo!("MockForge::parse_webhook_body") } async fn commit_status(&self, _commit: &git::Commit) -> git::forge::commit::Status { - todo!() + todo!("MockForge::commit_status") } async fn list_webhooks( &self, _webhook_url: &config::server::WebhookUrl, ) -> git::forge::webhook::Result> { - todo!() + todo!("MockForge::list_webhooks") } async fn unregister_webhook( &self, _webhook_id: &config::WebhookId, ) -> git::forge::webhook::Result<()> { - todo!() + todo!("MockForge::unregister_webhook") } async fn register_webhook( &self, _webhook_url: &config::server::WebhookUrl, ) -> git::forge::webhook::Result { - todo!() + Ok(config::RegisteredWebhook::new( + config::WebhookId::new(""), + config::WebhookAuth::new(ulid::Ulid::new()), + )) } } diff --git a/crates/forge/src/tests.rs b/crates/forge/src/tests.rs index 49e190c..62f60e1 100644 --- a/crates/forge/src/tests.rs +++ b/crates/forge/src/tests.rs @@ -30,7 +30,7 @@ fn test_github_name() { fn given_fs() -> kxio::fs::FileSystem { kxio::fs::temp().unwrap_or_else(|e| { - eprintln!("{e}"); + println!("{e}"); panic!("fs") }) } diff --git a/crates/git/src/commit.rs b/crates/git/src/commit.rs index 39f4c6c..c7172db 100644 --- a/crates/git/src/commit.rs +++ b/crates/git/src/commit.rs @@ -1,7 +1,19 @@ +use config::newtype; +use derive_more::Display; // use git_next_config as config; -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] +#[derive( + Clone, + Debug, + Hash, + PartialEq, + Eq, + PartialOrd, + Ord, + derive_more::Constructor, + derive_more::Display, +)] #[display("{}", sha)] pub struct Commit { sha: Sha, @@ -25,11 +37,8 @@ impl From for Commit { } } -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] -pub struct Sha(String); - -#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] -pub struct Message(String); +newtype!(Sha is a String, Display); +newtype!(Message is a String); #[derive(Clone, Debug)] pub struct Histories { diff --git a/crates/git/src/repo_details.rs b/crates/git/src/repo_details.rs index f0d761d..d1538ce 100644 --- a/crates/git/src/repo_details.rs +++ b/crates/git/src/repo_details.rs @@ -57,4 +57,10 @@ impl RepoDetails { pub fn git_remote(&self) -> GitRemote { GitRemote::new(self.forge.hostname().clone(), self.repo_path.clone()) } + + pub fn with_hostname(mut self, hostname: git_next_config::Hostname) -> Self { + let forge = self.forge; + self.forge = forge.with_hostname(hostname); + self + } } diff --git a/crates/git/src/repository/mock.rs b/crates/git/src/repository/mock.rs index f50008d..ee39700 100644 --- a/crates/git/src/repository/mock.rs +++ b/crates/git/src/repository/mock.rs @@ -12,27 +12,44 @@ use git_next_config as config; #[derive(Debug, Default, Clone)] pub struct MockRepository { open_repos: Arc>>, + clone_repos: Arc>>, } impl MockRepository { pub fn new() -> Self { Self { open_repos: Default::default(), + clone_repos: Default::default(), } } pub fn given_can_be_opened( &mut self, - gitdir: &config::GitDir, + repo_details: git::RepoDetails, ) -> git::repository::MockOpenRepository { - let open_repo = git::repository::MockOpenRepository::new(); + let gitdir = repo_details.gitdir.clone(); + let open_repo = git::repository::MockOpenRepository::new(repo_details); #[allow(clippy::unwrap_used)] self.open_repos .lock() - .map(|mut or| or.insert(gitdir.clone(), open_repo.clone())) + .map(|mut or| or.insert(gitdir, open_repo.clone())) .unwrap(); open_repo } + pub fn given_can_be_cloned( + &mut self, + repo_details: git::RepoDetails, + ) -> git::repository::MockOpenRepository { + let gitdir = repo_details.gitdir.clone(); + let clone_repo = git::repository::MockOpenRepository::new(repo_details); + #[allow(clippy::unwrap_used)] + self.clone_repos + .lock() + .map(|mut or| or.insert(gitdir, clone_repo.clone())) + .unwrap(); + clone_repo + } + pub fn seal(self) -> (git::Repository, Self) { (git::Repository::Mock(self.clone()), self) } @@ -59,13 +76,31 @@ impl git::repository::RepositoryLike for MockRepository { .map(|or| or.get(gitdir).cloned()) .transpose() .unwrap_or_else(|| Err(crate::repository::Error::InvalidGitDir(gitdir.clone()))) - .map(|mor| mor.into()) + .map(|mor| { + mor.log(format!("open gitdir:{gitdir}")); + mor.into() + }) } fn git_clone( &self, - _repo_details: &git::RepoDetails, + repo_details: &git::RepoDetails, ) -> std::result::Result { - todo!("MockRepository::git_clone") + let gitdir = &repo_details.gitdir; + #[allow(clippy::unwrap_used)] + self.clone_repos + .lock() + .map_err(|_| crate::repository::Error::MockLock) + .map(|or| or.get(gitdir).cloned()) + .transpose() + .unwrap_or_else(|| Err(crate::repository::Error::InvalidGitDir(gitdir.clone()))) + .map(|mor| { + let repo_path = &repo_details.repo_path; + let hostname = repo_details.forge.hostname(); + mor.log(format!( + "git_clone hostname:{hostname} repo_path:{repo_path}" + )); + mor.into() + }) } } diff --git a/crates/git/src/repository/mod.rs b/crates/git/src/repository/mod.rs index 14e1b1a..42fc979 100644 --- a/crates/git/src/repository/mod.rs +++ b/crates/git/src/repository/mod.rs @@ -1,14 +1,11 @@ // -#[cfg(test)] mod mock; -#[cfg(test)] pub use mock::MockRepository; -#[cfg(test)] pub use open::MockOpenRepository; mod open; mod real; -mod test; +pub mod test; #[cfg(test)] mod tests; @@ -35,22 +32,24 @@ use super::RepoDetails; #[allow(clippy::large_enum_variant)] pub enum Repository { Real, - #[cfg(test)] - Mock(MockRepository), Test(TestRepository), + Mock(MockRepository), } pub const fn new() -> Repository { Repository::Real } -#[cfg(test)] pub fn mock() -> MockRepository { MockRepository::new() } -pub const fn test(fs: kxio::fs::FileSystem) -> TestRepository { - TestRepository::new(false, fs, vec![], vec![]) +pub fn new_test(fs: kxio::fs::FileSystem, hostname: config::Hostname) -> Repository { + Repository::Test(test(fs, hostname)) +} + +pub fn test(fs: kxio::fs::FileSystem, hostname: config::Hostname) -> TestRepository { + TestRepository::new(false, hostname, fs, Default::default(), Default::default()) } // #[cfg(test)] @@ -66,15 +65,20 @@ pub fn open( repo_details: &RepoDetails, gitdir: config::GitDir, ) -> Result { + println!("validating repo in {gitdir:?}"); let repository = if !gitdir.exists() { + println!("dir doesn't exist - cloning..."); info!("Local copy not found - cloning..."); repository.git_clone(repo_details)? } else { + println!("dir exists - opening..."); info!("Local copy found - opening..."); repository.open(&gitdir)? }; + println!("open - validating"); info!("Validating..."); validate_repo(&repository, repo_details).map_err(|e| Error::Validation(e.to_string()))?; + println!("open - validated - okay"); Ok(repository) } @@ -88,10 +92,8 @@ impl std::ops::Deref for Repository { fn deref(&self) -> &Self::Target { match self { Self::Real => &real::RealRepository, - Self::Test(test_repository) => test_repository, - - #[cfg(test)] - Self::Mock(mock_repository) => mock_repository, + Self::Test(test) => test, + Self::Mock(mock) => mock, } } } @@ -134,6 +136,9 @@ pub enum Error { #[error("git clone: {0}")] Clone(String), + #[error("init: {0}")] + Init(String), + #[error("open: {0}")] Open(String), diff --git a/crates/git/src/repository/open/mod.rs b/crates/git/src/repository/open/mod.rs index 0082fe0..96d8b92 100644 --- a/crates/git/src/repository/open/mod.rs +++ b/crates/git/src/repository/open/mod.rs @@ -7,7 +7,6 @@ pub mod oreal; pub mod otest; -#[cfg(test)] pub mod omock; use std::{ @@ -18,12 +17,12 @@ use std::{ use crate as git; use git::repository::Direction; use git_next_config as config; -#[cfg(test)] pub use omock::MockOpenRepository; pub use oreal::RealOpenRepository; pub use otest::TestOpenRepository; #[derive(Clone, Debug)] +#[allow(clippy::large_enum_variant)] pub enum OpenRepository { /// A real git repository. /// @@ -44,7 +43,6 @@ pub enum OpenRepository { /// the behaviour of a git repository. Once the [Self::LocalOnly] /// variant is ready for use, tests should be converted to using /// that instead. - #[cfg(test)] Mock(git::repository::MockOpenRepository), // TODO: (#38) contain a mock model of a repo } @@ -57,21 +55,27 @@ pub fn real(gix_repo: gix::Repository) -> OpenRepository { #[cfg(not(tarpaulin_include))] // don't test mocks pub fn test( gitdir: &config::GitDir, + hostname: &config::Hostname, fs: kxio::fs::FileSystem, - on_fetch: Vec, - on_push: Vec, + on_fetch: Arc>>, + on_push: Arc>>, ) -> OpenRepository { - OpenRepository::Test(TestOpenRepository::new(gitdir, fs, on_fetch, on_push)) + let open_repo = TestOpenRepository::new(gitdir, hostname, fs, on_fetch, on_push); + open_repo.log("test"); + OpenRepository::Test(open_repo) } #[cfg(not(tarpaulin_include))] // don't test mocks pub fn test_bare( gitdir: &config::GitDir, + hostname: &config::Hostname, fs: kxio::fs::FileSystem, - on_fetch: Vec, - on_push: Vec, + on_fetch: Arc>>, + on_push: Arc>>, ) -> OpenRepository { - OpenRepository::Test(TestOpenRepository::new_bare(gitdir, fs, on_fetch, on_push)) + let open_repo = TestOpenRepository::new_bare(gitdir, hostname, fs, on_fetch, on_push); + open_repo.log("test_bare"); + OpenRepository::Test(open_repo) } pub trait OpenRepositoryLike { @@ -109,8 +113,6 @@ impl std::ops::Deref for OpenRepository { match self { Self::Real(real) => real, Self::Test(test) => test, - - #[cfg(test)] Self::Mock(mock) => mock, } } diff --git a/crates/git/src/repository/open/omock.rs b/crates/git/src/repository/open/omock.rs index be1968e..dcae4f2 100644 --- a/crates/git/src/repository/open/omock.rs +++ b/crates/git/src/repository/open/omock.rs @@ -1,21 +1,33 @@ // -use crate as git; +#![cfg(not(tarpaulin_include))] + +use crate::{self as git, RepoDetails}; use git_next_config as config; use std::{ + collections::HashMap, path::Path, sync::{Arc, Mutex}, }; #[derive(Clone, Debug, Default)] pub struct MockOpenRepository { + log: Arc>>, default_push_remote: Arc>>, default_fetch_remote: Arc>>, - operations: Arc>>, + commit_logs: HashMap>, + repo_details: RepoDetails, } impl MockOpenRepository { - pub fn new() -> Self { - Self::default() + pub fn new(repo_details: RepoDetails) -> Self { + Self { + repo_details, + ..Default::default() + } + } + pub fn with_default_remote(mut self, direction: git::repository::Direction) -> Self { + self.given_has_default_remote(direction, Some(self.repo_details.git_remote())); + self } pub fn given_has_default_remote( &mut self, @@ -43,10 +55,29 @@ impl MockOpenRepository { }; } - pub fn operations(&self) -> Vec { - self.operations + pub fn with_commit_log( + mut self, + branch_name: config::BranchName, + commits: Vec, + ) -> Self { + self.commit_logs.insert(branch_name, commits); + self + } + + pub fn log(&self, message: impl Into) { + let message: String = message.into(); + let _ = self.log.lock().map(|mut log| log.push(message)); + } + + pub fn take_log(&mut self) -> Vec { + println!("take_log: {:#?}", self.log); + self.log .lock() - .map(|operations| operations.clone()) + .map(|mut self_log| { + let out_log: Vec = self_log.clone(); + self_log.clear(); + out_log + }) .unwrap_or_default() } } @@ -76,10 +107,7 @@ impl git::repository::OpenRepositoryLike for MockOpenRepository { } fn fetch(&self) -> core::result::Result<(), crate::fetch::Error> { - self.operations - .lock() - .map_err(|_| crate::fetch::Error::Lock) - .map(|mut operations| operations.push("fetch".to_string()))?; + self.log("fetch"); Ok(()) } @@ -92,23 +120,22 @@ impl git::repository::OpenRepositoryLike for MockOpenRepository { ) -> core::result::Result<(), crate::push::Error> { let forge_alias = repo_details.forge.forge_alias(); let repo_alias = &repo_details.repo_alias; - self.operations - .lock() - .map_err(|_| crate::fetch::Error::Lock) - .map(|mut operations| { - operations.push(format!( - "push fa:{forge_alias} ra:{repo_alias} bn:{branch_name} tc:{to_commit} f:{force}" - )) - })?; + self.log(format!( + "push fa:{forge_alias} ra:{repo_alias} bn:{branch_name} tc:{to_commit} f:{force}" + )); Ok(()) } fn commit_log( &self, - _branch_name: &git_next_config::BranchName, - _find_commits: &[git::Commit], + branch_name: &git_next_config::BranchName, + find_commits: &[git::Commit], ) -> core::result::Result, git::commit::log::Error> { - todo!("MockOpenRepository::commit_log") + self.log(format!("commit_log {branch_name} {find_commits:?}")); + self.commit_logs + .get(branch_name) + .cloned() + .ok_or(git::commit::log::Error::Lock) } fn read_file( diff --git a/crates/git/src/repository/open/otest.rs b/crates/git/src/repository/open/otest.rs index 7720011..5dd5549 100644 --- a/crates/git/src/repository/open/otest.rs +++ b/crates/git/src/repository/open/otest.rs @@ -1,4 +1,6 @@ // +#![cfg(not(tarpaulin_include))] + use crate as git; use derive_more::{Constructor, Deref}; use git_next_config as config; @@ -61,9 +63,10 @@ impl OnPush { #[derive(Clone, Debug)] pub struct TestOpenRepository { - on_fetch: Vec, + log: Arc>>, + on_fetch: Arc>>, fetch_counter: Arc>, - on_push: Vec, + on_push: Arc>>, push_counter: Arc>, real: git::repository::RealOpenRepository, } @@ -77,19 +80,25 @@ impl git::repository::OpenRepositoryLike for TestOpenRepository { } fn fetch(&self) -> Result<(), git::fetch::Error> { + self.log("fetch"); let i: usize = *self .fetch_counter .read() .map_err(|_| git::fetch::Error::Lock)? .deref(); - eprintln!("Fetch: {i}"); + println!("Fetch: {i}"); self.fetch_counter .write() .map_err(|_| git::fetch::Error::Lock) .map(|mut c| *c += 1)?; - self.on_fetch.get(i).map(|f| f.invoke()).unwrap_or_else(|| { - unimplemented!("Unexpected fetch"); - }) + self.on_fetch + .lock() + .map_err(|_| git::fetch::Error::Lock) + .map(|a| { + a.get(i).map(|f| f.invoke()).unwrap_or_else(|| { + unimplemented!("Unexpected fetch"); + }) + })? } fn push( @@ -99,22 +108,29 @@ impl git::repository::OpenRepositoryLike for TestOpenRepository { to_commit: &git::GitRef, force: &git::push::Force, ) -> git::push::Result<()> { + self.log(format!( + "push branch:{branch_name} to:{to_commit} force:{force}" + )); let i: usize = *self .push_counter .read() .map_err(|_| git::fetch::Error::Lock)? .deref(); - eprintln!("Push: {i}"); + println!("Push: {i}"); self.push_counter .write() .map_err(|_| git::fetch::Error::Lock) .map(|mut c| *c += 1)?; self.on_push - .get(i) - .map(|f| f.invoke(repo_details, branch_name, to_commit, force)) - .unwrap_or_else(|| { - unimplemented!("Unexpected push"); - }) + .lock() + .map_err(|_| git::fetch::Error::Lock) + .map(|a| { + a.get(i) + .map(|f| f.invoke(repo_details, branch_name, to_commit, force)) + .unwrap_or_else(|| { + unimplemented!("Unexpected push"); + }) + })? } fn commit_log( @@ -130,21 +146,31 @@ impl git::repository::OpenRepositoryLike for TestOpenRepository { branch_name: &config::BranchName, file_name: &Path, ) -> git::file::Result { + self.log(format!("read_file branch:{branch_name} file:{file_name:?}")); self.real.read_file(branch_name, file_name) } } impl TestOpenRepository { pub fn new( gitdir: &config::GitDir, + hostname: &config::Hostname, fs: kxio::fs::FileSystem, - on_fetch: Vec, - on_push: Vec, + on_fetch: Arc>>, + on_push: Arc>>, ) -> Self { let pathbuf = fs.base().join(gitdir.to_path_buf()); + + // use std::os::unix::process::CommandExt as _; + // #[allow(clippy::expect_used)] + // std::process::Command::new("ls") + // .args(["-la".into(), pathbuf.clone()]) + // .exec(); + #[allow(clippy::expect_used)] - let gix = gix::init(pathbuf).expect("git init"); - Self::write_origin(gitdir, &fs); + let gix = gix::discover(pathbuf).expect("failed to open git repo"); + Self::write_origin(gitdir, hostname, &fs); Self { + log: Arc::new(Mutex::new(vec![format!("new gitdir:{gitdir:?}")])), on_fetch, fetch_counter: Arc::new(RwLock::new(0)), on_push, @@ -154,15 +180,17 @@ impl TestOpenRepository { } pub fn new_bare( gitdir: &config::GitDir, + hostname: &config::Hostname, fs: kxio::fs::FileSystem, - on_fetch: Vec, - on_push: Vec, + on_fetch: Arc>>, + on_push: Arc>>, ) -> Self { let pathbuf = fs.base().join(gitdir.to_path_buf()); #[allow(clippy::expect_used)] let gix = gix::init_bare(pathbuf).expect("git init bare"); - Self::write_origin(gitdir, &fs); + Self::write_origin(gitdir, hostname, &fs); Self { + log: Arc::new(Mutex::new(vec![format!("new bare gitdir:{gitdir:?}")])), on_fetch, fetch_counter: Arc::new(RwLock::new(0)), on_push, @@ -171,7 +199,11 @@ impl TestOpenRepository { } } - fn write_origin(gitdir: &config::GitDir, fs: &kxio::fs::FileSystem) { + fn write_origin( + gitdir: &config::GitDir, + hostname: &config::Hostname, + fs: &kxio::fs::FileSystem, + ) { let config_file = fs.base().join(gitdir.to_path_buf()).join(".git/config"); #[allow(clippy::expect_used)] let contents = fs @@ -180,7 +212,7 @@ impl TestOpenRepository { let updated_contents = format!( r#"{contents} [remote "origin"] - url = git@foo.example,net + url = git@{hostname} fetch = +refs/heads/*:refs/remotes/origin/* "# ); @@ -188,4 +220,21 @@ impl TestOpenRepository { fs.file_write(&config_file, &updated_contents) .expect("write updated .git/config") } + + pub fn log(&self, message: impl Into) { + let message: String = message.into(); + let _ = self.log.lock().map(|mut log| log.push(message)); + } + + pub fn take_log(&mut self) -> Vec { + println!("take_log: {:#?}", self.log); + self.log + .lock() + .map(|mut self_log| { + let out_log: Vec = self_log.clone(); + self_log.clear(); + out_log + }) + .unwrap_or_default() + } } diff --git a/crates/git/src/repository/open/tests.rs b/crates/git/src/repository/open/tests.rs index 65988b3..e3080d2 100644 --- a/crates/git/src/repository/open/tests.rs +++ b/crates/git/src/repository/open/tests.rs @@ -114,7 +114,7 @@ mod repo_config { "# ); - let rc = config::RepoConfig::load(toml.as_str())?; + let rc = config::RepoConfig::parse(toml.as_str())?; assert_eq!( rc, @@ -321,7 +321,7 @@ mod remote_branches { fn should_return_remote_branches() -> TestResult { let_assert!(Ok(fs) = kxio::fs::temp()); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let test_repository = git::repository::test(fs.clone()); + let test_repository = git::repository::test(fs.clone(), given::a_hostname()); let_assert!(Ok(open_repository) = test_repository.open(&gitdir)); let repo_config = &given::a_repo_config(); let branches = repo_config.branches(); @@ -347,7 +347,7 @@ mod commit_log { fn should_return_single_item_in_commit_log_when_not_searching() -> TestResult { let_assert!(Ok(fs) = kxio::fs::temp()); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let test_repository = git::repository::test(fs.clone()); + let test_repository = git::repository::test(fs.clone(), given::a_hostname()); let_assert!(Ok(open_repository) = test_repository.open(&gitdir)); let repo_config = &given::a_repo_config(); let branches = repo_config.branches(); @@ -364,7 +364,7 @@ mod commit_log { let_assert!(Ok(fs) = kxio::fs::temp()); let branch_name = given::a_branch_name(); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let test_repository = git::repository::test(fs.clone()); + let test_repository = git::repository::test(fs.clone(), given::a_hostname()); let_assert!(Ok(open_repository) = test_repository.open(&gitdir)); for _ in [0; 60] { // create 60 commits @@ -381,7 +381,7 @@ mod commit_log { let_assert!(Ok(fs) = kxio::fs::temp(), "create temp directory"); let branch_name = given::a_branch_name(); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let test_repository = git::repository::test(fs.clone()); + let test_repository = git::repository::test(fs.clone(), given::a_hostname()); let_assert!( Ok(open_repository) = test_repository.open(&gitdir), "open repository" @@ -425,7 +425,7 @@ mod read_file { let contents = given::a_name(); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let test_repository = git::repository::test(fs.clone()); + let test_repository = git::repository::test(fs.clone(), given::a_hostname()); let_assert!(Ok(open_repository) = test_repository.open(&gitdir)); then::commit_named_file_to_branch( &file_name, @@ -448,7 +448,7 @@ mod read_file { fn should_error_on_missing_file() -> TestResult { let_assert!(Ok(fs) = kxio::fs::temp()); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let test_repository = git::repository::test(fs.clone()); + let test_repository = git::repository::test(fs.clone(), given::a_hostname()); let_assert!(Ok(open_repository) = test_repository.open(&gitdir)); let repo_config = &given::a_repo_config(); let branches = repo_config.branches(); @@ -457,7 +457,7 @@ mod read_file { Err(err) = open_repository.read_file(&branches.dev(), &given::a_pathbuf()), "read file" ); - eprintln!("err: {err:#?}"); + println!("err: {err:#?}"); assert!(matches!(err, git::file::Error::FileNotFound)); Ok(()) } diff --git a/crates/git/src/repository/test.rs b/crates/git/src/repository/test.rs index 7331f84..44d0072 100644 --- a/crates/git/src/repository/test.rs +++ b/crates/git/src/repository/test.rs @@ -1,24 +1,33 @@ // -use derive_more::Constructor; +#![cfg(not(tarpaulin_include))] -use crate as git; +use derive_more::Constructor; +use std::sync::{Arc, Mutex}; + +use crate::{self as git, OpenRepository}; use git::repository::RepositoryLike; -use git_next_config as config; +use git_next_config::{self as config}; #[derive(Clone, Debug, Constructor)] pub struct TestRepository { is_bare: bool, + hostname: config::Hostname, fs: kxio::fs::FileSystem, - on_fetch: Vec, - on_push: Vec, + on_fetch: Arc>>, + on_push: Arc>>, } impl TestRepository { + pub fn init(&self, gitdir: &config::GitDir) -> Result { + gix::init(gitdir.to_path_buf()) + .map_err(|e| git::repository::Error::Init(e.to_string())) + .map(git::repository::open::real) + } pub fn on_fetch(&mut self, on_fetch: git::repository::OnFetch) { - self.on_fetch.push(on_fetch); + let _ = self.on_fetch.lock().map(|mut a| a.push(on_fetch)); } pub fn on_push(&mut self, on_push: git::repository::OnPush) { - self.on_push.push(on_push); + let _ = self.on_push.lock().map(|mut a| a.push(on_push)); } pub const fn fs(&self) -> &kxio::fs::FileSystem { @@ -30,6 +39,7 @@ impl RepositoryLike for TestRepository { if self.is_bare { Ok(git::repository::open::test_bare( gitdir, + &self.hostname, self.fs.clone(), self.on_fetch.clone(), self.on_push.clone(), @@ -37,6 +47,7 @@ impl RepositoryLike for TestRepository { } else { Ok(git::repository::open::test( gitdir, + &self.hostname, self.fs.clone(), self.on_fetch.clone(), self.on_push.clone(), @@ -44,10 +55,8 @@ impl RepositoryLike for TestRepository { } } - fn git_clone( - &self, - _repo_details: &crate::RepoDetails, - ) -> super::Result { - todo!() + fn git_clone(&self, repo_details: &crate::RepoDetails) -> super::Result { + let gitdir = &repo_details.gitdir; + self.open(gitdir) } } diff --git a/crates/git/src/repository/tests.rs b/crates/git/src/repository/tests.rs index 337df18..cff59ee 100644 --- a/crates/git/src/repository/tests.rs +++ b/crates/git/src/repository/tests.rs @@ -1,141 +1,83 @@ use crate as git; mod validate { - use crate::{validation::repo::validate_repo, GitRemote, RepoDetails}; + use crate::{tests::given, validation::repo::validate_repo}; use super::*; use assert2::let_assert; - use git_next_config::{ForgeDetails, GitDir, Hostname, RepoPath}; #[test] fn should_ok_a_valid_repo() { - let repo_details = RepoDetails::default() - .with_forge( - ForgeDetails::default().with_hostname(Hostname::new("localhost".to_string())), - ) - .with_repo_path(RepoPath::new("kemitix/test".to_string())); - let gitdir = GitDir::from("foo"); - let remote = GitRemote::new( - Hostname::new("localhost"), - RepoPath::new("kemitix/test".to_string()), - ); - + let fs = given::a_filesystem(); + let repo_details = given::repo_details(&fs); + let gitdir = &repo_details.gitdir; let mut mock_repository = git::repository::mock(); - { - let mut mock_open_repo = mock_repository.given_can_be_opened(&gitdir); - mock_open_repo - .given_has_default_remote(git::repository::Direction::Push, Some(remote.clone())); - mock_open_repo - .given_has_default_remote(git::repository::Direction::Fetch, Some(remote)); - } + mock_repository + .given_can_be_opened(repo_details.clone()) + .with_default_remote(git::repository::Direction::Push) + .with_default_remote(git::repository::Direction::Fetch); let (repository, _mock_repository) = mock_repository.seal(); - let_assert!(Ok(open_repository) = repository.open(&gitdir)); + let_assert!(Ok(open_repository) = repository.open(gitdir)); let_assert!(Ok(_) = validate_repo(&open_repository, &repo_details)); } #[test] fn should_fail_where_no_default_push_remote() { - let repo_details = RepoDetails::default() - .with_forge( - ForgeDetails::default().with_hostname(Hostname::new("localhost".to_string())), - ) - .with_repo_path(RepoPath::new("kemitix/test".to_string())); - let gitdir = GitDir::from("foo"); - let remote = GitRemote::new( - Hostname::new("localhost"), - RepoPath::new("kemitix/test".to_string()), - ); - + let fs = given::a_filesystem(); + let repo_details = given::repo_details(&fs); + let gitdir = &repo_details.gitdir; let mut mock_repository = git::repository::mock(); - { - let mut mock_open_repo = mock_repository.given_can_be_opened(&gitdir); - mock_open_repo.given_has_default_remote(git::repository::Direction::Push, None); - mock_open_repo - .given_has_default_remote(git::repository::Direction::Fetch, Some(remote)); - } + mock_repository + .given_can_be_opened(repo_details.clone()) + .with_default_remote(git::repository::Direction::Fetch); let (repository, _mock_repository) = mock_repository.seal(); - let_assert!(Ok(open_repository) = repository.open(&gitdir)); + let_assert!(Ok(open_repository) = repository.open(gitdir)); let_assert!(Err(_) = validate_repo(&open_repository, &repo_details)); } + #[test] fn should_fail_where_no_default_fetch_remote() { - let repo_details = RepoDetails::default() - .with_forge( - ForgeDetails::default().with_hostname(Hostname::new("localhost".to_string())), - ) - .with_repo_path(RepoPath::new("kemitix/test".to_string())); - let gitdir = GitDir::from("foo"); - let remote = GitRemote::new( - Hostname::new("localhost"), - RepoPath::new("kemitix/test".to_string()), - ); - + let fs = given::a_filesystem(); + let repo_details = given::repo_details(&fs); + let gitdir = &repo_details.gitdir; let mut mock_repository = git::repository::mock(); - { - let mut mock_open_repo = mock_repository.given_can_be_opened(&gitdir); - mock_open_repo.given_has_default_remote(git::repository::Direction::Push, None); - mock_open_repo - .given_has_default_remote(git::repository::Direction::Fetch, Some(remote)); - } + mock_repository + .given_can_be_opened(repo_details.clone()) + .with_default_remote(git::repository::Direction::Push); let (repository, _mock_repository) = mock_repository.seal(); - let_assert!(Ok(open_repository) = repository.open(&gitdir)); + let_assert!(Ok(open_repository) = repository.open(gitdir)); let_assert!(Err(_) = validate_repo(&open_repository, &repo_details)); } + #[test] fn should_fail_where_invalid_default_push_remote() { - let repo_details = RepoDetails::default() - .with_forge( - ForgeDetails::default().with_hostname(Hostname::new("localhost".to_string())), - ) - .with_repo_path(RepoPath::new("kemitix/test".to_string())); - let gitdir = GitDir::from("foo"); - let remote = GitRemote::new( - Hostname::new("localhost"), - RepoPath::new("kemitix/test".to_string()), - ); - let other_remote = GitRemote::new( - Hostname::new("localhost"), - RepoPath::new("kemitix/other".to_string()), - ); - + let fs = given::a_filesystem(); + let repo_details = given::repo_details(&fs); + let gitdir = &repo_details.gitdir; + let other_remote = given::repo_details(&fs).git_remote(); let mut mock_repository = git::repository::mock(); - { - let mut mock_open_repo = mock_repository.given_can_be_opened(&gitdir); - mock_open_repo - .given_has_default_remote(git::repository::Direction::Push, Some(other_remote)); - mock_open_repo - .given_has_default_remote(git::repository::Direction::Fetch, Some(remote)); - } + mock_repository + .given_can_be_opened(repo_details.clone()) + .with_default_remote(git::repository::Direction::Fetch) + .given_has_default_remote(git::repository::Direction::Push, Some(other_remote)); let (repository, _mock_repository) = mock_repository.seal(); - let_assert!(Ok(open_repository) = repository.open(&gitdir)); + let_assert!(Ok(open_repository) = repository.open(gitdir)); let_assert!(Err(_) = validate_repo(&open_repository, &repo_details)); } + #[test] fn should_fail_where_invalid_default_fetch_remote() { - let repo_details = RepoDetails::default() - .with_forge( - ForgeDetails::default().with_hostname(Hostname::new("localhost".to_string())), - ) - .with_repo_path(RepoPath::new("kemitix/test".to_string())); - let gitdir = GitDir::from("foo"); - let remote = GitRemote::new( - Hostname::new("localhost"), - RepoPath::new("kemitix/test".to_string()), - ); - let other_remote = GitRemote::new( - Hostname::new("localhost"), - RepoPath::new("kemitix/other".to_string()), - ); - + let fs = given::a_filesystem(); + let repo_details = given::repo_details(&fs); + let gitdir = &repo_details.gitdir; + let other_remote = given::repo_details(&fs).git_remote(); let mut mock_repository = git::repository::mock(); - { - let mut mock_open_repo = mock_repository.given_can_be_opened(&gitdir); - mock_open_repo.given_has_default_remote(git::repository::Direction::Push, Some(remote)); - mock_open_repo - .given_has_default_remote(git::repository::Direction::Fetch, Some(other_remote)); - } + mock_repository + .given_can_be_opened(repo_details.clone()) + .with_default_remote(git::repository::Direction::Push) + .given_has_default_remote(git::repository::Direction::Fetch, Some(other_remote)); let (repository, _mock_repository) = mock_repository.seal(); - let_assert!(Ok(open_repository) = repository.open(&gitdir)); + let_assert!(Ok(open_repository) = repository.open(gitdir)); let_assert!(Err(_) = validate_repo(&open_repository, &repo_details)); } } diff --git a/crates/git/src/tests.rs b/crates/git/src/tests.rs index 40dbb13..a82fb40 100644 --- a/crates/git/src/tests.rs +++ b/crates/git/src/tests.rs @@ -108,9 +108,9 @@ mod push { #[test] fn should_perform_a_fetch_then_push() { let fs = given::a_filesystem(); - let (mock_open_repository, gitdir, mock_repository) = given::an_open_repository(&fs); + let (mock_open_repository, repo_details, mock_repository) = + given::an_open_repository(&fs); let open_repository: OpenRepository = mock_open_repository.into(); - let repo_details = given::repo_details(&fs); let branch_name = &repo_details.branch; let commit = given::a_commit(); let gitref = GitRef::from(commit); @@ -123,14 +123,14 @@ mod push { &git::push::Force::No ) ); - let_assert!(Some(mock_open_repository) = mock_repository.get(&gitdir)); - let operations = mock_open_repository.operations(); + let_assert!(Some(mut mock_open_repository) = mock_repository.get(&repo_details.gitdir)); + let log = mock_open_repository.take_log(); let forge_alias = repo_details.forge.forge_alias(); let repo_alias = &repo_details.repo_alias; let to_commit = gitref; let force = "fast-forward"; assert_eq!( - operations, + log, vec![format!("fetch"), format!("push fa:{forge_alias} ra:{repo_alias} bn:{branch_name} tc:{to_commit} f:{force}")] ); } @@ -348,11 +348,15 @@ pub mod given { pub fn an_open_repository( fs: &kxio::fs::FileSystem, - ) -> (MockOpenRepository, GitDir, MockRepository) { + ) -> (MockOpenRepository, RepoDetails, MockRepository) { let mut mock = git::repository::mock(); - let gitdir = a_git_dir(fs); - let or = mock.given_can_be_opened(&gitdir); - (or, gitdir, mock) + let repo_details = given::repo_details(fs); + let or = mock.given_can_be_opened(repo_details.clone()); + (or, repo_details, mock) + } + + pub fn a_hostname() -> git_next_config::Hostname { + config::Hostname::new(given::a_name()) } } pub mod then { @@ -455,22 +459,22 @@ pub mod then { } fn exec(label: String, output: Result) -> TestResult { - eprintln!("== {label}"); + println!("== {label}"); match output { Ok(output) => { - eprintln!( + println!( "\nstdout:\n{}", String::from_utf8_lossy(output.stdout.as_slice()) ); - eprintln!( + println!( "\nstderr:\n{}", String::from_utf8_lossy(output.stderr.as_slice()) ); - eprintln!("============================="); + println!("============================="); Ok(()) } Err(err) => { - eprintln!("ERROR: {err:#?}"); + println!("ERROR: {err:#?}"); Ok(Err(err)?) } } diff --git a/crates/git/src/validation/tests.rs b/crates/git/src/validation/tests.rs index 549e82d..ed91afe 100644 --- a/crates/git/src/validation/tests.rs +++ b/crates/git/src/validation/tests.rs @@ -15,7 +15,7 @@ mod repos { fn where_repo_has_no_push_remote() { let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let test_repository = git::repository::test(fs.clone()); + let test_repository = git::repository::test(fs.clone(), given::a_hostname()); // default has no push or fetch remotes let repo_details = given::repo_details(&fs); let_assert!(Ok(repository) = test_repository.open(&gitdir), "open repo"); @@ -44,7 +44,7 @@ mod positions { fn where_fetch_fails_should_error() { let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); test_repository.on_fetch(git::repository::OnFetch::new( given::repo_branches(), gitdir.clone(), @@ -70,7 +70,7 @@ mod positions { fn where_main_branch_is_missing_or_commit_log_is_empty_should_error() { let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); test_repository.on_fetch(git::repository::OnFetch::new( given::repo_branches(), gitdir.clone(), @@ -104,7 +104,7 @@ mod positions { //given let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); let repo_config = given::a_repo_config(); test_repository.on_fetch(git::repository::OnFetch::new( repo_config.branches().clone(), @@ -144,7 +144,7 @@ mod positions { //given let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); let repo_config = given::a_repo_config(); test_repository.on_fetch(git::repository::OnFetch::new( repo_config.branches().clone(), @@ -186,7 +186,7 @@ mod positions { //given let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); let repo_config = given::a_repo_config(); test_repository.on_fetch(git::repository::OnFetch::new( repo_config.branches().clone(), @@ -234,7 +234,7 @@ mod positions { //given let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); let repo_config = given::a_repo_config(); test_repository.on_fetch(git::repository::OnFetch::new( repo_config.branches().clone(), @@ -304,7 +304,7 @@ mod positions { ); //then - eprintln!("Got: {err:?}"); + println!("Got: {err:?}"); // NOTE: assertions for correct push are in on_push above assert!(matches!( err, @@ -318,7 +318,7 @@ mod positions { //given let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); let repo_config = given::a_repo_config(); test_repository.on_fetch(git::repository::OnFetch::new( repo_config.branches().clone(), @@ -371,7 +371,7 @@ mod positions { ); //then - eprintln!("Got: {err:?}"); + println!("Got: {err:?}"); let_assert!( Ok(sha_next) = then::get_sha_for_branch(&fs, &gitdir, &repo_config.branches().next()), @@ -389,7 +389,7 @@ mod positions { //given let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); let repo_config = given::a_repo_config(); test_repository.on_fetch(git::repository::OnFetch::new( repo_config.branches().clone(), @@ -457,7 +457,7 @@ mod positions { ); //then - eprintln!("Got: {err:?}"); + println!("Got: {err:?}"); // NOTE: assertions for correct push are in on_push above assert!(matches!( err, @@ -471,7 +471,7 @@ mod positions { //given let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); let repo_config = given::a_repo_config(); test_repository.on_fetch(git::repository::OnFetch::new( repo_config.branches().clone(), @@ -522,7 +522,7 @@ mod positions { ); //then - eprintln!("Got: {err:?}"); + println!("Got: {err:?}"); let_assert!( Ok(sha_next) = then::get_sha_for_branch(&fs, &gitdir, &repo_config.branches().next()), @@ -540,7 +540,7 @@ mod positions { //given let_assert!(Ok(fs) = kxio::fs::temp(), "temp fs"); let gitdir: config::GitDir = fs.base().to_path_buf().into(); - let mut test_repository = git::repository::test(fs.clone()); + let mut test_repository = git::repository::test(fs.clone(), given::a_hostname()); let repo_config = given::a_repo_config(); test_repository.on_fetch(git::repository::OnFetch::new( repo_config.branches().clone(), @@ -571,7 +571,7 @@ mod positions { ); //then - eprintln!("positions: {positions:#?}"); + println!("positions: {positions:#?}"); let_assert!( Ok(main_sha) = diff --git a/crates/repo-actor/Cargo.toml b/crates/repo-actor/Cargo.toml index 4daf772..b1e20e0 100644 --- a/crates/repo-actor/Cargo.toml +++ b/crates/repo-actor/Cargo.toml @@ -42,12 +42,19 @@ ulid = { workspace = true } # boilerplate derive_more = { workspace = true } +thiserror = { workspace = true } # Actors actix = { workspace = true } actix-rt = { workspace = true } tokio = { workspace = true } +[dev-dependencies] +# Testing +assert2 = { workspace = true } +rand = { workspace = true } +pretty_assertions = { workspace = true } + [lints.clippy] nursery = { level = "warn", priority = -1 } # pedantic = "warn" diff --git a/crates/repo-actor/src/branch.rs b/crates/repo-actor/src/branch.rs index d514b82..5f44afc 100644 --- a/crates/repo-actor/src/branch.rs +++ b/crates/repo-actor/src/branch.rs @@ -1,78 +1,67 @@ // -use actix::prelude::*; - +use crate as actor; use git_next_config as config; use git_next_git as git; + +use derive_more::Display; use tracing::{info, warn}; -use crate::{MessageToken, ValidateRepo}; - -use std::time::Duration; - // advance next to the next commit towards the head of the dev branch #[tracing::instrument(fields(next), skip_all)] pub async fn advance_next( - next: git::Commit, - dev_commit_history: Vec, + next: &git::Commit, + dev_commit_history: &[git::Commit], repo_details: git::RepoDetails, repo_config: config::RepoConfig, repository: git::OpenRepository, - addr: Addr, - message_token: MessageToken, -) { - let next_commit = find_next_commit_on_dev(next, dev_commit_history); - let Some(commit) = next_commit else { - warn!("No commits to advance next to"); - return; - }; - if let Some(problem) = validate_commit_message(commit.message()) { - warn!("Can't advance next to commit '{}': {}", commit, problem); - return; - } + message_token: actor::messages::MessageToken, +) -> Result { + let commit = + find_next_commit_on_dev(next, dev_commit_history).ok_or_else(|| Error::NextAtDev)?; + validate_commit_message(commit.message())?; info!("Advancing next to commit '{}'", commit); - if let Err(err) = git::push::reset( + git::push::reset( &repository, &repo_details, &repo_config.branches().next(), &commit.into(), &git::push::Force::No, - ) { - warn!(?err, "Failed") - } - tokio::time::sleep(Duration::from_secs(10)).await; - addr.do_send(ValidateRepo { message_token }) + )?; + Ok(message_token) } #[tracing::instrument] -fn validate_commit_message(message: &git::commit::Message) -> Option { +fn validate_commit_message(message: &git::commit::Message) -> Result<()> { let message = &message.to_string(); if message.to_ascii_lowercase().starts_with("wip") { - return Some("Is Work-In-Progress".to_string()); + return Err(Error::IsWorkInProgress); } match git_conventional::Commit::parse(message) { Ok(commit) => { info!(?commit, "Pass"); - None + Ok(()) } Err(err) => { warn!(?err, "Fail"); - Some(err.kind().to_string()) + Err(Error::InvalidCommitMessage { + reason: err.kind().to_string(), + }) } } } pub fn find_next_commit_on_dev( - next: git::Commit, - dev_commit_history: Vec, + next: &git::Commit, + dev_commit_history: &[git::Commit], ) -> Option { - let mut next_commit: Option = None; - for commit in dev_commit_history.into_iter() { + let mut next_commit: Option<&git::Commit> = None; + for commit in dev_commit_history.iter() { if commit == next { break; }; next_commit.replace(commit); } - next_commit + next_commit.cloned() } // advance main branch to the commit 'next' @@ -82,15 +71,30 @@ pub async fn advance_main( repo_details: &git::RepoDetails, repo_config: &config::RepoConfig, repository: &git::OpenRepository, -) { +) -> Result<()> { info!("Advancing main to next"); - if let Err(err) = git::push::reset( + git::push::reset( repository, repo_details, &repo_config.branches().main(), &next.into(), &git::push::Force::No, - ) { - warn!(?err, "Failed") - }; + )?; + Ok(()) +} + +pub type Result = core::result::Result; +#[derive(Debug, thiserror::Error, Display)] +pub enum Error { + #[display("push: {}", 0)] + Push(#[from] git::push::Error), + + #[display("no commits to advance next to")] + NextAtDev, + + #[display("commit is a Work-in-progress")] + IsWorkInProgress, + + #[display("commit message is not in conventional commit format: {reason}")] + InvalidCommitMessage { reason: String }, } diff --git a/crates/repo-actor/src/handlers/advance_to_main.rs b/crates/repo-actor/src/handlers/advance_to_main.rs new file mode 100644 index 0000000..ab4ffb5 --- /dev/null +++ b/crates/repo-actor/src/handlers/advance_to_main.rs @@ -0,0 +1,52 @@ +// +use actix::prelude::*; +use tracing::Instrument as _; + +use crate as actor; + +impl Handler for actor::RepoActor { + type Result = (); + #[tracing::instrument(name = "RepoActor::AdvanceMainTo", skip_all, fields(repo = %self.repo_details, commit = ?msg))] + fn handle( + &mut self, + msg: actor::messages::AdvanceMainTo, + ctx: &mut Self::Context, + ) -> Self::Result { + let Some(repo_config) = self.repo_details.repo_config.clone() else { + tracing::warn!("No config loaded"); + return; + }; + let Some(repository) = self.open_repository.clone() else { + tracing::warn!("No repository opened"); + return; + }; + let repo_details = self.repo_details.clone(); + let addr = ctx.address(); + let message_token = self.message_token; + async move { + match actor::branch::advance_main( + msg.unwrap(), + &repo_details, + &repo_config, + &repository, + ) + .await + { + Err(err) => { + tracing::warn!("advance main: {err}"); + } + Ok(_) => match repo_config.source() { + git_next_config::RepoConfigSource::Repo => { + addr.do_send(actor::messages::LoadConfigFromRepo) + } + git_next_config::RepoConfigSource::Server => { + addr.do_send(actor::messages::ValidateRepo::new(message_token)) + } + }, + } + } + .in_current_span() + .into_actor(self) + .wait(ctx); + } +} diff --git a/crates/repo-actor/src/handlers/clone.rs b/crates/repo-actor/src/handlers/clone.rs new file mode 100644 index 0000000..d051018 --- /dev/null +++ b/crates/repo-actor/src/handlers/clone.rs @@ -0,0 +1,37 @@ +// +use actix::prelude::*; + +use crate as actor; +use git_next_git as git; + +impl Handler for actor::RepoActor { + type Result = (); + #[tracing::instrument(name = "RepoActor::CloneRepo", skip_all, fields(repo = %self.repo_details /*, gitdir = %self.repo_details.gitdir */))] + fn handle( + &mut self, + _msg: actor::messages::CloneRepo, + ctx: &mut Self::Context, + ) -> Self::Result { + println!("handler clone repo start"); + let gitdir = self.repo_details.gitdir.clone(); + match git::repository::open(&self.repository, &self.repo_details, gitdir) { + Ok(repository) => { + println!("- open okay"); + self.open_repository.replace(repository); + if self.repo_details.repo_config.is_none() { + println!("need to load config from repo"); + ctx.address().do_send(actor::messages::LoadConfigFromRepo); + } else { + println!("need to validate repo"); + ctx.address() + .do_send(actor::messages::ValidateRepo::new(self.message_token)); + } + } + Err(err) => { + println!("err: {err:?}"); + tracing::warn!("Could not open repo: {err}") + } + } + println!("handler clone repo finish"); + } +} diff --git a/crates/repo-actor/src/handlers/load_config_from_repo.rs b/crates/repo-actor/src/handlers/load_config_from_repo.rs new file mode 100644 index 0000000..d0f0011 --- /dev/null +++ b/crates/repo-actor/src/handlers/load_config_from_repo.rs @@ -0,0 +1,31 @@ +// +use actix::prelude::*; +use tracing::Instrument as _; + +use crate as actor; + +impl Handler for actor::RepoActor { + type Result = (); + #[tracing::instrument(name = "RepoActor::LoadConfigFromRepo", skip_all, fields(repo = %self.repo_details))] + fn handle( + &mut self, + _msg: actor::messages::LoadConfigFromRepo, + ctx: &mut Self::Context, + ) -> Self::Result { + let details = self.repo_details.clone(); + let addr = ctx.address(); + let Some(open_repository) = self.open_repository.clone() else { + tracing::warn!("missing open repository - can't load configuration"); + return; + }; + async move { + match actor::load::config_from_repository(details, open_repository).await { + Ok(repo_config) => addr.do_send(actor::messages::LoadedConfig::new(repo_config)), + Err(err) => tracing::warn!(?err, "Failed to load config"), + } + } + .in_current_span() + .into_actor(self) + .wait(ctx); + } +} diff --git a/crates/repo-actor/src/handlers/loaded_config.rs b/crates/repo-actor/src/handlers/loaded_config.rs new file mode 100644 index 0000000..3a2f005 --- /dev/null +++ b/crates/repo-actor/src/handlers/loaded_config.rs @@ -0,0 +1,20 @@ +// +use actix::prelude::*; + +use crate as actor; + +impl Handler for actor::RepoActor { + type Result = (); + #[tracing::instrument(name = "RepoActor::LoadedConfig", skip_all, fields(repo = %self.repo_details, branches = ?msg))] + fn handle( + &mut self, + msg: actor::messages::LoadedConfig, + ctx: &mut Self::Context, + ) -> Self::Result { + let repo_config = msg.unwrap(); + self.repo_details.repo_config.replace(repo_config); + + ctx.address() + .do_send(actor::messages::ValidateRepo::new(self.message_token)); + } +} diff --git a/crates/repo-actor/src/handlers/mod.rs b/crates/repo-actor/src/handlers/mod.rs new file mode 100644 index 0000000..b4480c2 --- /dev/null +++ b/crates/repo-actor/src/handlers/mod.rs @@ -0,0 +1,11 @@ +pub mod advance_to_main; +pub mod clone; +pub mod load_config_from_repo; +pub mod loaded_config; +pub mod start_monitoring; +pub mod validate_repo; +pub mod webhook_message; +pub mod webhook_registered; + +#[cfg(test)] +pub mod test; diff --git a/crates/repo-actor/src/handlers/start_monitoring.rs b/crates/repo-actor/src/handlers/start_monitoring.rs new file mode 100644 index 0000000..b60a7d7 --- /dev/null +++ b/crates/repo-actor/src/handlers/start_monitoring.rs @@ -0,0 +1,83 @@ +// +use actix::prelude::*; +use tracing::Instrument as _; + +use crate as actor; +use git_next_git as git; + +impl Handler for actor::RepoActor { + type Result = (); + #[tracing::instrument(name = "RepoActor::StartMonitoring", skip_all, + fields(token = %self.message_token, repo = %self.repo_details, main = %msg.main(), next = %msg.next(), dev = %msg.dev())) + ] + fn handle( + &mut self, + msg: actor::messages::StartMonitoring, + ctx: &mut Self::Context, + ) -> Self::Result { + let Some(repo_config) = self.repo_details.repo_config.clone() else { + tracing::warn!("No config loaded"); + return; + }; + + let next_ahead_of_main = msg.main() != msg.next(); + let dev_ahead_of_next = msg.next() != msg.dev(); + tracing::info!(next_ahead_of_main, dev_ahead_of_next, "StartMonitoring"); + + let addr = ctx.address(); + let forge = self.forge.clone(); + + if next_ahead_of_main { + let message_token = self.message_token; + let sleep_duration = self.sleep_duration; + async move { + // get the status - pass, fail, pending (all others map to fail, e.g. error) + let status = forge.commit_status(msg.next()).await; + tracing::info!(?status, "Checking next branch"); + match status { + git::forge::commit::Status::Pass => { + addr.do_send(actor::messages::AdvanceMainTo::new(msg.next().clone())); + } + git::forge::commit::Status::Pending => { + tokio::time::sleep(sleep_duration).await; + addr.do_send(actor::messages::ValidateRepo::new(message_token)); + } + git::forge::commit::Status::Fail => { + tracing::warn!("Checks have failed"); + } + } + } + .in_current_span() + .into_actor(self) + .wait(ctx); + } else if dev_ahead_of_next { + if let Some(repository) = self.open_repository.clone() { + let repo_details = self.repo_details.clone(); + let message_token = self.message_token; + let sleep_duration = self.sleep_duration; + async move { + match actor::branch::advance_next( + msg.next(), + msg.dev_commit_history(), + repo_details, + repo_config, + repository, + message_token, + ) + .await + { + Ok(message_token) => { + // pause to allow any CI checks to be started + tokio::time::sleep(sleep_duration).await; + addr.do_send(actor::messages::ValidateRepo::new(message_token)) + } + Err(err) => tracing::warn!("advance next: {err}"), + } + } + .in_current_span() + .into_actor(self) + .wait(ctx); + } + } + } +} diff --git a/crates/repo-actor/src/handlers/test.rs b/crates/repo-actor/src/handlers/test.rs new file mode 100644 index 0000000..15060dd --- /dev/null +++ b/crates/repo-actor/src/handlers/test.rs @@ -0,0 +1,18 @@ +use crate as repo_actor; +use actix::{Handler, Message}; +use git_next_git as git; +use repo_actor::message; + +message!(GetRepositoryLog => Vec); + +impl Handler for repo_actor::RepoActor { + type Result = Vec; + + fn handle(&mut self, _msg: GetRepositoryLog, _ctx: &mut Self::Context) -> Self::Result { + match &mut self.open_repository { + Some(git::OpenRepository::Test(tor)) => tor.take_log(), + Some(git::OpenRepository::Mock(mor)) => mor.take_log(), + _ => unimplemented!("Only Test and Mock repositories support this message"), + } + } +} diff --git a/crates/repo-actor/src/handlers/validate_repo.rs b/crates/repo-actor/src/handlers/validate_repo.rs new file mode 100644 index 0000000..efd3ded --- /dev/null +++ b/crates/repo-actor/src/handlers/validate_repo.rs @@ -0,0 +1,98 @@ +// +use actix::prelude::*; +use derive_more::Deref as _; +use tracing::Instrument as _; + +use crate as actor; +use git_next_git as git; + +impl Handler for actor::RepoActor { + type Result = (); + #[tracing::instrument(name = "RepoActor::ValidateRepo", skip_all, fields(repo = %self.repo_details, token = %msg.deref()))] + fn handle( + &mut self, + msg: actor::messages::ValidateRepo, + ctx: &mut Self::Context, + ) -> Self::Result { + println!("handler validate repo - start"); + match msg.unwrap() { + message_token if self.message_token < message_token => { + tracing::info!(%message_token, "New message token"); + self.message_token = message_token; + } + message_token if self.message_token > message_token => { + tracing::info!("Dropping message from previous generation"); + return; // message is expired + } + _ => { + // do nothing + } + } + if self.webhook_id.is_none() { + let forge_alias = self.repo_details.forge.forge_alias(); + let repo_alias = &self.repo_details.repo_alias; + let webhook_url = self.webhook.url(forge_alias, repo_alias); + let forge = self.forge.clone(); + let addr = ctx.address(); + async move { + if let Err(err) = + forge + .register_webhook(&webhook_url) + .await + .and_then(|registered_webhook| { + addr.try_send(actor::messages::WebhookRegistered::from( + registered_webhook, + )) + .map_err(|e| { + git::forge::webhook::Error::FailedToNotifySelf(e.to_string()) + }) + }) + { + tracing::warn!("registering webhook: {err}"); + } + } + .in_current_span() + .into_actor(self) + .wait(ctx); + } + if let (Some(repository), Some(repo_config)) = ( + self.open_repository.clone(), + self.repo_details.repo_config.clone(), + ) { + let repo_details = self.repo_details.clone(); + let addr = ctx.address(); + let message_token = self.message_token; + let sleep_duration = self.sleep_duration; + async move { + match git::validation::positions::validate_positions( + &repository, + &repo_details, + repo_config, + ) { + Ok(git::validation::positions::Positions { + main, + next, + dev, + dev_commit_history, + }) => { + addr.do_send(actor::messages::StartMonitoring::new( + main, + next, + dev, + dev_commit_history, + )); + } + Err(err) => { + tracing::warn!("{:?}", err); + tokio::time::sleep(sleep_duration).await; + addr.do_send(actor::messages::ValidateRepo::new(message_token)); + } + } + } + .in_current_span() + .into_actor(self) + .wait(ctx); + } + println!("handler validate repo - finish"); + } +} diff --git a/crates/repo-actor/src/webhook.rs b/crates/repo-actor/src/handlers/webhook_message.rs similarity index 95% rename from crates/repo-actor/src/webhook.rs rename to crates/repo-actor/src/handlers/webhook_message.rs index 21c808a..79bc3fa 100644 --- a/crates/repo-actor/src/webhook.rs +++ b/crates/repo-actor/src/handlers/webhook_message.rs @@ -1,13 +1,13 @@ // use actix::prelude::*; -use crate::{RepoActor, ValidateRepo}; +use crate as actor; use git_next_config as config; use git_next_git as git; use tracing::{info, warn}; -impl Handler for RepoActor { +impl Handler for actor::RepoActor { type Result = (); #[allow(clippy::cognitive_complexity)] // TODO: (#49) reduce complexity @@ -88,6 +88,7 @@ impl Handler for RepoActor { token = %message_token, "New commit" ); - ctx.address().do_send(ValidateRepo { message_token }); + ctx.address() + .do_send(actor::messages::ValidateRepo::new(message_token)); } } diff --git a/crates/repo-actor/src/handlers/webhook_registered.rs b/crates/repo-actor/src/handlers/webhook_registered.rs new file mode 100644 index 0000000..f11ff2b --- /dev/null +++ b/crates/repo-actor/src/handlers/webhook_registered.rs @@ -0,0 +1,17 @@ +// +use actix::prelude::*; + +use crate as actor; + +impl Handler for actor::RepoActor { + type Result = (); + #[tracing::instrument(name = "RepoActor::WebhookRegistered", skip_all, fields(repo = %self.repo_details, webhook_id = %msg.webhook_id()))] + fn handle( + &mut self, + msg: actor::messages::WebhookRegistered, + _ctx: &mut Self::Context, + ) -> Self::Result { + self.webhook_id.replace(msg.webhook_id().clone()); + self.webhook_auth.replace(msg.webhook_auth().clone()); + } +} diff --git a/crates/repo-actor/src/lib.rs b/crates/repo-actor/src/lib.rs index 3bf1815..eeddbcb 100644 --- a/crates/repo-actor/src/lib.rs +++ b/crates/repo-actor/src/lib.rs @@ -1,30 +1,58 @@ mod branch; +pub mod handlers; mod load; -pub mod status; -pub mod webhook; +pub mod messages; #[cfg(test)] mod tests; -use std::time::Duration; - use actix::prelude::*; -use config::RegisteredWebhook; -use git::validation::positions::{validate_positions, Positions}; -use crate as repo_actor; use git_next_config as config; use git_next_forge as forge; use git_next_git as git; use kxio::network::Network; -use tracing::{debug, info, warn, Instrument}; +use tracing::{info, warn, Instrument}; #[derive(Debug, derive_more::Display)] #[display("{}:{}:{}", generation, repo_details.forge.forge_alias(), repo_details.repo_alias)] +/// An actor that represents a Git Repository. +/// +/// When this actor is started it is sent the [CloneRepo] message. +/// +/// ```mermaid +/// stateDiagram-v2 +/// [*] --> CloneRepo :START +/// +/// CloneRepo --> LoadConfigFromRepo +/// CloneRepo --> ValidateRepo +/// +/// LoadConfigFromRepo --> LoadedConfig +/// +/// ValidateRepo --> WebhookRegistered +/// ValidateRepo --> StartMonitoring +/// ValidateRepo --> ValidateRepo :SLEEP 10s +/// +/// LoadedConfig --> ValidateRepo +/// +/// WebhookRegistered --> [*] +/// +/// StartMonitoring --> AdvanceMainTo +/// StartMonitoring --> ValidateRepo :SLEEP 10s +/// +/// AdvanceMainTo --> LoadConfigFromRepo +/// AdvanceMainTo --> ValidateRepo +/// +/// [*] --> WebhookMessage :WEBHOOK +/// +/// WebhookMessage --> ValidateRepo +/// ``` +/// pub struct RepoActor { + sleep_duration: std::time::Duration, generation: git::Generation, - message_token: MessageToken, + message_token: messages::MessageToken, repo_details: git::RepoDetails, webhook: config::server::Webhook, webhook_id: Option, // INFO: if [None] then no webhook is configured @@ -39,28 +67,30 @@ pub struct RepoActor { } impl RepoActor { pub fn new( - details: git::RepoDetails, + repo_details: git::RepoDetails, webhook: config::server::Webhook, generation: git::Generation, net: Network, - repo: git::Repository, + repository: git::Repository, + sleep_duration: std::time::Duration, ) -> Self { - let forge = forge::Forge::new(details.clone(), net.clone()); - debug!(?forge, "new"); + let message_token = messages::MessageToken::default(); + let forge = forge::Forge::new(repo_details.clone(), net.clone()); Self { generation, - message_token: MessageToken::new(), - repo_details: details, + message_token, + repo_details, webhook, webhook_id: None, webhook_auth: None, last_main_commit: None, last_next_commit: None, last_dev_commit: None, - repository: repo, + repository, open_repository: None, - net, forge, + net, + sleep_duration, } } } @@ -68,9 +98,11 @@ impl Actor for RepoActor { type Context = Context; #[tracing::instrument(name = "RepoActor::stopping", skip_all, fields(repo = %self.repo_details))] fn stopping(&mut self, ctx: &mut Self::Context) -> Running { + eprintln!("stopping"); info!("Checking webhook"); match self.webhook_id.take() { Some(webhook_id) => { + eprintln!("stopping - unregistering webhook"); info!(%webhook_id, "Unregistring webhook"); let forge = self.forge.clone(); async move { @@ -87,252 +119,3 @@ impl Actor for RepoActor { } } } - -#[derive(Message)] -#[rtype(result = "()")] -pub struct CloneRepo; -impl Handler for RepoActor { - type Result = (); - #[tracing::instrument(name = "RepoActor::CloneRepo", skip_all, fields(repo = %self.repo_details /*, gitdir = %self.repo_details.gitdir */))] - fn handle(&mut self, _msg: CloneRepo, ctx: &mut Self::Context) -> Self::Result { - let gitdir = self.repo_details.gitdir.clone(); - match git::repository::open(&self.repository, &self.repo_details, gitdir) { - Ok(repository) => { - self.open_repository.replace(repository); - if self.repo_details.repo_config.is_none() { - ctx.address().do_send(LoadConfigFromRepo); - } else { - ctx.address().do_send(ValidateRepo { - message_token: self.message_token, - }); - } - } - Err(err) => warn!("Could not open repo: {err}"), - } - } -} - -#[derive(Message)] -#[rtype(result = "()")] -pub struct LoadConfigFromRepo; -impl Handler for RepoActor { - type Result = (); - #[tracing::instrument(name = "RepoActor::LoadConfigFromRepo", skip_all, fields(repo = %self.repo_details))] - fn handle(&mut self, _msg: LoadConfigFromRepo, ctx: &mut Self::Context) -> Self::Result { - let details = self.repo_details.clone(); - let addr = ctx.address(); - let Some(open_repository) = self.open_repository.clone() else { - warn!("missing open repository - can't load configuration"); - return; - }; - repo_actor::load::load_file(details, addr, open_repository) - .in_current_span() - .into_actor(self) - .wait(ctx); - } -} - -#[derive(Message)] -#[rtype(result = "()")] -struct LoadedConfig(git_next_config::RepoConfig); -impl Handler for RepoActor { - type Result = (); - #[tracing::instrument(name = "RepoActor::LoadedConfig", skip_all, fields(repo = %self.repo_details, branches = %msg.0))] - fn handle(&mut self, msg: LoadedConfig, ctx: &mut Self::Context) -> Self::Result { - let repo_config = msg.0; - self.repo_details.repo_config.replace(repo_config); - - ctx.address().do_send(ValidateRepo { - message_token: self.message_token, - }); - } -} - -#[derive(derive_more::Constructor, Message)] -#[rtype(result = "()")] -pub struct ValidateRepo { - message_token: MessageToken, -} -impl Handler for RepoActor { - type Result = (); - #[tracing::instrument(name = "RepoActor::ValidateRepo", skip_all, fields(repo = %self.repo_details, token = %msg.message_token))] - fn handle(&mut self, msg: ValidateRepo, ctx: &mut Self::Context) -> Self::Result { - match msg.message_token { - message_token if self.message_token < message_token => { - info!(%message_token, "New message token"); - self.message_token = msg.message_token; - } - message_token if self.message_token > message_token => { - info!("Dropping message from previous generation"); - return; // message is expired - } - _ => { - // do nothing - } - } - if self.webhook_id.is_none() { - let forge_alias = self.repo_details.forge.forge_alias(); - let repo_alias = &self.repo_details.repo_alias; - let webhook_url = self.webhook.url(forge_alias, repo_alias); - let forge = self.forge.clone(); - let addr = ctx.address(); - async move { - if let Err(err) = - forge - .register_webhook(&webhook_url) - .await - .and_then(|registered_webhook| { - addr.try_send(WebhookRegistered::from(registered_webhook)) - .map_err(|e| { - git::forge::webhook::Error::FailedToNotifySelf(e.to_string()) - }) - }) - { - warn!("registering webhook: {err}"); - } - } - .in_current_span() - .into_actor(self) - .wait(ctx); - } - if let (Some(repository), Some(repo_config)) = ( - self.open_repository.clone(), - self.repo_details.repo_config.clone(), - ) { - let repo_details = self.repo_details.clone(); - let addr = ctx.address(); - let message_token = self.message_token; - async move { - match validate_positions(&repository, &repo_details, repo_config) { - Ok(Positions { - main, - next, - dev, - dev_commit_history, - }) => { - addr.do_send(StartMonitoring::new(main, next, dev, dev_commit_history)); - } - Err(err) => { - warn!("{:?}", err); - tokio::time::sleep(Duration::from_secs(10)).await; - addr.do_send(ValidateRepo::new(message_token)); - } - } - } - .in_current_span() - .into_actor(self) - .wait(ctx); - } - } -} - -#[derive(Debug, derive_more::Constructor, Message)] -#[rtype(result = "()")] -pub struct StartMonitoring { - main: git::Commit, - next: git::Commit, - dev: git::Commit, - dev_commit_history: Vec, -} -impl Handler for RepoActor { - type Result = (); - #[tracing::instrument(name = "RepoActor::StartMonitoring", skip_all, - fields(token = %self.message_token, repo = %self.repo_details, main = %msg.main, next= %msg.next, dev = %msg.dev)) - ] - fn handle(&mut self, msg: StartMonitoring, ctx: &mut Self::Context) -> Self::Result { - let Some(repo_config) = self.repo_details.repo_config.clone() else { - warn!("No config loaded"); - return; - }; - - let next_ahead_of_main = msg.main != msg.next; - let dev_ahead_of_next = msg.next != msg.dev; - info!(next_ahead_of_main, dev_ahead_of_next, "StartMonitoring"); - - let addr = ctx.address(); - let forge = self.forge.clone(); - - if next_ahead_of_main { - status::check_next(msg.next, addr, forge, self.message_token) - .in_current_span() - .into_actor(self) - .wait(ctx); - } else if dev_ahead_of_next { - if let Some(repository) = self.open_repository.clone() { - branch::advance_next( - msg.next, - msg.dev_commit_history, - self.repo_details.clone(), - repo_config, - repository, - addr, - self.message_token, - ) - .in_current_span() - .into_actor(self) - .wait(ctx); - } - } - } -} - -#[derive(Message)] -#[rtype(result = "()")] -pub struct WebhookRegistered(config::WebhookId, config::WebhookAuth); -impl From for WebhookRegistered { - fn from(value: RegisteredWebhook) -> Self { - Self(value.id().clone(), value.auth().clone()) - } -} -impl Handler for RepoActor { - type Result = (); - #[tracing::instrument(name = "RepoActor::WebhookRegistered", skip_all, fields(repo = %self.repo_details, webhook_id = %msg.0))] - fn handle(&mut self, msg: WebhookRegistered, _ctx: &mut Self::Context) -> Self::Result { - self.webhook_id.replace(msg.0); - self.webhook_auth.replace(msg.1); - } -} - -#[derive(Message)] -#[rtype(result = "()")] -pub struct AdvanceMainTo(git::Commit); -impl Handler for RepoActor { - type Result = (); - #[tracing::instrument(name = "RepoActor::AdvanceMainTo", skip_all, fields(repo = %self.repo_details, commit = %msg.0))] - fn handle(&mut self, msg: AdvanceMainTo, ctx: &mut Self::Context) -> Self::Result { - let Some(repo_config) = self.repo_details.repo_config.clone() else { - warn!("No config loaded"); - return; - }; - let Some(repository) = self.open_repository.clone() else { - warn!("No repository opened"); - return; - }; - let repo_details = self.repo_details.clone(); - let addr = ctx.address(); - let message_token = self.message_token; - async move { - branch::advance_main(msg.0, &repo_details, &repo_config, &repository).await; - match repo_config.source() { - git_next_config::RepoConfigSource::Repo => addr.do_send(LoadConfigFromRepo), - git_next_config::RepoConfigSource::Server => { - addr.do_send(ValidateRepo { message_token }) - } - } - } - .in_current_span() - .into_actor(self) - .wait(ctx); - } -} - -#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)] -pub struct MessageToken(u32); -impl MessageToken { - pub fn new() -> Self { - Self::default() - } - pub const fn next(&self) -> Self { - Self(self.0 + 1) - } -} diff --git a/crates/repo-actor/src/load.rs b/crates/repo-actor/src/load.rs index 313e344..7da477e 100644 --- a/crates/repo-actor/src/load.rs +++ b/crates/repo-actor/src/load.rs @@ -1,75 +1,54 @@ -use std::path::PathBuf; - // -use actix::prelude::*; - -use tracing::{error, info}; +use derive_more::Display; +use std::path::PathBuf; +use tracing::info; use git_next_config as config; use git_next_git as git; -use super::{LoadedConfig, RepoActor}; - /// Loads the [RepoConfig] from the `.git-next.toml` file in the repository #[tracing::instrument(skip_all, fields(branch = %repo_details.branch))] -pub async fn load_file( +pub async fn config_from_repository( repo_details: git::RepoDetails, - addr: Addr, open_repository: git::OpenRepository, -) { +) -> Result { info!("Loading .git-next.toml from repo"); - let repo_config = match load(&repo_details, &open_repository).await { - Ok(repo_config) => repo_config, - Err(err) => { - error!(?err, "Failed to load config"); - return; - } - }; - info!("Loaded .git-next.toml from repo"); - addr.do_send(LoadedConfig(repo_config)); -} - -async fn load( - details: &git::RepoDetails, - open_repository: &git::OpenRepository, -) -> Result { - let contents = open_repository.read_file(&details.branch, &PathBuf::from(".git-next.toml"))?; - let config = config::RepoConfig::load(&contents)?; - let config = validate(config, open_repository).await?; + let contents = + open_repository.read_file(&repo_details.branch, &PathBuf::from(".git-next.toml"))?; + let config = config::RepoConfig::parse(&contents)?; + let branches = open_repository.remote_branches()?; + required_branch(&config.branches().main(), &branches)?; + required_branch(&config.branches().next(), &branches)?; + required_branch(&config.branches().dev(), &branches)?; Ok(config) } -#[derive(Debug, derive_more::From, derive_more::Display)] +fn required_branch( + branch_name: &config::BranchName, + branches: &[config::BranchName], +) -> Result<()> { + branches + .iter() + .find(|branch| *branch == branch_name) + .ok_or_else(|| Error::BranchNotFound(branch_name.clone()))?; + Ok(()) +} + +pub type Result = core::result::Result; +#[derive(Debug, thiserror::Error, Display)] pub enum Error { - File(git::file::Error), - Config(config::server::Error), - Toml(toml::de::Error), - Branch(git::push::Error), + #[display("file")] + File(#[from] git::file::Error), + + #[display("config")] + Config(#[from] config::server::Error), + + #[display("toml")] + Toml(#[from] toml::de::Error), + + #[display("push")] + Push(#[from] git::push::Error), + + #[display("branch not found: {}", 0)] BranchNotFound(config::BranchName), } - -pub async fn validate( - config: config::RepoConfig, - open_repository: &git::OpenRepository, -) -> Result { - let branches = open_repository.remote_branches()?; - if !branches - .iter() - .any(|branch| branch == &config.branches().main()) - { - return Err(Error::BranchNotFound(config.branches().main())); - } - if !branches - .iter() - .any(|branch| branch == &config.branches().next()) - { - return Err(Error::BranchNotFound(config.branches().next())); - } - if !branches - .iter() - .any(|branch| branch == &config.branches().dev()) - { - return Err(Error::BranchNotFound(config.branches().dev())); - } - Ok(config) -} diff --git a/crates/repo-actor/src/messages.rs b/crates/repo-actor/src/messages.rs new file mode 100644 index 0000000..811fb2a --- /dev/null +++ b/crates/repo-actor/src/messages.rs @@ -0,0 +1,89 @@ +// +use actix::prelude::*; +use config::newtype; +use derive_more::{Constructor, Display}; + +use git_next_config as config; +use git_next_git as git; + +#[macro_export] +macro_rules! message { + ($name:ident wraps $value:ty) => { + git_next_config::newtype!($name is a $value); + impl Message for $name { + type Result = (); + } + }; + ($name:ident) => { + git_next_config::newtype!($name); + impl Message for $name { + type Result = (); + } + }; + ($name:ident wraps $value:ty => $result:ty) => { + git_next_config::newtype!($name is a $value); + impl Message for $name { + type Result = $result; + } + }; + ($name:ident => $result:ty) => { + git_next_config::newtype!($name); + impl Message for $name { + type Result = $result; + } + }; +} + +message!(LoadConfigFromRepo); +message!(CloneRepo); +message!(LoadedConfig wraps config::RepoConfig); +message!(ValidateRepo wraps MessageToken); + +#[derive(Debug, Constructor, Message)] +#[rtype(result = "()")] +pub struct StartMonitoring { + main: git::Commit, + next: git::Commit, + dev: git::Commit, + dev_commit_history: Vec, +} +impl StartMonitoring { + pub const fn main(&self) -> &git::Commit { + &self.main + } + pub const fn next(&self) -> &git::Commit { + &self.next + } + pub const fn dev(&self) -> &git::Commit { + &self.dev + } + pub fn dev_commit_history(&self) -> &[git::Commit] { + &self.dev_commit_history + } +} + +message!(WebhookRegistered wraps (config::WebhookId, config::WebhookAuth)); +impl WebhookRegistered { + pub const fn webhook_id(&self) -> &config::WebhookId { + &self.0 .0 + } + pub const fn webhook_auth(&self) -> &config::WebhookAuth { + &self.0 .1 + } +} +impl From for WebhookRegistered { + fn from(value: config::RegisteredWebhook) -> Self { + let webhook_id = value.id().clone(); + let webhook_auth = value.auth().clone(); + Self::from((webhook_id, webhook_auth)) + } +} + +message!(AdvanceMainTo wraps git::Commit); + +newtype!(MessageToken is a u32, Copy, Default, Display); +impl MessageToken { + pub const fn next(&self) -> Self { + Self(self.0 + 1) + } +} diff --git a/crates/repo-actor/src/status.rs b/crates/repo-actor/src/status.rs deleted file mode 100644 index b7b4ea4..0000000 --- a/crates/repo-actor/src/status.rs +++ /dev/null @@ -1,33 +0,0 @@ -// -use actix::prelude::*; - -use git_next_forge as forge; -use git_next_git as git; -use tracing::{info, warn}; - -use crate::{MessageToken, ValidateRepo}; - -use super::AdvanceMainTo; - -pub async fn check_next( - next: git::Commit, - addr: Addr, - forge: forge::Forge, - message_token: MessageToken, -) { - // get the status - pass, fail, pending (all others map to fail, e.g. error) - let status = forge.commit_status(&next).await; - info!(?status, "Checking next branch"); - match status { - git::forge::commit::Status::Pass => { - addr.do_send(AdvanceMainTo(next)); - } - git::forge::commit::Status::Pending => { - tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; - addr.do_send(ValidateRepo { message_token }); - } - git::forge::commit::Status::Fail => { - warn!("Checks have failed"); - } - } -} diff --git a/crates/repo-actor/src/tests.rs b/crates/repo-actor/src/tests.rs index b9c7df4..f85b984 100644 --- a/crates/repo-actor/src/tests.rs +++ b/crates/repo-actor/src/tests.rs @@ -1,34 +1,1036 @@ // +#![allow(unused_imports)] // TODO remove this + +use crate as actor; +use git_next_config as config; +use git_next_forge as forge; +use git_next_git as git; + use super::*; + +use assert2::let_assert; + +type TestResult = Result<(), Box>; + +mod repo_actor { + use super::*; + use actix::prelude::*; + + mod clone_repo { + use crate::{ + handlers::test::GetRepositoryLog, + messages::{CloneRepo, WebhookRegistered}, + tests::given, + }; + + use super::*; + + #[actix::test] + async fn should_clone() -> TestResult { + let fs = given::a_filesystem(); + let net = given::a_network(); + let repo_details = given::repo_details(&fs); + let repo_path = repo_details.repo_path.clone(); + let hostname = repo_details.forge.hostname().clone(); + let mut repository = git::repository::mock(); + repository + .given_can_be_cloned(repo_details.clone()) + .with_default_remote(git::repository::Direction::Push) + .with_default_remote(git::repository::Direction::Fetch); + let actor = + given::a_repo_actor(repo_details, git::Repository::Mock(repository), net.into()); + let addr = actor.start(); + + addr.send(CloneRepo::new()).await?; + + let logs = addr.send(GetRepositoryLog).await?; + + assert_eq!( + logs.first(), + Some(format!( + "git_clone hostname:{hostname} repo_path:{repo_path}" + )) + .as_ref() + ); + + System::current().stop(); + + Ok(()) + } + + #[actix::test] + async fn should_open() -> TestResult { + let fs = given::a_filesystem(); + let (open_repo, repo_details, test_repo) = given::an_open_repository(&fs); + let gitdir = repo_details.gitdir.clone(); + println!("open_repo: {open_repo:#?}"); + let net = given::a_network(); + // open_repo + // .given_default_remote(git::repository::Direction::Push); + // .with_default_remote(git::repository::Direction::Fetch); + let actor = + given::a_repo_actor(repo_details, git::Repository::Test(test_repo), net.into()); + let addr = actor.start(); + + addr.send(CloneRepo::new()).await?; + + let logs = addr.send(GetRepositoryLog).await?; + + assert_eq!(logs.first(), Some(format!("open gitdir:{gitdir}")).as_ref()); + + System::current().stop(); + + Ok(()) + } + } +} mod branch { - use super::super::branch::*; + use super::*; + mod advance_next { + use super::*; + mod when_at_dev { + // next and dev branches are the same + use super::*; + #[tokio::test] + async fn should_not_push() -> TestResult { + let next = given::a_commit(); + let dev_commit_history = &[next.clone()]; + let fs = given::a_filesystem(); + let repo_config = given::a_repo_config(); + let (repository, repo_details, _) = given::an_open_repository(&fs); + // no on_push defined - so any call to push will cause an error + let message_token = given::a_message_token(); + let_assert!( + Err(err) = actor::branch::advance_next( + &next, + dev_commit_history, + repo_details, + repo_config, + repository, + message_token, + ) + .await + ); + println!("Got: {err}"); + assert!(matches!(err, actor::branch::Error::NextAtDev)); + Ok(()) + } + } + mod can_advance { + // dev has at least one commit ahead of next + use super::*; + mod to_wip_commit { + // commit on dev is either invalid message or a WIP + use super::*; + #[tokio::test] + async fn should_not_push() -> TestResult { + let next = given::a_commit(); + let dev = given::a_commit_with_message(&git::commit::Message::new( + "wip: test: message".to_string(), + )); + let dev_commit_history = &[dev.clone(), next.clone()]; + let fs = given::a_filesystem(); + let repo_config = given::a_repo_config(); + let (repository, repo_details, _) = given::an_open_repository(&fs); + // no on_push defined - so any call to push will cause an error + let message_token = given::a_message_token(); + let_assert!( + Err(err) = actor::branch::advance_next( + &next, + dev_commit_history, + repo_details, + repo_config, + repository, + message_token, + ) + .await + ); + println!("Got: {err}"); + assert!(matches!(err, actor::branch::Error::IsWorkInProgress)); + Ok(()) + } + } + mod to_invalid_commit { + // commit on dev is either invalid message or a WIP + use super::*; + #[tokio::test] + async fn should_not_push_and_error() -> TestResult { + let next = given::a_commit(); + let dev = given::a_commit(); + let dev_commit_history = &[dev.clone(), next.clone()]; + let fs = given::a_filesystem(); + let repo_config = given::a_repo_config(); + let (repository, repo_details, _) = given::an_open_repository(&fs); + // no on_push defined - so any call to push will cause an error + let message_token = given::a_message_token(); + let_assert!( + Err(err) = actor::branch::advance_next( + &next, + dev_commit_history, + repo_details, + repo_config, + repository, + message_token, + ) + .await + ); + println!("Got: {err}"); + assert!(matches!( + err, + actor::branch::Error::InvalidCommitMessage{reason} + if reason == "Missing type in the commit summary, expected `type: description`" + )); + Ok(()) + } + } + mod to_valid_commit { + // commit on dev is valid conventional commit message + use super::*; + mod push_is_err { + use git::repository::{OnFetch, OnPush}; + + // the git push command fails + use super::*; + #[tokio::test] + async fn should_error() -> TestResult { + let next = given::a_commit(); + let dev = given::a_commit_with_message(&git::commit::Message::new( + "test: message".to_string(), + )); + let dev_commit_history = &[dev.clone(), next.clone()]; + let fs = given::a_filesystem(); + let repo_config = given::a_repo_config(); + let (repository, repo_details, mut test_repository) = + given::an_open_repository(&fs); + let gitdir = &repo_details.gitdir; + let repo_branches = repo_config.branches(); + test_repository.on_fetch(OnFetch::new( + repo_branches.clone(), + gitdir.clone(), + fs.clone(), + |_, _, _| git::fetch::Result::Ok(()), + )); + test_repository.on_push(OnPush::new( + repo_branches.clone(), + gitdir.clone(), + fs, + |_, _, _, _, _, _, _| git::push::Result::Err(git::push::Error::Lock), + )); + let message_token = given::a_message_token(); + let_assert!( + Err(err) = actor::branch::advance_next( + &next, + dev_commit_history, + repo_details, + repo_config, + repository, + message_token, + ) + .await + ); + println!("Got: {err:?}"); + assert!(matches!( + err, + actor::branch::Error::Push(git::push::Error::Lock) + )); + Ok(()) + } + } + mod push_is_ok { + // the git push command succeeds + use git_next_git::repository::{OnFetch, OnPush}; + + // the git push command fails + use super::*; + #[tokio::test] + async fn should_ok() -> TestResult { + let next = given::a_commit(); + let dev = given::a_commit_with_message(&git::commit::Message::new( + "test: message".to_string(), + )); + let dev_commit_history = &[dev.clone(), next.clone()]; + let fs = given::a_filesystem(); + let repo_config = given::a_repo_config(); + let (repository, repo_details, mut test_repository) = + given::an_open_repository(&fs); + let gitdir = &repo_details.gitdir; + let repo_branches = repo_config.branches(); + test_repository.on_fetch(OnFetch::new( + repo_branches.clone(), + gitdir.clone(), + fs.clone(), + |_, _, _| git::fetch::Result::Ok(()), + )); + test_repository.on_push(OnPush::new( + repo_branches.clone(), + gitdir.clone(), + fs, + |_, _, _, _, _, _, _| git::push::Result::Ok(()), + )); + let message_token = given::a_message_token(); + let_assert!( + Ok(mt) = actor::branch::advance_next( + &next, + dev_commit_history, + repo_details, + repo_config, + repository, + message_token, + ) + .await + ); + println!("Got: {mt:?}"); + assert_eq!(mt, message_token); + Ok(()) + } + } + } + } + } + mod advance_main { + use git::repository::{OnFetch, OnPush}; + + use super::*; + #[tokio::test] + async fn push_is_error_should_error() { + let commit = given::a_commit(); + let fs = given::a_filesystem(); + let repo_config = given::a_repo_config(); + let (open_repository, repo_details, mut test_repository) = + given::an_open_repository(&fs); + let gitdir = &repo_details.gitdir; + let repo_branches = repo_config.branches(); + test_repository.on_fetch(OnFetch::new( + repo_branches.clone(), + gitdir.clone(), + fs.clone(), + |_, _, _| git::fetch::Result::Ok(()), + )); + test_repository.on_push(OnPush::new( + repo_branches.clone(), + gitdir.clone(), + fs, + |_, _, _, _, _, _, _| git::push::Result::Err(git::push::Error::Lock), + )); + let_assert!( + Err(err) = actor::branch::advance_main( + commit, + &repo_details, + &repo_config, + &open_repository + ) + .await + ); + assert!(matches!( + err, + actor::branch::Error::Push(git::push::Error::Lock) + )); + } + #[tokio::test] + async fn push_is_ok_should_ok() { + let commit = given::a_commit(); + let fs = given::a_filesystem(); + let repo_config = given::a_repo_config(); + let (open_repository, repo_details, mut test_repository) = + given::an_open_repository(&fs); + let gitdir = &repo_details.gitdir; + let repo_branches = repo_config.branches(); + test_repository.on_fetch(OnFetch::new( + repo_branches.clone(), + gitdir.clone(), + fs.clone(), + |_, _, _| git::fetch::Result::Ok(()), + )); + test_repository.on_push(OnPush::new( + repo_branches.clone(), + gitdir.clone(), + fs, + |_, _, _, _, _, _, _| git::push::Result::Ok(()), + )); + let_assert!( + Ok(_) = actor::branch::advance_main( + commit, + &repo_details, + &repo_config, + &open_repository + ) + .await + ); + } + } #[actix_rt::test] async fn test_find_next_commit_on_dev() { - let next = git::Commit::new( - git::commit::Sha::new("current-next".to_string()), - git::commit::Message::new("foo".to_string()), - ); - let expected = git::Commit::new( - git::commit::Sha::new("dev-next".to_string()), - git::commit::Message::new("next-should-go-here".to_string()), - ); + let next = given::a_commit(); + let expected = given::a_commit(); let dev_commit_history = vec![ - git::Commit::new( - git::commit::Sha::new("dev".to_string()), - git::commit::Message::new("future".to_string()), - ), + given::a_commit(), // dev HEAD expected.clone(), - next.clone(), - git::Commit::new( - git::commit::Sha::new("current-main".to_string()), - git::commit::Message::new("history".to_string()), - ), + next.clone(), // next - advancing towards dev HEAD + given::a_commit(), // parent of next ]; - let next_commit = find_next_commit_on_dev(next, dev_commit_history); + let next_commit = actor::branch::find_next_commit_on_dev(&next, &dev_commit_history); assert_eq!(next_commit, Some(expected), "Found the wrong commit"); } } +mod load { + use std::path::PathBuf; + + use git_next_git::common::repo_details; + + use super::*; + + #[tokio::test] + async fn when_file_not_found_should_error() -> TestResult { + //given + let fs = given::a_filesystem(); + let (open_repository, repo_details, _) = given::an_open_repository(&fs); + // create file o create commit on the branch that we then look for config file on + let file_name = given::a_pathbuf(); // not the file we want to load + let contents = given::a_name(); + let branch_name = &repo_details.branch; + let_assert!( + Ok(_) = then::commit_named_file_to_branch( + &file_name, + &contents, + &fs, + &repo_details.gitdir, + branch_name + ), + "create branch" + ); + //when + let_assert!( + Err(err) = actor::load::config_from_repository(repo_details, open_repository).await + ); + //then + println!("Got: {err:?}"); + assert!(matches!( + err, + actor::load::Error::File(git::file::Error::FileNotFound) + )); + Ok(()) + } + + #[tokio::test] + async fn when_file_format_invalid_should_error() -> TestResult { + //given + let fs = given::a_filesystem(); + let (open_repository, repo_details, _) = given::an_open_repository(&fs); + // create file to create commit on the branch that we then look for config file on + let file_name = PathBuf::from(".git-next.toml"); // the file we want to load + let contents = given::a_name(); // not a valid file content + let branch_name = &repo_details.branch; + let_assert!( + Ok(_) = then::commit_named_file_to_branch( + &file_name, + &contents, + &fs, + &repo_details.gitdir, + branch_name + ), + "create branch" + ); + //when + let_assert!( + Err(err) = actor::load::config_from_repository(repo_details, open_repository).await + ); + //then + println!("Got: {err:?}"); + assert!(matches!(err, actor::load::Error::Toml(_))); + Ok(()) + } + + #[tokio::test] + async fn when_main_branch_is_missing_should_error() -> TestResult { + //given + let fs = given::a_filesystem(); + let (open_repository, repo_details, _) = given::an_open_repository(&fs); + let gitdir = &repo_details.gitdir; + // create file to create commit on the branch that we then look for config file on + let file_name = PathBuf::from(".git-next.toml"); // the file we want to load + let branches = given::repo_branches(); + let main = branches.main(); + let next = branches.next(); + let dev = branches.dev(); + let contents = format!( + r#" + [branches] + main = "{main}" + next = "{next}" + dev = "{dev}" + "# + ); + let branch_name = &repo_details.branch; + let_assert!( + Ok(_) = + then::commit_named_file_to_branch(&file_name, &contents, &fs, gitdir, branch_name), + "create branch" + ); + //when + let_assert!( + Err(err) = actor::load::config_from_repository(repo_details, open_repository).await + ); + //then + println!("Got: {err:?}"); + assert!(matches!(err, actor::load::Error::BranchNotFound(branch) if branch == main)); + Ok(()) + } + + #[tokio::test] + async fn when_next_branch_is_missing_should_error() -> TestResult { + //given + let fs = given::a_filesystem(); + let (open_repository, repo_details, _) = given::an_open_repository(&fs); + // create file to create commit on the branch that we then look for config file on + let file_name = PathBuf::from(".git-next.toml"); // the file we want to load + let branches = given::repo_branches(); + let main = branches.main(); + let next = branches.next(); + let dev = branches.dev(); + let contents = format!( + r#" + [branches] + main = "{main}" + next = "{next}" + dev = "{dev}" + "# + ); + let branch_name = &repo_details.branch; + let_assert!( + Ok(_) = then::commit_named_file_to_branch( + &file_name, + &contents, + &fs, + &repo_details.gitdir, + branch_name + ), + "create branch" + ); + // create main branch + let_assert!( + Ok(_) = then::create_a_commit_on_branch(&fs, &repo_details.gitdir, &main), + "create main branch" + ); + //when + let_assert!( + Err(err) = actor::load::config_from_repository(repo_details, open_repository).await + ); + //then + println!("Got: {err:?}"); + assert!(matches!(err, actor::load::Error::BranchNotFound(branch) if branch == next)); + Ok(()) + } + + #[tokio::test] + async fn when_dev_branch_is_missing_should_error() -> TestResult { + //given + let fs = given::a_filesystem(); + let (open_repository, repo_details, _) = given::an_open_repository(&fs); + let gitdir = &repo_details.gitdir; + // create file to create commit on the branch that we then look for config file on + let file_name = PathBuf::from(".git-next.toml"); // the file we want to load + let branches = given::repo_branches(); + let main = branches.main(); + let next = branches.next(); + let dev = branches.dev(); + let contents = format!( + r#" + [branches] + main = "{main}" + next = "{next}" + dev = "{dev}" + "# + ); + let branch_name = &repo_details.branch; + let_assert!( + Ok(_) = + then::commit_named_file_to_branch(&file_name, &contents, &fs, gitdir, branch_name), + "create branch" + ); + // create main branch + let_assert!( + Ok(_) = then::create_a_commit_on_branch(&fs, gitdir, &main), + "create main branch" + ); + // create next branch + let_assert!( + Ok(_) = then::create_a_commit_on_branch(&fs, gitdir, &next), + "create next branch" + ); + //when + let_assert!( + Err(err) = actor::load::config_from_repository(repo_details, open_repository).await + ); + //then + println!("Got: {err:?}"); + assert!(matches!(err, actor::load::Error::BranchNotFound(branch) if branch == dev)); + Ok(()) + } + + #[tokio::test] + async fn when_valid_file_should_return_repo_config() -> TestResult { + //given + let fs = given::a_filesystem(); + let (open_repository, repo_details, _) = given::an_open_repository(&fs); + let gitdir = &repo_details.gitdir; + // create file to create commit on the branch that we then look for config file on + let file_name = PathBuf::from(".git-next.toml"); // the file we want to load + let repo_config = given::a_repo_config(); + let branches = repo_config.branches(); + let main = branches.main(); + let next = branches.next(); + let dev = branches.dev(); + let contents = format!( + r#" + [branches] + main = "{main}" + next = "{next}" + dev = "{dev}" + "# + ); + let branch_name = &repo_details.branch; + let_assert!( + Ok(_) = + then::commit_named_file_to_branch(&file_name, &contents, &fs, gitdir, branch_name), + "create branch" + ); + // create main branch + let_assert!( + Ok(_) = then::create_a_commit_on_branch(&fs, gitdir, &main), + "create main branch" + ); + // create next branch + let_assert!( + Ok(_) = then::create_a_commit_on_branch(&fs, gitdir, &next), + "create next branch" + ); + // create dev branch + let_assert!( + Ok(_) = then::create_a_commit_on_branch(&fs, gitdir, &dev), + "create dev branch" + ); + //when + let_assert!( + Ok(result) = actor::load::config_from_repository(repo_details, open_repository).await + ); + //then + println!("Got: {result:?}"); + assert_eq!(result, repo_config); + Ok(()) + } +} +pub mod given { + #![allow(dead_code)] // TODO: remove this + #![allow(unused_imports)] // TODO: remove this + + use git_next_git::repository::RepositoryLike as _; + use rand::RngCore; + use std::path::PathBuf; + + use super::*; + use config::{ + server::{Webhook, WebhookUrl}, + BranchName, ForgeAlias, ForgeConfig, ForgeType, GitDir, RepoAlias, RepoBranches, + RepoConfig, ServerRepoConfig, WebhookAuth, WebhookId, + }; + use git::{Generation, RepoDetails}; + + pub fn a_webhook_auth() -> WebhookAuth { + WebhookAuth::generate() + } + + pub enum Header { + Valid(WebhookAuth, config::webhook::message::Body), + Missing, + Invalid, + } + + pub fn a_webhook_message_body() -> config::webhook::message::Body { + config::webhook::message::Body::new(a_name()) + } + + pub fn repo_branches() -> RepoBranches { + RepoBranches::new( + format!("main-{}", a_name()), + format!("next-{}", a_name()), + format!("dev-{}", a_name()), + ) + } + + pub fn a_forge_alias() -> ForgeAlias { + ForgeAlias::new(a_name()) + } + + pub fn a_repo_alias() -> RepoAlias { + RepoAlias::new(a_name()) + } + + pub fn a_network() -> kxio::network::MockNetwork { + kxio::network::MockNetwork::new() + } + + pub fn a_webhook_url( + forge_alias: &ForgeAlias, + repo_alias: &RepoAlias, + ) -> git_next_config::server::WebhookUrl { + config::server::Webhook::new(a_name()).url(forge_alias, repo_alias) + } + + pub fn any_webhook_url() -> git_next_config::server::WebhookUrl { + a_webhook_url(&a_forge_alias(), &a_repo_alias()) + } + + pub fn a_pathbuf() -> PathBuf { + PathBuf::from(given::a_name()) + } + + pub fn a_name() -> String { + use rand::Rng; + use std::iter; + + fn generate(len: usize) -> String { + const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + let mut rng = rand::thread_rng(); + let one_char = || CHARSET[rng.gen_range(0..CHARSET.len())] as char; + iter::repeat_with(one_char).take(len).collect() + } + generate(5) + } + + pub fn a_webhook_id() -> WebhookId { + WebhookId::new(a_name()) + } + + pub fn a_github_webhook_id() -> i64 { + rand::thread_rng().next_u32().into() + } + + pub fn a_branch_name() -> BranchName { + BranchName::new(a_name()) + } + + pub fn a_git_dir(fs: &kxio::fs::FileSystem) -> GitDir { + let dir_name = a_name(); + let dir = fs.base().join(dir_name); + GitDir::new(&dir) + } + + pub fn a_forge_config() -> ForgeConfig { + ForgeConfig::new( + ForgeType::MockForge, + a_name(), + a_name(), + a_name(), + Default::default(), // no repos + ) + } + + pub fn a_server_repo_config() -> ServerRepoConfig { + let main = a_branch_name().into_string(); + let next = a_branch_name().into_string(); + let dev = a_branch_name().into_string(); + ServerRepoConfig::new( + format!("{}/{}", a_name(), a_name()), + main.clone(), + None, + Some(main), + Some(next), + Some(dev), + ) + } + + pub fn a_repo_config() -> RepoConfig { + RepoConfig::new(given::repo_branches(), config::RepoConfigSource::Repo) + } + + pub fn a_commit() -> git::Commit { + git::Commit::new(a_commit_sha(), a_commit_message()) + } + + pub fn a_commit_with_message(message: &git::commit::Message) -> git::Commit { + git::Commit::new(a_commit_sha(), message.to_owned()) + } + + pub fn a_commit_with_sha(sha: &git::commit::Sha) -> git::Commit { + git::Commit::new(sha.to_owned(), a_commit_message()) + } + + pub fn a_commit_with(sha: &git::commit::Sha, message: &git::commit::Message) -> git::Commit { + git::Commit::new(sha.to_owned(), message.to_owned()) + } + + pub fn a_commit_message() -> git::commit::Message { + git::commit::Message::new(a_name()) + } + + pub fn a_commit_sha() -> git::commit::Sha { + git::commit::Sha::new(a_name()) + } + + pub fn a_webhook_push( + sha: &git::commit::Sha, + message: &git::commit::Message, + ) -> config::webhook::Push { + let branch = a_branch_name(); + config::webhook::Push::new(branch, sha.to_string(), message.to_string()) + } + + pub fn a_filesystem() -> kxio::fs::FileSystem { + kxio::fs::temp().unwrap_or_else(|e| panic!("{}", e)) + } + + pub fn repo_details(fs: &kxio::fs::FileSystem) -> git::RepoDetails { + let generation = git::Generation::new(); + let repo_alias = a_repo_alias(); + let server_repo_config = a_server_repo_config(); + let forge_alias = a_forge_alias(); + let forge_config = a_forge_config(); + let gitdir = a_git_dir(fs); + RepoDetails::new( + generation, + &repo_alias, + &server_repo_config, + &forge_alias, + &forge_config, + gitdir, + ) + } + + pub fn an_open_repository( + fs: &kxio::fs::FileSystem, + ) -> ( + git::repository::OpenRepository, + git::RepoDetails, + git::repository::test::TestRepository, + ) { + #![allow(clippy::expect_used)] + let gitdir = a_git_dir(fs); + let hostname = given::a_hostname(); + let repo_details = given::repo_details(fs) + .with_gitdir(gitdir.clone()) + .with_hostname(hostname.clone()); + let repo = git::repository::test(fs.clone(), hostname); + let _ = repo.init(&gitdir).expect("init new repo"); // discard the real repo + let or = repo.open(&gitdir).expect("open repo"); // re-open as a test repo + (or, repo_details, repo) + } + + pub fn a_message_token() -> messages::MessageToken { + messages::MessageToken::default() + } + + pub fn a_webhook(url: &WebhookUrl) -> Webhook { + Webhook::new(url.clone().into()) + } + + pub fn a_repo_actor( + repo_details: git::RepoDetails, + repository: git::Repository, + net: kxio::network::Network, + ) -> actor::RepoActor { + let forge_alias = repo_details.forge.forge_alias(); + let repo_alias = &repo_details.repo_alias; + let webhook_url = given::a_webhook_url(forge_alias, repo_alias); + let webhook = given::a_webhook(&webhook_url); + let generation = Generation::new(); + actor::RepoActor::new( + repo_details, + webhook, + generation, + net, + repository, + std::time::Duration::from_nanos(1), + ) + } + + pub fn a_git_remote(repo_details: &git::RepoDetails) -> git::GitRemote { + let hostname = repo_details.forge.hostname().clone(); + let repo_path = repo_details.repo_path.clone(); + git::GitRemote::new(hostname, repo_path) + } + + pub fn a_hostname() -> config::Hostname { + config::Hostname::new(given::a_name()) + } + + pub fn a_repo_path() -> config::RepoPath { + config::RepoPath::new(given::a_name()) + } +} +pub mod then { + #![allow(dead_code)] // TODO: remove this + #![allow(unused_imports)] // TODO: remove this + + use std::path::{Path, PathBuf}; + + type TestResult = Result<(), Box>; + + use super::*; + + pub fn commit_named_file_to_branch( + file_name: &Path, + contents: &str, + fs: &kxio::fs::FileSystem, + gitdir: &config::GitDir, + branch_name: &config::BranchName, + ) -> TestResult { + // git checkout ${branch_name} + git_checkout_new_branch(branch_name, gitdir)?; + // echo ${word} > file-${word} + let pathbuf = PathBuf::from(gitdir); + let file = fs.base().join(pathbuf).join(file_name); + #[allow(clippy::expect_used)] + fs.file_write(&file, contents)?; + // git add ${file} + git_add_file(gitdir, &file)?; + // git commit -m"Added ${file}" + git_commit(gitdir, &file)?; + + then::push_branch(fs, gitdir, branch_name)?; + + Ok(()) + } + + pub fn create_a_commit_on_branch( + fs: &kxio::fs::FileSystem, + gitdir: &config::GitDir, + branch_name: &config::BranchName, + ) -> TestResult { + // git checkout ${branch_name} + git_checkout_new_branch(branch_name, gitdir)?; + // echo ${word} > file-${word} + let word = given::a_name(); + let pathbuf = PathBuf::from(gitdir); + let file = fs.base().join(pathbuf).join(&word); + fs.file_write(&file, &word)?; + // git add ${file} + git_add_file(gitdir, &file)?; + // git commit -m"Added ${file}" + git_commit(gitdir, &file)?; + + then::push_branch(fs, gitdir, branch_name)?; + + Ok(()) + } + + fn push_branch( + fs: &kxio::fs::FileSystem, + gitdir: &config::GitDir, + branch_name: &config::BranchName, + ) -> TestResult { + let gitrefs = fs + .base() + .join(gitdir.to_path_buf()) + .join(".git") + .join("refs"); + let local_branch = gitrefs.join("heads").join(branch_name.to_string().as_str()); + let origin_heads = gitrefs.join("remotes").join("origin"); + let remote_branch = origin_heads.join(branch_name.to_string().as_str()); + let contents = fs.file_read_to_string(&local_branch)?; + fs.dir_create_all(&origin_heads)?; + fs.file_write(&remote_branch, &contents)?; + Ok(()) + } + + pub fn git_checkout_new_branch( + branch_name: &git_next_config::BranchName, + gitdir: &git_next_config::GitDir, + ) -> TestResult { + exec( + format!("git checkout -b {}", branch_name), + std::process::Command::new("/usr/bin/git") + .current_dir(gitdir.to_path_buf()) + .args(["checkout", "-b", branch_name.to_string().as_str()]) + .output(), + )?; + Ok(()) + } + + pub fn git_switch( + branch_name: &git_next_config::BranchName, + gitdir: &git_next_config::GitDir, + ) -> TestResult { + exec( + format!("git switch {}", branch_name), + std::process::Command::new("/usr/bin/git") + .current_dir(gitdir.to_path_buf()) + .args(["switch", branch_name.to_string().as_str()]) + .output(), + ) + } + + fn exec(label: String, output: Result) -> TestResult { + println!("== {label}"); + match output { + Ok(output) => { + println!( + "\nstdout:\n{}", + String::from_utf8_lossy(output.stdout.as_slice()) + ); + println!( + "\nstderr:\n{}", + String::from_utf8_lossy(output.stderr.as_slice()) + ); + println!("============================="); + Ok(()) + } + Err(err) => { + println!("ERROR: {err:#?}"); + Ok(Err(err)?) + } + } + } + + fn git_add_file(gitdir: &git_next_config::GitDir, file: &Path) -> TestResult { + exec( + format!("git add {file:?}"), + std::process::Command::new("/usr/bin/git") + .current_dir(gitdir.to_path_buf()) + .args(["add", file.display().to_string().as_str()]) + .output(), + ) + } + + fn git_commit(gitdir: &git_next_config::GitDir, file: &Path) -> TestResult { + exec( + format!(r#"git commit -m"Added {file:?}""#), + std::process::Command::new("/usr/bin/git") + .current_dir(gitdir.to_path_buf()) + .args([ + "commit", + format!(r#"-m"Added {}"#, file.display().to_string().as_str()).as_str(), + ]) + .output(), + ) + } + + pub fn git_log_all(gitdir: &config::GitDir) -> TestResult { + exec( + "git log --all --oneline --decorate --graph".to_string(), + std::process::Command::new("/usr/bin/git") + .current_dir(gitdir.to_path_buf()) + .args(["log", "--all", "--oneline", "--decorate", "--graph"]) + .output(), + ) + } + + pub fn get_sha_for_branch( + fs: &kxio::fs::FileSystem, + gitdir: &git_next_config::GitDir, + branch_name: &git_next_config::BranchName, + ) -> Result> { + let main_ref = fs + .base() + .join(gitdir.to_path_buf()) + .join(".git") + .join("refs") + .join("heads") + .join(branch_name.to_string().as_str()); + let sha = fs.file_read_to_string(&main_ref)?; + Ok(git::commit::Sha::new(sha.trim().to_string())) + } +} diff --git a/crates/server/src/actors/server.rs b/crates/server/src/actors/server.rs index 68df2b9..c44f59a 100644 --- a/crates/server/src/actors/server.rs +++ b/crates/server/src/actors/server.rs @@ -8,7 +8,7 @@ use git_next_config::{ self as config, ForgeAlias, ForgeConfig, GitDir, RepoAlias, ServerRepoConfig, }; use git_next_git::{Generation, RepoDetails, Repository}; -use git_next_repo_actor::{CloneRepo, RepoActor}; +use git_next_repo_actor as repo_actor; use kxio::{fs::FileSystem, network::Network}; use tracing::{error, info, warn}; @@ -39,6 +39,7 @@ pub struct Server { fs: FileSystem, net: Network, repo: Repository, + sleep_duration: std::time::Duration, } impl Actor for Server { type Context = Context; @@ -100,13 +101,19 @@ impl Handler for Server { // Forge Actors for (forge_alias, forge_config) in server_config.forges() { - self.create_forge_repos(forge_config, forge_alias.clone(), server_storage, webhook) - .into_iter() - .map(|a| self.start_actor(a)) - .map(|(repo_alias, addr)| { - AddWebhookRecipient::new(forge_alias.clone(), repo_alias, addr.recipient()) - }) - .for_each(|msg| webhook_router.do_send(msg)); + self.create_forge_repos( + forge_config, + forge_alias.clone(), + server_storage, + webhook, + self.sleep_duration, + ) + .into_iter() + .map(|a| self.start_actor(a)) + .map(|(repo_alias, addr)| { + AddWebhookRecipient::new(forge_alias.clone(), repo_alias, addr.recipient()) + }) + .for_each(|msg| webhook_router.do_send(msg)); } let webhook = WebhookActor::new(socket_addr, webhook_router.recipient()).start(); @@ -114,7 +121,12 @@ impl Handler for Server { } } impl Server { - pub fn new(fs: FileSystem, net: Network, repo: Repository) -> Self { + pub fn new( + fs: FileSystem, + net: Network, + repo: Repository, + sleep_duration: std::time::Duration, + ) -> Self { let generation = Generation::new(); Self { generation, @@ -122,6 +134,7 @@ impl Server { fs, net, repo, + sleep_duration, } } fn create_forge_data_directories( @@ -151,14 +164,21 @@ impl Server { forge_name: ForgeAlias, server_storage: &ServerStorage, webhook: &Webhook, - ) -> Vec<(ForgeAlias, RepoAlias, RepoActor)> { + sleep_duration: std::time::Duration, + ) -> Vec<(ForgeAlias, RepoAlias, repo_actor::RepoActor)> { let span = tracing::info_span!("create_forge_repos", name = %forge_name, config = %forge_config); let _guard = span.enter(); info!("Creating Forge"); let mut repos = vec![]; - let creator = self.create_actor(forge_name, forge_config.clone(), server_storage, webhook); + let creator = self.create_actor( + forge_name, + forge_config.clone(), + server_storage, + webhook, + sleep_duration, + ); for (repo_alias, server_repo_config) in forge_config.repos() { let forge_repo = creator((repo_alias, server_repo_config)); info!( @@ -176,7 +196,9 @@ impl Server { forge_config: ForgeConfig, server_storage: &ServerStorage, webhook: &Webhook, - ) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeAlias, RepoAlias, RepoActor) { + sleep_duration: std::time::Duration, + ) -> impl Fn((RepoAlias, &ServerRepoConfig)) -> (ForgeAlias, RepoAlias, repo_actor::RepoActor) + { let server_storage = server_storage.clone(); let webhook = webhook.clone(); let net = self.net.clone(); @@ -208,12 +230,13 @@ impl Server { gitdir, ); info!("Starting Repo Actor"); - let actor = RepoActor::new( + let actor = repo_actor::RepoActor::new( repo_details, webhook.clone(), generation, net.clone(), repo.clone(), + sleep_duration, ); (forge_name.clone(), repo_alias, actor) } @@ -221,13 +244,13 @@ impl Server { fn start_actor( &self, - actor: (ForgeAlias, RepoAlias, RepoActor), - ) -> (RepoAlias, Addr) { + actor: (ForgeAlias, RepoAlias, repo_actor::RepoActor), + ) -> (RepoAlias, Addr) { let (forge_name, repo_alias, actor) = actor; let span = tracing::info_span!("start_actor", forge = %forge_name, repo = %repo_alias); let _guard = span.enter(); let addr = actor.start(); - addr.do_send(CloneRepo); + addr.do_send(repo_actor::messages::CloneRepo); info!("Started"); (repo_alias, addr) } diff --git a/crates/server/src/config/tests.rs b/crates/server/src/config/tests.rs index f0b0076..6f829c3 100644 --- a/crates/server/src/config/tests.rs +++ b/crates/server/src/config/tests.rs @@ -18,7 +18,7 @@ fn test_repo_config_load() -> Result<()> { [options] "#; - let config = RepoConfig::load(toml)?; + let config = RepoConfig::parse(toml)?; assert_eq!( config, diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index e264869..0a386cd 100644 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -1,19 +1,18 @@ +// mod actors; mod config; -// + use actix::prelude::*; -use git_next_git::Repository; use kxio::{fs::FileSystem, network::Network}; - use std::path::PathBuf; - use tracing::{error, info, level_filters::LevelFilter}; use crate::actors::{ file_watcher::{self, FileUpdated}, server::Server, }; +use git_next_git::Repository; pub fn init(fs: FileSystem) { let file_name = "git-next-server.toml"; @@ -37,11 +36,16 @@ pub fn init(fs: FileSystem) { } } -pub async fn start(fs: FileSystem, net: Network, repo: Repository) { +pub async fn start( + fs: FileSystem, + net: Network, + repo: Repository, + sleep_duration: std::time::Duration, +) { init_logging(); info!("Starting Server..."); - let server = Server::new(fs.clone(), net.clone(), repo).start(); + let server = Server::new(fs.clone(), net.clone(), repo, sleep_duration).start(); server.do_send(FileUpdated); info!("Starting File Watcher..."); @@ -58,6 +62,7 @@ pub async fn start(fs: FileSystem, net: Network, repo: Repository) { info!("Server running - Press Ctrl-C to stop..."); let _ = actix_rt::signal::ctrl_c().await; info!("Ctrl-C received, shutting down..."); + // TODO: (#94) perform a controlled shutdown of server and file watcher } pub fn init_logging() {