refactor: move MessageToken to repo-actor crate
All checks were successful
Rust / build (push) Successful in 1m11s
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

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)]
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 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(
forge: &ForgeJoEnv,
@ -148,7 +148,7 @@ async fn get_commit_histories(
repo_details: &RepoDetails,
repo_config: &RepoConfig,
net: &network::Network,
) -> Result<CommitHistories, network::NetworkError> {
) -> Result<git::commit::Histories, network::NetworkError> {
let main =
(get_commit_history(repo_details, &repo_config.branches().main(), vec![], net).await)?;
let main_head = main[0].clone();
@ -173,7 +173,7 @@ async fn get_commit_histories(
dev = dev.len(),
"Commit histories"
);
let histories = CommitHistories { main, next, dev };
let histories = git::commit::Histories { main, next, dev };
Ok(histories)
}

View file

@ -7,7 +7,7 @@ use git_next_git::{self as git, GitRef, RepoDetails, Repository};
use kxio::network::{self, Network};
use tracing::{error, info, warn};
use crate::{validation, CommitStatus};
use crate::validation;
struct ForgeJo;
#[derive(Clone, Debug)]
@ -62,7 +62,7 @@ impl super::ForgeLike for ForgeJoEnv {
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 hostname = &repo_details.forge.hostname();
let repo_path = &repo_details.repo_path;
@ -87,21 +87,21 @@ impl super::ForgeLike for ForgeJoEnv {
Ok(response) => {
match response.response_body() {
Some(status) => match status.state {
CommitStatusState::Success => CommitStatus::Pass,
CommitStatusState::Pending => CommitStatus::Pending,
CommitStatusState::Failure => CommitStatus::Fail,
CommitStatusState::Error => CommitStatus::Fail,
CommitStatusState::Blank => CommitStatus::Pending,
CommitStatusState::Success => git::commit::Status::Pass,
CommitStatusState::Pending => git::commit::Status::Pending,
CommitStatusState::Failure => git::commit::Status::Fail,
CommitStatusState::Error => git::commit::Status::Fail,
CommitStatusState::Blank => git::commit::Status::Pending,
},
None => {
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) => {
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 types;
pub use types::*;
#[async_trait::async_trait]
pub trait ForgeLike {
fn name(&self) -> String;
@ -48,7 +45,7 @@ pub trait ForgeLike {
) -> git::push::Result;
/// 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.
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_git::{self as git, GitRef};
use crate::{branch, file, validation, CommitStatus};
use crate::{branch, file, validation};
struct MockForge;
#[derive(Clone, Debug)]
@ -48,7 +48,7 @@ impl super::ForgeLike for MockForgeEnv {
todo!()
}
async fn commit_status(&self, _commit: &git::Commit) -> CommitStatus {
async fn commit_status(&self, _commit: &git::Commit) -> git::commit::Status {
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 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
#[tracing::instrument(fields(next), skip_all)]
@ -17,7 +17,7 @@ pub async fn advance_next(
forge: gitforge::Forge,
repository: git::OpenRepository,
addr: Addr<super::RepoActor>,
message_token: gitforge::MessageToken,
message_token: MessageToken,
) {
let next_commit = find_next_commit_on_dev(next, dev_commit_history);
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)]
pub struct RepoActor {
generation: Generation,
message_token: gitforge::MessageToken,
message_token: MessageToken,
details: RepoDetails,
webhook: Webhook,
webhook_id: Option<WebhookId>, // INFO: if [None] then no webhook is configured
@ -55,7 +55,7 @@ impl RepoActor {
debug!(?forge, "new");
Self {
generation,
message_token: gitforge::MessageToken::new(),
message_token: MessageToken::new(),
details,
webhook,
webhook_id: None,
@ -150,7 +150,7 @@ impl Handler<LoadedConfig> for RepoActor {
#[derive(derive_more::Constructor, Message)]
#[rtype(result = "()")]
pub struct ValidateRepo {
message_token: gitforge::MessageToken,
message_token: MessageToken,
}
impl Handler<ValidateRepo> for RepoActor {
type Result = ();
@ -305,3 +305,14 @@ impl Handler<AdvanceMainTo> for RepoActor {
.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 git_next_git as git;
use git_next_gitforge::{CommitStatus, Forge, MessageToken};
use tracing::{info, warn};
use crate::ValidateRepo;
use crate::{MessageToken, ValidateRepo};
use super::AdvanceMainTo;
pub async fn check_next(
next: git::Commit,
addr: Addr<super::RepoActor>,
forge: Forge,
forge: git_next_gitforge::Forge,
message_token: MessageToken,
) {
// get the status - pass, fail, pending (all others map to fail, e.g. error)
let status = forge.commit_status(&next).await;
info!(?status, "Checking next branch");
match status {
CommitStatus::Pass => {
git::commit::Status::Pass => {
addr.do_send(AdvanceMainTo(next));
}
CommitStatus::Pending => {
git::commit::Status::Pending => {
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
addr.do_send(ValidateRepo { message_token });
}
CommitStatus::Fail => {
git::commit::Status::Fail => {
warn!("Checks have failed");
}
}