From 1bb20d4d5886987e36a85e02086633ca083e739d Mon Sep 17 00:00:00 2001 From: Tapish Rathore Date: Wed, 20 Sep 2023 20:30:07 +0530 Subject: [PATCH 1/6] Add settings struct and db saving function and deserialize it --- vibi-dpu/src/core/review.rs | 46 +++++++++++++++++++++++-------------- vibi-dpu/src/db/mod.rs | 3 ++- vibi-dpu/src/utils/mod.rs | 3 ++- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/vibi-dpu/src/core/review.rs b/vibi-dpu/src/core/review.rs index a93edfd1..40f59d0b 100644 --- a/vibi-dpu/src/core/review.rs +++ b/vibi-dpu/src/core/review.rs @@ -4,7 +4,8 @@ use serde_json::Value; use crate::{ utils::{hunk::{HunkMap, PrHunkItem}, - review::Review, + review::Review, + repo_config::RepoConfig, gitops::{commit_exists, git_pull, get_excluded_files, @@ -13,17 +14,18 @@ use crate::{ generate_blame}}, db::{hunk::{get_hunk_from_db, store_hunkmap_to_db}, repo::get_clone_url_clone_dir, - review::{save_review_to_db}}, + review::save_review_to_db, + repo_config::save_repo_config_to_db}, bitbucket::config::get_client, core::coverage::process_coverage}; pub async fn process_review(message_data: &Vec) { - let review_opt = parse_review(message_data); - if review_opt.is_none() { - eprintln!("Unable to deserialize review message"); + let parse_opt = parse_review(message_data); + if parse_opt.is_none() { + eprintln!("Unable to deserialize review message and repo config"); return; } - let review = review_opt.expect("review_opt is empty"); + let (review, repo_config) = parse_opt.expect("parse_opt is empty"); if hunk_already_exists(&review) { return; } @@ -94,29 +96,29 @@ async fn commit_check(review: &Review) { } } -fn parse_review(message_data: &Vec) -> Option{ +fn parse_review(message_data: &Vec) -> Option<(Review, RepoConfig)>{ let data_res = serde_json::from_slice::(&message_data); if data_res.is_err() { let e = data_res.expect_err("No error in data_res"); eprintln!("Incoming message does not contain valid reviews: {:?}", e); return None; } - let data = data_res.expect("Uncaught error in deserializing message_data"); - println!("data == {:?}", &data["eventPayload"]["repository"]); - let repo_provider = data["repositoryProvider"].to_string().trim_matches('"').to_string(); - let repo_name = data["eventPayload"]["repository"]["name"].to_string().trim_matches('"').to_string(); + let deserialized_data = data_res.expect("Uncaught error in deserializing message_data"); + println!("deserialized_data == {:?}", &deserialized_data["eventPayload"]["repository"]); + let repo_provider = deserialized_data["repositoryProvider"].to_string().trim_matches('"').to_string(); + let repo_name = deserialized_data["eventPayload"]["repository"]["name"].to_string().trim_matches('"').to_string(); println!("repo NAME == {}", &repo_name); - let workspace_name = data["eventPayload"]["repository"]["workspace"]["slug"].to_string().trim_matches('"').to_string(); + let workspace_name = deserialized_data["eventPayload"]["repository"]["workspace"]["slug"].to_string().trim_matches('"').to_string(); let clone_opt = get_clone_url_clone_dir(&repo_provider, &workspace_name, &repo_name); if clone_opt.is_none() { eprintln!("Unable to get clone url and directory"); return None; } let (clone_url, clone_dir) = clone_opt.expect("Empty clone_opt"); - let pr_id = data["eventPayload"]["pullrequest"]["id"].to_string().trim_matches('"').to_string(); + let pr_id = deserialized_data["eventPayload"]["pullrequest"]["id"].to_string().trim_matches('"').to_string(); let review = Review::new( - data["eventPayload"]["pullrequest"]["destination"]["commit"]["hash"].to_string().replace("\"", ""), - data["eventPayload"]["pullrequest"]["source"]["commit"]["hash"].to_string().replace("\"", ""), + deserialized_data["eventPayload"]["pullrequest"]["destination"]["commit"]["hash"].to_string().replace("\"", ""), + deserialized_data["eventPayload"]["pullrequest"]["source"]["commit"]["hash"].to_string().replace("\"", ""), pr_id.clone(), repo_name.clone(), workspace_name.clone(), @@ -124,11 +126,21 @@ fn parse_review(message_data: &Vec) -> Option{ format!("bitbucket/{}/{}/{}", &workspace_name, &repo_name, &pr_id), clone_dir, clone_url, - data["eventPayload"]["pullrequest"]["author"]["uuid"].to_string().replace("\"", ""), + deserialized_data["eventPayload"]["pullrequest"]["author"]["uuid"].to_string().replace("\"", ""), ); println!("review = {:?}", &review); save_review_to_db(&review); - return Some(review); + let repo_config_res = serde_json::from_value(deserialized_data["repoConfig"].clone()); + if repo_config_res.is_err() { + let e = repo_config_res.expect_err("No error in repo_config_res"); + eprintln!("Unable to deserialze repo_config_res: {:?}", e); + let default_config = RepoConfig::default(); + return Some((review, default_config)); + } + let repo_config = repo_config_res.expect("Uncaught error in repo_config_res"); + println!("repo_config = {:?}", &repo_config); + save_repo_config_to_db(&repo_config, &review.repo_name(), &review.repo_owner(), &review.provider()); + return Some((review, repo_config)); } fn publish_hunkmap(hunkmap: &HunkMap) { diff --git a/vibi-dpu/src/db/mod.rs b/vibi-dpu/src/db/mod.rs index 9873e050..8f71c4d4 100644 --- a/vibi-dpu/src/db/mod.rs +++ b/vibi-dpu/src/db/mod.rs @@ -5,4 +5,5 @@ mod config; pub mod webhook; pub mod user; pub mod hunk; -pub mod review; \ No newline at end of file +pub mod review; +pub mod repo_config; \ No newline at end of file diff --git a/vibi-dpu/src/utils/mod.rs b/vibi-dpu/src/utils/mod.rs index 0afbdf8b..d0625899 100644 --- a/vibi-dpu/src/utils/mod.rs +++ b/vibi-dpu/src/utils/mod.rs @@ -6,4 +6,5 @@ pub mod hunk; pub mod review; pub mod gitops; pub mod user; -pub mod lineitem; \ No newline at end of file +pub mod lineitem; +pub mod repo_config; \ No newline at end of file From fd6a990e71ce09bad2d83a76165a481cc29fe094 Mon Sep 17 00:00:00 2001 From: Tapish Rathore Date: Wed, 20 Sep 2023 20:52:55 +0530 Subject: [PATCH 2/6] Implement checking settings before commenting and auto-assigning --- vibi-dpu/src/core/coverage.rs | 41 ++++++++++++++++++------------- vibi-dpu/src/core/review.rs | 8 +++--- vibi-dpu/src/db/repo_config.rs | 28 +++++++++++++++++++++ vibi-dpu/src/utils/repo_config.rs | 26 ++++++++++++++++++++ 4 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 vibi-dpu/src/db/repo_config.rs create mode 100644 vibi-dpu/src/utils/repo_config.rs diff --git a/vibi-dpu/src/core/coverage.rs b/vibi-dpu/src/core/coverage.rs index 967c9364..7e638129 100644 --- a/vibi-dpu/src/core/coverage.rs +++ b/vibi-dpu/src/core/coverage.rs @@ -2,9 +2,10 @@ use std::collections::{HashMap, HashSet}; use crate::{utils::hunk::{HunkMap, PrHunkItem}, db::user::get_workspace_user_from_db, bitbucket::{comment::add_comment, reviewer::add_reviewers}}; use crate::utils::review::Review; +use crate::utils::repo_config::RepoConfig; use crate::bitbucket::auth::get_access_token_review; -pub async fn process_coverage(hunkmap: &HunkMap, review: &Review) { +pub async fn process_coverage(hunkmap: &HunkMap, review: &Review, repo_config: &RepoConfig) { let access_token_opt = get_access_token_review(review).await; if access_token_opt.is_none() { eprintln!("Unable to acquire access_token in process_coverage"); @@ -15,23 +16,25 @@ pub async fn process_coverage(hunkmap: &HunkMap, review: &Review) { // calculate number of hunks for each userid let coverage_map = calculate_coverage(&hunkmap.repo_owner(), prhunk); if !coverage_map.is_empty() { - // get user for each user id - // add reviewers - let mut author_set: HashSet = HashSet::new(); - author_set.insert(prhunk.author().to_string()); - for blame in prhunk.blamevec() { - if author_set.contains(blame.author()) { - continue; + if repo_config.comment() { + // create comment text + let comment = comment_text(coverage_map, repo_config.auto_assign()); + // add comment + add_comment(&comment, review, &access_token).await; + } + if repo_config.auto_assign() { + // add reviewers + let mut author_set: HashSet = HashSet::new(); + author_set.insert(prhunk.author().to_string()); + for blame in prhunk.blamevec() { + if author_set.contains(blame.author()) { + continue; + } + author_set.insert(blame.author().to_string()); + let author_id = blame.author(); + add_reviewers(blame.author(), review, &access_token).await; } - author_set.insert(blame.author().to_string()); - let author_id = blame.author(); - add_reviewers(blame.author(), review, &access_token).await; } - // create comment text - let comment = comment_text(coverage_map); - // add comment - add_comment(&comment, review, &access_token).await; - // TODO - implement settings } } } @@ -74,7 +77,7 @@ fn calculate_coverage(repo_owner: &str, prhunk: &PrHunkItem) -> HashMap) -> String { +fn comment_text(coverage_map: HashMap, auto_assign: bool) -> String { let mut comment = "Relevant users for this PR:\n\n".to_string(); // Added two newlines comment += "| Contributor Name/Alias | Code Coverage |\n"; // Added a newline at the end comment += "| -------------- | --------------- |\n"; // Added a newline at the end @@ -82,6 +85,10 @@ fn comment_text(coverage_map: HashMap) -> String { for (key, value) in coverage_map.iter() { comment += &format!("| {} | {}% |\n", key, value); // Added a newline at the end } + if auto_assign { + comment += "\n\n"; + comment += "Auto assigning to all relevant reviewers"; + } comment += "\n\n"; comment += "Code coverage is calculated based on the git blame information of the PR. To know more, hit us up at contact@vibinex.com.\n\n"; // Added two newlines comment += "To change comment and auto-assign settings, go to [your Vibinex settings page.](https://vibinex.com/settings)\n"; // Added a newline at the end diff --git a/vibi-dpu/src/core/review.rs b/vibi-dpu/src/core/review.rs index 40f59d0b..37420cbb 100644 --- a/vibi-dpu/src/core/review.rs +++ b/vibi-dpu/src/core/review.rs @@ -26,16 +26,17 @@ pub async fn process_review(message_data: &Vec) { return; } let (review, repo_config) = parse_opt.expect("parse_opt is empty"); + println!("deserialized repo_config, review = {:?}, {:?}", &repo_config, &review); if hunk_already_exists(&review) { return; } println!("Processing PR : {}", &review.id()); commit_check(&review).await; let hunkmap_opt = process_review_changes(&review).await; - send_hunkmap(&hunkmap_opt, &review).await; + send_hunkmap(&hunkmap_opt, &review, &repo_config).await; } -async fn send_hunkmap(hunkmap_opt: &Option, review: &Review) { +async fn send_hunkmap(hunkmap_opt: &Option, review: &Review, repo_config: &RepoConfig) { if hunkmap_opt.is_none() { eprintln!("Empty hunkmap in send_hunkmap"); return; @@ -46,7 +47,8 @@ async fn send_hunkmap(hunkmap_opt: &Option, review: &Review) { publish_hunkmap(&hunkmap); let hunkmap_async = hunkmap.clone(); let review_async = review.clone(); - process_coverage(&hunkmap_async, &review_async).await; + let repo_config_clone = repo_config.clone(); + process_coverage(&hunkmap_async, &review_async, &repo_config_clone).await; } fn hunk_already_exists(review: &Review) -> bool { diff --git a/vibi-dpu/src/db/repo_config.rs b/vibi-dpu/src/db/repo_config.rs new file mode 100644 index 00000000..8d197cc8 --- /dev/null +++ b/vibi-dpu/src/db/repo_config.rs @@ -0,0 +1,28 @@ +use sled::IVec; + +use crate::db::config::get_db; +use crate::utils::repo_config::RepoConfig; + +pub fn save_repo_config_to_db(repo_config: &RepoConfig, + repo_name: &str, repo_owner: &str, repo_provider: &str) { + let db = get_db(); + let config_key = format!("{}/{}/{}/config", repo_provider, repo_owner, repo_name); + println!("config_key = {}", &config_key); + + // Serialize repo struct to JSON + let parse_res = serde_json::to_vec(repo_config); + if parse_res.is_err() { + let e = parse_res.expect_err("Empty error in parse_res in save_repo_config_to_db"); + eprintln!("Unable to serialize repo in save_repo_config_to_db: {:?}, error: {:?}", &repo_config, e); + return; + } + let config_json = parse_res.expect("Uncaught error in parse_res save_repo_config_to_db"); + // Insert JSON into sled DB + let insert_res = db.insert(IVec::from(config_key.as_bytes()), config_json); + if insert_res.is_err() { + let e = insert_res.expect_err("No error in insert_res save_repo_config_to_db"); + eprintln!("Failed to upsert repo config into sled DB: {:?}", e); + return; + } + println!("Repo Config succesfully upserted: {:?}", repo_config); +} \ No newline at end of file diff --git a/vibi-dpu/src/utils/repo_config.rs b/vibi-dpu/src/utils/repo_config.rs new file mode 100644 index 00000000..0eada825 --- /dev/null +++ b/vibi-dpu/src/utils/repo_config.rs @@ -0,0 +1,26 @@ +use serde::{Serialize, Deserialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct RepoConfig { + comment: bool, + auto_assign: bool +} + +impl RepoConfig { + // Getters + pub fn comment(&self) -> bool { + self.comment + } + + pub fn auto_assign(&self) -> bool { + self.auto_assign + } + + // Function to create a default RepoConfig + pub fn default() -> Self { + RepoConfig { + comment: false, + auto_assign: false + } + } +} \ No newline at end of file From ff6b6f296c815146bc1e3abd72ff20e5262fedb3 Mon Sep 17 00:00:00 2001 From: Tapish Rathore Date: Wed, 20 Sep 2023 21:28:18 +0530 Subject: [PATCH 3/6] Add debug and info print statements --- vibi-dpu/src/core/coverage.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vibi-dpu/src/core/coverage.rs b/vibi-dpu/src/core/coverage.rs index 7e638129..1f676011 100644 --- a/vibi-dpu/src/core/coverage.rs +++ b/vibi-dpu/src/core/coverage.rs @@ -15,8 +15,13 @@ pub async fn process_coverage(hunkmap: &HunkMap, review: &Review, repo_config: & for prhunk in hunkmap.prhunkvec() { // calculate number of hunks for each userid let coverage_map = calculate_coverage(&hunkmap.repo_owner(), prhunk); + let coverage_cond = !coverage_map.is_empty(); + println!("!coverage_map.is_empty() = {:?}", &coverage_cond); + println!("repo_config.comment() = {:?}", repo_config.comment()); + println!("repo_config.auto_assign() = {:?}", repo_config.auto_assign()); if !coverage_map.is_empty() { if repo_config.comment() { + println!("Inserting comment..."); // create comment text let comment = comment_text(coverage_map, repo_config.auto_assign()); // add comment @@ -24,6 +29,7 @@ pub async fn process_coverage(hunkmap: &HunkMap, review: &Review, repo_config: & } if repo_config.auto_assign() { // add reviewers + println!("Adding reviewers..."); let mut author_set: HashSet = HashSet::new(); author_set.insert(prhunk.author().to_string()); for blame in prhunk.blamevec() { From d71bee7ea69db71de472aeae7b5364dc9db78dcf Mon Sep 17 00:00:00 2001 From: Tapish Rathore Date: Wed, 25 Oct 2023 09:07:37 +0530 Subject: [PATCH 4/6] merge main --- vibi-dpu/src/core/review.rs | 59 +++++++++++++++---------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/vibi-dpu/src/core/review.rs b/vibi-dpu/src/core/review.rs index 37420cbb..228a20cf 100644 --- a/vibi-dpu/src/core/review.rs +++ b/vibi-dpu/src/core/review.rs @@ -4,8 +4,7 @@ use serde_json::Value; use crate::{ utils::{hunk::{HunkMap, PrHunkItem}, - review::Review, - repo_config::RepoConfig, + review::Review, gitops::{commit_exists, git_pull, get_excluded_files, @@ -14,29 +13,27 @@ use crate::{ generate_blame}}, db::{hunk::{get_hunk_from_db, store_hunkmap_to_db}, repo::get_clone_url_clone_dir, - review::save_review_to_db, - repo_config::save_repo_config_to_db}, + review::{save_review_to_db}}, bitbucket::config::get_client, core::coverage::process_coverage}; pub async fn process_review(message_data: &Vec) { - let parse_opt = parse_review(message_data); - if parse_opt.is_none() { - eprintln!("Unable to deserialize review message and repo config"); + let review_opt = parse_review(message_data); + if review_opt.is_none() { + eprintln!("Unable to deserialize review message"); return; } - let (review, repo_config) = parse_opt.expect("parse_opt is empty"); - println!("deserialized repo_config, review = {:?}, {:?}", &repo_config, &review); + let review = review_opt.expect("review_opt is empty"); if hunk_already_exists(&review) { return; } println!("Processing PR : {}", &review.id()); commit_check(&review).await; let hunkmap_opt = process_review_changes(&review).await; - send_hunkmap(&hunkmap_opt, &review, &repo_config).await; + send_hunkmap(&hunkmap_opt, &review).await; } -async fn send_hunkmap(hunkmap_opt: &Option, review: &Review, repo_config: &RepoConfig) { +async fn send_hunkmap(hunkmap_opt: &Option, review: &Review) { if hunkmap_opt.is_none() { eprintln!("Empty hunkmap in send_hunkmap"); return; @@ -47,8 +44,7 @@ async fn send_hunkmap(hunkmap_opt: &Option, review: &Review, repo_confi publish_hunkmap(&hunkmap); let hunkmap_async = hunkmap.clone(); let review_async = review.clone(); - let repo_config_clone = repo_config.clone(); - process_coverage(&hunkmap_async, &review_async, &repo_config_clone).await; + process_coverage(&hunkmap_async, &review_async).await; } fn hunk_already_exists(review: &Review) -> bool { @@ -98,29 +94,29 @@ async fn commit_check(review: &Review) { } } -fn parse_review(message_data: &Vec) -> Option<(Review, RepoConfig)>{ +fn parse_review(message_data: &Vec) -> Option{ let data_res = serde_json::from_slice::(&message_data); if data_res.is_err() { let e = data_res.expect_err("No error in data_res"); eprintln!("Incoming message does not contain valid reviews: {:?}", e); return None; } - let deserialized_data = data_res.expect("Uncaught error in deserializing message_data"); - println!("deserialized_data == {:?}", &deserialized_data["eventPayload"]["repository"]); - let repo_provider = deserialized_data["repositoryProvider"].to_string().trim_matches('"').to_string(); - let repo_name = deserialized_data["eventPayload"]["repository"]["name"].to_string().trim_matches('"').to_string(); + let data = data_res.expect("Uncaught error in deserializing message_data"); + println!("data == {:?}", &data["eventPayload"]["repository"]); + let repo_provider = data["repositoryProvider"].to_string().trim_matches('"').to_string(); + let repo_name = data["eventPayload"]["repository"]["name"].to_string().trim_matches('"').to_string(); println!("repo NAME == {}", &repo_name); - let workspace_name = deserialized_data["eventPayload"]["repository"]["workspace"]["slug"].to_string().trim_matches('"').to_string(); + let workspace_name = data["eventPayload"]["repository"]["workspace"]["slug"].to_string().trim_matches('"').to_string(); let clone_opt = get_clone_url_clone_dir(&repo_provider, &workspace_name, &repo_name); if clone_opt.is_none() { eprintln!("Unable to get clone url and directory"); return None; } let (clone_url, clone_dir) = clone_opt.expect("Empty clone_opt"); - let pr_id = deserialized_data["eventPayload"]["pullrequest"]["id"].to_string().trim_matches('"').to_string(); + let pr_id = data["eventPayload"]["pullrequest"]["id"].to_string().trim_matches('"').to_string(); let review = Review::new( - deserialized_data["eventPayload"]["pullrequest"]["destination"]["commit"]["hash"].to_string().replace("\"", ""), - deserialized_data["eventPayload"]["pullrequest"]["source"]["commit"]["hash"].to_string().replace("\"", ""), + data["eventPayload"]["pullrequest"]["destination"]["commit"]["hash"].to_string().replace("\"", ""), + data["eventPayload"]["pullrequest"]["source"]["commit"]["hash"].to_string().replace("\"", ""), pr_id.clone(), repo_name.clone(), workspace_name.clone(), @@ -128,26 +124,17 @@ fn parse_review(message_data: &Vec) -> Option<(Review, RepoConfig)>{ format!("bitbucket/{}/{}/{}", &workspace_name, &repo_name, &pr_id), clone_dir, clone_url, - deserialized_data["eventPayload"]["pullrequest"]["author"]["uuid"].to_string().replace("\"", ""), + data["eventPayload"]["pullrequest"]["author"]["uuid"].to_string().replace("\"", ""), ); println!("review = {:?}", &review); save_review_to_db(&review); - let repo_config_res = serde_json::from_value(deserialized_data["repoConfig"].clone()); - if repo_config_res.is_err() { - let e = repo_config_res.expect_err("No error in repo_config_res"); - eprintln!("Unable to deserialze repo_config_res: {:?}", e); - let default_config = RepoConfig::default(); - return Some((review, default_config)); - } - let repo_config = repo_config_res.expect("Uncaught error in repo_config_res"); - println!("repo_config = {:?}", &repo_config); - save_repo_config_to_db(&repo_config, &review.repo_name(), &review.repo_owner(), &review.provider()); - return Some((review, repo_config)); + return Some(review); } fn publish_hunkmap(hunkmap: &HunkMap) { let client = get_client(); let hunkmap_json = serde_json::to_string(&hunkmap).expect("Unable to serialize hunkmap"); + let key_clone = hunkmap.db_key().to_string(); tokio::spawn(async move { let url = format!("{}/api/hunks", env::var("SERVER_URL").expect("SERVER_URL must be set")); @@ -158,10 +145,10 @@ fn publish_hunkmap(hunkmap: &HunkMap) { .send() .await { Ok(_) => { - println!("Hunkmap published successfully!"); + println!("[publish_hunkmap] Hunkmap published successfully for: {} !", &key_clone); }, Err(e) => { - eprintln!("Failed to publish hunkmap: {}", e); + eprintln!("[publish_hunkmap] Failed to publish hunkmap: {} for: {}", e, &key_clone); } }; }); From 332ac0c9fc1648fa43ad32629cf8f006182e2fc9 Mon Sep 17 00:00:00 2001 From: Tapish Rathore Date: Wed, 25 Oct 2023 09:21:43 +0530 Subject: [PATCH 5/6] add reviewer.rs changes after merging main --- vibi-dpu/src/core/review.rs | 50 ++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/vibi-dpu/src/core/review.rs b/vibi-dpu/src/core/review.rs index 228a20cf..bb544ad5 100644 --- a/vibi-dpu/src/core/review.rs +++ b/vibi-dpu/src/core/review.rs @@ -4,7 +4,8 @@ use serde_json::Value; use crate::{ utils::{hunk::{HunkMap, PrHunkItem}, - review::Review, + review::Review, + repo_config::RepoConfig, gitops::{commit_exists, git_pull, get_excluded_files, @@ -13,27 +14,29 @@ use crate::{ generate_blame}}, db::{hunk::{get_hunk_from_db, store_hunkmap_to_db}, repo::get_clone_url_clone_dir, - review::{save_review_to_db}}, + review::save_review_to_db, + repo_config::save_repo_config_to_db}, bitbucket::config::get_client, core::coverage::process_coverage}; pub async fn process_review(message_data: &Vec) { let review_opt = parse_review(message_data); if review_opt.is_none() { - eprintln!("Unable to deserialize review message"); + eprintln!("Unable to deserialize review message and repo config"); return; } - let review = review_opt.expect("review_opt is empty"); + let (review, repo_config) = review_opt.expect("parse_opt is empty"); + println!("deserialized repo_config, review = {:?}, {:?}", &repo_config, &review); if hunk_already_exists(&review) { return; } println!("Processing PR : {}", &review.id()); commit_check(&review).await; let hunkmap_opt = process_review_changes(&review).await; - send_hunkmap(&hunkmap_opt, &review).await; + send_hunkmap(&hunkmap_opt, &review, &repo_config).await; } -async fn send_hunkmap(hunkmap_opt: &Option, review: &Review) { +async fn send_hunkmap(hunkmap_opt: &Option, review: &Review, repo_config: &RepoConfig) { if hunkmap_opt.is_none() { eprintln!("Empty hunkmap in send_hunkmap"); return; @@ -44,7 +47,8 @@ async fn send_hunkmap(hunkmap_opt: &Option, review: &Review) { publish_hunkmap(&hunkmap); let hunkmap_async = hunkmap.clone(); let review_async = review.clone(); - process_coverage(&hunkmap_async, &review_async).await; + let repo_config_clone = repo_config.clone(); + process_coverage(&hunkmap_async, &review_async, &repo_config_clone).await; } fn hunk_already_exists(review: &Review) -> bool { @@ -94,29 +98,29 @@ async fn commit_check(review: &Review) { } } -fn parse_review(message_data: &Vec) -> Option{ +fn parse_review(message_data: &Vec) -> Option<(Review, RepoConfig)>{ let data_res = serde_json::from_slice::(&message_data); if data_res.is_err() { let e = data_res.expect_err("No error in data_res"); eprintln!("Incoming message does not contain valid reviews: {:?}", e); return None; } - let data = data_res.expect("Uncaught error in deserializing message_data"); - println!("data == {:?}", &data["eventPayload"]["repository"]); - let repo_provider = data["repositoryProvider"].to_string().trim_matches('"').to_string(); - let repo_name = data["eventPayload"]["repository"]["name"].to_string().trim_matches('"').to_string(); + let deserialized_data = data_res.expect("Uncaught error in deserializing message_data"); + println!("deserialized_data == {:?}", &deserialized_data["eventPayload"]["repository"]); + let repo_provider = deserialized_data["repositoryProvider"].to_string().trim_matches('"').to_string(); + let repo_name = deserialized_data["eventPayload"]["repository"]["name"].to_string().trim_matches('"').to_string(); println!("repo NAME == {}", &repo_name); - let workspace_name = data["eventPayload"]["repository"]["workspace"]["slug"].to_string().trim_matches('"').to_string(); + let workspace_name = deserialized_data["eventPayload"]["repository"]["workspace"]["slug"].to_string().trim_matches('"').to_string(); let clone_opt = get_clone_url_clone_dir(&repo_provider, &workspace_name, &repo_name); if clone_opt.is_none() { eprintln!("Unable to get clone url and directory"); return None; } let (clone_url, clone_dir) = clone_opt.expect("Empty clone_opt"); - let pr_id = data["eventPayload"]["pullrequest"]["id"].to_string().trim_matches('"').to_string(); + let pr_id = deserialized_data["eventPayload"]["pullrequest"]["id"].to_string().trim_matches('"').to_string(); let review = Review::new( - data["eventPayload"]["pullrequest"]["destination"]["commit"]["hash"].to_string().replace("\"", ""), - data["eventPayload"]["pullrequest"]["source"]["commit"]["hash"].to_string().replace("\"", ""), + deserialized_data["eventPayload"]["pullrequest"]["destination"]["commit"]["hash"].to_string().replace("\"", ""), + deserialized_data["eventPayload"]["pullrequest"]["source"]["commit"]["hash"].to_string().replace("\"", ""), pr_id.clone(), repo_name.clone(), workspace_name.clone(), @@ -124,11 +128,21 @@ fn parse_review(message_data: &Vec) -> Option{ format!("bitbucket/{}/{}/{}", &workspace_name, &repo_name, &pr_id), clone_dir, clone_url, - data["eventPayload"]["pullrequest"]["author"]["uuid"].to_string().replace("\"", ""), + deserialized_data["eventPayload"]["pullrequest"]["author"]["uuid"].to_string().replace("\"", ""), ); println!("review = {:?}", &review); save_review_to_db(&review); - return Some(review); + let repo_config_res = serde_json::from_value(deserialized_data["repoConfig"].clone()); + if repo_config_res.is_err() { + let e = repo_config_res.expect_err("No error in repo_config_res"); + eprintln!("Unable to deserialze repo_config_res: {:?}", e); + let default_config = RepoConfig::default(); + return Some((review, default_config)); + } + let repo_config = repo_config_res.expect("Uncaught error in repo_config_res"); + println!("repo_config = {:?}", &repo_config); + save_repo_config_to_db(&repo_config, &review.repo_name(), &review.repo_owner(), &review.provider()); + return Some((review, repo_config)); } fn publish_hunkmap(hunkmap: &HunkMap) { From 02ad66af40c06ea92009e873cfceb80ae2e6e480 Mon Sep 17 00:00:00 2001 From: Tapish Rathore Date: Thu, 26 Oct 2023 08:56:13 +0530 Subject: [PATCH 6/6] replace default settings to true --- vibi-dpu/src/utils/repo_config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vibi-dpu/src/utils/repo_config.rs b/vibi-dpu/src/utils/repo_config.rs index 0eada825..3f74e42a 100644 --- a/vibi-dpu/src/utils/repo_config.rs +++ b/vibi-dpu/src/utils/repo_config.rs @@ -19,8 +19,8 @@ impl RepoConfig { // Function to create a default RepoConfig pub fn default() -> Self { RepoConfig { - comment: false, - auto_assign: false + comment: true, + auto_assign: true } } } \ No newline at end of file