Skip to content

Commit

Permalink
Merge pull request #32 from Alokit-Innovations/akg/testAuthorFix
Browse files Browse the repository at this point in the history
bitbucket/reviewer: renamed multiple variables & functions to make more readable.
  • Loading branch information
tapishr authored Oct 26, 2023
2 parents 18448eb + 6b286e8 commit 9392585
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions vibi-dpu/src/bitbucket/reviewer.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
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::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: &BitbucketUser, 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;
}
let (reviewers, pr_info_json) = reviewers_opt.expect("Empty reviewers_opt");
let (reviewers, pr_info_json) = reviewers_opt.expect("Empty reviewers_opt");
let put_payload = prepare_put_body(&reviewers, &pr_info_json);
put_reviewers(&url, access_token, &put_payload).await;
}

async fn add_user_to_reviewers(response_res: Option<Response>, user_from_db: &BitbucketUser) -> Option<(Vec<BitbucketUser>, Value)> {
let reviewers_opt = parse_reviewers_from_prinfo(response_res).await;
async fn get_updated_reviewers_vec(pr_opt: Option<Response>, user_from_db: &BitbucketUser) -> Option<(Vec<BitbucketUser>, 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;
Expand Down Expand Up @@ -58,27 +54,27 @@ fn prepare_put_body(updated_reviewers: &Vec<BitbucketUser>, pr_info_json: &Value
// Update obj
let obj = obj_opt.expect("empty obj_opt");
obj.insert("reviewers".to_string(), reviewers_obj);
obj.remove("summary"); // API gives error if not removed
obj.remove("summary"); // API gives error if not removed
return Some(response_json);
}

async fn parse_reviewers_from_prinfo(response_res: Option<Response>) -> Option<(Vec<BitbucketUser>, Value)>{
if response_res.is_none() {
async fn parse_reviewers_from_prinfo(pr_opt: Option<Response>) -> Option<(Vec<BitbucketUser>, 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::<Value>().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::<Value>().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();
Expand All @@ -89,10 +85,10 @@ async fn parse_reviewers_from_prinfo(response_res: Option<Response>) -> Option<(
return None;
}
let reviewers: Vec<BitbucketUser> = 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<Value>) {
async fn put_reviewers(url: &str, access_token: &str, put_body_opt: &Option<Value>) {
if put_body_opt.is_none() {
eprintln!("Empty put request body, not adding reviewers");
return;
Expand All @@ -113,7 +109,7 @@ async fn put_reviewers(url: &str, access_token: &str,put_body_opt: &Option<Value
// for debugging
match response_res {
Ok(v) => println!("response v = {:?}", v.text().await),
Err(e) => println!("response err = {:?}", e)
Err(e) => println!("response err = {:?}", e),
};
}

Expand All @@ -135,8 +131,7 @@ async fn get_pr_info(url: &str, access_token: &str) -> Option<Response> {
return Some(get_response);
}

fn prepare_get_prinfo_url(repo_owner: &str,
repo_name: &str, review_id: &str) -> String {
fn prepare_get_prinfo_url(repo_owner: &str, repo_name: &str, review_id: &str) -> String {
let url = format!(
"{}/repositories/{}/{}/pullrequests/{}",
"https://api.bitbucket.org/2.0".to_string(),
Expand All @@ -146,4 +141,4 @@ fn prepare_get_prinfo_url(repo_owner: &str,
);
println!("add reviews url = {}", &url);
return url;
}
}

0 comments on commit 9392585

Please sign in to comment.