refactor: move MessageToken to repo-actor crate

This commit is contained in:
Paul Campbell 2024-05-23 08:30:58 +01:00
parent 564e14a370
commit 4053563b30
9 changed files with 50 additions and 55 deletions

View file

@ -18,3 +18,17 @@ pub struct Sha(String);
#[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)] #[derive(Clone, Debug, PartialEq, Eq, derive_more::Constructor, derive_more::Display)]
pub struct Message(String); pub struct Message(String);
#[derive(Debug)]
pub enum Status {
Pass,
Fail,
Pending,
}
#[derive(Clone, Debug)]
pub struct Histories {
pub main: Vec<Commit>,
pub next: Vec<Commit>,
pub dev: Vec<Commit>,
}

View file

@ -3,7 +3,7 @@ use git_next_git::{self as git, RepoDetails};
use kxio::network; use kxio::network;
use tracing::{debug, error, info, warn}; use tracing::{debug, error, info, warn};
use crate::{forgejo::ForgeJoEnv, validation, CommitHistories, ForgeLike as _}; use crate::{forgejo::ForgeJoEnv, validation, ForgeLike as _};
pub async fn validate_positions( pub async fn validate_positions(
forge: &ForgeJoEnv, forge: &ForgeJoEnv,
@ -148,7 +148,7 @@ async fn get_commit_histories(
repo_details: &RepoDetails, repo_details: &RepoDetails,
repo_config: &RepoConfig, repo_config: &RepoConfig,
net: &network::Network, net: &network::Network,
) -> Result<CommitHistories, network::NetworkError> { ) -> Result<git::commit::Histories, network::NetworkError> {
let main = let main =
(get_commit_history(repo_details, &repo_config.branches().main(), vec![], net).await)?; (get_commit_history(repo_details, &repo_config.branches().main(), vec![], net).await)?;
let main_head = main[0].clone(); let main_head = main[0].clone();
@ -173,7 +173,7 @@ async fn get_commit_histories(
dev = dev.len(), dev = dev.len(),
"Commit histories" "Commit histories"
); );
let histories = CommitHistories { main, next, dev }; let histories = git::commit::Histories { main, next, dev };
Ok(histories) Ok(histories)
} }

View file

@ -7,7 +7,7 @@ use git_next_git::{self as git, GitRef, RepoDetails, Repository};
use kxio::network::{self, Network}; use kxio::network::{self, Network};
use tracing::{error, info, warn}; use tracing::{error, info, warn};
use crate::{validation, CommitStatus}; use crate::validation;
struct ForgeJo; struct ForgeJo;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -62,7 +62,7 @@ impl super::ForgeLike for ForgeJoEnv {
repository.push(&self.repo_details, branch_name, to_commit, force) repository.push(&self.repo_details, branch_name, to_commit, force)
} }
async fn commit_status(&self, commit: &git::Commit) -> CommitStatus { async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status {
let repo_details = &self.repo_details; let repo_details = &self.repo_details;
let hostname = &repo_details.forge.hostname(); let hostname = &repo_details.forge.hostname();
let repo_path = &repo_details.repo_path; let repo_path = &repo_details.repo_path;
@ -87,21 +87,21 @@ impl super::ForgeLike for ForgeJoEnv {
Ok(response) => { Ok(response) => {
match response.response_body() { match response.response_body() {
Some(status) => match status.state { Some(status) => match status.state {
CommitStatusState::Success => CommitStatus::Pass, CommitStatusState::Success => git::commit::Status::Pass,
CommitStatusState::Pending => CommitStatus::Pending, CommitStatusState::Pending => git::commit::Status::Pending,
CommitStatusState::Failure => CommitStatus::Fail, CommitStatusState::Failure => git::commit::Status::Fail,
CommitStatusState::Error => CommitStatus::Fail, CommitStatusState::Error => git::commit::Status::Fail,
CommitStatusState::Blank => CommitStatus::Pending, CommitStatusState::Blank => git::commit::Status::Pending,
}, },
None => { None => {
warn!("No status found for commit"); warn!("No status found for commit");
CommitStatus::Pending // assume issue is transient and allow retry git::commit::Status::Pending // assume issue is transient and allow retry
} }
} }
} }
Err(e) => { Err(e) => {
error!(?e, "Failed to get commit status"); error!(?e, "Failed to get commit status");
CommitStatus::Pending // assume issue is transient and allow retry git::commit::Status::Pending // assume issue is transient and allow retry
} }
} }
} }

View file

@ -13,9 +13,6 @@ mod github;
mod mock_forge; mod mock_forge;
mod types;
pub use types::*;
#[async_trait::async_trait] #[async_trait::async_trait]
pub trait ForgeLike { pub trait ForgeLike {
fn name(&self) -> String; fn name(&self) -> String;
@ -48,7 +45,7 @@ pub trait ForgeLike {
) -> git::push::Result; ) -> git::push::Result;
/// Checks the results of any (e.g. CI) status checks for the commit. /// Checks the results of any (e.g. CI) status checks for the commit.
async fn commit_status(&self, commit: &git::Commit) -> CommitStatus; async fn commit_status(&self, commit: &git::Commit) -> git::commit::Status;
/// Clones a repo to disk. /// Clones a repo to disk.
fn repo_clone(&self, gitdir: GitDir) -> Result<OpenRepository, git::repository::Error>; fn repo_clone(&self, gitdir: GitDir) -> Result<OpenRepository, git::repository::Error>;

View file

@ -2,7 +2,7 @@ use git::OpenRepository;
use git_next_config::{BranchName, GitDir, RepoConfig}; use git_next_config::{BranchName, GitDir, RepoConfig};
use git_next_git::{self as git, GitRef}; use git_next_git::{self as git, GitRef};
use crate::{branch, file, validation, CommitStatus}; use crate::{branch, file, validation};
struct MockForge; struct MockForge;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -48,7 +48,7 @@ impl super::ForgeLike for MockForgeEnv {
todo!() todo!()
} }
async fn commit_status(&self, _commit: &git::Commit) -> CommitStatus { async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
todo!() todo!()
} }

