From f7cbcd34eb1fd2ae66ba76ab9b9be5cd01ca9e37 Mon Sep 17 00:00:00 2001 From: Avikalp Kumar Gupta Date: Thu, 26 Oct 2023 17:17:38 +0530 Subject: [PATCH 1/2] bitbucket/reviewer: renamed multiple variables & functions to make more readable. --- vibi-dpu/src/bitbucket/reviewer.rs | 41 ++++++++++++++---------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/vibi-dpu/src/bitbucket/reviewer.rs b/vibi-dpu/src/bitbucket/reviewer.rs index 582fe1e3..4a71ff53 100644 --- a/vibi-dpu/src/bitbucket/reviewer.rs +++ b/vibi-dpu/src/bitbucket/reviewer.rs @@ -1,19 +1,16 @@ -use reqwest::{Response, header::{HeaderMap, HeaderValue}}; -use serde::Serialize; +use reqwest::Response; use serde_json::Value; -use reqwest::Error; -use std::io; -use crate::{db::user::get_workspace_user_from_db, utils::user::WorkspaceUser}; +use crate::utils::user::WorkspaceUser; use crate::utils::review::Review; use crate::utils::user::BitbucketUser; -use super::{config::{bitbucket_base_url, get_client, prepare_headers}}; +use super::config::{get_client, prepare_headers}; pub async fn add_reviewers(user: &WorkspaceUser, review: &Review, access_token: &str) { let url = prepare_get_prinfo_url(review.repo_owner(), review.repo_name(), review.id()); - let get_response = get_pr_info(&url, access_token).await; - let reviewers_opt = add_user_to_reviewers(get_response, user).await; + let pr_info = get_pr_info(&url, access_token).await; + let reviewers_opt = get_updated_reviewers_vec(pr_info, user).await; if reviewers_opt.is_none() { eprintln!("[add_reviewers] Unable to add reviewers for review: {}", review.id()); return; @@ -23,8 +20,8 @@ pub async fn add_reviewers(user: &WorkspaceUser, review: &Review, access_token: put_reviewers(&url, access_token, &put_payload).await; } -async fn add_user_to_reviewers(response_res: Option, user_from_db: &WorkspaceUser) -> Option<(Vec, Value)> { - let reviewers_opt = parse_reviewers_from_prinfo(response_res).await; +async fn get_updated_reviewers_vec(pr_opt: Option, user_from_db: &WorkspaceUser) -> Option<(Vec, Value)> { + let reviewers_opt = parse_reviewers_from_prinfo(pr_opt).await; if reviewers_opt.is_none() { eprintln!("Unable to parse and add reviewers"); return None; @@ -66,23 +63,23 @@ fn prepare_put_body(updated_reviewers: &Vec, pr_info_json: &Value return Some(response_json); } -async fn parse_reviewers_from_prinfo(response_res: Option) -> Option<(Vec, Value)>{ - if response_res.is_none() { +async fn parse_reviewers_from_prinfo(pr_opt: Option) -> Option<(Vec, Value)>{ + if pr_opt.is_none() { eprintln!("Empty get response for pr_info"); return None; } - let get_response = response_res.expect("Error in getting response"); - println!("get API status: {}", get_response.status()); - let response_json_res = get_response.json::().await; - if response_json_res.is_err() { - let e = response_json_res.expect_err("No error in response_json_res"); + let pr_info_response = pr_opt.expect("Error in getting response"); + println!("get API status: {}", pr_info_response.status()); + let pr_info_json = pr_info_response.json::().await; + if pr_info_json.is_err() { + let e = pr_info_json.expect_err("No error in pr_info_json"); eprintln!("Unable to deserialize response_json: {:?}", e); return None; } - let response_json = response_json_res.expect("Uncaught error in response_json_res"); - let reviewers_opt = response_json.get("reviewers"); + let pr_info = pr_info_json.expect("Uncaught error in pr_info_json"); + let reviewers_opt = pr_info.get("reviewers"); if reviewers_opt.is_none() { - eprintln!("No reviewers found in response: {:?}", &response_json); + eprintln!("No reviewers found in response: {:?}", &pr_info); return None; } let reviewers_value = reviewers_opt.expect("Empty reviewers_opt").to_owned(); @@ -93,10 +90,10 @@ async fn parse_reviewers_from_prinfo(response_res: Option) -> Option<( return None; } let reviewers: Vec = reviewers_res.expect("Uncaught error in response_res"); - return Some((reviewers, response_json)); + return Some((reviewers, pr_info)); } -async fn put_reviewers(url: &str, access_token: &str,put_body_opt: &Option) { +async fn put_reviewers(url: &str, access_token: &str, put_body_opt: &Option) { if put_body_opt.is_none() { eprintln!("Empty put request body, not adding reviewers"); return; From 6b286e8581433222414676408ef8e471509ee5ee Mon Sep 17 00:00:00 2001 From: Avikalp Kumar Gupta Date: Thu, 26 Oct 2023 17:24:30 +0530 Subject: [PATCH 2/2] removed unnecessary linting changes --- vibi-dpu/src/bitbucket/reviewer.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/vibi-dpu/src/bitbucket/reviewer.rs b/vibi-dpu/src/bitbucket/reviewer.rs index 6d185304..dba418eb 100644 --- a/vibi-dpu/src/bitbucket/reviewer.rs +++ b/vibi-dpu/src/bitbucket/reviewer.rs @@ -11,10 +11,7 @@ pub async fn add_reviewers(user: &BitbucketUser, review: &Review, access_token: let pr_info = get_pr_info(&url, access_token).await; let reviewers_opt = get_updated_reviewers_vec(pr_info, user).await; if reviewers_opt.is_none() { - eprintln!( - "[add_reviewers] Unable to add reviewers for review: {}", - review.id() - ); + eprintln!("[add_reviewers] Unable to add reviewers for review: {}", review.id()); return; } let (reviewers, pr_info_json) = reviewers_opt.expect("Empty reviewers_opt"); @@ -22,10 +19,7 @@ pub async fn add_reviewers(user: &BitbucketUser, review: &Review, access_token: put_reviewers(&url, access_token, &put_payload).await; } -async fn get_updated_reviewers_vec( - pr_opt: Option, - user_from_db: &BitbucketUser, -) -> Option<(Vec, Value)> { +async fn get_updated_reviewers_vec(pr_opt: Option, user_from_db: &BitbucketUser) -> Option<(Vec, Value)> { let reviewers_opt = parse_reviewers_from_prinfo(pr_opt).await; if reviewers_opt.is_none() { eprintln!("Unable to parse and add reviewers"); @@ -64,9 +58,7 @@ fn prepare_put_body(updated_reviewers: &Vec, pr_info_json: &Value return Some(response_json); } -async fn parse_reviewers_from_prinfo( - pr_opt: Option, -) -> Option<(Vec, Value)> { +async fn parse_reviewers_from_prinfo(pr_opt: Option) -> Option<(Vec, Value)> { if pr_opt.is_none() { eprintln!("Empty get response for pr_info"); return None; @@ -110,8 +102,7 @@ async fn put_reviewers(url: &str, access_token: &str, put_body_opt: &Option