Skip to content

Commit

Permalink
fix refresh_token logic and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tapishr committed Sep 18, 2023
1 parent c9bca44 commit 9d4f5b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
31 changes: 17 additions & 14 deletions vibi-dpu/src/bitbucket/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,17 @@ pub async fn refresh_git_auth(clone_url: &str, directory: &str) -> Option<String
}
let authinfo = authinfo_opt.expect("empty authinfo_opt in refresh_git_auth");
let mut access_token = authinfo.access_token().to_string();
let authinfo_opt = update_access_token(&authinfo).await;
let authinfo_opt = update_access_token(&authinfo, clone_url, directory).await;
if authinfo_opt.is_none() {
eprintln!("Empty authinfo_opt from update_access_token");
return None;
}
let mut new_auth_info = authinfo_opt
.expect("empty auhtinfo_opt from update_access_token");
println!("New auth info = {:?}", &new_auth_info);
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 Some(access_token);
}

pub async fn update_access_token(auth_info: &AuthInfo) -> Option<AuthInfo> {
pub async fn update_access_token(auth_info: &AuthInfo,
clone_url: &str, directory: &str) -> Option<AuthInfo> {
let now = SystemTime::now();
let now_secs = now.duration_since(UNIX_EPOCH).expect("Time went backwards").as_secs();
let timestamp_opt = auth_info.timestamp();
Expand All @@ -81,13 +77,20 @@ pub async fn update_access_token(auth_info: &AuthInfo) -> Option<AuthInfo> {
}
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<AuthInfo> {
Expand Down
2 changes: 1 addition & 1 deletion vibi-dpu/src/utils/auth.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
28 changes: 23 additions & 5 deletions vibi-dpu/src/utils/gitops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down

0 comments on commit 9d4f5b5

Please sign in to comment.