Compare commits
1 commit
1d3ade6bdd
...
4a0e31081a
Author | SHA1 | Date | |
---|---|---|---|
4a0e31081a |
21 changed files with 127 additions and 218 deletions
|
@ -10,7 +10,6 @@ mod git_remote;
|
|||
pub mod push;
|
||||
mod repo_details;
|
||||
pub mod repository;
|
||||
mod user_notification;
|
||||
pub mod validation;
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -25,5 +24,3 @@ pub use git_remote::GitRemote;
|
|||
pub use repo_details::RepoDetails;
|
||||
pub use repository::OpenRepository;
|
||||
pub use repository::Repository;
|
||||
pub use user_notification::UserNotification;
|
||||
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
use crate::Commit;
|
||||
use git_next_config::{ForgeAlias, RepoAlias};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum UserNotification {
|
||||
CICheckFailed(ForgeAlias, RepoAlias, Commit),
|
||||
RepoConfigLoadFailure(ForgeAlias, RepoAlias, String),
|
||||
WebhookRegistration(ForgeAlias, RepoAlias, String),
|
||||
DevNotBasedOnMain {
|
||||
forge_alias: git_next_config::ForgeAlias,
|
||||
repo_alias: git_next_config::RepoAlias,
|
||||
dev_branch: git_next_config::BranchName,
|
||||
main_branch: git_next_config::BranchName,
|
||||
},
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
use crate::{self as git, UserNotification};
|
||||
use crate as git;
|
||||
use git_next_config as config;
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
@ -40,14 +40,10 @@ pub fn validate_positions(
|
|||
// Validations:
|
||||
// Dev must be on main branch, else the USER must rebase it
|
||||
if is_not_based_on(&commit_histories.dev, &main) {
|
||||
return Err(Error::UserIntervention(
|
||||
UserNotification::DevNotBasedOnMain {
|
||||
forge_alias: repo_details.forge.forge_alias().clone(),
|
||||
repo_alias: repo_details.repo_alias.clone(),
|
||||
dev_branch,
|
||||
main_branch,
|
||||
},
|
||||
));
|
||||
return Err(Error::UserIntervention(format!(
|
||||
"Branch '{}' not based on '{}'",
|
||||
dev_branch, main_branch
|
||||
)));
|
||||
}
|
||||
// verify that next is on main or at most one commit on top of main, else reset it back to main
|
||||
if is_not_based_on(
|
||||
|
@ -124,8 +120,8 @@ pub enum Error {
|
|||
#[error("{0} - not retrying")]
|
||||
NonRetryable(String),
|
||||
|
||||
#[error("user intervention required")]
|
||||
UserIntervention(UserNotification),
|
||||
#[error("{0} - user intervention required")]
|
||||
UserIntervention(String),
|
||||
}
|
||||
impl From<git::fetch::Error> for Error {
|
||||
fn from(value: git::fetch::Error) -> Self {
|
||||
|
|
|
@ -30,18 +30,21 @@ impl Handler<actor::messages::AdvanceMain> for actor::RepoActor {
|
|||
Err(err) => {
|
||||
tracing::warn!("advance main: {err}");
|
||||
}
|
||||
Ok(_) => match repo_config.source() {
|
||||
git_next_config::RepoConfigSource::Repo => {
|
||||
actor::do_send(addr, actor::messages::LoadConfigFromRepo, self.log.as_ref());
|
||||
Ok(_) => {
|
||||
let log = self.log.clone();
|
||||
match repo_config.source() {
|
||||
git_next_config::RepoConfigSource::Repo => {
|
||||
actor::do_send(addr, actor::messages::LoadConfigFromRepo, &log);
|
||||
}
|
||||
git_next_config::RepoConfigSource::Server => {
|
||||
actor::do_send(
|
||||
addr,
|
||||
actor::messages::ValidateRepo::new(message_token),
|
||||
&log,
|
||||
);
|
||||
}
|
||||
}
|
||||
git_next_config::RepoConfigSource::Server => {
|
||||
actor::do_send(
|
||||
addr,
|
||||
actor::messages::ValidateRepo::new(message_token),
|
||||
self.log.as_ref(),
|
||||
);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ impl Handler<actor::messages::AdvanceNext> for actor::RepoActor {
|
|||
actor::do_send(
|
||||
addr,
|
||||
actor::messages::ValidateRepo::new(message_token),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
}
|
||||
Err(err) => tracing::warn!("advance next: {err}"),
|
||||
|
|
|
@ -11,7 +11,7 @@ impl Handler<actor::messages::CheckCIStatus> for actor::RepoActor {
|
|||
msg: actor::messages::CheckCIStatus,
|
||||
ctx: &mut Self::Context,
|
||||
) -> Self::Result {
|
||||
actor::logger(self.log.as_ref(), "start: CheckCIStatus");
|
||||
actor::logger(&self.log, "start: CheckCIStatus");
|
||||
let addr = ctx.address();
|
||||
let forge = self.forge.duplicate();
|
||||
let next = msg.unwrap();
|
||||
|
@ -24,7 +24,7 @@ impl Handler<actor::messages::CheckCIStatus> for actor::RepoActor {
|
|||
actor::do_send(
|
||||
addr,
|
||||
actor::messages::ReceiveCIStatus::new((next, status)),
|
||||
log.as_ref(),
|
||||
&log,
|
||||
);
|
||||
}
|
||||
.in_current_span()
|
||||
|
|
|
@ -12,29 +12,29 @@ impl Handler<actor::messages::CloneRepo> for actor::RepoActor {
|
|||
_msg: actor::messages::CloneRepo,
|
||||
ctx: &mut Self::Context,
|
||||
) -> Self::Result {
|
||||
actor::logger(self.log.as_ref(), "Handler: CloneRepo: start");
|
||||
actor::logger(&self.log, "Handler: CloneRepo: start");
|
||||
tracing::debug!("Handler: CloneRepo: start");
|
||||
match git::repository::open(&*self.repository_factory, &self.repo_details) {
|
||||
Ok(repository) => {
|
||||
actor::logger(self.log.as_ref(), "open okay");
|
||||
actor::logger(&self.log, "open okay");
|
||||
tracing::debug!("open okay");
|
||||
self.open_repository.replace(repository);
|
||||
if self.repo_details.repo_config.is_none() {
|
||||
actor::do_send(
|
||||
ctx.address(),
|
||||
actor::messages::LoadConfigFromRepo,
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
} else {
|
||||
actor::do_send(
|
||||
ctx.address(),
|
||||
actor::messages::RegisterWebhook::new(),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
actor::logger(self.log.as_ref(), "open failed");
|
||||
actor::logger(&self.log, "open failed");
|
||||
tracing::debug!("err: {err:?}");
|
||||
tracing::warn!("Could not open repo: {err}")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
//
|
||||
use actix::prelude::*;
|
||||
use git_next_git::UserNotification;
|
||||
use tracing::Instrument as _;
|
||||
|
||||
use crate::{self as actor};
|
||||
|
@ -19,27 +18,18 @@ impl Handler<actor::messages::LoadConfigFromRepo> for actor::RepoActor {
|
|||
};
|
||||
let open_repository = open_repository.duplicate();
|
||||
let repo_details = self.repo_details.clone();
|
||||
let forge_alias = repo_details.forge.forge_alias().clone();
|
||||
let repo_alias = repo_details.repo_alias.clone();
|
||||
let addr = ctx.address();
|
||||
let notify_user_recipient = self.notify_user_recipient.clone();
|
||||
let log = self.log.clone();
|
||||
async move {
|
||||
match actor::load::config_from_repository(repo_details, &*open_repository).await {
|
||||
Ok(repo_config) => actor::do_send(
|
||||
addr,
|
||||
actor::messages::ReceiveRepoConfig::new(repo_config),
|
||||
log.as_ref(),
|
||||
),
|
||||
Err(err) => actor::notify_user(
|
||||
notify_user_recipient.as_ref(),
|
||||
UserNotification::RepoConfigLoadFailure(
|
||||
forge_alias,
|
||||
repo_alias,
|
||||
err.to_string(),
|
||||
),
|
||||
log.as_ref(),
|
||||
),
|
||||
Ok(repo_config) => {
|
||||
actor::logger(&log, "send: LoadedConfig");
|
||||
addr.do_send(actor::messages::ReceiveRepoConfig::new(repo_config))
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::warn!(?err, "Failed to load config");
|
||||
// self.notify_user(UserNotification::RepoConfigLoadFailure(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
.in_current_span()
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
//
|
||||
use crate::{self as actor};
|
||||
use crate::{self as actor, UserNotification};
|
||||
use actix::prelude::*;
|
||||
use git::UserNotification;
|
||||
use git_next_git as git;
|
||||
|
||||
impl Handler<actor::messages::ReceiveCIStatus> for actor::RepoActor {
|
||||
|
@ -13,43 +12,33 @@ impl Handler<actor::messages::ReceiveCIStatus> for actor::RepoActor {
|
|||
ctx: &mut Self::Context,
|
||||
) -> Self::Result {
|
||||
let log = self.log.clone();
|
||||
actor::logger(log.as_ref(), "start: ReceiveCIStatus");
|
||||
actor::logger(&log, "start: ReceiveCIStatus");
|
||||
let addr = ctx.address();
|
||||
let (next, status) = msg.unwrap();
|
||||
let forge_alias = self.repo_details.forge.forge_alias().clone();
|
||||
let repo_alias = self.repo_details.repo_alias.clone();
|
||||
let message_token = self.message_token;
|
||||
let sleep_duration = self.sleep_duration;
|
||||
|
||||
tracing::debug!(?status, "");
|
||||
match status {
|
||||
git::forge::commit::Status::Pass => {
|
||||
actor::do_send(
|
||||
addr,
|
||||
actor::messages::AdvanceMain::new(next),
|
||||
self.log.as_ref(),
|
||||
);
|
||||
actor::do_send(addr, actor::messages::AdvanceMain::new(next), &self.log);
|
||||
}
|
||||
git::forge::commit::Status::Pending => {
|
||||
std::thread::sleep(sleep_duration);
|
||||
actor::do_send(
|
||||
addr,
|
||||
actor::messages::ValidateRepo::new(message_token),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
}
|
||||
git::forge::commit::Status::Fail => {
|
||||
tracing::warn!("Checks have failed");
|
||||
actor::notify_user(
|
||||
self.notify_user_recipient.as_ref(),
|
||||
UserNotification::CICheckFailed(forge_alias, repo_alias, next),
|
||||
log.as_ref(),
|
||||
);
|
||||
self.notify_user(UserNotification::CICheckFailed(next));
|
||||
actor::delay_send(
|
||||
addr,
|
||||
sleep_duration,
|
||||
actor::messages::ValidateRepo::new(message_token),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ impl Handler<actor::messages::ReceiveRepoConfig> for actor::RepoActor {
|
|||
actor::do_send(
|
||||
ctx.address(),
|
||||
actor::messages::RegisterWebhook::new(),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,21 +2,19 @@
|
|||
use actix::prelude::*;
|
||||
use tracing::Instrument as _;
|
||||
|
||||
use crate::{self as actor};
|
||||
use crate as actor;
|
||||
use actor::{messages::RegisterWebhook, RepoActor};
|
||||
use git_next_git::UserNotification;
|
||||
|
||||
impl Handler<RegisterWebhook> for RepoActor {
|
||||
type Result = ();
|
||||
|
||||
fn handle(&mut self, _msg: RegisterWebhook, ctx: &mut Self::Context) -> Self::Result {
|
||||
if self.webhook_id.is_none() {
|
||||
let forge_alias = self.repo_details.forge.forge_alias().clone();
|
||||
let repo_alias = self.repo_details.repo_alias.clone();
|
||||
let webhook_url = self.webhook.url(&forge_alias, &repo_alias);
|
||||
let forge_alias = self.repo_details.forge.forge_alias();
|
||||
let repo_alias = &self.repo_details.repo_alias;
|
||||
let webhook_url = self.webhook.url(forge_alias, repo_alias);
|
||||
let forge = self.forge.duplicate();
|
||||
let addr = ctx.address();
|
||||
let notify_user_recipient = self.notify_user_recipient.clone();
|
||||
let log = self.log.clone();
|
||||
tracing::debug!("registering webhook");
|
||||
async move {
|
||||
|
@ -26,25 +24,16 @@ impl Handler<RegisterWebhook> for RepoActor {
|
|||
actor::do_send(
|
||||
addr,
|
||||
actor::messages::WebhookRegistered::from(registered_webhook),
|
||||
log.as_ref(),
|
||||
);
|
||||
}
|
||||
Err(err) => {
|
||||
actor::notify_user(
|
||||
notify_user_recipient.as_ref(),
|
||||
UserNotification::WebhookRegistration(
|
||||
forge_alias,
|
||||
repo_alias,
|
||||
err.to_string(),
|
||||
),
|
||||
log.as_ref(),
|
||||
&log,
|
||||
);
|
||||
}
|
||||
Err(err) => tracing::warn!(?err, "registering webhook"),
|
||||
}
|
||||
}
|
||||
.in_current_span()
|
||||
.into_actor(self)
|
||||
.wait(ctx);
|
||||
tracing::debug!("registering webhook done");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,14 +14,14 @@ impl Handler<actor::messages::ValidateRepo> for actor::RepoActor {
|
|||
msg: actor::messages::ValidateRepo,
|
||||
ctx: &mut Self::Context,
|
||||
) -> Self::Result {
|
||||
actor::logger(self.log.as_ref(), "start: ValidateRepo");
|
||||
actor::logger(&self.log, "start: ValidateRepo");
|
||||
|
||||
// Message Token - make sure we are only triggered for the latest/current token
|
||||
match self.token_status(msg.unwrap()) {
|
||||
TokenStatus::Current => {} // do nothing
|
||||
TokenStatus::Expired => {
|
||||
actor::logger(
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
format!("discarded: old message token: {}", self.message_token),
|
||||
);
|
||||
return; // message is expired
|
||||
|
@ -29,27 +29,24 @@ impl Handler<actor::messages::ValidateRepo> for actor::RepoActor {
|
|||
TokenStatus::New(message_token) => {
|
||||
self.message_token = message_token;
|
||||
actor::logger(
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
format!("new message token: {}", self.message_token),
|
||||
);
|
||||
}
|
||||
}
|
||||
actor::logger(
|
||||
self.log.as_ref(),
|
||||
format!("accepted token: {}", self.message_token),
|
||||
);
|
||||
actor::logger(&self.log, format!("accepted token: {}", self.message_token));
|
||||
|
||||
// Repository positions
|
||||
let Some(ref open_repository) = self.open_repository else {
|
||||
actor::logger(self.log.as_ref(), "no open repository");
|
||||
actor::logger(&self.log, "no open repository");
|
||||
return;
|
||||
};
|
||||
actor::logger(self.log.as_ref(), "have open repository");
|
||||
actor::logger(&self.log, "have open repository");
|
||||
let Some(repo_config) = self.repo_details.repo_config.clone() else {
|
||||
actor::logger(self.log.as_ref(), "no repo config");
|
||||
actor::logger(&self.log, "no repo config");
|
||||
return;
|
||||
};
|
||||
actor::logger(self.log.as_ref(), "have repo config");
|
||||
actor::logger(&self.log, "have repo config");
|
||||
|
||||
match git::validation::positions::validate_positions(
|
||||
&**open_repository,
|
||||
|
@ -67,50 +64,46 @@ impl Handler<actor::messages::ValidateRepo> for actor::RepoActor {
|
|||
actor::do_send(
|
||||
ctx.address(),
|
||||
actor::messages::CheckCIStatus::new(next),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
} else if next != dev {
|
||||
actor::do_send(
|
||||
ctx.address(),
|
||||
actor::messages::AdvanceNext::new((next, dev_commit_history)),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
)
|
||||
} else {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
Err(git::validation::positions::Error::Retryable(message)) => {
|
||||
actor::logger(self.log.as_ref(), message);
|
||||
actor::logger(&self.log, message);
|
||||
let addr = ctx.address();
|
||||
let message_token = self.message_token;
|
||||
let sleep_duration = self.sleep_duration;
|
||||
let log = self.log.clone();
|
||||
async move {
|
||||
tracing::debug!("sleeping before retrying...");
|
||||
actor::logger(log.as_ref(), "before sleep");
|
||||
actor::logger(&log, "before sleep");
|
||||
tokio::time::sleep(sleep_duration).await;
|
||||
actor::logger(log.as_ref(), "after sleep");
|
||||
actor::logger(&log, "after sleep");
|
||||
actor::do_send(
|
||||
addr,
|
||||
actor::messages::ValidateRepo::new(message_token),
|
||||
log.as_ref(),
|
||||
&log,
|
||||
);
|
||||
}
|
||||
.in_current_span()
|
||||
.into_actor(self)
|
||||
.wait(ctx);
|
||||
}
|
||||
Err(git::validation::positions::Error::UserIntervention(user_notification)) => {
|
||||
actor::notify_user(
|
||||
self.notify_user_recipient.as_ref(),
|
||||
user_notification,
|
||||
self.log.as_ref(),
|
||||
)
|
||||
}
|
||||
Err(git::validation::positions::Error::NonRetryable(message)) => {
|
||||
actor::logger(self.log.as_ref(), message);
|
||||
Err(git::validation::positions::Error::NonRetryable(message))
|
||||
| Err(git::validation::positions::Error::UserIntervention(message)) => {
|
||||
actor::logger(&self.log, message);
|
||||
}
|
||||
}
|
||||
|
||||
tracing::debug!("Handler: ValidateRepo: finish");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,30 +17,23 @@ impl Handler<WebhookNotification> for actor::RepoActor {
|
|||
#[tracing::instrument(name = "RepoActor::WebhookMessage", skip_all, fields(token = %self.message_token, repo = %self.repo_details))]
|
||||
fn handle(&mut self, msg: WebhookNotification, ctx: &mut Self::Context) -> Self::Result {
|
||||
let Some(config) = &self.repo_details.repo_config else {
|
||||
actor::logger(self.log.as_ref(), "server has no repo config");
|
||||
actor::logger(&self.log, "server has no repo config");
|
||||
warn!("No repo config");
|
||||
return;
|
||||
};
|
||||
if validate_notification(
|
||||
&msg,
|
||||
self.webhook_auth.as_ref(),
|
||||
&*self.forge,
|
||||
self.log.as_ref(),
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
if validate_notification(&msg, &self.webhook_auth, &*self.forge, &self.log).is_err() {
|
||||
return;
|
||||
}
|
||||
let body = msg.body();
|
||||
match self.forge.parse_webhook_body(body) {
|
||||
Err(err) => {
|
||||
actor::logger(self.log.as_ref(), "message parse error - not a push");
|
||||
actor::logger(&self.log, "message parse error - not a push");
|
||||
warn!(?err, "Not a 'push'");
|
||||
return;
|
||||
}
|
||||
Ok(push) => match push.branch(config.branches()) {
|
||||
None => {
|
||||
actor::logger(self.log.as_ref(), "unknown branch");
|
||||
actor::logger(&self.log, "unknown branch");
|
||||
warn!(
|
||||
?push,
|
||||
"Unrecognised branch, we should be filtering to only the ones we want"
|
||||
|
@ -52,7 +45,7 @@ impl Handler<WebhookNotification> for actor::RepoActor {
|
|||
push,
|
||||
config.branches().main(),
|
||||
&mut self.last_main_commit,
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
|
@ -64,7 +57,7 @@ impl Handler<WebhookNotification> for actor::RepoActor {
|
|||
push,
|
||||
config.branches().next(),
|
||||
&mut self.last_next_commit,
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
|
@ -76,7 +69,7 @@ impl Handler<WebhookNotification> for actor::RepoActor {
|
|||
push,
|
||||
config.branches().dev(),
|
||||
&mut self.last_dev_commit,
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
)
|
||||
.is_err()
|
||||
{
|
||||
|
@ -93,16 +86,16 @@ impl Handler<WebhookNotification> for actor::RepoActor {
|
|||
actor::do_send(
|
||||
ctx.address(),
|
||||
actor::messages::ValidateRepo::new(message_token),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_notification(
|
||||
msg: &WebhookNotification,
|
||||
webhook_auth: Option<&WebhookAuth>,
|
||||
webhook_auth: &Option<WebhookAuth>,
|
||||
forge: &dyn ForgeLike,
|
||||
log: Option<&RepoActorLog>,
|
||||
log: &Option<RepoActorLog>,
|
||||
) -> Result<(), ()> {
|
||||
let Some(expected_authorization) = webhook_auth else {
|
||||
actor::logger(log, "server has no auth token");
|
||||
|
@ -129,7 +122,7 @@ fn handle_push(
|
|||
push: Push,
|
||||
branch: BranchName,
|
||||
last_commit: &mut Option<Commit>,
|
||||
log: Option<&RepoActorLog>,
|
||||
log: &Option<RepoActorLog>,
|
||||
) -> Result<(), ()> {
|
||||
actor::logger(log, "message is for dev branch");
|
||||
let commit = git::Commit::from(push);
|
||||
|
|
|
@ -16,7 +16,7 @@ impl Handler<actor::messages::WebhookRegistered> for actor::RepoActor {
|
|||
actor::do_send(
|
||||
ctx.address(),
|
||||
actor::messages::ValidateRepo::new(self.message_token),
|
||||
self.log.as_ref(),
|
||||
&self.log,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ mod branch;
|
|||
pub mod handlers;
|
||||
mod load;
|
||||
pub mod messages;
|
||||
mod user_notification;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
@ -12,8 +13,9 @@ use actix::prelude::*;
|
|||
|
||||
use derive_more::Deref;
|
||||
use git_next_config as config;
|
||||
use git_next_git::{self as git, UserNotification};
|
||||
use git_next_git as git;
|
||||
use messages::NotifyUser;
|
||||
pub use user_notification::UserNotification;
|
||||
|
||||
use kxio::network::Network;
|
||||
use tracing::{info, warn, Instrument};
|
||||
|
@ -83,6 +85,12 @@ impl RepoActor {
|
|||
notify_user_recipient,
|
||||
}
|
||||
}
|
||||
|
||||
fn notify_user(&mut self, user_notification: UserNotification) {
|
||||
if let Some(recipient) = &self.notify_user_recipient {
|
||||
recipient.do_send(NotifyUser::from(user_notification));
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Actor for RepoActor {
|
||||
type Context = Context<Self>;
|
||||
|
@ -110,8 +118,12 @@ impl Actor for RepoActor {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn delay_send<M>(addr: Addr<RepoActor>, delay: Duration, msg: M, log: Option<&RepoActorLog>)
|
||||
where
|
||||
pub fn delay_send<M>(
|
||||
addr: Addr<RepoActor>,
|
||||
delay: Duration,
|
||||
msg: M,
|
||||
log: &Option<crate::RepoActorLog>,
|
||||
) where
|
||||
M: actix::Message + Send + 'static + std::fmt::Debug,
|
||||
RepoActor: actix::Handler<M>,
|
||||
<M as actix::Message>::Result: Send,
|
||||
|
@ -123,7 +135,7 @@ where
|
|||
do_send(addr, msg, log)
|
||||
}
|
||||
|
||||
pub fn do_send<M>(_addr: Addr<RepoActor>, msg: M, log: Option<&RepoActorLog>)
|
||||
pub fn do_send<M>(_addr: Addr<RepoActor>, msg: M, log: &Option<crate::RepoActorLog>)
|
||||
where
|
||||
M: actix::Message + Send + 'static + std::fmt::Debug,
|
||||
RepoActor: actix::Handler<M>,
|
||||
|
@ -136,23 +148,10 @@ where
|
|||
_addr.do_send(msg)
|
||||
}
|
||||
|
||||
pub fn logger(log: Option<&RepoActorLog>, message: impl Into<String>) {
|
||||
pub fn logger(log: &Option<crate::RepoActorLog>, message: impl Into<String>) {
|
||||
if let Some(log) = log {
|
||||
let message: String = message.into();
|
||||
tracing::debug!(message);
|
||||
let _ = log.write().map(|mut l| l.push(message));
|
||||
}
|
||||
}
|
||||
pub fn notify_user(
|
||||
recipient: Option<&Recipient<NotifyUser>>,
|
||||
user_notification: UserNotification,
|
||||
log: Option<&RepoActorLog>,
|
||||
) {
|
||||
let msg = NotifyUser::from(user_notification);
|
||||
let log_message = format!("send: {:?}", msg);
|
||||
tracing::debug!(log_message);
|
||||
logger(log, log_message);
|
||||
if let Some(recipient) = &recipient {
|
||||
recipient.do_send(msg);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,11 +2,12 @@
|
|||
use config::newtype;
|
||||
use derive_more::Display;
|
||||
|
||||
use git::UserNotification;
|
||||
use git_next_actor_macros::message;
|
||||
use git_next_config as config;
|
||||
use git_next_git as git;
|
||||
|
||||
use crate::UserNotification;
|
||||
|
||||
message!(LoadConfigFromRepo: "Request to load the `git-next.toml` from the git repo.");
|
||||
message!(CloneRepo: "Request to clone (or open) the git repo.");
|
||||
message!(ReceiveRepoConfig: config::RepoConfig: r#"Notification that the `git-next.toml` file has been loaded from the repo and parsed.
|
||||
|
|
|
@ -45,12 +45,16 @@ async fn when_read_file_ok_should_send_config_loaded() -> TestResult {
|
|||
|
||||
//then
|
||||
tracing::debug!(?log, "");
|
||||
log.require_message_containing("send: ReceiveRepoConfig")?;
|
||||
log.no_message_contains("send: NotifyUsers")?;
|
||||
log.read().map_err(|e| e.to_string()).map(|l| {
|
||||
assert!(l
|
||||
.iter()
|
||||
.any(|message| message.contains("send: LoadedConfig")))
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[actix::test]
|
||||
#[ignore] //TODO: (#95) should notify user
|
||||
async fn when_read_file_err_should_notify_user() -> TestResult {
|
||||
//given
|
||||
let fs = given::a_filesystem();
|
||||
|
@ -76,7 +80,13 @@ async fn when_read_file_err_should_notify_user() -> TestResult {
|
|||
|
||||
//then
|
||||
tracing::debug!(?log, "");
|
||||
log.require_message_containing("send: NotifyUser")?;
|
||||
log.no_message_contains("send: ReceiveRepoConfig")?;
|
||||
log.read().map_err(|e| e.to_string()).map(|l| {
|
||||
assert!(l.iter().any(|message| message.contains("send: NotifyUser")));
|
||||
assert!(
|
||||
!l.iter()
|
||||
.any(|message| message.contains("send: LoadedConfig")),
|
||||
"not send LoadedConfig"
|
||||
);
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -88,6 +88,7 @@ async fn when_fail_should_recheck_after_delay() -> TestResult {
|
|||
}
|
||||
|
||||
#[test_log::test(actix::test)]
|
||||
#[ignore] //TODO: (#95) should notify user
|
||||
async fn when_fail_should_notify_user() -> TestResult {
|
||||
//given
|
||||
let fs = given::a_filesystem();
|
||||
|
|
|
@ -36,6 +36,7 @@ async fn when_registered_ok_should_send_webhook_registered() -> TestResult {
|
|||
}
|
||||
|
||||
#[actix::test]
|
||||
#[ignore] //TODO: (#95) should notify user
|
||||
async fn when_registered_error_should_send_notify_user() -> TestResult {
|
||||
//given
|
||||
let fs = given::a_filesystem();
|
||||
|
|
|
@ -279,52 +279,7 @@ async fn repo_with_dev_ahead_of_next_should_advance_next() -> TestResult {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[test_log::test(actix::test)]
|
||||
async fn repo_with_dev_not_ahead_of_main_should_notify_user() -> TestResult {
|
||||
//given
|
||||
let fs = given::a_filesystem();
|
||||
let (mut open_repository, repo_details) = given::an_open_repository(&fs);
|
||||
#[allow(clippy::unwrap_used)]
|
||||
let repo_config = repo_details.repo_config.clone().unwrap();
|
||||
// Validate repo branches
|
||||
expect::fetch_ok(&mut open_repository);
|
||||
let branches = repo_config.branches();
|
||||
// commit_log main
|
||||
let main_commit = expect::main_commit_log(&mut open_repository, branches.main());
|
||||
// next - on main
|
||||
let next_commit = main_commit.clone();
|
||||
let next_branch_log = vec![next_commit.clone(), given::a_commit()];
|
||||
// dev - not ahead of next
|
||||
let dev_commit = given::a_named_commit("dev");
|
||||
let dev_branch_log = vec![dev_commit];
|
||||
// commit_log next
|
||||
open_repository
|
||||
.expect_commit_log()
|
||||
.times(1)
|
||||
.with(eq(branches.next()), eq([main_commit.clone()]))
|
||||
.return_once(move |_, _| Ok(next_branch_log));
|
||||
// commit_log dev
|
||||
let dev_commit_log = dev_branch_log.clone();
|
||||
open_repository
|
||||
.expect_commit_log()
|
||||
.times(1)
|
||||
.with(eq(branches.dev()), eq([main_commit]))
|
||||
.return_once(move |_, _| Ok(dev_commit_log));
|
||||
|
||||
//when
|
||||
let (addr, log) = when::start_actor_with_open_repository(
|
||||
Box::new(open_repository),
|
||||
repo_details,
|
||||
given::a_forge(),
|
||||
);
|
||||
addr.send(crate::messages::ValidateRepo::new(MessageToken::default()))
|
||||
.await?;
|
||||
System::current().stop();
|
||||
|
||||
//then
|
||||
log.require_message_containing("send: NotifyUser")?;
|
||||
Ok(())
|
||||
}
|
||||
// TODO: (#95) test: repo with dev not a child of main should notify user
|
||||
|
||||
#[test_log::test(actix::test)]
|
||||
async fn should_accept_message_with_current_token() -> TestResult {
|
||||
|
@ -427,7 +382,7 @@ async fn should_send_validate_repo_when_retryable_error() -> TestResult {
|
|||
}
|
||||
|
||||
#[test_log::test(actix::test)]
|
||||
async fn should_send_notify_user_when_non_retryable_error() -> TestResult {
|
||||
async fn should_send_nothing_when_non_retryable_error() -> TestResult {
|
||||
//given
|
||||
let fs = given::a_filesystem();
|
||||
let (mut open_repository, repo_details) = given::an_open_repository(&fs);
|
||||
|
@ -469,6 +424,6 @@ async fn should_send_notify_user_when_non_retryable_error() -> TestResult {
|
|||
|
||||
//then
|
||||
log.require_message_containing("accepted token")?;
|
||||
log.require_message_containing("send: NotifyUser")?;
|
||||
log.no_message_contains("send:")?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
7
crates/repo-actor/src/user_notification.rs
Normal file
7
crates/repo-actor/src/user_notification.rs
Normal file
|
@ -0,0 +1,7 @@
|
|||
use git_next_git::Commit;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum UserNotification {
|
||||
CICheckFailed(Commit),
|
||||
RepoConfigLoadFailure(String),
|
||||
}
|
Loading…
Reference in a new issue