Compare commits

..

2 commits

Author SHA1 Message Date
3e7fca7640 WIP: validate positions
All checks were successful
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
2024-05-26 07:43:03 +01:00
4655ec505a WIP: add github crate 2024-05-26 07:43:03 +01:00
3 changed files with 157 additions and 14 deletions

View file

@ -4,23 +4,26 @@ use git::ForgeLike as _;
use git_next_config as config; use git_next_config as config;
use git_next_git as git; use git_next_git as git;
use kxio::network;
use tracing::{debug, error, info, warn}; use tracing::{debug, error, info, warn};
// TODO: move this module into git crate validation module // TODO: remove legacy commit log
#[allow(clippy::cognitive_complexity)] // TODO: (#83) reduce complexity // TODO: move this module into forge, git or server crate
pub fn validate_positions( pub async fn validate_positions(
forge: &forgejo::ForgeJo, forge: &forgejo::ForgeJo,
repository: &git::OpenRepository, repository: &git::OpenRepository,
repo_config: config::RepoConfig, repo_config: config::RepoConfig,
) -> git::validation::Result { ) -> git::validation::Result {
let repo_details = &forge.repo_details;
// Collect Commit Histories for `main`, `next` and `dev` branches // Collect Commit Histories for `main`, `next` and `dev` branches
repository.fetch()?; repository.fetch()?;
let commit_histories = get_commit_histories(repository, &repo_config); let commit_histories =
get_commit_histories(repository, repo_details, &repo_config, &forge.net).await;
let commit_histories = match commit_histories { let commit_histories = match commit_histories {
Ok(commit_histories) => commit_histories, Ok(commit_histories) => commit_histories,
Err(err) => { Err(err) => {
error!(?err, "Failed to get commit histories"); error!(?err, "Failed to get commit histories");
return Err(git::validation::Error::CommitLog(err)); return Err(git::validation::Error::Network(Box::new(err)));
} }
}; };
// Validations // Validations
@ -144,14 +147,37 @@ pub fn validate_positions(
}) })
} }
fn get_commit_histories( async fn get_commit_histories(
repository: &git::repository::OpenRepository, repository: &git::repository::OpenRepository,
repo_details: &git::RepoDetails,
repo_config: &config::RepoConfig, repo_config: &config::RepoConfig,
) -> Result<git::commit::Histories, git::commit::log::Error> { net: &network::Network,
let main = (repository.commit_log(&repo_config.branches().main(), &[]))?; ) -> Result<git::commit::Histories, network::NetworkError> {
let main_head = [main[0].clone()]; let main = (get_commit_history(
let next = repository.commit_log(&repo_config.branches().next(), &main_head)?; repository,
let dev = repository.commit_log(&repo_config.branches().dev(), &main_head)?; 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)?;
debug!( debug!(
main = main.len(), main = main.len(),
next = next.len(), next = next.len(),
@ -161,3 +187,120 @@ fn get_commit_histories(
let histories = git::commit::Histories { main, next, dev }; let histories = git::commit::Histories { main, next, dev };
Ok(histories) 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<Vec<git::Commit>, 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<Vec<git::Commit>, 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::<Vec<Commit>>(request).await?;
let commits = response
.response_body()
.unwrap_or_default()
.into_iter()
.map(git::Commit::from)
.collect::<Vec<_>>();
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<Commit> for git::Commit {
fn from(value: Commit) -> Self {
Self::new(
git::commit::Sha::new(value.sha),
git::commit::Message::new(value.commit.message),
)
}
}

View file

@ -48,7 +48,7 @@ impl git::ForgeLike for ForgeJo {
repository: git::OpenRepository, repository: git::OpenRepository,
repo_config: config::RepoConfig, repo_config: config::RepoConfig,
) -> git::validation::Result { ) -> git::validation::Result {
branch::validate_positions(self, &repository, repo_config) branch::validate_positions(self, &repository, repo_config).await
} }
fn branch_reset( fn branch_reset(

View file

@ -12,9 +12,9 @@ pub struct Positions {
#[derive(Debug, derive_more::Display)] #[derive(Debug, derive_more::Display)]
pub enum Error { pub enum Error {
Fetch(git::fetch::Error), Network(Box<kxio::network::NetworkError>),
CommitLog(git::commit::log::Error), Fetch(git::fetch::Error),
#[display("Failed to Reset Branch {branch} to {commit}")] #[display("Failed to Reset Branch {branch} to {commit}")]
FailedToResetBranch { FailedToResetBranch {