From 8a35911d0064af6104490e215590c9e615b8ea70 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 26 May 2024 07:42:47 +0100 Subject: [PATCH] refactor: get commit from from local repo (part 2/3) Remove the legacy implementation --- .../src/branch/validate_positions.rs | 162 ++---------------- crates/forge-forgejo/src/lib.rs | 2 +- crates/git/src/validation.rs | 4 +- 3 files changed, 13 insertions(+), 155 deletions(-) diff --git a/crates/forge-forgejo/src/branch/validate_positions.rs b/crates/forge-forgejo/src/branch/validate_positions.rs index 38794836..fb8ff5ab 100644 --- a/crates/forge-forgejo/src/branch/validate_positions.rs +++ b/crates/forge-forgejo/src/branch/validate_positions.rs @@ -4,24 +4,22 @@ use git::ForgeLike as _; use git_next_config as config; use git_next_git as git; -use kxio::network; use tracing::{debug, error, info, warn}; -pub async fn validate_positions( +#[allow(clippy::cognitive_complexity)] // TODO: (#83) reduce complexity +pub fn validate_positions( forge: &forgejo::ForgeJo, repository: &git::OpenRepository, repo_config: config::RepoConfig, ) -> git::validation::Result { - let repo_details = &forge.repo_details; // Collect Commit Histories for `main`, `next` and `dev` branches repository.fetch()?; - let commit_histories = - get_commit_histories(repository, repo_details, &repo_config, &forge.net).await; + let commit_histories = get_commit_histories(repository, &repo_config); let commit_histories = match commit_histories { Ok(commit_histories) => commit_histories, Err(err) => { error!(?err, "Failed to get commit histories"); - return Err(git::validation::Error::Network(Box::new(err))); + return Err(git::validation::Error::CommitLog(err)); } }; // Validations @@ -145,37 +143,14 @@ pub async fn validate_positions( }) } -async fn get_commit_histories( +fn get_commit_histories( repository: &git::repository::OpenRepository, - repo_details: &git::RepoDetails, repo_config: &config::RepoConfig, - net: &network::Network, -) -> Result { - let main = (get_commit_history( - repository, - repo_details, - &repo_config.branches().main(), - &[], - net, - ) - .await)?; - let main_head = main[0].clone(); - let next = (get_commit_history( - repository, - repo_details, - &repo_config.branches().next(), - &[main_head.clone()], - net, - ) - .await)?; - let dev = (get_commit_history( - repository, - repo_details, - &repo_config.branches().dev(), - &[main_head], - net, - ) - .await)?; +) -> Result { + let main = (repository.commit_log(&repo_config.branches().main(), &[]))?; + let main_head = [main[0].clone()]; + let next = repository.commit_log(&repo_config.branches().next(), &main_head)?; + let dev = repository.commit_log(&repo_config.branches().dev(), &main_head)?; debug!( main = main.len(), next = next.len(), @@ -185,120 +160,3 @@ async fn get_commit_histories( let histories = git::commit::Histories { main, next, dev }; Ok(histories) } - -#[tracing::instrument(skip_all, fields(%branch_name, ?find_commits))] -async fn get_commit_history( - repository: &git::repository::OpenRepository, - repo_details: &git::RepoDetails, - branch_name: &config::BranchName, - find_commits: &[git::Commit], - net: &kxio::network::Network, -) -> Result, network::NetworkError> { - let original = original_get_commit_history(repo_details, branch_name, find_commits, net).await; - let updated = repository.commit_log(branch_name, find_commits); - match (original, updated) { - (Ok(original), Ok(updated)) => { - if updated == original { - info!("new version matches"); - Ok(updated) - } else { - error!("new version doesn't match original"); - Ok(original) - } - } - (Ok(original), Err(err_updated)) => { - warn!(?err_updated, "original ok, updated error"); - Ok(original) - } - (Err(err_original), Ok(updated)) => { - warn!(?err_original, "original err, updated ok"); - Ok(updated) - } - (Err(err_orignal), Err(err_updated)) => { - error!(?err_orignal, ?err_updated, "original err, updated err"); - Err(err_orignal) - } - } -} - -#[tracing::instrument(fields(%branch_name),skip_all)] -async fn original_get_commit_history( - repo_details: &git::RepoDetails, - branch_name: &config::BranchName, - find_commits: &[git::Commit], - net: &kxio::network::Network, -) -> Result, network::NetworkError> { - let hostname = &repo_details.forge.hostname(); - let repo_path = &repo_details.repo_path; - - let mut page = 1; - let limit = match find_commits.is_empty() { - true => 1, - false => 50, - }; - let options = "stat=false&verification=false&files=false"; - let mut all_commits = Vec::new(); - loop { - let api_token = &repo_details.forge.token(); - use secrecy::ExposeSecret; - let token = api_token.expose_secret(); - let url = network::NetUrl::new(format!( - "https://{hostname}/api/v1/repos/{repo_path}/commits?sha={branch_name}&{options}&token={token}&page={page}&limit={limit}" - )); - - let request = network::NetRequest::new( - network::RequestMethod::Get, - url, - network::NetRequestHeaders::new(), - network::RequestBody::None, - network::ResponseType::Json, - None, - network::NetRequestLogging::None, - ); - let response = net.get::>(request).await?; - let commits = response - .response_body() - .unwrap_or_default() - .into_iter() - .map(git::Commit::from) - .collect::>(); - - let found = find_commits.is_empty() - || find_commits - .iter() - .any(|find_commit| commits.iter().any(|commit| commit == find_commit)); - let at_end = commits.len() < limit; // i.e. less than the number of commits requested - for commit in commits { - all_commits.push(commit.clone()); - if find_commits.contains(&commit) { - break; - } - } - if found || at_end { - break; - } - page += 1; - } - - Ok(all_commits) -} - -#[allow(dead_code)] -#[derive(Debug, Default, serde::Deserialize)] -struct Commit { - sha: String, - commit: RepoCommit, -} -#[allow(dead_code)] -#[derive(Debug, Default, serde::Deserialize)] -struct RepoCommit { - message: String, -} -impl From for git::Commit { - fn from(value: Commit) -> Self { - Self::new( - git::commit::Sha::new(value.sha), - git::commit::Message::new(value.commit.message), - ) - } -} diff --git a/crates/forge-forgejo/src/lib.rs b/crates/forge-forgejo/src/lib.rs index bdc029df..4f43df91 100644 --- a/crates/forge-forgejo/src/lib.rs +++ b/crates/forge-forgejo/src/lib.rs @@ -48,7 +48,7 @@ impl git::ForgeLike for ForgeJo { repository: git::OpenRepository, repo_config: config::RepoConfig, ) -> git::validation::Result { - branch::validate_positions(self, &repository, repo_config).await + branch::validate_positions(self, &repository, repo_config) } fn branch_reset( diff --git a/crates/git/src/validation.rs b/crates/git/src/validation.rs index 2b7a8ae2..d2d5f919 100644 --- a/crates/git/src/validation.rs +++ b/crates/git/src/validation.rs @@ -12,10 +12,10 @@ pub struct Positions { #[derive(Debug, derive_more::Display)] pub enum Error { - Network(Box), - Fetch(git::fetch::Error), + CommitLog(git::commit::log::Error), + #[display("Failed to Reset Branch {branch} to {commit}")] FailedToResetBranch { branch: BranchName,