From 858fe9984a1ba4b4945e0b4a9a8c19a56b7e8100 Mon Sep 17 00:00:00 2001 From: Jacob Morgan Date: Thu, 4 Apr 2024 18:03:53 +0200 Subject: [PATCH] Notify on new commit via ntfy --- src/gitops/gitops.rs | 3 +++ src/notifications/ntfy.rs | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/gitops/gitops.rs b/src/gitops/gitops.rs index 87e1012..309b717 100644 --- a/src/gitops/gitops.rs +++ b/src/gitops/gitops.rs @@ -10,6 +10,7 @@ use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; use std::path::Path; use k8s_openapi::api::apps::v1::{Deployment, StatefulSet}; +use crate::notifications::ntfy::notify_commit; fn delete_local_repo() -> Result<(), std::io::Error> { let local_path = Path::new("/tmp/repos/"); @@ -226,6 +227,8 @@ pub async fn run_git_operations(workload: Workload) -> Result<(), Box stage_changes(&repo)?; commit_changes(&repo, &commit_message, &commit_name, &commit_email)?; push_changes(&repo, &access_token)?; + notify_commit(&workload).await?; + } Ok(()) diff --git a/src/notifications/ntfy.rs b/src/notifications/ntfy.rs index 0ec85f3..30f0eee 100644 --- a/src/notifications/ntfy.rs +++ b/src/notifications/ntfy.rs @@ -3,6 +3,50 @@ use crate::models::models::Workload; use ntfy::payload::{Action, ActionType}; use ntfy::{Auth, Dispatcher, NtfyError, Payload, Priority}; + +pub async fn notify_commit(workload: &Workload) -> Result<(), NtfyError> { + //get settings + let settings = Settings::new().unwrap(); + let token = settings.notifications.ntfy.token; + let topic = settings.notifications.ntfy.topic; + let url = settings.notifications.ntfy.url; + + let dispatcher = Dispatcher::builder(&url) + .credentials(Auth::new("", &token)) // Add optional credentials + .build()?; // Build dispatcher + + //let action = Action::new( + // ActionType::Http, + // "Acknowledge", + // Url::parse(&url)?, + //); + + //make message for payload about new container update + let message = format!( + "Deployment {} has been updated to version {}", + workload.name, workload.latest_version + ); + + let payload = Payload::new(&topic) + .message(message) // Add optional message + .title(&workload.name) // Add optiona title + .tags(["Update"]) // Add optional tags + .priority(Priority::Default) // Edit priority + //.actions([action]) // Add optional actions + //.click(Url::parse("https://example.com")?) // Add optional clickable url + //.attach(Url::parse("https://example.com/file.jpg")?) // Add optional url attachment + //.delay(Local::now() + Duration::minutes(1)) // Add optional delay + .markdown(true); // Use markdown + + match dispatcher.send(&payload).await { + Ok(_) => log::info!("Payload sent successfully."), + Err(e) => log::error!("Failed to send payload: {}", e), + } + log::info!("Notification sent"); + Ok(()) +} + + pub async fn send_notification(workload: &Workload) -> Result<(), NtfyError> { //get settings let settings = Settings::new().unwrap();