(d4ce964) Record status, owner and tag changes in a history log, and bump to 0.29.0
The comments directory was already an append-only log with one event kind. It is now .worklane/history/<uuid>/, with the kind in the entry and in the filename after the stamp, so a ticket says how it got where it is and not only what was said about it. mv, chown and set --tags each write an entry after their own save and behind their own no-change return, so a refused or no-op edit records nothing. --actor is on all four writers now. An old comments directory is adopted on first access and a file with no marker reads as the comment it is, so nothing is migrated or rewritten. The log moves to src/history.rs, out of util.rs and handlers.rs, and the card's counter counts comments only: a ticket that has moved three times has not been talked about. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
a1bdb5df49
commit
f454d212a4
+57
-140
@@ -10,6 +10,7 @@ use crate::command::{
|
||||
AddArgs, AppendArgs, CampaignAddArgs, CampaignAppendArgs, CampaignSetArgs, CheckArgs,
|
||||
ChownArgs, LsArgs, RelateArgs, SetArgs, TagAddArgs, TagSetArgs,
|
||||
};
|
||||
use crate::history::{Kind, change, format_history, read_history, write_entry};
|
||||
use crate::install;
|
||||
use crate::tag::{TAGS_FILE, TagIndex, tags_path};
|
||||
use crate::util::*;
|
||||
@@ -351,93 +352,6 @@ pub fn cmd_add(args: &AddArgs, start: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Public for the TUI's detail view, which shows the same comments `show` does.
|
||||
pub fn read_comments(comments_dir: &Path) -> Result<Vec<(String, String, String, String)>> {
|
||||
if !comments_dir.exists() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let mut comments: Vec<(String, String, String, String)> = Vec::new();
|
||||
|
||||
for entry in fs::read_dir(comments_dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.extension().is_some_and(|e| e == "md") {
|
||||
let content = fs::read_to_string(&path)?;
|
||||
let filename = path
|
||||
.file_stem()
|
||||
.map(|s| s.to_string_lossy().to_string())
|
||||
.unwrap_or_default();
|
||||
|
||||
let author = content
|
||||
.lines()
|
||||
.find(|l| l.starts_with("**Author**"))
|
||||
.and_then(|l| l.split_once("**Author**:"))
|
||||
.map(|(_, value)| value)
|
||||
.map(|s| s.trim().to_string())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
let time = content
|
||||
.lines()
|
||||
.find(|l| l.starts_with("**Time**"))
|
||||
.and_then(|l| l.split_once("**Time**:"))
|
||||
.map(|(_, value)| value)
|
||||
.map(|s| s.trim().to_string())
|
||||
.unwrap_or_else(|| "unknown".to_string());
|
||||
|
||||
let body = if let Some(after_comment) = content.strip_prefix("**Comment**\n") {
|
||||
if let Some(end) = after_comment.find("\n\n**Author**") {
|
||||
after_comment[..end].trim()
|
||||
} else {
|
||||
after_comment.trim()
|
||||
}
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
comments.push((filename, author, time, body.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
comments.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
|
||||
Ok(comments)
|
||||
}
|
||||
|
||||
/// Write a comment under the first free name derived from `stem`.
|
||||
///
|
||||
/// `create_new` rather than `fs::write`: a name that already exists means another
|
||||
/// comment landed in the same microsecond, and truncating it is exactly the data
|
||||
/// loss this guards against. The suffix is zero padded so the names keep sorting
|
||||
/// in the order they were written.
|
||||
fn write_new_comment(comments_dir: &Path, stem: &str, content: &str) -> Result<String> {
|
||||
for attempt in 0..100 {
|
||||
let filename = match attempt {
|
||||
0 => format!("{}.md", stem),
|
||||
n => format!("{}_{:02}.md", stem, n),
|
||||
};
|
||||
|
||||
match fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(comments_dir.join(&filename))
|
||||
{
|
||||
Ok(mut file) => {
|
||||
file.write_all(content.as_bytes())?;
|
||||
return Ok(filename);
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
Err(anyhow!(
|
||||
"No free comment filename for '{}' in {}",
|
||||
stem,
|
||||
comments_dir.display()
|
||||
))
|
||||
}
|
||||
|
||||
pub fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
|
||||
let (_status, title, content) = resolve_ticket(search, worklane_dir)?;
|
||||
let ticket_uuid = parse_ticket_field(&content, "ID");
|
||||
@@ -453,9 +367,8 @@ pub fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> {
|
||||
println!("\n{}", content_trim_colored(&content, &resolved));
|
||||
|
||||
if ticket_uuid != "_Not set_" {
|
||||
let comments_dir = comments_dir(worklane_dir, &ticket_uuid);
|
||||
let comments = read_comments(&comments_dir)?;
|
||||
let formatted = format_comments(&comments);
|
||||
let entries = read_history(worklane_dir, &ticket_uuid)?;
|
||||
let formatted = format_history(&entries);
|
||||
println!("{}", formatted);
|
||||
}
|
||||
|
||||
@@ -642,7 +555,13 @@ fn check_blockers(worklane_dir: &Path, content: &str, title: &str) -> Result<()>
|
||||
/// A status is a [`Status`] rather than a string, so an unknown one is clap's to refuse
|
||||
/// and nothing here has to check. What is left is the order of the flow, which is
|
||||
/// `Status::ALL`.
|
||||
pub fn cmd_mv(ticket_id: &str, target: Status, force: bool, start: &Path) -> Result<()> {
|
||||
pub fn cmd_mv(
|
||||
ticket_id: &str,
|
||||
target: Status,
|
||||
force: bool,
|
||||
actor: &Option<String>,
|
||||
start: &Path,
|
||||
) -> Result<()> {
|
||||
let target_status = target.as_str();
|
||||
|
||||
let worklane_dir = find_worklane_dir(start)?;
|
||||
@@ -712,6 +631,17 @@ pub fn cmd_mv(ticket_id: &str, target: Status, force: bool, start: &Path) -> Res
|
||||
&title,
|
||||
)?;
|
||||
|
||||
// After the move, never before: an entry for a move that was refused would be a log
|
||||
// of things that did not happen. The no-op above returns without one for the same
|
||||
// reason.
|
||||
write_entry(
|
||||
&worklane_dir,
|
||||
&parse_ticket_field(&resolved, "ID"),
|
||||
Kind::Status,
|
||||
&change(¤t_status, target_status),
|
||||
&resolve_actor(actor.as_deref()),
|
||||
)?;
|
||||
|
||||
println!(
|
||||
"Moved ticket '{}' from {} to {}",
|
||||
color_text(&title, Color::Green),
|
||||
@@ -740,26 +670,13 @@ pub fn cmd_comment(
|
||||
return Err(anyhow!("Could not determine ticket ID."));
|
||||
}
|
||||
|
||||
let comments_dir = comments_dir(&worklane_dir, &ticket_uuid);
|
||||
fs::create_dir_all(&comments_dir)?;
|
||||
|
||||
let stem = Local::now().format(COMMENT_FILE_FORMAT).to_string();
|
||||
|
||||
let author = resolve_actor(actor.as_deref());
|
||||
|
||||
let comment_content = format!(
|
||||
"**Comment**\n\
|
||||
{}\n\
|
||||
\n\
|
||||
**Author**: {}\n\
|
||||
**Time**: {}\n\
|
||||
",
|
||||
let filename = write_entry(
|
||||
&worklane_dir,
|
||||
&ticket_uuid,
|
||||
Kind::Comment,
|
||||
body,
|
||||
author,
|
||||
Local::now().format(TIMESTAMP_FORMAT)
|
||||
);
|
||||
|
||||
let filename = write_new_comment(&comments_dir, &stem, &comment_content)?;
|
||||
&resolve_actor(actor.as_deref()),
|
||||
)?;
|
||||
|
||||
println!("Comment added to '{}' ({})", title, filename);
|
||||
Ok(())
|
||||
@@ -993,6 +910,23 @@ pub fn cmd_set(args: &SetArgs, start: &Path) -> Result<()> {
|
||||
let renamed = changed.contains(&"title").then_some(()).and(args.title.as_ref());
|
||||
save_ticket(&worklane_dir, &status, &uuid, &title, renamed, &content)?;
|
||||
|
||||
// Tags alone, of everything `set` writes. The other fields change often enough that a
|
||||
// log of them is noise, and `apply` is what decides this: a field it did not record as
|
||||
// changed was already what the call asked for. Emptying the field counts, since
|
||||
// `--clear tags` is the same field going somewhere.
|
||||
if changed.contains(&"tags") || cleared.contains(&"tags") {
|
||||
write_entry(
|
||||
&worklane_dir,
|
||||
&uuid,
|
||||
Kind::Tags,
|
||||
&change(
|
||||
&parse_ticket_field(&original, "Tags"),
|
||||
&parse_ticket_field(&content, "Tags"),
|
||||
),
|
||||
&resolve_actor(args.actor.as_deref()),
|
||||
)?;
|
||||
}
|
||||
|
||||
let shown = args.title.clone().unwrap_or(title);
|
||||
if !changed.is_empty() {
|
||||
report(&changed, &shown, "Updated");
|
||||
@@ -1037,6 +971,19 @@ pub fn cmd_chown(args: &ChownArgs, start: &Path) -> Result<()> {
|
||||
|
||||
save_ticket(&worklane_dir, &status, &uuid, &title, None, &content)?;
|
||||
|
||||
// Behind the no-change return above, so a chown to the owner a ticket already has
|
||||
// records nothing, and after the save, so a failed write logs nothing.
|
||||
write_entry(
|
||||
&worklane_dir,
|
||||
&uuid,
|
||||
Kind::Owner,
|
||||
&change(
|
||||
&parse_ticket_field(&original, "Owner"),
|
||||
&parse_ticket_field(&content, "Owner"),
|
||||
),
|
||||
&resolve_actor(args.actor.as_deref()),
|
||||
)?;
|
||||
|
||||
let verb = match user {
|
||||
Some(_) => "Updated",
|
||||
None => "Cleared",
|
||||
@@ -1854,33 +1801,3 @@ pub fn cmd_install_tool(force: bool, print: bool) -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The collision path cannot be provoked from the CLI, since two processes
|
||||
/// will not share a microsecond, but it is the whole point of the guard.
|
||||
#[test]
|
||||
fn a_colliding_stem_takes_the_next_name_and_keeps_the_first() {
|
||||
let dir = Fixture::new("worklane-test");
|
||||
|
||||
let first = write_new_comment(&dir, "20260820_143012_123456", "first").unwrap();
|
||||
let second = write_new_comment(&dir, "20260820_143012_123456", "second").unwrap();
|
||||
let third = write_new_comment(&dir, "20260820_143012_123456", "third").unwrap();
|
||||
|
||||
assert_eq!(first, "20260820_143012_123456.md");
|
||||
assert_eq!(second, "20260820_143012_123456_01.md");
|
||||
assert_eq!(third, "20260820_143012_123456_02.md");
|
||||
|
||||
// Nothing was truncated on the way through
|
||||
assert_eq!(fs::read_to_string(dir.join(&first)).unwrap(), "first");
|
||||
assert_eq!(fs::read_to_string(dir.join(&second)).unwrap(), "second");
|
||||
assert_eq!(fs::read_to_string(dir.join(&third)).unwrap(), "third");
|
||||
|
||||
// read_comments sorts on the stem, so the names must sort in write order
|
||||
let mut names = vec![second.clone(), third.clone(), first.clone()];
|
||||
names.sort();
|
||||
assert_eq!(names, vec![first, second, third]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user