Skip to content
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

added previously removed function. #58

Merged
merged 8 commits into from
Oct 31, 2023
1 change: 0 additions & 1 deletion ablejobs_command.txt

This file was deleted.

1 change: 1 addition & 0 deletions vibi-dpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ rand = "0.8.5"
chrono = "0.4.26"
tonic = "0.9.2"
once_cell = "1.18.0" # MIT
jsonwebtoken = "8.3.0" # MIT

# todo - check all lib licences
3 changes: 1 addition & 2 deletions vibi-dpu/src/bitbucket/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use std::env;
use std::str;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use reqwest::Client;
use super::config::get_client;
use crate::db::auth::{save_auth_info_to_db, auth_info};
use crate::utils::reqwest_client::get_client;
use crate::utils::auth::AuthInfo;
use crate::utils::review::Review;

Expand Down
11 changes: 2 additions & 9 deletions vibi-dpu/src/bitbucket/comment.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use std::{env, collections::HashMap};

use reqwest::{Response, header::{HeaderMap, HeaderValue}};
use serde::Serialize;
use serde_json::Value;

use crate::db::auth::auth_info;
use crate::db::user::get_workspace_user_from_db;
use crate::utils::review::Review;
use crate::utils::user::BitbucketUser;

use super::{config::{bitbucket_base_url, get_client, prepare_headers}};
use crate::utils::reqwest_client::get_client;
use super::config::{bitbucket_base_url, prepare_headers};

