Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions ceres/src/api_service/import_api_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
2 changes: 0 additions & 2 deletions ceres/src/api_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ pub trait ApiHandler: Send + Sync {

fn strip_relative(&self, path: &Path) -> Result<PathBuf, GitError>;

async fn get_root_commit(&self) -> Commit;

async fn get_root_tree(&self) -> Tree;

async fn get_binary_tree_by_path(
Expand Down
4 changes: 0 additions & 4 deletions ceres/src/api_service/mono_api_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 6 additions & 13 deletions ceres/src/pack/import_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Refs>) {
let result = self
.storage
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
33 changes: 24 additions & 9 deletions ceres/src/pack/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
any::Any,
collections::HashSet,
pin::Pin,
sync::{
Expand All @@ -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,
Expand All @@ -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<Refs>);

async fn receiver_handler(
Expand Down Expand Up @@ -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::<ImportRepo>() {
return import_repo.attach_to_monorepo_parent().await;
}
}
Ok(())
}

async fn save_entry(&self, entry_list: Vec<Entry>) -> 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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self>) -> Arc<dyn Any + Send + Sync> {
self
}
}
152 changes: 76 additions & 76 deletions ceres/src/pack/monorepo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Refs>) {
let storage = self.storage.mono_storage();

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<SHA1>, Option<SHA1>)>;
Expand Down
14 changes: 10 additions & 4 deletions ceres/src/protocol/smart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::<MonoRepo>() {
//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();
Expand Down
Loading
Loading