|
|
|
@ -4,23 +4,26 @@ 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};
|
|
|
|
|
|
|
|
|
|
// TODO: move this module into git crate validation module
|
|
|
|
|
#[allow(clippy::cognitive_complexity)] // TODO: (#83) reduce complexity
|
|
|
|
|
pub fn validate_positions(
|
|
|
|
|
// TODO: remove legacy commit log
|
|
|
|
|
// TODO: move this module into forge, git or server crate
|
|
|
|
|
pub async 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_config);
|
|
|
|
|
let commit_histories =
|
|
|
|
|
get_commit_histories(repository, repo_details, &repo_config, &forge.net).await;
|
|
|
|
|
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::CommitLog(err));
|
|
|
|
|
return Err(git::validation::Error::Network(Box::new(err)));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
// Validations
|
|
|
|
@ -144,14 +147,37 @@ pub fn validate_positions(
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_commit_histories(
|
|
|
|
|
async fn get_commit_histories(
|
|
|
|
|
repository: &git::repository::OpenRepository,
|
|
|
|
|
repo_details: &git::RepoDetails,
|
|
|
|
|
repo_config: &config::RepoConfig,
|
|
|
|
|
) -> Result<git::commit::Histories, git::commit::log::Error> {
|
|
|
|
|
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)?;
|
|
|
|
|
net: &network::Network,
|
|
|
|
|
) -> Result<git::commit::Histories, network::NetworkError> {
|
|
|
|
|
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)?;
|
|
|
|
|
debug!(
|
|
|
|
|
main = main.len(),
|
|
|
|
|
next = next.len(),
|
|
|
|
@ -161,3 +187,120 @@ 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<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),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|