View file

@ -1,26 +0,0 @@
use git_next_git as git;
#[derive(Debug)]
pub enum CommitStatus {
Pass,
Fail,
Pending,
}
#[derive(Clone, Debug)]
pub struct CommitHistories {
pub main: Vec<git::Commit>,
pub next: Vec<git::Commit>,
pub dev: Vec<git::Commit>,
}
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
pub struct MessageToken(u32);
impl MessageToken {
pub fn new() -> Self {
Self::default()
}
pub const fn next(&self) -> Self {
Self(self.0 + 1)
}
}

View file

@ -6,7 +6,7 @@ use git_next_config::RepoConfig;
use git_next_git as git; use git_next_git as git;
use tracing::{info, warn}; use tracing::{info, warn};
use crate::{gitforge, ValidateRepo}; use crate::{gitforge, MessageToken, ValidateRepo};
// advance next to the next commit towards the head of the dev branch // advance next to the next commit towards the head of the dev branch
#[tracing::instrument(fields(next), skip_all)] #[tracing::instrument(fields(next), skip_all)]
@ -17,7 +17,7 @@ pub async fn advance_next(
forge: gitforge::Forge, forge: gitforge::Forge,
repository: git::OpenRepository, repository: git::OpenRepository,
addr: Addr<super::RepoActor>, addr: Addr<super::RepoActor>,
message_token: gitforge::MessageToken, message_token: MessageToken,
) { ) {
let next_commit = find_next_commit_on_dev(next, dev_commit_history); let next_commit = find_next_commit_on_dev(next, dev_commit_history);
let Some(commit) = next_commit else { let Some(commit) = next_commit else {

View file

@ -27,7 +27,7 @@ use self::webhook::WebhookId;
#[display("{}:{}:{}", generation, details.forge.forge_name(), details.repo_alias)] #[display("{}:{}:{}", generation, details.forge.forge_name(), details.repo_alias)]
pub struct RepoActor { pub struct RepoActor {
generation: Generation, generation: Generation,
message_token: gitforge::MessageToken, message_token: MessageToken,
details: RepoDetails, details: RepoDetails,
webhook: Webhook, webhook: Webhook,
webhook_id: Option<WebhookId>, // INFO: if [None] then no webhook is configured webhook_id: Option<WebhookId>, // INFO: if [None] then no webhook is configured
@ -55,7 +55,7 @@ impl RepoActor {
debug!(?forge, "new"); debug!(?forge, "new");
Self { Self {
generation, generation,
message_token: gitforge::MessageToken::new(), message_token: MessageToken::new(),
details, details,
webhook, webhook,
webhook_id: None, webhook_id: None,
@ -150,7 +150,7 @@ impl Handler<LoadedConfig> for RepoActor {
#[derive(derive_more::Constructor, Message)] #[derive(derive_more::Constructor, Message)]
#[rtype(result = "()")] #[rtype(result = "()")]
pub struct ValidateRepo { pub struct ValidateRepo {
message_token: gitforge::MessageToken, message_token: MessageToken,
} }
impl Handler<ValidateRepo> for RepoActor { impl Handler<ValidateRepo> for RepoActor {
type Result = (); type Result = ();
@ -305,3 +305,14 @@ impl Handler<AdvanceMainTo> for RepoActor {
.wait(ctx); .wait(ctx);
} }
} }
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
pub struct MessageToken(u32);
impl MessageToken {
pub fn new() -> Self {
Self::default()
}
pub const fn next(&self) -> Self {
Self(self.0 + 1)
}
}

View file

@ -2,31 +2,30 @@
use actix::prelude::*; use actix::prelude::*;
use git_next_git as git; use git_next_git as git;
use git_next_gitforge::{CommitStatus, Forge, MessageToken};
use tracing::{info, warn}; use tracing::{info, warn};
use crate::ValidateRepo; use crate::{MessageToken, ValidateRepo};
use super::AdvanceMainTo; use super::AdvanceMainTo;
pub async fn check_next( pub async fn check_next(
next: git::Commit, next: git::Commit,
addr: Addr<super::RepoActor>, addr: Addr<super::RepoActor>,
forge: Forge, forge: git_next_gitforge::Forge,
message_token: MessageToken, message_token: MessageToken,
) { ) {
// get the status - pass, fail, pending (all others map to fail, e.g. error) // get the status - pass, fail, pending (all others map to fail, e.g. error)
let status = forge.commit_status(&next).await; let status = forge.commit_status(&next).await;
info!(?status, "Checking next branch"); info!(?status, "Checking next branch");
match status { match status {
CommitStatus::Pass => { git::commit::Status::Pass => {
addr.do_send(AdvanceMainTo(next)); addr.do_send(AdvanceMainTo(next));
} }
CommitStatus::Pending => { git::commit::Status::Pending => {
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await; tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
addr.do_send(ValidateRepo { message_token }); addr.do_send(ValidateRepo { message_token });
} }
CommitStatus::Fail => { git::commit::Status::Fail => {
warn!("Checks have failed"); warn!("Checks have failed");
} }
} }