diff --git a/ceres/src/api_service/import_api_service.rs b/ceres/src/api_service/import_api_service.rs index 805c3949c..82cb450fd 100644 --- a/ceres/src/api_service/import_api_service.rs +++ b/ceres/src/api_service/import_api_service.rs @@ -49,21 +49,6 @@ impl ApiHandler for ImportApiService { } } - async fn get_root_commit(&self) -> Commit { - let storage = self.storage.git_db_storage(); - let refs = storage - .get_default_ref(self.repo.repo_id) - .await - .unwrap() - .unwrap(); - storage - .get_commit_by_hash(self.repo.repo_id, &refs.ref_git_id) - .await - .unwrap() - .unwrap() - .into() - } - async fn get_root_tree(&self) -> Tree { let storage = self.storage.git_db_storage(); let refs = storage @@ -301,4 +286,19 @@ impl ImportApiService { } Ok(false) } + + async fn get_root_commit(&self) -> Commit { + let storage = self.storage.git_db_storage(); + let refs = storage + .get_default_ref(self.repo.repo_id) + .await + .unwrap() + .unwrap(); + storage + .get_commit_by_hash(self.repo.repo_id, &refs.ref_git_id) + .await + .unwrap() + .unwrap() + .into() + } } diff --git a/ceres/src/api_service/mod.rs b/ceres/src/api_service/mod.rs index e86320cc5..58b0238b3 100644 --- a/ceres/src/api_service/mod.rs +++ b/ceres/src/api_service/mod.rs @@ -52,8 +52,6 @@ pub trait ApiHandler: Send + Sync { fn strip_relative(&self, path: &Path) -> Result; - async fn get_root_commit(&self) -> Commit; - async fn get_root_tree(&self) -> Tree; async fn get_binary_tree_by_path( diff --git a/ceres/src/api_service/mono_api_service.rs b/ceres/src/api_service/mono_api_service.rs index 5a6d30383..dfb5cc8ee 100644 --- a/ceres/src/api_service/mono_api_service.rs +++ b/ceres/src/api_service/mono_api_service.rs @@ -159,10 +159,6 @@ impl ApiHandler for MonoApiService { Ok(path.to_path_buf()) } - async fn get_root_commit(&self) -> Commit { - unreachable!() - } - async fn get_root_tree(&self) -> Tree { let storage = self.storage.mono_storage(); let refs = storage.get_ref("/").await.unwrap().unwrap(); diff --git a/ceres/src/pack/import_repo.rs b/ceres/src/pack/import_repo.rs index 351f4e5db..c2880f87d 100644 --- a/ceres/src/pack/import_repo.rs +++ b/ceres/src/pack/import_repo.rs @@ -39,6 +39,11 @@ pub struct ImportRepo { #[async_trait] impl RepoHandler for ImportRepo { + + fn is_monorepo(&self) -> bool { + false + } + async fn head_hash(&self) -> (String, Vec) { let result = self .storage @@ -56,10 +61,6 @@ impl RepoHandler for ImportRepo { storage.save_entry(self.repo.repo_id, entry_list).await } - async fn post_receiver_handler(&self) -> Result<(), GitError> { - self.attach_to_monorepo_parent().await - } - async fn check_entry(&self, _: &Entry) -> Result<(), GitError> { Ok(()) } @@ -292,14 +293,6 @@ impl RepoHandler for ImportRepo { Ok(()) } - async fn save_or_update_mr(&self) -> Result<(), MegaError> { - Ok(()) - } - - async fn post_mr_operation(&self) -> Result<(), MegaError> { - Ok(()) - } - async fn check_commit_exist(&self, hash: &str) -> bool { self.storage .git_db_storage() @@ -320,7 +313,7 @@ impl RepoHandler for ImportRepo { impl ImportRepo { // attach import repo to monorepo parent tree - async fn attach_to_monorepo_parent(&self) -> Result<(), GitError> { + pub(crate) async fn attach_to_monorepo_parent(&self) -> Result<(), GitError> { let iter = self .command_list .clone() diff --git a/ceres/src/pack/mod.rs b/ceres/src/pack/mod.rs index 7c9a3f1a6..813ec5c19 100644 --- a/ceres/src/pack/mod.rs +++ b/ceres/src/pack/mod.rs @@ -1,4 +1,5 @@ use std::{ + any::Any, collections::HashSet, pin::Pin, sync::{ @@ -14,7 +15,10 @@ use sysinfo::System; use tokio::sync::{mpsc::UnboundedReceiver, Semaphore}; use tokio_stream::wrappers::ReceiverStream; -use crate::protocol::import_refs::{RefCommand, Refs}; +use crate::{ + pack::import_repo::ImportRepo, + protocol::import_refs::{RefCommand, Refs}, +}; use callisto::raw_blob; use common::{ config::PackConfig, @@ -37,7 +41,9 @@ pub mod import_repo; pub mod monorepo; #[async_trait] -pub trait RepoHandler: Send + Sync + 'static { +pub trait RepoHandler: Any + Send + Sync + 'static { + fn is_monorepo(&self) -> bool; + async fn head_hash(&self) -> (String, Vec); async fn receiver_handler( @@ -82,13 +88,16 @@ pub trait RepoHandler: Send + Sync + 'static { } } } - self.post_receiver_handler().await + if !self.is_monorepo() { + if let Some(import_repo) = (&self as &dyn Any).downcast_ref::() { + return import_repo.attach_to_monorepo_parent().await; + } + } + Ok(()) } async fn save_entry(&self, entry_list: Vec) -> Result<(), MegaError>; - async fn post_receiver_handler(&self) -> Result<(), GitError>; - async fn check_entry(&self, entry: &Entry) -> Result<(), GitError>; /// Asynchronously retrieves the full pack data for the specified repository path. @@ -116,10 +125,6 @@ pub trait RepoHandler: Send + Sync + 'static { async fn update_refs(&self, refs: &RefCommand) -> Result<(), GitError>; - async fn save_or_update_mr(&self) -> Result<(), MegaError>; - - async fn post_mr_operation(&self) -> Result<(), MegaError>; - async fn check_commit_exist(&self, hash: &str) -> bool; async fn check_default_branch(&self) -> bool; @@ -245,3 +250,13 @@ pub trait RepoHandler: Send + Sync + 'static { } } } + +impl dyn RepoHandler { + pub fn as_any(&self) -> &dyn Any { + self + } + + pub fn into_any(self: Arc) -> Arc { + self + } +} diff --git a/ceres/src/pack/monorepo.rs b/ceres/src/pack/monorepo.rs index 5648acdcd..dc54f5e84 100644 --- a/ceres/src/pack/monorepo.rs +++ b/ceres/src/pack/monorepo.rs @@ -56,6 +56,10 @@ pub struct MonoRepo { #[async_trait] impl RepoHandler for MonoRepo { + fn is_monorepo(&self) -> bool { + true + } + async fn head_hash(&self) -> (String, Vec) { let storage = self.storage.mono_storage(); @@ -148,10 +152,6 @@ impl RepoHandler for MonoRepo { storage.save_entry(&commit_id, entry_list).await } - async fn post_receiver_handler(&self) -> Result<(), GitError> { - Ok(()) - } - async fn check_entry(&self, entry: &Entry) -> Result<(), GitError> { if self.current_commit.read().await.is_none() { if entry.obj_type == ObjectType::Commit { @@ -310,78 +310,6 @@ impl RepoHandler for MonoRepo { Ok(()) } - async fn save_or_update_mr(&self) -> Result<(), MegaError> { - let storage = self.storage.mr_storage(); - let path_str = self.path.to_str().unwrap(); - let username = self.username(); - - match storage.get_open_mr_by_path(path_str, &username).await? { - Some(mr) => { - self.update_existing_mr(mr).await?; - } - None => { - let link_guard = self.mr_link.read().await; - let mr_link = link_guard.as_ref().unwrap(); - let commit_guard = self.current_commit.read().await; - let title = if let Some(commit) = commit_guard.as_ref() { - commit.format_message() - } else { - String::new() - }; - storage - .new_mr( - path_str, - mr_link, - &title, - &self.from_hash, - &self.to_hash, - &username, - ) - .await?; - } - }; - Ok(()) - } - - async fn post_mr_operation(&self) -> Result<(), MegaError> { - let link_guard = self.mr_link.read().await; - let link = link_guard.as_ref().unwrap(); - - if self.bellatrix.enable_build() { - let buck_files = self.search_buck_under_mr(&self.path).await?; - if buck_files.is_empty() { - tracing::error!( - "Search BUCK file under {:?} failed, please manually check BUCK file exists!!", - self.path - ); - } else { - for buck_file in buck_files { - let req = OrionBuildRequest { - repo: buck_file.path.to_str().unwrap().to_string(), - buck_hash: buck_file.buck.to_string(), - buckconfig_hash: buck_file.buck_config.to_string(), - mr: link.to_string(), - args: Some(vec![]), - }; - let bellatrix = self.bellatrix.clone(); - tokio::spawn(async move { - let _ = bellatrix.on_post_receive(req).await; - }); - } - } - } - let mr_info = self - .storage - .mr_storage() - .get_mr(link) - .await? - .expect("MR Not Found"); - - let check_reg = CheckerRegistry::new(self.storage.clone().into()); - check_reg.run_checks(mr_info.into()).await?; - Ok(()) - } - async fn check_commit_exist(&self, hash: &str) -> bool { self.storage .mono_storage() @@ -543,6 +471,78 @@ impl MonoRepo { pub fn username(&self) -> String { self.username.clone().unwrap_or(String::from("Admin")) } + + pub async fn save_or_update_mr(&self) -> Result<(), MegaError> { + let storage = self.storage.mr_storage(); + let path_str = self.path.to_str().unwrap(); + let username = self.username(); + + match storage.get_open_mr_by_path(path_str, &username).await? { + Some(mr) => { + self.update_existing_mr(mr).await?; + } + None => { + let link_guard = self.mr_link.read().await; + let mr_link = link_guard.as_ref().unwrap(); + let commit_guard = self.current_commit.read().await; + let title = if let Some(commit) = commit_guard.as_ref() { + commit.format_message() + } else { + String::new() + }; + storage + .new_mr( + path_str, + mr_link, + &title, + &self.from_hash, + &self.to_hash, + &username, + ) + .await?; + } + }; + Ok(()) + } + + pub async fn post_mr_operation(&self) -> Result<(), MegaError> { + let link_guard = self.mr_link.read().await; + let link = link_guard.as_ref().unwrap(); + + if self.bellatrix.enable_build() { + let buck_files = self.search_buck_under_mr(&self.path).await?; + if buck_files.is_empty() { + tracing::error!( + "Search BUCK file under {:?} failed, please manually check BUCK file exists!!", + self.path + ); + } else { + for buck_file in buck_files { + let req = OrionBuildRequest { + repo: buck_file.path.to_str().unwrap().to_string(), + buck_hash: buck_file.buck.to_string(), + buckconfig_hash: buck_file.buck_config.to_string(), + mr: link.to_string(), + args: Some(vec![]), + }; + let bellatrix = self.bellatrix.clone(); + tokio::spawn(async move { + let _ = bellatrix.on_post_receive(req).await; + }); + } + } + } + let mr_info = self + .storage + .mr_storage() + .get_mr(link) + .await? + .expect("MR Not Found"); + + let check_reg = CheckerRegistry::new(self.storage.clone().into()); + check_reg.run_checks(mr_info.into()).await?; + Ok(()) + } } type DiffResult = Vec<(PathBuf, Option, Option)>; diff --git a/ceres/src/protocol/smart.rs b/ceres/src/protocol/smart.rs index 2e3e2a332..c1c47777f 100644 --- a/ceres/src/protocol/smart.rs +++ b/ceres/src/protocol/smart.rs @@ -8,6 +8,7 @@ use tokio_stream::wrappers::ReceiverStream; use callisto::sea_orm_active_enums::RefTypeEnum; use common::errors::ProtocolError; +use crate::pack::monorepo::MonoRepo; use crate::protocol::import_refs::RefCommand; use crate::protocol::ZERO_ID; use crate::protocol::{Capability, ServiceType, SideBind, SmartProtocol, TransportProtocol}; @@ -250,10 +251,15 @@ impl SmartProtocol { } add_pkt_line_string(&mut report_status, command.get_status()); } - //3. update MR - repo_handler.save_or_update_mr().await.unwrap(); - //4. post operation - repo_handler.post_mr_operation().await.unwrap(); + if repo_handler.is_monorepo() { + let any = repo_handler.into_any(); + if let Ok(monorepo) = any.downcast::() { + //3.1 update MR + monorepo.save_or_update_mr().await.unwrap(); + //3.2 post operation + monorepo.post_mr_operation().await.unwrap(); + } + } report_status.put(&PKT_LINE_END_MARKER[..]); let length = report_status.len(); diff --git a/jupiter/src/storage/mr_storage.rs b/jupiter/src/storage/mr_storage.rs index 0875ede8b..1e65206e7 100644 --- a/jupiter/src/storage/mr_storage.rs +++ b/jupiter/src/storage/mr_storage.rs @@ -265,10 +265,11 @@ impl MrStorage { pub async fn get_checks_config_by_path( &self, - path: &str, + _: &str, ) -> Result, MegaError> { let models = path_check_configs::Entity::find() - .filter(path_check_configs::Column::Path.eq(path)) + // .filter(path_check_configs::Column::Path.eq(path)) + .filter(path_check_configs::Column::Enabled.eq(true)) .all(self.get_connection()) .await?; Ok(models)