Skip to content

Commit

Permalink
fix contract and change Repository in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tapishr committed Jun 1, 2024
1 parent 71d3bfe commit 9fc3615
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 119 deletions.
1 change: 1 addition & 0 deletions vibi-dpu/src/bitbucket/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub async fn get_workspace_repos(workspace: &str, access_token: &str) -> Option<
clone_val["name".to_string()].as_str() == Some("ssh")
}).collect::<Vec<&Value>>()[0]["href"].to_string().replace('\"',""),
Some(repo_json["project"]["name"].to_string().trim_matches('"').to_string()),
Some(repo_json["project"]["type"].to_string().trim_matches('"').to_string()),
repo_json["workspace"]["slug"].to_string().trim_matches('"').to_string(),
None,
"bitbucket".to_string(),
Expand Down
266 changes: 150 additions & 116 deletions vibi-dpu/src/core/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,134 +16,151 @@ use crate::utils::user::ProviderEnum;

#[derive(Debug, Deserialize, Serialize, Clone)]
struct PublishRequest {
installationId: String,
info: Vec<SetupInfo>,
installationId: String,
info: Vec<SetupInfo>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct AliasRequest {
repo_name: String,
repo_owner: String,
repo_provider: String,
aliases: Vec<String>
repo_name: String,
repo_owner: String,
repo_provider: String,
aliases: Vec<String>
}
#[derive(Debug, Deserialize, Serialize, Clone)]
struct AliasResponse {
aliases: Vec<AliasResponseHandles>,
aliases: Vec<AliasResponseHandles>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
struct AliasResponseHandles {
git_alias: String,
github: Option<Vec<String>>,
bitbucket: Option<Vec<String>>
git_alias: String,
github: Option<Vec<String>>,
bitbucket: Option<Vec<String>>
}

#[derive(Serialize, Deserialize, Debug)]
struct BitbucketDBRepo {
repo_name: String,
repo_owner: String,
repo_provider: String,
#[serde(default)]
clone_ssh_url: Option<String>,
#[serde(default)]
is_private: Option<bool>,
#[serde(default)]
uuid: Option<String>,
#[serde(default)]
workspace: Option<String>,
project_name: Option<String>,
project_type: Option<String>,
}

pub async fn send_setup_info(setup_info: &Vec<SetupInfo>) {
log::info!("Sending repo names and owners to Vibinex...");
let installation_id = env::var("INSTALL_ID")
.expect("INSTALL_ID must be set");
log::debug!("[send_setup_info] install_id = {:?}", &installation_id);
let base_url = env::var("SERVER_URL")
.expect("SERVER_URL must be set");
let body = PublishRequest {
installationId: installation_id,
info: setup_info.to_vec(),
};
log::debug!("[send_setup_info] body = {:?}", &body);
let client = get_client();
let setup_url = format!("{base_url}/api/dpu/setup");
let post_res = client
.post(&setup_url)
.json(&body)
.send()
.await;
if post_res.is_err() {
let e = post_res.expect_err("No error in post_res in send_setup_info");
log::error!("[send_setup_info] error in send_setup_info post_res: {:?}, url: {:?}", e, &setup_url);
return;
}
let resp = post_res.expect("Uncaught error in post_res");
if !resp.status().is_success() {
log::error!("[send_setup_info] Unable to send setup info to server, status = {:?}", resp.status());
return;
}
log::debug!("[send_setup_info] Response: {:?}", resp.text().await);
log::info!("Sending repo names and owners to Vibinex...");
let installation_id = env::var("INSTALL_ID")
.expect("INSTALL_ID must be set");
log::debug!("[send_setup_info] install_id = {:?}", &installation_id);
let base_url = env::var("SERVER_URL")
.expect("SERVER_URL must be set");
let body = PublishRequest {
installationId: installation_id,
info: setup_info.to_vec(),
};
log::debug!("[send_setup_info] body = {:?}", &body);
let client = get_client();
let setup_url = format!("{base_url}/api/dpu/setup");
let post_res = client
.post(&setup_url)
.json(&body)
.send()
.await;
if post_res.is_err() {
let e = post_res.expect_err("No error in post_res in send_setup_info");
log::error!("[send_setup_info] error in send_setup_info post_res: {:?}, url: {:?}", e, &setup_url);
return;
}
let resp = post_res.expect("Uncaught error in post_res");
if !resp.status().is_success() {
log::error!("[send_setup_info] Unable to send setup info to server, status = {:?}", resp.status());
return;
}
log::debug!("[send_setup_info] Response: {:?}", resp.text().await);
}

pub async fn send_aliases(repo: &Repository, aliases: &Vec<String>) {
log::info!("Sending alias list to Vibinex...");
let base_url = env::var("SERVER_URL")
.expect("SERVER_URL must be set");
let body = AliasRequest {
repo_name: repo.name().to_owned(),
repo_owner: repo.owner().to_owned(),
repo_provider: repo.provider().to_owned(),
aliases: aliases.to_owned()
};
log::debug!("[send_aliases] body = {:?}", &body);
let client = get_client();
let alias_url = format!("{base_url}/api/dpu/aliases");
let post_res = client
.post(&alias_url)
.json(&body)
.send()
.await;
if post_res.is_err() {
let e = post_res.expect_err("No error in post_res in send_aliases");
log::error!("[send_aliases] error in send_aliases post_res: {:?}, url: {:?}", e, &alias_url);
return;
}
let resp = post_res.expect("Uncaught error in post_res");
log::debug!("[send_aliases] Response: {:?}", resp.text().await);
log::info!("Sending alias list to Vibinex...");
let base_url = env::var("SERVER_URL")
.expect("SERVER_URL must be set");
let body = AliasRequest {
repo_name: repo.name().to_owned(),
repo_owner: repo.owner().to_owned(),
repo_provider: repo.provider().to_owned(),
aliases: aliases.to_owned()
};
log::debug!("[send_aliases] body = {:?}", &body);
let client = get_client();
let alias_url = format!("{base_url}/api/dpu/aliases");
let post_res = client
.post(&alias_url)
.json(&body)
.send()
.await;
if post_res.is_err() {
let e = post_res.expect_err("No error in post_res in send_aliases");
log::error!("[send_aliases] error in send_aliases post_res: {:?}, url: {:?}", e, &alias_url);
return;
}
let resp = post_res.expect("Uncaught error in post_res");
log::debug!("[send_aliases] Response: {:?}", resp.text().await);
}

pub async fn get_handles_from_server(review: &Review) -> Option<HashMap<String, Vec<String>>>{
let base_url = env::var("SERVER_URL")
.expect("SERVER_URL must be set");
let client = get_client();
let alias_url = format!("{base_url}/api/dpu/aliases?repo_name={}&repo_owner={}&repo_provider={}",
review.repo_name(),
review.repo_owner(),
review.provider());
let get_res = client
.get(&alias_url)
.send()
.await;
let base_url = env::var("SERVER_URL")
.expect("SERVER_URL must be set");
let client = get_client();
let alias_url = format!("{base_url}/api/dpu/aliases?repo_name={}&repo_owner={}&repo_provider={}",
review.repo_name(),
review.repo_owner(),
review.provider());
let get_res = client
.get(&alias_url)
.send()
.await;

if let Err(e) = get_res {
log::error!("[get_handles_from_server] error in get_res: {:?}, url: {:?}", e, &alias_url);
return None;
}
if let Err(e) = get_res {
log::error!("[get_handles_from_server] error in get_res: {:?}, url: {:?}", e, &alias_url);
return None;
}

let resp = get_res.expect("Uncaught error in get_res");
let body_text = resp.text().await.expect("Unable to read response body");
log::debug!("[get_handles_from_server] body text = {:?}", &body_text);
let alias_response: AliasResponse = serde_json::from_str(&body_text)
.expect("Failed to deserialize JSON response");
let alias_handles = alias_response.aliases.to_owned();
let mut aliases_map = HashMap::<String, Vec<String>>::new();
for alias_handle in alias_handles {
if review.provider().to_owned() == ProviderEnum::Github.to_string()
&& alias_handle.github.is_some() {
let gh_handles = alias_handle.github.expect("Empty github handles");
update_handles_in_db(&alias_handle.git_alias, &review.provider(), gh_handles.clone());
aliases_map.insert(alias_handle.git_alias, gh_handles);
continue;
}
if review.provider().to_owned() == ProviderEnum::Bitbucket.to_string()
&& alias_handle.bitbucket.is_some() {
let bb_handles = alias_handle.bitbucket.expect("Empty github handles");
update_handles_in_db(&alias_handle.git_alias, &review.provider(), bb_handles.clone());
aliases_map.insert(alias_handle.git_alias, bb_handles);
}
}
if aliases_map.is_empty() {
log::error!(
"[get_handles_from_server] No aliases found for review - {:?}",
&review);
return None;
}
Some(aliases_map)
let resp = get_res.expect("Uncaught error in get_res");
let body_text = resp.text().await.expect("Unable to read response body");
log::debug!("[get_handles_from_server] body text = {:?}", &body_text);
let alias_response: AliasResponse = serde_json::from_str(&body_text)
.expect("Failed to deserialize JSON response");
let alias_handles = alias_response.aliases.to_owned();
let mut aliases_map = HashMap::<String, Vec<String>>::new();
for alias_handle in alias_handles {
if review.provider().to_owned() == ProviderEnum::Github.to_string()
&& alias_handle.github.is_some() {
let gh_handles = alias_handle.github.expect("Empty github handles");
update_handles_in_db(&alias_handle.git_alias, &review.provider(), gh_handles.clone());
aliases_map.insert(alias_handle.git_alias, gh_handles);
continue;
}
if review.provider().to_owned() == ProviderEnum::Bitbucket.to_string()
&& alias_handle.bitbucket.is_some() {
let bb_handles = alias_handle.bitbucket.expect("Empty github handles");
update_handles_in_db(&alias_handle.git_alias, &review.provider(), bb_handles.clone());
aliases_map.insert(alias_handle.git_alias, bb_handles);
}
}
if aliases_map.is_empty() {
log::error!(
"[get_handles_from_server] No aliases found for review - {:?}",
&review);
return None;
}
Some(aliases_map)
}

pub async fn get_access_token (review: &Option<Review>, provider: &str) -> Option<String> {
Expand All @@ -152,7 +169,7 @@ pub async fn get_access_token (review: &Option<Review>, provider: &str) -> Optio
let access_token_opt = bitbucket::auth::refresh_git_auth(review).await;
if access_token_opt.is_none() {
log::error!("[get_access_token] Unable to get access token, review: {:?}",
&review);
&review);
return None;
}
access_token = access_token_opt.expect("Empty access_token_opt");
Expand All @@ -161,7 +178,7 @@ pub async fn get_access_token (review: &Option<Review>, provider: &str) -> Optio
let access_token_opt = github::auth::gh_access_token(review).await;
if access_token_opt.is_none() {
log::error!("[get_access_token] Unable to get access token, review: {:?}",
&review);
&review);
return None;
}
access_token = access_token_opt.expect("Empty access_token");
Expand All @@ -184,7 +201,7 @@ pub async fn user_selected_repos(provider: &str) -> Option<Vec<Repository>> {
let topic_name = env::var("INSTALL_ID").expect("INSTALL_ID must be set");
let server_prefix_url = env::var("SERVER_URL").expect("SERVER_URL must be set");
let url = format!("{}/api/dpu/repos?topicId={}&provider={}",
&server_prefix_url, &topic_name, &provider);
&server_prefix_url, &topic_name, &provider);
let repos_res = client.get(url).send().await;
if let Err(e) = repos_res {
log::error!("[user_selected_repos] Unable to get repos from server, {:?}", e);
Expand All @@ -194,24 +211,41 @@ pub async fn user_selected_repos(provider: &str) -> Option<Vec<Repository>> {
return parse_repos_response(repos_response).await;
}

async fn parse_repos_response(response: Response) -> Option<Vec<Repository>>{
async fn parse_repos_response(response: Response) -> Option<Vec<Repository>> {
let repos_value_res = response.json::<Value>().await;
log::debug!("[parse_repos_response] ========================================<><><><><> repos_values_res = {:?}", &repos_value_res);
if let Err(e) = &repos_value_res {
log::error!("[parse_repos_response] Unable to parse response {:?}", e);
return None;
}
let repos_value = repos_value_res.expect("Uncaught error in repos_values_res");
let repos_value = repos_value_res.expect("Uncaught error in repos_value_res");
let repolist_json_opt = repos_value.get("repoList");
if repolist_json_opt.is_none() {
log::error!("[parse_repos_response] No repoList key in response json: {:?}", &repos_value);
return None;
}
let repolist_json = repolist_json_opt.expect("Uncaught error in repolist_json_opt");
let repolist_vec_res = serde_json::from_value(repolist_json.clone());
let repolist_vec_res: Result<Vec<BitbucketDBRepo>, _> = serde_json::from_value(repolist_json.clone());
if let Err(e) = &repolist_vec_res {
log::error!("[parse_repos_response] Unable to parse vec of UserSelectedRepo from response: {:?}", e);
log::error!("[parse_repos_response] Unable to parse vec of BitbucketDBRepo from response: {:?}", e);
return None;
}
let repolist_vec: Vec<Repository> = repolist_vec_res.expect("Uncaught error in repolist_vec_res");
return Some(repolist_vec);
let repolist_vec: Vec<BitbucketDBRepo> = repolist_vec_res.expect("Uncaught error in repolist_vec_res");

// Transform BitbucketDBRepo to Repository
let repositories: Vec<Repository> = repolist_vec.into_iter().map(|repo| {
Repository::new(
repo.repo_name,
repo.uuid.unwrap_or_else(String::new), // Handle Option<String>
repo.repo_owner,
repo.is_private.unwrap_or(false), // Handle Option<bool>
repo.clone_ssh_url.unwrap_or_else(String::new), // Handle Option<String>
repo.project_name,
repo.project_type,
repo.workspace.unwrap_or_else(String::new), // Handle Option<String>
None,
repo.repo_provider)
}).collect();

return Some(repositories);
}
2 changes: 2 additions & 0 deletions vibi-dpu/src/github/repos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn deserialize_and_save_repos_object_graphql(repos_json: &Vec<Value>) -> Vec<Rep
.trim_matches('"')
.to_string(),
None,
None,
repo_json["owner"]["login"]
.to_string()
.trim_matches('"')
Expand Down Expand Up @@ -223,6 +224,7 @@ fn deserialize_repo_object(repo_json: &Value) -> Repository {
.trim_matches('"')
.to_string(),
None,
None,
repo_json["owner"]["login"]
.to_string()
.trim_matches('"')
Expand Down
9 changes: 6 additions & 3 deletions vibi-dpu/src/utils/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub struct Repository {
owner: String,
is_private: bool,
clone_ssh_url: String,
project: Option<String>,
project_name: Option<String>,
project_type: Option<String>,
workspace: String,
local_dir: Option<String>,
provider: String,
Expand All @@ -22,7 +23,8 @@ impl Repository {
owner: String,
is_private: bool,
clone_ssh_url: String,
project: Option<String>,
project_name: Option<String>,
project_type: Option<String>,
workspace: String,
local_dir: Option<String>,
provider: String,
Expand All @@ -33,7 +35,8 @@ impl Repository {
owner,
is_private,
clone_ssh_url,
project,
project_name,
project_type,
workspace,
local_dir,
provider,
Expand Down

0 comments on commit 9fc3615

Please sign in to comment.