fix: create git graph log to after doing a fetch
Some checks failed
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
Rust / build (push) Failing after 9m27s

This commit is contained in:
Paul Campbell 2024-08-30 07:49:43 +01:00
parent 4f6669548c
commit 4269695611
2 changed files with 53 additions and 40 deletions

View file

@ -15,6 +15,7 @@ use crate::{
use git_next_core::git::{ use git_next_core::git::{
self, self,
validation::positions::{validate, Error, Positions}, validation::positions::{validate, Error, Positions},
UserNotification,
}; };
impl Handler<ValidateRepo> for RepoActor { impl Handler<ValidateRepo> for RepoActor {
@ -63,26 +64,19 @@ impl Handler<ValidateRepo> for RepoActor {
}; };
logger(self.log.as_ref(), "have repo config"); logger(self.log.as_ref(), "have repo config");
info!("collecting git graph log"); match validate(&**open_repository, &self.repo_details, &repo_config) {
let git_log = git::graph::log(&self.repo_details); Ok((
info!(?git_log, "collected git graph log"); Positions {
self.update_tui_log(git_log.clone());
info!("sent to ui git graph log");
match validate(
&**open_repository,
&self.repo_details,
&repo_config,
git_log,
) {
Ok(Positions {
main, main,
next, next,
dev, dev,
dev_commit_history, dev_commit_history,
next_is_valid, next_is_valid,
}) => { },
git_log,
)) => {
debug!(%main, %next, %dev, "positions"); debug!(%main, %next, %dev, "positions");
self.update_tui_log(git_log);
if next_is_valid && next != main { if next_is_valid && next != main {
do_send(&ctx.address(), CheckCIStatus::new(next), self.log.as_ref()); do_send(&ctx.address(), CheckCIStatus::new(next), self.log.as_ref());
} else if next != dev { } else if next != dev {
@ -119,6 +113,11 @@ impl Handler<ValidateRepo> for RepoActor {
} }
Err(Error::UserIntervention(user_notification)) => { Err(Error::UserIntervention(user_notification)) => {
self.alert_tui(format!("[USER INTERVENTION: {user_notification}]")); self.alert_tui(format!("[USER INTERVENTION: {user_notification}]"));
if let UserNotification::CICheckFailed { log, .. }
| UserNotification::DevNotBasedOnMain { log, .. } = &user_notification
{
self.update_tui_log(log.clone());
}
notify_user( notify_user(
self.notify_user_recipient.as_ref(), self.notify_user_recipient.as_ref(),
user_notification, user_notification,

View file

@ -1,4 +1,4 @@
use tracing::{debug, instrument}; use tracing::{debug, info, instrument};
// //
use crate::{ use crate::{
@ -29,13 +29,14 @@ pub fn validate(
open_repository: &dyn OpenRepositoryLike, open_repository: &dyn OpenRepositoryLike,
repo_details: &git::RepoDetails, repo_details: &git::RepoDetails,
repo_config: &RepoConfig, repo_config: &RepoConfig,
log: git::graph::Log, ) -> Result<(Positions, git::graph::Log)> {
) -> Result<Positions> {
let main_branch = repo_config.branches().main(); let main_branch = repo_config.branches().main();
let next_branch = repo_config.branches().next(); let next_branch = repo_config.branches().next();
let dev_branch = repo_config.branches().dev(); let dev_branch = repo_config.branches().dev();
// Collect Commit Histories for `main`, `next` and `dev` branches // Collect Commit Histories for `main`, `next` and `dev` branches
open_repository.fetch()?; open_repository.fetch()?;
let git_log = git::graph::log(repo_details);
let commit_histories = get_commit_histories(open_repository, repo_config)?; let commit_histories = get_commit_histories(open_repository, repo_config)?;
// branch tips // branch tips
let main = commit_histories let main = commit_histories
@ -64,7 +65,7 @@ pub fn validate(
main_branch, main_branch,
dev_commit: dev, dev_commit: dev,
main_commit: main, main_commit: main,
log, log: git_log,
}, },
)); ));
} }
@ -80,23 +81,38 @@ pub fn validate(
&main, &main,
) { ) {
tracing::info!("Main not on same commit as next, or it's parent - resetting next to main",); tracing::info!("Main not on same commit as next, or it's parent - resetting next to main",);
return reset_next_to_main(open_repository, repo_details, &main, &next, &next_branch); return Err(reset_next_to_main(
open_repository,
repo_details,
&main,
&next,
&next_branch,
));
} }
// verify that next is an ancestor of dev, else reset it back to main if dev not ahead of main // verify that next is an ancestor of dev, else reset it back to main if dev not ahead of main
if is_not_based_on(&commit_histories.dev, &next) if is_not_based_on(&commit_histories.dev, &next)
&& commit_histories.main.first() == commit_histories.dev.first() && commit_histories.main.first() == commit_histories.dev.first()
{ {
tracing::info!("Next is not an ancestor of dev - resetting next to main"); tracing::info!("Next is not an ancestor of dev - resetting next to main");
return reset_next_to_main(open_repository, repo_details, &main, &next, &next_branch); return Err(reset_next_to_main(
open_repository,
repo_details,
&main,
&next,
&next_branch,
));
} }
let next_is_valid = is_based_on(&commit_histories.dev, &next); let next_is_valid = is_based_on(&commit_histories.dev, &next);
Ok(git::validation::positions::Positions { Ok((
git::validation::positions::Positions {
main, main,
next, next,
dev, dev,
dev_commit_history: commit_histories.dev, dev_commit_history: commit_histories.dev,
next_is_valid, next_is_valid,
}) },
git_log,
))
} }
#[allow(clippy::result_large_err)] #[allow(clippy::result_large_err)]
@ -106,22 +122,20 @@ fn reset_next_to_main(
main: &git::Commit, main: &git::Commit,
next: &git::Commit, next: &git::Commit,
next_branch: &BranchName, next_branch: &BranchName,
) -> Result<Positions> { ) -> Error {
git::push::reset( if let Err(err) = git::push::reset(
open_repository, open_repository,
repo_details, repo_details,
next_branch, next_branch,
&main.clone().into(), &main.clone().into(),
&git::push::Force::From(next.clone().into()), &git::push::Force::From(next.clone().into()),
) ) {
.map_err(|err| {
Error::NonRetryable(format!( Error::NonRetryable(format!(
"Failed to reset branch '{next_branch}' to commit '{next}': {err}" "Failed to reset branch '{next_branch}' to commit '{next}': {err}"
)) ))
})?; } else {
Err(Error::Retryable(format!( Error::Retryable(format!("Branch {next_branch} has been reset"))
"Branch {next_branch} has been reset" }
)))
} }
fn is_not_based_on(commits: &[git::commit::Commit], needle: &git::Commit) -> bool { fn is_not_based_on(commits: &[git::commit::Commit], needle: &git::Commit) -> bool {