Skip to content

Commit

Permalink
Notify on new commit via ntfy
Browse files Browse the repository at this point in the history
  • Loading branch information
slackspace-io committed Apr 4, 2024
1 parent 4aa7ec1 commit 858fe99
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/gitops/gitops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/");
Expand Down Expand Up @@ -226,6 +227,8 @@ pub async fn run_git_operations(workload: Workload) -> Result<(), Box<dyn Error>
stage_changes(&repo)?;
commit_changes(&repo, &commit_message, &commit_name, &commit_email)?;
push_changes(&repo, &access_token)?;
notify_commit(&workload).await?;

}

Ok(())
Expand Down
44 changes: 44 additions & 0 deletions src/notifications/ntfy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 858fe99

Please sign in to comment.