2024-04-11 14:20:59 +01:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
2024-04-09 22:43:23 +01:00
|
|
|
use actix::prelude::*;
|
|
|
|
|
|
|
|
use kxio::network;
|
2024-04-10 11:46:51 +01:00
|
|
|
use tracing::{error, info, warn};
|
2024-04-09 18:18:19 +01:00
|
|
|
|
2024-04-11 14:20:59 +01:00
|
|
|
use crate::server::{
|
2024-04-11 15:43:02 +01:00
|
|
|
actors::repo::branch,
|
2024-04-11 14:20:59 +01:00
|
|
|
config::{self, BranchName},
|
|
|
|
forge,
|
|
|
|
};
|
2024-04-09 16:16:01 +01:00
|
|
|
|
2024-04-09 22:43:23 +01:00
|
|
|
use super::{RepoActor, StartMonitoring, StartRepo};
|
2024-04-09 18:18:19 +01:00
|
|
|
|
2024-04-11 07:46:30 +01:00
|
|
|
#[tracing::instrument(fields(forge_name = %repo_details.forge.name, repo_name = %repo_details.name), skip_all)]
|
2024-04-09 16:16:01 +01:00
|
|
|
pub async fn validate_positions(
|
2024-04-09 22:43:23 +01:00
|
|
|
repo_details: config::RepoDetails,
|
|
|
|
config: config::RepoConfig,
|
|
|
|
addr: Addr<RepoActor>,
|
|
|
|
net: network::Network,
|
2024-04-09 15:31:59 +01:00
|
|
|
) {
|
2024-04-09 18:18:19 +01:00
|
|
|
let commit_histories = match repo_details.forge.forge_type {
|
2024-04-09 22:43:23 +01:00
|
|
|
config::ForgeType::ForgeJo => {
|
2024-04-09 18:18:19 +01:00
|
|
|
forge::forgejo::get_commit_histories(&repo_details, &config, &net).await
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let commit_histories = match commit_histories {
|
|
|
|
Ok(commit_histories) => commit_histories,
|
|
|
|
Err(err) => {
|
|
|
|
error!(?err, "Failed to get commit histories");
|
|
|
|
return;
|
2024-04-09 16:16:01 +01:00
|
|
|
}
|
|
|
|
};
|
2024-04-09 18:18:19 +01:00
|
|
|
let Some(main) = commit_histories.main.first().cloned() else {
|
|
|
|
warn!("No commits on main branch '{}'", config.branches().main());
|
|
|
|
return;
|
|
|
|
};
|
2024-04-11 15:43:02 +01:00
|
|
|
// verify that next is an ancestor of dev, and force update to it main if it isn't
|
|
|
|
let Some(next) = commit_histories.next.first().cloned() else {
|
|
|
|
warn!("No commits on next branch '{}", config.branches().next());
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let next_is_ancestor_of_dev = commit_histories.dev.iter().any(|dev| dev == &next);
|
|
|
|
if !next_is_ancestor_of_dev {
|
|
|
|
info!("Next is not an ancestor of dev - resetting next to main");
|
|
|
|
if branch::reset(
|
|
|
|
&config.branches().next(),
|
|
|
|
main,
|
|
|
|
ResetForce::Force(next.into()),
|
|
|
|
&repo_details,
|
|
|
|
)
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
addr.do_send(super::ValidateRepo)
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-09 18:18:19 +01:00
|
|
|
let next_commits = commit_histories
|
|
|
|
.next
|
|
|
|
.into_iter()
|
|
|
|
.take(2)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if !next_commits.contains(&main) {
|
|
|
|
warn!(
|
|
|
|
"Main branch '{}' is not on the same commit as next branch '{}', or it's parent",
|
|
|
|
config.branches().main(),
|
|
|
|
config.branches().next()
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-10 18:02:04 +01:00
|
|
|
let Some(next) = next_commits.first().cloned() else {
|
|
|
|
warn!("No commits on next branch '{}'", config.branches().next());
|
|
|
|
return;
|
|
|
|
};
|
2024-04-09 18:18:19 +01:00
|
|
|
let dev_has_next = commit_histories
|
|
|
|
.dev
|
|
|
|
.iter()
|
|
|
|
.any(|commit| commit == &next_commits[0]);
|
|
|
|
if !dev_has_next {
|
|
|
|
warn!(
|
|
|
|
"Dev branch '{}' is not based on next branch '{}'",
|
|
|
|
config.branches().dev(),
|
|
|
|
config.branches().next()
|
|
|
|
);
|
|
|
|
return; // dev is not based on next
|
|
|
|
}
|
|
|
|
let dev_has_main = commit_histories.dev.iter().any(|commit| commit == &main);
|
|
|
|
if !dev_has_main {
|
|
|
|
warn!(
|
|
|
|
"Dev branch '{}' is not based on main branch '{}'",
|
|
|
|
config.branches().dev(),
|
|
|
|
config.branches().main()
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-10 18:02:04 +01:00
|
|
|
let Some(dev) = commit_histories.dev.first().cloned() else {
|
|
|
|
warn!("No commits on dev branch '{}'", config.branches().dev());
|
|
|
|
return;
|
|
|
|
};
|
2024-04-10 15:18:48 +01:00
|
|
|
addr.do_send(StartMonitoring {
|
|
|
|
main,
|
|
|
|
next,
|
|
|
|
dev,
|
|
|
|
dev_commit_history: commit_histories.dev,
|
|
|
|
});
|
2024-04-09 15:31:59 +01:00
|
|
|
}
|
2024-04-09 19:30:05 +01:00
|
|
|
|
2024-04-09 22:43:23 +01:00
|
|
|
// advance next to the next commit towards the head of the dev branch
|
2024-04-09 19:30:05 +01:00
|
|
|
pub async fn advance_next(
|
2024-04-10 15:18:48 +01:00
|
|
|
next: forge::Commit,
|
|
|
|
dev_commit_history: Vec<forge::Commit>,
|
|
|
|
repo_details: config::RepoDetails,
|
|
|
|
repo_config: config::RepoConfig,
|
2024-04-09 22:43:23 +01:00
|
|
|
addr: Addr<RepoActor>,
|
2024-04-09 19:30:05 +01:00
|
|
|
) {
|
2024-04-10 11:46:51 +01:00
|
|
|
info!("Advance Next");
|
2024-04-10 15:18:48 +01:00
|
|
|
let user = repo_details.forge.user;
|
|
|
|
let token = repo_details.forge.token;
|
|
|
|
let hostname = repo_details.forge.hostname;
|
|
|
|
let path = repo_details.repo;
|
|
|
|
let next_commit = find_next_commit_on_dev(next, dev_commit_history);
|
|
|
|
let Some(commit) = next_commit else {
|
|
|
|
warn!("No commits to advance next to");
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
let next = repo_config.branches().next();
|
|
|
|
let command =
|
|
|
|
format!("/usr/bin/git push https://{user}:{token}@{hostname}/{path}.git {commit}:{next}");
|
|
|
|
info!("Running command: {}", command);
|
|
|
|
match gix::command::prepare(command)
|
|
|
|
.with_shell_allow_argument_splitting()
|
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(mut child) => {
|
|
|
|
match child.wait() {
|
|
|
|
Ok(exit_status) => {
|
|
|
|
info!(%exit_status, "Advance Next Success");
|
2024-04-10 22:40:33 +01:00
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
|
2024-04-10 15:18:48 +01:00
|
|
|
addr.do_send(StartRepo);
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Advance Next Failed (wait)")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Advance Next Failed (spawn)")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_next_commit_on_dev(
|
|
|
|
next: forge::Commit,
|
|
|
|
dev_commit_history: Vec<forge::Commit>,
|
|
|
|
) -> Option<forge::Commit> {
|
|
|
|
let mut next_commit: Option<forge::Commit> = None;
|
|
|
|
for commit in dev_commit_history.into_iter() {
|
|
|
|
if commit == next {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
next_commit.replace(commit);
|
|
|
|
}
|
|
|
|
next_commit
|
2024-04-09 22:43:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// advance main branch to the commit 'next'
|
|
|
|
pub async fn advance_main(
|
2024-04-10 17:36:08 +01:00
|
|
|
next: forge::Commit,
|
|
|
|
repo_details: config::RepoDetails,
|
|
|
|
repo_config: config::RepoConfig,
|
2024-04-09 22:43:23 +01:00
|
|
|
addr: Addr<RepoActor>,
|
|
|
|
) {
|
2024-04-10 11:46:51 +01:00
|
|
|
info!("Advance Main");
|
2024-04-11 17:45:11 +01:00
|
|
|
let result = reset(
|
|
|
|
&repo_config.branches().main(),
|
|
|
|
next,
|
|
|
|
ResetForce::Normal,
|
|
|
|
&repo_details,
|
|
|
|
);
|
|
|
|
match result {
|
|
|
|
Ok(_) => {
|
|
|
|
info!("Advance Next Success");
|
|
|
|
addr.do_send(StartRepo);
|
2024-04-10 17:36:08 +01:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2024-04-11 17:45:11 +01:00
|
|
|
warn!(?err, "Advance Next Failed")
|
2024-04-10 17:36:08 +01:00
|
|
|
}
|
|
|
|
};
|
2024-04-09 19:30:05 +01:00
|
|
|
}
|
2024-04-10 15:18:48 +01:00
|
|
|
|
2024-04-11 15:43:02 +01:00
|
|
|
#[derive(Clone)]
|
2024-04-11 14:20:59 +01:00
|
|
|
pub struct GitRef(pub String);
|
|
|
|
impl From<forge::Commit> for GitRef {
|
|
|
|
fn from(value: forge::Commit) -> Self {
|
|
|
|
Self(value.sha)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl From<BranchName> for GitRef {
|
|
|
|
fn from(value: BranchName) -> Self {
|
|
|
|
Self(value.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Display for GitRef {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-04-11 15:43:02 +01:00
|
|
|
write!(f, "{}", self.0)
|
2024-04-11 14:20:59 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-11 15:43:02 +01:00
|
|
|
|
|
|
|
pub enum ResetForce {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
Normal,
|
|
|
|
Force(GitRef),
|
|
|
|
}
|
2024-04-11 14:20:59 +01:00
|
|
|
pub fn reset(
|
|
|
|
branch: &BranchName,
|
|
|
|
gitref: impl Into<GitRef>,
|
2024-04-11 15:43:02 +01:00
|
|
|
reset_force: ResetForce,
|
2024-04-11 14:20:59 +01:00
|
|
|
repo_details: &config::RepoDetails,
|
|
|
|
) -> Result<(), std::io::Error> {
|
|
|
|
let gitref: GitRef = gitref.into();
|
|
|
|
let user = &repo_details.forge.user;
|
|
|
|
let token = &repo_details.forge.token;
|
|
|
|
let hostname = &repo_details.forge.hostname;
|
|
|
|
let path = &repo_details.repo;
|
2024-04-11 15:43:02 +01:00
|
|
|
let origin = format!("https://{user}:{token}@{hostname}/{path}.git");
|
|
|
|
let force = match reset_force {
|
|
|
|
ResetForce::Normal => "".to_string(),
|
|
|
|
ResetForce::Force(old_ref) => format!("--force-with-lease={branch}:{old_ref}"),
|
|
|
|
};
|
|
|
|
let command = format!("/usr/bin/git push {origin} {gitref}:{branch} {force}");
|
|
|
|
info!("Running command: {}", command);
|
2024-04-11 14:20:59 +01:00
|
|
|
match gix::command::prepare(command)
|
|
|
|
.with_shell_allow_argument_splitting()
|
|
|
|
.spawn()
|
|
|
|
{
|
|
|
|
Ok(mut child) => match child.wait() {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Advance Next Failed (wait)");
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Advance Next Failed (spawn)");
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-10 15:18:48 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_find_next_commit_on_dev() {
|
|
|
|
let next = forge::Commit::new("current-next");
|
|
|
|
let dev_commit_history = vec![
|
|
|
|
forge::Commit::new("dev"),
|
|
|
|
forge::Commit::new("dev-next"),
|
|
|
|
forge::Commit::new("current-next"),
|
|
|
|
forge::Commit::new("current-main"),
|
|
|
|
];
|
|
|
|
let next_commit = find_next_commit_on_dev(next, dev_commit_history);
|
|
|
|
assert_eq!(next_commit, Some(forge::Commit::new("dev-next")));
|
|
|
|
}
|
|
|
|
}
|