#[derive(Serialize)]
struct Comment {
Expand Down
13 changes: 1 addition & 12 deletions vibi-dpu/src/bitbucket/config.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
use std::{env, collections::HashMap};
use std::error::Error;

use reqwest::{Response, header::{HeaderMap, HeaderValue}};
use serde_json::Value;
use std::sync::Arc;
use once_cell::sync::Lazy;
use reqwest::Client;

static CLIENT: Lazy<Arc<Client>> = Lazy::new(|| {
Arc::new(Client::new())
});

pub fn get_client() -> Arc<Client> {
Arc::clone(&CLIENT)
}
use crate::utils::reqwest_client::get_client;

pub fn bitbucket_base_url() -> String {
env::var("BITBUCKET_BASE_URL").expect("BITBUCKET_BASE_URL must be set")
Expand Down
83 changes: 60 additions & 23 deletions vibi-dpu/src/bitbucket/prs.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use crate::db::prs::update_pr_info_in_db;
use crate::utils::{pr_info::PrInfo, reqwest_client::get_client};
use reqwest::header::HeaderMap;
use serde_json::Value;
use std::collections::HashMap;
use std::str;
use crate::{utils::pr_info::PrInfo, db::prs::update_pr_info_in_db};

use super::config::{get_client, prepare_auth_headers, bitbucket_base_url};
use super::config::{bitbucket_base_url, prepare_auth_headers};

pub async fn list_prs_bitbucket(repo_owner: &str, repo_name: &str, access_token: &str, state: &str) -> Option<Vec<String>> {
pub async fn list_prs_bitbucket(repo_owner: &str,repo_name: &str,access_token: &str,state: &str,) -> Option<Vec<String>> {
let headers_opt = prepare_auth_headers(access_token);
if headers_opt.is_none() {
eprintln!("[list_prs_bitbucket] Unable to prepare auth headers: {}", repo_name);
return None;
}
let headers = headers_opt.expect("Empty headers_opt");
let headers = headers_opt.expect("Empty headers_opt");
let mut params = HashMap::new();
params.insert("state".to_string(), state.to_string());
let pr_list_opt = get_list_prs(&headers, &params, repo_owner, repo_name).await;
Expand All @@ -23,7 +24,10 @@ async fn get_list_prs(headers: &HeaderMap, params: &HashMap<String, String>, rep
let client = get_client();
let base_url = bitbucket_base_url();
let response_result = client
.get(&format!("{}/repositories/{}/{}/pullrequests", &base_url, repo_owner, repo_name))
.get(&format!(
"{}/repositories/{}/{}/pullrequests",
&base_url, repo_owner, repo_name
))
.headers(headers.to_owned())
.query(params)
.send()
Expand All @@ -36,14 +40,20 @@ async fn get_list_prs(headers: &HeaderMap, params: &HashMap<String, String>, rep

let response = response_result.expect("Uncaught error in parsing response");
if !response.status().is_success() {
eprintln!("[get_list_prs] Request failed with status: {:?}", response.status());
eprintln!(
"[get_list_prs] Request failed with status: {:?}",
response.status()
);
return None;
}

let parse_result = response.json::<Value>().await;
if parse_result.is_err() {
let parse_result_err = parse_result.expect_err("No error in parsing");
eprintln!("[get_list_prs] Failed to parse JSON: {:?}", parse_result_err);
eprintln!(
"[get_list_prs] Failed to parse JSON: {:?}",
parse_result_err
);
return None;
}
let prs_data = parse_result.expect("Uncaught error in parsing Prs data");
Expand All @@ -53,32 +63,43 @@ async fn get_list_prs(headers: &HeaderMap, params: &HashMap<String, String>, rep
eprintln!("[get_list_prs] Unable to parse get_list_prs: {:?}", e);
return None;
}
let pr_list_parsed: Vec<Value> = pr_list_parse_res.expect("Uncaught error in pr_list_parse_res");
let pr_list_parsed: Vec<Value> =
pr_list_parse_res.expect("Uncaught error in pr_list_parse_res");
let mut pr_list: Vec<String> = Vec::new();
for pr in pr_list_parsed.iter() {
pr_list.push(pr["id"].to_string().trim_matches('"').to_string());
}
if pr_list.is_empty() {
eprintln!("[get_list_prs] pr_list is empty for parsed value: {:?}", &pr_list_parsed);
eprintln!(
"[get_list_prs] pr_list is empty for parsed value: {:?}",
&pr_list_parsed
);
return None;
}
return Some(pr_list);
}



pub async fn get_pr_info(workspace_slug: &str, repo_slug: &str, access_token: &str, pr_number: &str) -> Option<PrInfo> {
pub async fn get_pr_info(
workspace_slug: &str,
repo_slug: &str,
access_token: &str,
pr_number: &str,
) -> Option<PrInfo> {
let base_url = bitbucket_base_url();
let url = format!("{}/repositories/{}/{}/pullrequests/{}", &base_url, workspace_slug, repo_slug, pr_number);
let url = format!(
"{}/repositories/{}/{}/pullrequests/{}",
&base_url, workspace_slug, repo_slug, pr_number
);
println!("[get_pr_info] url: {:?}", &url);
println!("[get_pr_info] access token: {:?}", access_token);
let client = get_client();
let response_result = client.get(&url)
let response_result = client
.get(&url)
.header("Authorization", format!("Bearer {}", access_token))
.header("Accept", "application/json")
.send()
.await;

if response_result.is_err() {
let res_err = response_result.expect_err("No error in getting Pr response");
println!("Error getting PR info: {:?}", res_err);
Expand All @@ -89,23 +110,39 @@ pub async fn get_pr_info(workspace_slug: &str, repo_slug: &str, access_token: &s
println!("Failed to get PR info, response: {:?}", response);
return None;
}
let pr_data: Value = response.json().await.unwrap_or_default(); //TODO - remove unwrap
let pr_data: Value = response.json().await.expect("Error parsing PR data");
let pr_info = PrInfo {
base_head_commit: pr_data["destination"]["commit"]["hash"].as_str().unwrap_or_default().to_string(),
pr_head_commit: pr_data["source"]["commit"]["hash"].as_str().unwrap_or_default().to_string(),
state: pr_data["state"].as_str().unwrap_or_default().to_string(),
pr_branch: pr_data["source"]["branch"]["name"].as_str().unwrap_or_default().to_string(),
base_head_commit: pr_data["destination"]["commit"]["hash"]
.to_string()
.trim_matches('"')
.to_string(),
pr_head_commit: pr_data["source"]["commit"]["hash"]
.to_string()
.trim_matches('"')
.to_string(),
state: pr_data["state"].to_string().trim_matches('"').to_string(),
pr_branch: pr_data["source"]["branch"]["name"]
.to_string()
.trim_matches('"')
.to_string(),
};
println!("[get_pr_info] pr_info: {:?}", &pr_info);
Some(pr_info)
}

pub async fn get_and_store_pr_info(workspace_slug: &str,repo_slug: &str,access_token: &str, pr_number: &str) {
pub async fn get_and_store_pr_info(
workspace_slug: &str,
repo_slug: &str,
access_token: &str,
pr_number: &str,
) {
if let Some(pr_info) = get_pr_info(workspace_slug, repo_slug, access_token, pr_number).await {
// If PR information is available, store it in the database
println!("[get_and_store_pr_info|update_pr_info_in_db] workspace_slug: {}, repo_slug: {}, pr_number: {}, pr_info: {:?}", &workspace_slug, &repo_slug, &pr_number, &pr_info);
update_pr_info_in_db(workspace_slug, repo_slug, &pr_info, pr_number).await;
} else {
eprintln!("No PR info available for PR number: {:?} repository: {:?} repo_owner{:?}", pr_number, repo_slug, workspace_slug);
eprintln!(
"No PR info available for PR number: {:?} repository: {:?} repo_owner{:?}",
pr_number, repo_slug, workspace_slug
);
}
}
3 changes: 2 additions & 1 deletion vibi-dpu/src/bitbucket/reviewer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use serde_json::Value;

use crate::utils::review::Review;
use crate::utils::user::BitbucketUser;
use crate::utils::reqwest_client::get_client;

use super::config::{get_client, prepare_headers};
use super::config::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());
Expand Down
4 changes: 2 additions & 2 deletions vibi-dpu/src/bitbucket/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use reqwest::{header::HeaderValue, Response, Error};
use serde_json::json;

use crate::{db::webhook::save_webhook_to_db, utils::webhook::{Webhook, WebhookResponse}, bitbucket::config::{bitbucket_base_url, get_api_values}};

use super::config::{get_client, prepare_auth_headers};
use crate::utils::reqwest_client::get_client;
use super::config::prepare_auth_headers;


pub async fn get_webhooks_in_repo(workspace_slug: &str, repo_slug: &str, access_token: &str) -> Vec<Webhook> {
Expand Down
1 change: 1 addition & 0 deletions vibi-dpu/src/core/bitbucket/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod setup;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use tokio::{task, fs};

use crate::bitbucket::auth::get_access_token_from_bitbucket;
use crate::bitbucket::config::get_client;
use crate::utils::reqwest_client::get_client;
use crate::bitbucket::repo::get_workspace_repos;
use crate::bitbucket::workspace::get_bitbucket_workspaces;
use crate::bitbucket::webhook::{get_webhooks_in_repo, add_webhook};
Expand Down Expand Up @@ -94,7 +94,6 @@ pub async fn handle_install_bitbucket(installation_code: &str) {
send_setup_info(&pubreqs).await;
}


async fn send_setup_info(setup_info: &Vec<SetupInfo>) {
let installation_id = env::var("INSTALL_ID")
.expect("INSTALL_ID must be set");
Expand Down
1 change: 1 addition & 0 deletions vibi-dpu/src/core/github/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod setup;
46 changes: 46 additions & 0 deletions vibi-dpu/src/core/github/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// setup_gh.rs

use crate::github::auth::fetch_access_token; // Import shared utilities
use reqwest::Client;
use serde::{Deserialize, Serialize};
use tokio::task;

#[derive(Debug, Deserialize, Serialize, Clone)]
struct SetupInfoGithub {
provider: String,
owner: String,
repos: Vec<String>,
}

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

pub async fn handle_install_github(installation_code: &str) {
// TODO: Implement the logic to handle GitHub installation

// For example:
// 1. Get access token from GitHub using the installation code
let auth_info = fetch_access_token(installation_code).await;
println!("[handle_install_github] auth_info = {:?}", &auth_info);
// 2. Fetch user repositories and other necessary data
// 3. Process webhooks or other setup tasks
// 4. Send setup info or any other post-processing
}

async fn get_github_repositories(access_token: &str) -> Vec<String> {
// TODO: Implement the logic to fetch user repositories from GitHub
Vec::new()
}

async fn process_webhooks_github(repo_name: String, access_token: String) {
// TODO: Implement the logic to process GitHub webhooks
}

async fn send_setup_info_github(setup_info: &Vec<SetupInfoGithub>) {
// TODO: Implement the logic to send setup info for GitHub
}

// Add other necessary functions and utilities specific to GitHub setup
6 changes: 4 additions & 2 deletions vibi-dpu/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod setup;
pub mod review;
mod coverage;
mod coverage;

pub mod bitbucket;
pub mod github;
4 changes: 2 additions & 2 deletions vibi-dpu/src/core/review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use crate::{
get_excluded_files,
generate_diff,
process_diffmap,
generate_blame}},
generate_blame},
reqwest_client::get_client},
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},
bitbucket::config::get_client,
core::coverage::process_coverage};

pub async fn process_review(message_data: &Vec<u8>) {
Expand Down
Loading