Skip to content

Commit

Permalink
chore: slack notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Nov 11, 2024
1 parent e2ce4be commit 95fccc5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 44 deletions.
4 changes: 2 additions & 2 deletions packages/deploy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ andromeda-cw20 = { path = "../../contracts/fungible-tokens/andromeda-cw20" }
andromeda-cw721 = { path = "../../contracts/non-fungible-tokens/andromeda-cw721" }

andromeda-std = { workspace = true }
env_logger = "0.11.5" # or whatever the latest version is
log = "0.4" # env_logger depends on the log crate
env_logger = "0.11.5"
log = "0.4"
dotenv = "0.15.0"
reqwest = { version = "0.11", features = ["json", "blocking"] }
serde_json = "1.0"
Expand Down
39 changes: 29 additions & 10 deletions packages/deploy/src/adodb.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::slack::send_notification;
use crate::{chains::get_chain, contracts::all_contracts, error::DeployError};
use adodb::ExecuteMsgFns;
use andromeda_adodb::ADODBContract;
use andromeda_kernel::KernelContract;
Expand All @@ -6,17 +8,15 @@ use cw_orch::prelude::*;
use cw_orch_daemon::DaemonBuilder;
use kernel::QueryMsgFns;

use crate::{chains::get_chain, contracts::all_contracts, error::DeployError};

pub fn deploy(
chain: String,
kernel_address: String,
contracts: Option<Vec<String>>,
) -> Result<(), DeployError> {
let chain = get_chain(chain);
let daemon = DaemonBuilder::new(chain).build().unwrap();
let daemon = DaemonBuilder::new(chain.clone()).build().unwrap();
let kernel = KernelContract::new(daemon.clone());
kernel.set_address(&Addr::unchecked(kernel_address));
kernel.set_address(&Addr::unchecked(kernel_address.clone()));

let adodb = ADODBContract::new(daemon.clone());
let adodb_addr = kernel.key_address("adodb")?;
Expand All @@ -25,12 +25,31 @@ pub fn deploy(
let all_contracts = all_contracts();

let contracts_to_deploy = contracts.unwrap_or_default();
contracts_to_deploy.iter().for_each(|name| {
let contract = all_contracts.iter().find(|(n, _, _)| n == name);
if contract.is_none() {
log::warn!("Contract {} not found", name);
}
});
let invalid_contracts = contracts_to_deploy
.iter()
.filter(|name| !all_contracts.iter().any(|(n, _, _)| &n == name))
.cloned()
.collect::<Vec<String>>();
if !invalid_contracts.is_empty() {
let error_message = format!(
"⚠️ *Deployment Warning*\n```\n| Invalid Contracts | {} |\n```",
invalid_contracts.join(", ")
);
send_notification(&error_message).unwrap();
}

let valid_contracts = contracts_to_deploy
.iter()
.filter(|name| all_contracts.iter().any(|(n, _, _)| &n == name))
.cloned()
.collect::<Vec<String>>();

let deployment_msg = format!(
"🚀 *ADO Library Deployment Started*\n```\n| Chain | {} |\n| Kernel Address | {} |\n| Contracts | {} |```",
chain.chain_id, kernel_address, valid_contracts.join(", ")
);
send_notification(&deployment_msg).unwrap();

for (name, version, upload) in all_contracts {
if !contracts_to_deploy.is_empty() && !contracts_to_deploy.contains(&name) {
continue;
Expand Down
1 change: 1 addition & 0 deletions packages/deploy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod contract_interface;
pub mod contracts;
pub mod error;
pub mod os;
pub mod slack;
36 changes: 4 additions & 32 deletions packages/deploy/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
use cw_orch::core::serde_json;
use reqwest::blocking::Client;
use andromeda_deploy::slack::send_notification;
use std::env;

use andromeda_deploy::adodb;
use andromeda_deploy::os;
use chrono::Local;
use dotenv::dotenv;

fn send_slack_notification(message: &str) -> Result<(), Box<dyn std::error::Error>> {
let webhook_url = env::var("SLACK_WEBHOOK_URL").ok();
if webhook_url.is_none() {
return Ok(());
}

let payload = serde_json::json!({
"text": message,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message
}
}
]
});

Client::new()
.post(webhook_url.unwrap())
.json(&payload)
.send()?;

Ok(())
}

fn main() {
env_logger::init();
dotenv().ok();
Expand All @@ -50,7 +22,7 @@ fn main() {
kernel_address.as_deref().unwrap_or("Not provided")
);

if let Err(e) = send_slack_notification(&start_message) {
if let Err(e) = send_notification(&start_message) {
eprintln!("Failed to send Slack notification: {}", e);
}

Expand All @@ -68,7 +40,7 @@ fn main() {
e
);

if let Err(notification_error) = send_slack_notification(&error_message) {
if let Err(notification_error) = send_notification(&error_message) {
eprintln!("Failed to send Slack notification: {}", notification_error);
}
std::process::exit(1);
Expand All @@ -85,7 +57,7 @@ fn main() {
kernel_address.as_ref().unwrap()
);

if let Err(e) = send_slack_notification(&completion_message) {
if let Err(e) = send_notification(&completion_message) {
eprintln!("Failed to send Slack notification: {}", e);
}
}
Expand Down
29 changes: 29 additions & 0 deletions packages/deploy/src/slack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use reqwest::blocking::Client;
use std::env;

pub fn send_notification(message: &str) -> Result<(), Box<dyn std::error::Error>> {
let webhook_url = env::var("SLACK_WEBHOOK_URL").ok();
if webhook_url.is_none() {
return Ok(());
}

let payload = serde_json::json!({
"text": message,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message
}
}
]
});

Client::new()
.post(webhook_url.unwrap())
.json(&payload)
.send()?;

Ok(())
}

0 comments on commit 95fccc5

Please sign in to comment.