use anyhow::{bail, Result};
use kranz_engine::ticket::Ticket;
use kranz_engine::ticket_notes::{self, TicketNote};
use std::path::Path;
const AUTHOR_ENV: &str = "KRANZ_NOTE_AUTHOR";
pub fn note_author() -> String {
std::env::var(AUTHOR_ENV)
.ok()
.map(|a| a.trim().to_string())
.filter(|a| !a.is_empty())
.unwrap_or_else(|| "operator".to_string())
}
pub fn cmd_ticket_note(repo: &Path, slug: &str, text: &str) -> Result<String> {
let path = Ticket::tickets_dir(repo).join(format!("{slug}.md"));
if !path.is_file() {
bail!("ticket '{slug}' not found at {}", path.display());
}
let note = ticket_notes::append_note(repo, slug, ¬e_author(), text)?;
Ok(format!("noted on '{slug}' at {}\n", note.ts.to_rfc3339()))
}
pub fn cmd_ticket_notes(repo: &Path, slug: &str) -> Result<String> {
let notes = ticket_notes::read_notes(repo, slug)?;
Ok(render_ticket_notes(slug, ¬es))
}
pub fn render_ticket_notes(slug: &str, notes: &[TicketNote]) -> String {
if notes.is_empty() {
return format!("no notes on '{slug}'\n");
}
let mut out = format!("notes on '{slug}' ({}):\n", notes.len());
for note in notes {
out.push_str(&format!(
"{} {}: {}\n",
note.ts.to_rfc3339(),
note.author,
note.text
));
}
out
}