2024-07-15 22:09:52 +02:00
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use lettre::message::header::ContentType;
|
|
|
|
use lettre::message::Mailbox;
|
|
|
|
use lettre::transport::smtp::authentication::Credentials;
|
|
|
|
use lettre::{Message, SmtpTransport, Transport};
|
|
|
|
use std::env;
|
2024-07-18 01:03:05 +02:00
|
|
|
use urlencoding::encode;
|
|
|
|
|
|
|
|
use crate::CONFIG;
|
2024-07-15 22:09:52 +02:00
|
|
|
|
|
|
|
pub fn send_notification(
|
|
|
|
recipient: &str,
|
|
|
|
branches: &HashSet<String>,
|
|
|
|
pr_number: &str,
|
|
|
|
pr_title: &str,
|
|
|
|
last: bool,
|
|
|
|
) -> Result<()> {
|
|
|
|
let mut body = format!(
|
2024-07-19 23:21:54 +02:00
|
|
|
"This is your friendly neighbourhood pr-tracker.<br>
|
|
|
|
PR <a href=\"https://github.com/NixOS/nixpkgs/pull/{pr_number}\">#{pr_number}</a>\
|
|
|
|
(\"{pr_title}\") has reached:<br>
|
|
|
|
{:#?}<br>",
|
2024-07-15 22:09:52 +02:00
|
|
|
branches
|
|
|
|
);
|
|
|
|
if last {
|
2024-07-19 23:21:54 +02:00
|
|
|
body += "This is the last update you will get for this pr.<br>\
|
|
|
|
Thx for using this service<br>\
|
2024-07-15 22:09:52 +02:00
|
|
|
Goodbye";
|
2024-07-18 01:03:05 +02:00
|
|
|
} else {
|
|
|
|
body += &format!(
|
2024-07-19 23:21:54 +02:00
|
|
|
"<a href=\"{}/unsubscribe?pr={pr_number}&email={}\">Unsubscribe from this PR</a><br>",
|
2024-07-19 22:24:40 +02:00
|
|
|
&CONFIG.url,
|
|
|
|
encode(recipient)
|
|
|
|
);
|
|
|
|
body += &format!(
|
|
|
|
"<a href=\"{}/unsubscribe?email={}\">Unsubscribe from all PRs</a>",
|
2024-07-18 01:03:05 +02:00
|
|
|
&CONFIG.url,
|
|
|
|
encode(recipient)
|
|
|
|
);
|
2024-07-15 22:09:52 +02:00
|
|
|
}
|
2024-07-26 21:27:43 +02:00
|
|
|
let sending_address = &CONFIG.email_address;
|
|
|
|
let sending_user = match &CONFIG.email_user {
|
|
|
|
Some(address) => address,
|
|
|
|
_ => &sending_address,
|
2024-07-15 22:09:52 +02:00
|
|
|
};
|
2024-07-26 21:27:43 +02:00
|
|
|
let sending_passwd = env::var("PR_TRACKER_MAIL_PASSWD")?;
|
2024-07-15 22:09:52 +02:00
|
|
|
|
2024-07-26 21:27:43 +02:00
|
|
|
let sending_server = CONFIG.email_server.as_ref();
|
2024-07-15 22:09:52 +02:00
|
|
|
|
|
|
|
let email = Message::builder()
|
2024-07-19 22:24:40 +02:00
|
|
|
.from(format!("PR-Tracker <{}>", sending_address).parse().unwrap())
|
|
|
|
.to(Mailbox::new(None, recipient.parse().unwrap()))
|
2024-07-15 22:09:52 +02:00
|
|
|
.subject(format!(
|
|
|
|
"PR-tracker: {pr_number}: {pr_title} has reached {:?}",
|
|
|
|
branches
|
|
|
|
))
|
2024-07-19 22:57:59 +02:00
|
|
|
.header(ContentType::TEXT_HTML)
|
2024-07-19 22:24:40 +02:00
|
|
|
.body(body)
|
|
|
|
.unwrap();
|
2024-07-15 22:09:52 +02:00
|
|
|
|
|
|
|
let creds = Credentials::new(sending_user.to_string(), sending_passwd.to_string());
|
|
|
|
|
|
|
|
// Open a remote connection to gmail
|
2024-07-19 22:24:40 +02:00
|
|
|
let mailer = SmtpTransport::relay(&sending_server)
|
|
|
|
.unwrap()
|
2024-07-15 22:09:52 +02:00
|
|
|
.credentials(creds)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Send the email
|
2024-07-19 22:24:40 +02:00
|
|
|
mailer.send(&email).unwrap();
|
2024-07-15 22:09:52 +02:00
|
|
|
|
|
|
|
println!("Email sent successfully!");
|
|
|
|
Ok(())
|
|
|
|
}
|