git-next/crates/cli/src/alerts/mod.rs

120 lines
3.6 KiB
Rust
Raw Normal View History

use std::ops::Deref as _;
//
use actix::prelude::*;
use derive_more::derive::Constructor;
use git_next_core::{git::UserNotification, server::Shout};
pub use history::History;
use messages::NotifyUser;
mod desktop;
mod email;
mod handlers;
mod history;
pub mod messages;
mod webhook;
#[derive(Debug, Constructor)]
pub struct AlertsActor {
shout: Option<Shout>, // config for sending alerts to users
#[allow(dead_code)] // TODO (#128) Prevent duplicate user notifications
history: History, // record of alerts sent recently (e.g. 24 hours)
net: kxio::network::Network,
}
impl Actor for AlertsActor {
type Context = Context<Self>;
}
fn short_message(msg: &NotifyUser) -> String {
let tail = match msg.deref() {
UserNotification::CICheckFailed {
forge_alias,
repo_alias,
commit,
} => format!("CI Check Failed: {forge_alias}/{repo_alias}: {commit}"),
UserNotification::RepoConfigLoadFailure {
forge_alias,
repo_alias,
reason: _,
} => format!("Invalid Repo Config: {forge_alias}/{repo_alias}"),
UserNotification::WebhookRegistration {
forge_alias,
repo_alias,
reason: _,
} => format!("Failed Webhook Registration: {forge_alias}/{repo_alias}"),
UserNotification::DevNotBasedOnMain {
forge_alias,
repo_alias,
dev_branch: _,
main_branch: _,
dev_commit: _,
main_commit: _,
} => format!("Dev not based on Main: {forge_alias}/{repo_alias}"),
};
format!("[git-next] {tail}")
}
fn full_message(msg: &NotifyUser) -> String {
match msg.deref() {
UserNotification::CICheckFailed {
forge_alias,
repo_alias,
commit,
} => {
let sha = commit.sha();
let message = commit.message();
[
"CI Checks had Failed".to_string(),
format!("Forge: {forge_alias}\nRepo : {repo_alias}"),
format!("Commit:\n - {sha}\n - {message}"),
]
.join("\n\n")
}
UserNotification::RepoConfigLoadFailure {
forge_alias,
repo_alias,
reason,
} => [
"Failed to read or parse the .git-next.toml file from repo".to_string(),
format!(" - {reason}"),
format!("Forge: {forge_alias}\nRepo : {repo_alias}"),
]
.join("\n\n"),
UserNotification::WebhookRegistration {
forge_alias,
repo_alias,
reason,
} => [
"Failed to register webhook with the forge".to_string(),
format!(" - {reason}"),
format!("Forge: {forge_alias}\nRepo : {repo_alias}"),
]
.join("\n\n"),
UserNotification::DevNotBasedOnMain {
forge_alias,
repo_alias,
dev_branch,
main_branch,
dev_commit,
main_commit,
} => {
let dev_sha = dev_commit.sha();
let dev_message = dev_commit.message();
let main_sha = main_commit.sha();
let main_message = main_commit.message();
[
format!("The branch '{dev_branch}' is not based on the branch '{main_branch}'."),
format!("TODO: Rebase '{dev_branch}' onto '{main_branch}'."),
format!("Forge: {forge_alias}\nRepo : {repo_alias}"),
format!("{dev_branch}:\n - {dev_sha}\n - {dev_message}"),
format!("{main_branch}:\n - {main_sha}\n - {main_message}"),
]
.join("\n\n")
}
}
}