use crate::config::{Notifications, NotifyTarget, TargetType, WebhookFormat};
use crate::forge::{ForgeKind, RepoId, Target};
use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::fmt::Write as _;
use std::time::Duration;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Kind {
NeedsApproval,
Failed,
Done,
NewAuthor,
}
#[derive(Clone, Debug, Serialize)]
pub struct Event {
pub kind: Kind,
pub title: String,
pub summary: String,
pub repo: Option<String>,
pub number: Option<u64>,
pub pr_title: Option<String>,
pub url: Option<String>,
pub job: Option<i64>,
}
impl Event {
pub fn new(kind: Kind, title: &str, summary: &str) -> Self {
Self { kind, title: title.into(), summary: summary.into(), repo: None, number: None, pr_title: None, url: None, job: None }
}
pub fn about(kind: Kind, repo: &RepoId, target: Target, number: u64, summary: &str) -> Self {
Self {
repo: Some(repo.path.clone()),
number: Some(number),
url: Some(web_url(repo, target, number)),
..Self::new(kind, &format!("{repo}#{number}"), summary)
}
}
}
pub fn web_url(repo: &RepoId, target: Target, n: u64) -> String {
let what = match (repo.forge, target) {
(ForgeKind::GitHub, Target::Pr) => "pull",
(ForgeKind::GitLab, Target::Pr) => "-/merge_requests",
(ForgeKind::GitLab, Target::Issue) => "-/issues",
(ForgeKind::Forgejo, Target::Pr) => "pulls",
(ForgeKind::GitHub | ForgeKind::Forgejo, Target::Issue) => "issues",
};
format!("https://{}/{}/{what}/{n}", repo.host, repo.path)
}
fn desktop_on(n: &Notifications, kind: Kind) -> bool {
match kind {
Kind::NeedsApproval => n.needs_approval,
Kind::Failed => n.failed,
Kind::Done => n.done,
Kind::NewAuthor => n.new_author,
}
}
pub fn wants(t: &NotifyTarget, kind: Kind) -> bool {
t.kinds.is_empty() || t.kinds.contains(&kind)
}
pub fn route<'a>(n: &'a Notifications, only: Option<&[String]>, kind: Kind) -> (bool, Vec<&'a NotifyTarget>) {
let picked = |name: &str| only.is_none_or(|o| o.iter().any(|x| x == name));
(picked("desktop") && desktop_on(n, kind), n.targets.iter().filter(|t| picked(&t.name) && wants(t, kind)).collect())
}
pub fn send(n: &Notifications, only: Option<&[String]>, ev: &Event) {
if std::env::var_os("NIGHTPORTER_NO_NOTIFY").is_some() {
return;
}
let (desk, targets) = route(n, only, ev.kind);
if desk {
let ev = ev.clone();
std::thread::spawn(move || desktop(&ev));
}
let targets: Vec<NotifyTarget> = targets.into_iter().cloned().collect();
if targets.is_empty() {
return;
}
let Ok(rt) = tokio::runtime::Handle::try_current() else {
tracing::debug!("no async runtime; notification targets skipped");
return;
};
for t in targets {
let ev = ev.clone();
rt.spawn(async move {
if let Err(e) = deliver(&t, &ev).await {
tracing::warn!("notification target {:?}: {e:#}", t.name);
}
});
}
}
fn popup(ev: &Event) -> notify_rust::Notification {
let body = match ev.pr_title.as_deref().filter(|t| !t.is_empty()) {
Some(t) => format!("{t}\n{}", ev.summary),
None => ev.summary.clone(),
};
let mut n = notify_rust::Notification::new();
n.summary(&format!("Night Porter: {}", ev.title)).body(&body).appname("Night Porter");
n
}
fn desktop(ev: &Event) {
#[allow(unused_mut, reason = "only mutated on Linux")]
let mut n = popup(ev);
#[cfg(all(unix, not(target_os = "macos")))]
if ev.url.is_some() {
n.action("default", "Open");
}
match n.show() {
Err(e) => tracing::debug!("notification failed: {e}"),
#[cfg(all(unix, not(target_os = "macos")))]
Ok(h) => {
if let Some(url) = ev.url.clone() {
h.wait_for_action(|a| {
if a == "default" {
let _ = std::process::Command::new("xdg-open").arg(&url).status();
}
});
}
}
#[cfg(not(all(unix, not(target_os = "macos"))))]
Ok(_) => {}
}
}
pub async fn deliver(t: &NotifyTarget, ev: &Event) -> Result<()> {
let limit = Duration::from_secs(10);
match t.kind {
TargetType::Webhook => {
let url = match (&t.url, &t.url_env) {
(Some(u), _) => u.clone(),
(None, Some(var)) => std::env::var(var).with_context(|| format!("environment variable {var} is not set"))?,
(None, None) => bail!("no url"),
};
let (headers, body, ctype) = payload(t.format, ev);
let mut r = crate::http::plain().post(url).header(reqwest::header::CONTENT_TYPE, ctype).body(body);
for (k, v) in headers {
r = r.header(k, v);
}
let resp = r.send().await.map_err(|e| anyhow::Error::new(e.without_url()))?;
if !resp.status().is_success() {
bail!("HTTP {}", resp.status());
}
}
TargetType::Command => {
use std::process::Stdio;
use tokio::io::AsyncWriteExt;
let (bin, args) = t.command.split_first().context("empty command")?;
let mut child = tokio::process::Command::new(bin)
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.kill_on_drop(true)
.spawn()
.with_context(|| format!("run {bin}"))?;
let mut stdin = child.stdin.take().context("no stdin")?;
let input = serde_json::to_vec(ev)?;
let out = tokio::time::timeout(limit, async move {
let _ = stdin.write_all(&input).await;
drop(stdin);
child.wait_with_output().await
})
.await
.context("timed out after 10s")??;
if !out.status.success() {
let err: String = String::from_utf8_lossy(&out.stderr).chars().take(200).collect();
bail!("{bin} exited with {}: {}", out.status, err.trim());
}
}
}
Ok(())
}
pub fn payload(format: WebhookFormat, ev: &Event) -> (Vec<(&'static str, String)>, String, &'static str) {
const JSON: &str = "application/json";
let head = format!("Night Porter: {}", ev.title);
let pr = ev.pr_title.as_deref().filter(|t| !t.is_empty());
match format {
WebhookFormat::Json => (vec![], serde_json::to_string(ev).unwrap_or_default(), JSON),
WebhookFormat::Slack => {
let esc = |s: &str| s.replace('&', "&").replace('<', "<").replace('>', ">");
let mut text = format!("*{}*\n{}", esc(&head), esc(&ev.summary));
if let Some(u) = &ev.url {
let label = pr.map_or_else(|| ev.title.clone(), |t| format!("{}: {t}", ev.title));
let _ = write!(text, "\n<{u}|{}>", esc(&label));
}
(vec![], json!({ "text": text }).to_string(), JSON)
}
WebhookFormat::Discord => {
let esc = |s: &str| {
s.chars().fold(String::new(), |mut o, c| {
if "\\*_~`|[]()<>#-".contains(c) {
o.push('\\');
}
o.push(c);
o
})
};
let mut text = format!("**{}**\n{}", esc(&head), esc(&ev.summary));
if let Some(t) = pr {
text.push('\n');
text.push_str(&esc(t));
}
if let Some(u) = &ev.url {
text.push('\n');
text.push_str(u);
}
let text: String = text.chars().take(1900).collect();
(vec![], json!({ "content": text, "allowed_mentions": { "parse": [] } }).to_string(), JSON)
}
WebhookFormat::Ntfy => {
let tag = match ev.kind {
Kind::NeedsApproval => "bell",
Kind::Failed => "warning",
Kind::Done => "white_check_mark",
Kind::NewAuthor => "bust_in_silhouette",
};
let ascii = |s: &str| s.chars().map(|c| if c.is_ascii_graphic() || c == ' ' { c } else { '?' }).collect::<String>();
let mut h = vec![("Title", ascii(&head)), ("Tags", tag.to_string())];
if let Some(u) = &ev.url {
h.push(("Click", ascii(u)));
}
let body = pr.map_or_else(|| ev.summary.clone(), |t| format!("{t}\n{}", ev.summary));
(h, body, "text/plain; charset=utf-8")
}
}
}
pub async fn test(n: &Notifications, name: Option<&str>) -> Result<String> {
let ev = Event {
pr_title: Some("Example pull request".into()),
url: Some("https://github.com/night-porter/nightporter".into()),
..Event::about(
Kind::NeedsApproval,
&RepoId::new(ForgeKind::GitHub, "github.com", "night-porter/nightporter"),
Target::Pr,
1,
"Test notification from `nightporter notify test`",
)
};
let targets: Vec<&NotifyTarget> = n.targets.iter().filter(|t| name.is_none_or(|x| x == t.name)).collect();
if let Some(x) = name
&& x != "desktop"
&& targets.is_empty()
{
let names: Vec<&str> = std::iter::once("desktop").chain(n.targets.iter().map(|t| t.name.as_str())).collect();
bail!("no notification target {x:?}; there are: {}", names.join(", "));
}
let (mut out, mut failed) = (String::new(), 0);
if name.is_none_or(|x| x == "desktop") {
match popup(&ev).show() {
Ok(_) => out.push_str("desktop: sent (check your notification center)\n"),
Err(e) => {
failed += 1;
let _ = writeln!(out, "desktop: failed: {e}");
}
}
}
for t in targets {
match deliver(t, &ev).await {
Ok(()) => {
let _ = writeln!(out, "{}: sent", t.name);
}
Err(e) => {
failed += 1;
let _ = writeln!(out, "{}: failed: {e:#}", t.name);
}
}
}
if failed > 0 {
bail!("{out}{failed} failed");
}
Ok(out.trim_end().into())
}
#[cfg(test)]
mod tests {
use super::*;
fn ev() -> Event {
Event {
pr_title: Some("Fix <!channel> & @everyone".into()),
job: Some(3),
..Event::about(Kind::Failed, &RepoId::new(ForgeKind::GitLab, "gitlab.com", "grp/proj"), Target::Pr, 7, "checks need you")
}
}
fn target(kinds: &[Kind]) -> NotifyTarget {
let mut t: NotifyTarget = toml::from_str("name = \"x\"\ntype = \"command\"\ncommand = [\"true\"]").unwrap();
t.kinds = kinds.to_vec();
t
}
#[test]
fn urls() {
let gh = RepoId::new(ForgeKind::GitHub, "github.com", "o/r");
assert_eq!(web_url(&gh, Target::Pr, 7), "https://github.com/o/r/pull/7");
assert_eq!(web_url(&gh, Target::Issue, 7), "https://github.com/o/r/issues/7");
assert_eq!(ev().url.as_deref(), Some("https://gitlab.com/grp/proj/-/merge_requests/7"));
}
#[test]
fn kinds_filter() {
assert!(wants(&target(&[]), Kind::Done), "no kinds = all");
assert!(wants(&target(&[Kind::Failed, Kind::NeedsApproval]), Kind::Failed));
assert!(!wants(&target(&[Kind::Failed]), Kind::Done));
}
#[test]
fn routing_follows_notify_lists() {
fn names((d, ts): (bool, Vec<&NotifyTarget>)) -> (bool, Vec<&str>) {
(d, ts.into_iter().map(|t| t.name.as_str()).collect())
}
let mut n = Notifications::default();
for (name, kinds) in [("phone", vec![Kind::Failed]), ("team", vec![])] {
let mut t = target(&kinds);
t.name = name.into();
n.targets.push(t);
}
assert_eq!(names(route(&n, None, Kind::Failed)), (true, vec!["phone", "team"]));
assert_eq!(names(route(&n, None, Kind::Done)), (true, vec!["team"]), "phone only wants failed");
let only = ["phone".to_string()];
assert_eq!(names(route(&n, Some(&only), Kind::Failed)), (false, vec!["phone"]));
assert_eq!(names(route(&n, Some(&["desktop".into()]), Kind::Done)), (true, vec![]));
n.done = false;
assert_eq!(names(route(&n, Some(&["desktop".into()]), Kind::Done)), (false, vec![]), "the toggle still applies");
}
#[test]
fn json_payload() {
let (h, body, ct) = payload(WebhookFormat::Json, &ev());
assert!(h.is_empty() && ct == "application/json");
let v: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(v["kind"], "failed");
assert_eq!(v["repo"], "grp/proj");
assert_eq!(v["number"], 7);
assert_eq!(v["job"], 3);
assert_eq!(v["url"], "https://gitlab.com/grp/proj/-/merge_requests/7");
}
#[test]
fn slack_payload_escapes_and_links() {
let (_, body, _) = payload(WebhookFormat::Slack, &ev());
let text = serde_json::from_str::<serde_json::Value>(&body).unwrap()["text"].as_str().unwrap().to_string();
assert!(text.starts_with("*Night Porter: grp/proj#7*\nchecks need you\n"));
assert!(text.contains("<https://gitlab.com/grp/proj/-/merge_requests/7|grp/proj#7: Fix <!channel> & @everyone>"));
}
#[test]
fn discord_payload_never_pings() {
let (_, body, _) = payload(WebhookFormat::Discord, &ev());
let v: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(v["allowed_mentions"]["parse"], json!([]));
let c = v["content"].as_str().unwrap();
assert!(c.starts_with("**Night Porter: grp/proj\\#7**") && c.ends_with("/-/merge_requests/7"), "{c}");
let mut e = ev();
e.pr_title = Some("[Approve job](https://evil.example)".into());
let (_, body, _) = payload(WebhookFormat::Discord, &e);
let v: serde_json::Value = serde_json::from_str(&body).unwrap();
assert!(v["content"].as_str().unwrap().contains("\\[Approve job\\]\\(https://evil.example\\)"));
}
#[test]
fn ntfy_payload_headers() {
let mut e = ev();
e.title = "grp/proj#7 ✓".into();
let (h, body, ct) = payload(WebhookFormat::Ntfy, &e);
assert_eq!(ct, "text/plain; charset=utf-8");
assert!(h.contains(&("Title", "Night Porter: grp/proj#7 ?".into())), "headers stay ASCII");
assert!(h.contains(&("Click", "https://gitlab.com/grp/proj/-/merge_requests/7".into())));
assert!(h.contains(&("Tags", "warning".into())));
assert_eq!(body, "Fix <!channel> & @everyone\nchecks need you");
}
#[tokio::test]
async fn command_gets_json_on_stdin() {
let d = tempfile::tempdir().unwrap();
let out = d.path().join("ev.json");
let mut t = target(&[]);
t.command = vec!["sh".into(), "-c".into(), format!("cat > '{}'", out.display())];
deliver(&t, &ev()).await.unwrap();
let v: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(&out).unwrap()).unwrap();
assert_eq!(v["summary"], "checks need you");
t.command = vec!["false".into()];
assert!(deliver(&t, &ev()).await.is_err());
}
fn webhook(url: Option<String>, url_env: Option<&str>, format: WebhookFormat) -> NotifyTarget {
let mut t: NotifyTarget = toml::from_str("name = \"hook\"\ntype = \"webhook\"").unwrap();
(t.url, t.url_env, t.format) = (url, url_env.map(Into::into), format);
t
}
#[tokio::test]
async fn webhooks_post_the_payload() {
let (base, log) = crate::e2e_tests::mock(vec![("POST", "/hook", "{}".into())]).await;
deliver(&webhook(Some(format!("{base}/hook")), None, WebhookFormat::Slack), &ev()).await.unwrap();
let (method, path, body) = log.lock().unwrap()[0].clone();
assert_eq!((method.as_str(), path.as_str()), ("POST", "/hook"));
assert!(body.contains("Night Porter: grp/proj#7"), "{body}");
let e = deliver(&webhook(Some(format!("{base}/missing")), None, WebhookFormat::Json), &ev()).await.unwrap_err();
assert_eq!(e.to_string(), "HTTP 404 Not Found");
let e = deliver(&webhook(None, Some("NIGHTPORTER_TEST_UNSET_HOOK"), WebhookFormat::Json), &ev()).await.unwrap_err();
assert!(e.to_string().contains("NIGHTPORTER_TEST_UNSET_HOOK is not set"));
assert_eq!(deliver(&webhook(None, None, WebhookFormat::Json), &ev()).await.unwrap_err().to_string(), "no url");
let mut t = target(&[]);
t.command.clear();
assert_eq!(deliver(&t, &ev()).await.unwrap_err().to_string(), "empty command");
}
#[tokio::test]
async fn test_sends_to_the_named_targets_and_reports_each() {
let d = tempfile::tempdir().unwrap();
let out = d.path().join("ev.json");
let mut n = Notifications::default();
let mut ok = target(&[Kind::Done]);
ok.name = "phone".into();
ok.command = vec!["sh".into(), "-c".into(), format!("cat > '{}'", out.display())];
let mut bad = target(&[]);
bad.name = "broken".into();
bad.command = vec!["false".into()];
n.targets = vec![ok, bad];
assert_eq!(test(&n, Some("phone")).await.unwrap(), "phone: sent");
let v: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(&out).unwrap()).unwrap();
assert_eq!(v["kind"], "needs_approval", "kinds are ignored by a test: {v}");
let e = test(&n, Some("broken")).await.unwrap_err().to_string();
assert!(e.starts_with("broken: failed: false exited with") && e.ends_with("1 failed"), "{e}");
let e = test(&n, Some("nope")).await.unwrap_err().to_string();
assert_eq!(e, "no notification target \"nope\"; there are: desktop, phone, broken");
}
#[test]
fn popup_shows_the_pr_title_above_the_summary() {
let p = popup(&ev());
assert_eq!(p.summary, "Night Porter: grp/proj#7");
assert_eq!(p.body, "Fix <!channel> & @everyone\nchecks need you");
let mut e = ev();
e.pr_title = Some(String::new());
assert_eq!(popup(&e).body, "checks need you");
}
#[test]
fn desktop_toggles_per_kind() {
let n = Notifications { needs_approval: false, ..Notifications::default() };
assert!(!desktop_on(&n, Kind::NeedsApproval));
assert_eq!(desktop_on(&n, Kind::NewAuthor), n.new_author);
assert_eq!(web_url(&RepoId::new(ForgeKind::GitLab, "gitlab.com", "g/p"), Target::Issue, 2), "https://gitlab.com/g/p/-/issues/2");
assert_eq!(web_url(&RepoId::new(ForgeKind::Forgejo, "codeberg.org", "o/r"), Target::Pr, 2), "https://codeberg.org/o/r/pulls/2");
}
#[test]
fn targets_are_validated() {
let ok =
"[[notifications.targets]]\nname = \"phone\"\ntype = \"webhook\"\nurl_env = \"NTFY\"\nformat = \"ntfy\"\nkinds = [\"failed\"]";
crate::config::Config::parse(ok).unwrap();
for bad in [
"[[notifications.targets]]\nname = \"desktop\"\ntype = \"command\"\ncommand = [\"x\"]",
"[[notifications.targets]]\nname = \"a\"\ntype = \"webhook\"",
"[[notifications.targets]]\nname = \"a\"\ntype = \"webhook\"\nurl = \"ftp://x\"",
"[[notifications.targets]]\nname = \"a\"\ntype = \"command\"",
"[[notifications.targets]]\nname = \"a\"\ntype = \"command\"\ncommand = [\"x\"]\nkinds = [\"sometimes\"]",
] {
assert!(crate::config::Config::parse(bad).is_err(), "{bad}");
}
}
}