2024-05-03 17:50:50 +01:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use actix::prelude::*;
|
|
|
|
|
2024-05-11 19:46:20 +01:00
|
|
|
use git_next_config::{RepoConfig, RepoConfigSource};
|
|
|
|
use git_next_git as git;
|
2024-04-16 22:21:55 +01:00
|
|
|
use tracing::{info, warn};
|
2024-04-09 18:18:19 +01:00
|
|
|
|
2024-05-11 18:57:18 +01:00
|
|
|
use crate::{
|
2024-05-08 07:34:35 +01:00
|
|
|
actors::repo::{LoadConfigFromRepo, RepoActor, ValidateRepo},
|
|
|
|
gitforge,
|
2024-05-03 17:50:50 +01:00
|
|
|
};
|
2024-04-09 16:16:01 +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-11 17:58:30 +01:00
|
|
|
#[tracing::instrument(fields(next), skip_all)]
|
2024-04-09 19:30:05 +01:00
|
|
|
pub async fn advance_next(
|
2024-05-11 19:46:20 +01:00
|
|
|
next: git::Commit,
|
|
|
|
dev_commit_history: Vec<git::Commit>,
|
|
|
|
repo_config: RepoConfig,
|
2024-04-18 19:15:26 +01:00
|
|
|
forge: gitforge::Forge,
|
2024-05-11 19:46:20 +01:00
|
|
|
repository: git::Repository,
|
2024-05-03 17:50:50 +01:00
|
|
|
addr: Addr<super::RepoActor>,
|
2024-05-05 08:17:32 +01:00
|
|
|
message_token: super::MessageToken,
|
2024-04-09 19:30:05 +01:00
|
|
|
) {
|
2024-04-10 15:18:48 +01:00
|
|
|
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;
|
|
|
|
};
|
2024-04-12 16:21:45 +01:00
|
|
|
if let Some(problem) = validate_commit_message(commit.message()) {
|
|
|
|
warn!("Can't advance next to commit '{}': {}", commit, problem);
|
|
|
|
return;
|
|
|
|
}
|
2024-04-12 17:41:09 +01:00
|
|
|
info!("Advancing next to commit '{}'", commit);
|
2024-04-18 19:15:26 +01:00
|
|
|
if let Err(err) = forge.branch_reset(
|
2024-05-09 21:53:50 +01:00
|
|
|
&repository,
|
2024-04-18 19:15:26 +01:00
|
|
|
repo_config.branches().next(),
|
|
|
|
commit.into(),
|
2024-05-16 14:45:25 +01:00
|
|
|
git::push::Force::No,
|
2024-04-18 19:15:26 +01:00
|
|
|
) {
|
2024-04-13 16:22:49 +01:00
|
|
|
warn!(?err, "Failed")
|
2024-04-16 22:21:55 +01:00
|
|
|
}
|
2024-05-03 17:50:50 +01:00
|
|
|
tokio::time::sleep(Duration::from_secs(10)).await;
|
2024-05-05 08:17:32 +01:00
|
|
|
addr.do_send(ValidateRepo { message_token })
|
2024-04-10 15:18:48 +01:00
|
|
|
}
|
|
|
|
|
2024-04-12 16:21:45 +01:00
|
|
|
#[tracing::instrument]
|
2024-05-11 19:46:20 +01:00
|
|
|
fn validate_commit_message(message: &git::commit::Message) -> Option<String> {
|
2024-04-12 16:21:45 +01:00
|
|
|
let message = &message.to_string();
|
|
|
|
if message.to_ascii_lowercase().starts_with("wip") {
|
|
|
|
return Some("Is Work-In-Progress".to_string());
|
|
|
|
}
|
|
|
|
match git_conventional::Commit::parse(message) {
|
|
|
|
Ok(commit) => {
|
|
|
|
info!(?commit, "Pass");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
warn!(?err, "Fail");
|
|
|
|
Some(err.kind().to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 07:59:31 +01:00
|
|
|
pub fn find_next_commit_on_dev(
|
2024-05-11 19:46:20 +01:00
|
|
|
next: git::Commit,
|
|
|
|
dev_commit_history: Vec<git::Commit>,
|
|
|
|
) -> Option<git::Commit> {
|
|
|
|
let mut next_commit: Option<git::Commit> = None;
|
2024-04-10 15:18:48 +01:00
|
|
|
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'
|
2024-04-11 17:58:30 +01:00
|
|
|
#[tracing::instrument(fields(next), skip_all)]
|
2024-04-09 22:43:23 +01:00
|
|
|
pub async fn advance_main(
|
2024-05-11 19:46:20 +01:00
|
|
|
next: git::Commit,
|
|
|
|
repo_config: RepoConfig,
|
2024-04-16 22:21:55 +01:00
|
|
|
forge: gitforge::Forge,
|
2024-05-11 19:46:20 +01:00
|
|
|
repository: git::Repository,
|
2024-05-03 17:50:50 +01:00
|
|
|
addr: Addr<RepoActor>,
|
2024-05-05 08:17:32 +01:00
|
|
|
message_token: super::MessageToken,
|
2024-04-09 22:43:23 +01:00
|
|
|
) {
|
2024-04-12 17:41:09 +01:00
|
|
|
info!("Advancing main to next");
|
2024-04-18 19:15:26 +01:00
|
|
|
if let Err(err) = forge.branch_reset(
|
2024-05-09 21:53:50 +01:00
|
|
|
&repository,
|
2024-04-18 19:15:26 +01:00
|
|
|
repo_config.branches().main(),
|
|
|
|
next.into(),
|
2024-05-16 14:45:25 +01:00
|
|
|
git::push::Force::No,
|
2024-04-18 19:15:26 +01:00
|
|
|
) {
|
2024-04-12 19:43:21 +01:00
|
|
|
warn!(?err, "Failed")
|
2024-04-10 17:36:08 +01:00
|
|
|
};
|
2024-05-08 07:34:35 +01:00
|
|
|
match repo_config.source() {
|
|
|
|
RepoConfigSource::Repo => addr.do_send(LoadConfigFromRepo),
|
|
|
|
RepoConfigSource::Server => addr.do_send(ValidateRepo { message_token }),
|
|
|
|
}
|
2024-04-09 19:30:05 +01:00
|
|
|
}
|