-
Notifications
You must be signed in to change notification settings - Fork 122
feat(mono): added merge checker #1367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| pub mod api_service; | ||
| pub mod lfs; | ||
| pub mod merge_checker; | ||
| pub mod model; | ||
| pub mod pack; | ||
| pub mod protocol; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| use std::{collections::HashMap, sync::Arc}; | ||
|
|
||
| use async_trait::async_trait; | ||
| use serde::Serialize; | ||
| use utoipa::ToSchema; | ||
|
|
||
| use callisto::{check_result, sea_orm_active_enums::CheckTypeEnum}; | ||
| use common::errors::MegaError; | ||
| use jupiter::{model::mr_dto::MrInfoDto, storage::Storage}; | ||
|
|
||
| use crate::merge_checker::mr_sync_checker::MrSyncChecker; | ||
|
|
||
| pub mod mr_sync_checker; | ||
|
|
||
| #[async_trait] | ||
| pub trait Checker: Send + Sync { | ||
| async fn run(&self, params: &serde_json::Value) -> CheckResult; | ||
|
|
||
| async fn build_params(&self, mr_info: &MrInfoDto) -> Result<serde_json::Value, MegaError>; | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, ToSchema)] | ||
| pub enum CheckType { | ||
| GpgSignature, | ||
| BranchProtection, | ||
| CommitMessage, | ||
| MrSync, | ||
| MergeConflict, | ||
| CiStatus, | ||
| CodeReview, | ||
| } | ||
|
|
||
| impl From<CheckTypeEnum> for CheckType { | ||
| fn from(value: CheckTypeEnum) -> Self { | ||
| match value { | ||
| CheckTypeEnum::GpgSignature => CheckType::GpgSignature, | ||
| CheckTypeEnum::BranchProtection => CheckType::BranchProtection, | ||
| CheckTypeEnum::CommitMessage => CheckType::CommitMessage, | ||
| CheckTypeEnum::MrSync => CheckType::MrSync, | ||
| CheckTypeEnum::MergeConflict => CheckType::MergeConflict, | ||
| CheckTypeEnum::CiStatus => CheckType::CiStatus, | ||
| CheckTypeEnum::CodeReview => CheckType::CodeReview, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<CheckType> for CheckTypeEnum { | ||
| fn from(value: CheckType) -> Self { | ||
| match value { | ||
| CheckType::GpgSignature => CheckTypeEnum::GpgSignature, | ||
| CheckType::BranchProtection => CheckTypeEnum::BranchProtection, | ||
| CheckType::CommitMessage => CheckTypeEnum::CommitMessage, | ||
| CheckType::MrSync => CheckTypeEnum::MrSync, | ||
| CheckType::MergeConflict => CheckTypeEnum::MergeConflict, | ||
| CheckType::CiStatus => CheckTypeEnum::CiStatus, | ||
| CheckType::CodeReview => CheckTypeEnum::CodeReview, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct CheckResult { | ||
| pub check_type_code: CheckType, | ||
| pub status: String, | ||
| pub message: String, | ||
| } | ||
|
|
||
| pub struct CheckerRegistry { | ||
| checkers: HashMap<CheckType, Box<dyn Checker>>, | ||
| storage: Arc<Storage>, | ||
| } | ||
|
|
||
| impl CheckerRegistry { | ||
| pub fn new(storage: Arc<Storage>) -> Self { | ||
| let mut r = CheckerRegistry { | ||
| checkers: HashMap::new(), | ||
| storage: storage.clone(), | ||
| }; | ||
| r.register(CheckType::MrSync, Box::new(MrSyncChecker { storage })); | ||
| r | ||
| } | ||
|
|
||
| pub fn register(&mut self, check_type: CheckType, checker: Box<dyn Checker>) { | ||
| self.checkers.insert(check_type, checker); | ||
| } | ||
|
|
||
| pub async fn run_checks(&self, mr_info: MrInfoDto) -> Result<(), MegaError> { | ||
| let check_configs = self | ||
| .storage | ||
| .mr_storage() | ||
| .get_checks_config_by_path(&mr_info.path) | ||
| .await?; | ||
| let mut save_models = vec![]; | ||
|
|
||
| for c_config in check_configs { | ||
| if let Some(checker) = self.checkers.get(&c_config.check_type_code.into()) { | ||
| let params = checker.build_params(&mr_info).await?; | ||
| let res = checker.run(¶ms).await; | ||
| let model = check_result::Model::new( | ||
| &mr_info.path, | ||
| &mr_info.link, | ||
| &mr_info.to_hash, | ||
| res.check_type_code.into(), | ||
| &res.status, | ||
| &res.message, | ||
| ); | ||
| save_models.push(model); | ||
| } | ||
| } | ||
| self.storage | ||
| .mr_storage() | ||
| .save_check_results(save_models) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use async_trait::async_trait; | ||
| use serde::Deserialize; | ||
|
|
||
| use common::errors::MegaError; | ||
| use jupiter::{model::mr_dto::MrInfoDto, storage::Storage}; | ||
|
|
||
| use crate::merge_checker::{CheckResult, CheckType, Checker}; | ||
|
|
||
| pub struct MrSyncChecker { | ||
| pub storage: Arc<Storage>, | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| pub(crate) struct MrSyncParams { | ||
| mr_from: String, | ||
| current: String, | ||
| } | ||
|
|
||
| impl MrSyncParams { | ||
| fn from_value(v: &serde_json::Value) -> anyhow::Result<Self> { | ||
| Ok(serde_json::from_value(v.clone())?) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl Checker for MrSyncChecker { | ||
| async fn run(&self, params: &serde_json::Value) -> CheckResult { | ||
| let params = MrSyncParams::from_value(params).expect("parse params err"); | ||
| let mut res = CheckResult { | ||
| check_type_code: CheckType::MrSync, | ||
| status: String::from("PENDING"), | ||
| message: String::new(), | ||
| }; | ||
| if params.mr_from == params.current { | ||
| res.status = String::from("PASSED"); | ||
| } else { | ||
| res.status = String::from("FAILED"); | ||
| res.message = | ||
| String::from("The pull request must not have any unresolved merge conflicts"); | ||
| } | ||
| res | ||
| } | ||
|
|
||
| async fn build_params(&self, mr_info: &MrInfoDto) -> Result<serde_json::Value, MegaError> { | ||
| let refs = self | ||
| .storage | ||
| .mono_storage() | ||
| .get_ref(&mr_info.path) | ||
| .await? | ||
| .expect("Err: MR Related Refs Not Found"); | ||
| Ok(serde_json::json!({ | ||
| "mr_from": mr_info.from_hash, | ||
| "current": refs.ref_commit_hash | ||
| })) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -35,6 +35,7 @@ use mercury::{ | |||||
|
|
||||||
| use crate::{ | ||||||
| api_service::{mono_api_service::MonoApiService, ApiHandler}, | ||||||
| merge_checker::CheckerRegistry, | ||||||
| model::mr::BuckFile, | ||||||
| pack::RepoHandler, | ||||||
| protocol::import_refs::{RefCommand, Refs}, | ||||||
|
|
@@ -343,10 +344,10 @@ impl RepoHandler for MonoRepo { | |||||
| } | ||||||
|
|
||||||
| async fn post_mr_operation(&self) -> Result<(), MegaError> { | ||||||
| if self.bellatrix.enable_build() { | ||||||
| let link_guard = self.mr_link.read().await; | ||||||
| let mr = link_guard.as_ref().unwrap(); | ||||||
| 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!( | ||||||
|
|
@@ -359,7 +360,7 @@ impl RepoHandler for MonoRepo { | |||||
| 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: mr.to_string(), | ||||||
| mr: link.to_string(), | ||||||
| args: Some(vec![]), | ||||||
| }; | ||||||
| let bellatrix = self.bellatrix.clone(); | ||||||
|
|
@@ -369,6 +370,15 @@ impl RepoHandler for MonoRepo { | |||||
| } | ||||||
| } | ||||||
| } | ||||||
| let mr_info = self | ||||||
| .storage | ||||||
| .mr_storage() | ||||||
| .get_mr(link) | ||||||
| .await? | ||||||
| .expect("MR Not Found"); | ||||||
|
||||||
| .expect("MR Not Found"); | |
| .expect(&format!("Failed to find MR for link: {}", link)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| //! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.14 | ||
|
|
||
| use super::sea_orm_active_enums::CheckTypeEnum; | ||
| use sea_orm::entity::prelude::*; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] | ||
| #[sea_orm(table_name = "check_result")] | ||
| pub struct Model { | ||
| pub created_at: DateTime, | ||
| pub updated_at: DateTime, | ||
| #[sea_orm(primary_key, auto_increment = false)] | ||
| pub id: i64, | ||
| pub path: String, | ||
| pub mr_link: String, | ||
| pub commit_id: String, | ||
| pub check_type_code: CheckTypeEnum, | ||
| pub status: String, | ||
| pub message: String, | ||
| } | ||
|
|
||
| #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
| pub enum Relation {} | ||
|
|
||
| impl ActiveModelBehavior for ActiveModel {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| use crate::{check_result, entity_ext::generate_id, sea_orm_active_enums::CheckTypeEnum}; | ||
|
|
||
| impl check_result::Model { | ||
| pub fn new( | ||
| path: &str, | ||
| mr_link: &str, | ||
| commit_id: &str, | ||
| check_type_code: CheckTypeEnum, | ||
| status: &str, | ||
| message: &str, | ||
| ) -> Self { | ||
| let now = chrono::Utc::now().naive_utc(); | ||
| Self { | ||
| id: generate_id(), | ||
| created_at: now, | ||
| updated_at: now, | ||
| path: path.to_owned(), | ||
| mr_link: mr_link.to_owned(), | ||
| commit_id: commit_id.to_owned(), | ||
| check_type_code, | ||
| status: status.to_owned(), | ||
| message: message.to_owned(), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod check_result; | ||
| pub mod item_assignees; | ||
| pub mod item_labels; | ||
| pub mod label; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message "parse params err" is not descriptive. Consider using a more helpful message like "Failed to parse MR sync parameters" to help with debugging.