From 9d4f5b544c0104baa149aa693a67a76c46f87c81 Mon Sep 17 00:00:00 2001 From: Tapish Rathore Date: Mon, 18 Sep 2023 07:24:58 +0530 Subject: [PATCH] fix refresh_token logic and error handling --- vibi-dpu/src/bitbucket/auth.rs | 31 +++++++++++++++++-------------- vibi-dpu/src/utils/auth.rs | 2 +- vibi-dpu/src/utils/gitops.rs | 28 +++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/vibi-dpu/src/bitbucket/auth.rs b/vibi-dpu/src/bitbucket/auth.rs index c7b14223..5307b653 100644 --- a/vibi-dpu/src/bitbucket/auth.rs +++ b/vibi-dpu/src/bitbucket/auth.rs @@ -57,21 +57,17 @@ pub async fn refresh_git_auth(clone_url: &str, directory: &str) -> Option Option { +pub async fn update_access_token(auth_info: &AuthInfo, + clone_url: &str, directory: &str) -> Option { let now = SystemTime::now(); let now_secs = now.duration_since(UNIX_EPOCH).expect("Time went backwards").as_secs(); let timestamp_opt = auth_info.timestamp(); @@ -81,13 +77,20 @@ pub async fn update_access_token(auth_info: &AuthInfo) -> Option { } let timestamp = timestamp_opt.expect("Empty timestamp"); let expires_at = timestamp + auth_info.expires_in(); - println!(" expires_at = {expires_at}, now_secs = {now_secs}"); - if expires_at <= now_secs { - // auth info has expired - let new_auth_info_opt = bitbucket_refresh_token(auth_info.refresh_token()).await; - return new_auth_info_opt; + if expires_at > now_secs { + eprintln!("Not yet expired, expires_at = {}, now_secs = {}", expires_at, now_secs); + return Some(auth_info.to_owned()); } - return None; + // auth info has expired + println!("auth info expired, expires_at = {}, now_secs = {}", expires_at, now_secs); + let new_auth_info_opt = bitbucket_refresh_token(auth_info.refresh_token()).await; + let mut new_auth_info = new_auth_info_opt.clone() + .expect("empty auhtinfo_opt from update_access_token"); + println!("New auth info = {:?}", &new_auth_info); + let access_token = new_auth_info.access_token().to_string(); + set_git_remote_url(clone_url, directory, &access_token); + save_auth_info_to_db(&mut new_auth_info); + return new_auth_info_opt; } async fn bitbucket_refresh_token(refresh_token: &str) -> Option { diff --git a/vibi-dpu/src/utils/auth.rs b/vibi-dpu/src/utils/auth.rs index c5d473bc..224eb811 100644 --- a/vibi-dpu/src/utils/auth.rs +++ b/vibi-dpu/src/utils/auth.rs @@ -1,7 +1,7 @@ use serde::Deserialize; use serde::Serialize; -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug, Deserialize, Serialize, Clone)] pub struct AuthInfo { access_token: String, refresh_token: String, diff --git a/vibi-dpu/src/utils/gitops.rs b/vibi-dpu/src/utils/gitops.rs index 04aae1b3..6b64158a 100644 --- a/vibi-dpu/src/utils/gitops.rs +++ b/vibi-dpu/src/utils/gitops.rs @@ -31,14 +31,33 @@ pub fn commit_exists(commit: &str, directory: &str) -> bool { return false; } let output = output_res.expect("Uncaught error in output_res"); + if !output.status.success() { + eprintln!("git rev-list, exit code: {:?}", + output.status.code()); + // for debugging + match str::from_utf8(&output.stderr) { + Ok(v) => println!("git rev-list stderr = {:?}", v), + Err(e) => {/* error handling */ println!("git rev-list stderr error {}", e)}, + }; + return false; + } println!("Execute git rev-list, exit code: {:?}", output.status.code()); + if output.status.code() == Some(128) { + // for debugging + match str::from_utf8(&output.stderr) { + Ok(v) => eprintln!("git rev-list stderr = {:?}", v), + Err(e) => {/* error handling */ eprintln!("git rev-list stderr error {}", e)}, + }; + return false; + } + // for debugging match str::from_utf8(&output.stderr) { - Ok(v) => println!("git rev-list stderr = {:?}", v), - Err(e) => {/* error handling */ println!("git rev-list stderr error {}", e)}, + Ok(v) => eprintln!("git rev-list stderr = {:?}", v), + Err(e) => {/* error handling */ eprintln!("git rev-list stderr error {}", e)}, }; match str::from_utf8(&output.stdout) { Ok(v) => println!("git rev-list stdout = {:?}", v), - Err(e) => {/* error handling */ println!("git rev-list stdout error {}", e)}, + Err(e) => {/* error handling */ eprintln!("git rev-list stdout error {}", e)}, }; return true; } @@ -48,8 +67,7 @@ pub async fn git_pull(review: &Review) { println!("directory = {}", &directory); let access_token_opt = refresh_git_auth(review.clone_url(), review.clone_dir()).await; if access_token_opt.is_none() { - eprintln!("Unable to get access_token from refresh_git_auth"); - return; + eprintln!("no refresh token acquired"); } let access_token = access_token_opt.expect("Empty access_token"); set_git_url(review.clone_url(), directory, &access_token);