diff --git a/AGENTS.md b/AGENTS.md index b409651..f291666 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -51,6 +51,7 @@ - Colorization functions (`colorize_line`, `colorize_metadata`, `colorize_ordered`, `content_trim_colored`) handle markdown formatting - `resolve_ticket()` scans all status directories — it's the single source for ticket lookup by UUID prefix, title, or filename - `parse_ticket_field()` handles extracting metadata values from markdown format +- Field lookup, read and write, is scoped to the lines under the `## Metadata` heading and anchored to the start of the line. Prose above it is free text: tickets in this repo quote field markers in their descriptions, and an unscoped search reads or rewrites those instead. `set_status_field()` carries the same scoping ### Ticket Resolution - `resolve_ticket()` returns `(status, title, content)` tuple diff --git a/Cargo.lock b/Cargo.lock index 2787026..36784e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1029,7 +1029,7 @@ dependencies = [ [[package]] name = "worklane" -version = "0.2.0" +version = "0.2.1" dependencies = [ "anyhow", "assert_cmd", diff --git a/Cargo.toml b/Cargo.toml index 32c867e..892f4bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "worklane" -version = "0.2.0" +version = "0.2.1" edition = "2024" [dependencies] diff --git a/src/util.rs b/src/util.rs index 513aea6..b267dd9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -451,33 +451,61 @@ fn is_delimiter(chars: &[char], at: usize) -> bool { chars.get(at) == Some(&'*') && chars.get(at + 1) == Some(&'*') } -pub fn parse_ticket_field(content: &str, field: &str) -> String { - let pattern = format!("**{}**:", field); +const METADATA_HEADING: &str = "## Metadata"; + +fn is_metadata_heading(line: &str) -> bool { + line.trim_end() == METADATA_HEADING +} + +/// The lines of the Metadata section, or nothing when the heading is absent. +/// +/// Field lookup has to be anchored here. Prose above the section is free text, so a +/// description quoting a field marker would otherwise shadow the real value below it. +fn metadata_lines(content: &str) -> impl Iterator { content .lines() - .find(|line| line.contains(&pattern)) - .map(|line| { - line.splitn(2, &pattern) - .nth(1) - .unwrap_or("_Not set_") - .trim() - .to_string() - }) - .unwrap_or("_Not set_".to_string()) + .skip_while(|line| !is_metadata_heading(line)) + .skip(1) + .take_while(|line| !line.starts_with("## ")) +} + +/// A metadata line without its list marker, so lookup can match on a prefix rather +/// than searching the whole line. +fn metadata_field_body(line: &str) -> &str { + let line = line.trim_start(); + line.strip_prefix("- ").unwrap_or(line) +} + +pub fn parse_ticket_field(content: &str, field: &str) -> String { + let prefix = format!("**{}**:", field); + metadata_lines(content) + .find_map(|line| metadata_field_body(line).strip_prefix(&prefix)) + .map(|value| value.trim().to_string()) + .unwrap_or_else(|| "_Not set_".to_string()) } fn is_status_line(line: &str) -> bool { + let body = metadata_field_body(line); // The second spelling is what earlier versions wrote, repair it on the way through. - line.starts_with("- **Status**:") || line.starts_with("- **-Status**:") + body.starts_with("**Status**:") || body.starts_with("**-Status**:") } // Rewrite the Status field in the Metadata section, leaving every other line untouched. +// +// Scoped to the section for the same reason as parse_ticket_field: a description line +// shaped like the Status field would otherwise be rewritten in its place, corrupting +// the prose and leaving the real metadata stale. pub fn set_status_field(content: &str, target_status: &str, title: &str) -> Result { let mut updated = String::with_capacity(content.len()); + let mut in_metadata = false; let mut found = false; for line in content.lines() { - if !found && is_status_line(line) { + if line.starts_with("## ") { + in_metadata = is_metadata_heading(line); + } + + if in_metadata && !found && is_status_line(line) { updated.push_str("- **Status**: "); updated.push_str(target_status); found = true; @@ -733,6 +761,84 @@ mod tests { format!("# T\n\n## Metadata\n\n- **Priority**: {}\n", raw) } + /// A ticket whose prose quotes the field markers it also carries as metadata, + /// as a list so the quoted lines start with the marker. This repo files tickets + /// about the metadata format, so it is the normal case here rather than a corner. + fn ticket_shadowing_its_own_metadata() -> String { + "# Shadow\n\ + \n\ + ## Description\n\ + The current format is:\n\ + - **Owner**: nobody\n\ + - **Status**: backlog\n\ + \n\ + ## Metadata\n\ + \n\ + - **ID**: real-id\n\ + - **Owner**: alice\n\ + - **Status**: backlog\n" + .to_string() + } + + #[test] + fn prose_quoting_a_field_marker_does_not_shadow_the_metadata() { + let content = ticket_shadowing_its_own_metadata(); + assert_eq!(parse_ticket_field(&content, "Owner"), "alice"); + assert_eq!(parse_ticket_field(&content, "ID"), "real-id"); + assert_eq!(parse_ticket_field(&content, "Status"), "backlog"); + } + + #[test] + fn a_field_only_mentioned_in_prose_reads_as_unset() { + let content = "# T\n\n## Description\n- **Due**: 2026-12-31\n\n## Metadata\n\n- **ID**: x\n"; + assert_eq!(parse_ticket_field(content, "Due"), "_Not set_"); + } + + #[test] + fn a_field_lookup_needs_the_metadata_heading() { + // No section, no fields: there is nothing to anchor to. + assert_eq!( + parse_ticket_field("# T\n\n- **Owner**: alice\n", "Owner"), + "_Not set_" + ); + } + + #[test] + fn a_field_lookup_stops_at_the_next_section() { + let content = "# T\n\n## Metadata\n\n- **ID**: x\n\n## Notes\n\n- **Owner**: alice\n"; + assert_eq!(parse_ticket_field(content, "Owner"), "_Not set_"); + assert_eq!(parse_ticket_field(content, "ID"), "x"); + } + + /// Matching on a prefix, not a substring: a field name inside another field's + /// value is not that field. + #[test] + fn a_field_marker_inside_another_value_is_not_matched() { + let content = "# T\n\n## Metadata\n\n- **Tags**: mentions **Owner**: bob\n"; + assert_eq!(parse_ticket_field(content, "Owner"), "_Not set_"); + assert_eq!(parse_ticket_field(content, "Tags"), "mentions **Owner**: bob"); + } + + #[test] + fn setting_the_status_leaves_prose_that_looks_like_metadata_alone() { + let content = ticket_shadowing_its_own_metadata(); + let updated = set_status_field(&content, "committed", "Shadow").unwrap(); + + assert!( + updated.contains("## Description\nThe current format is:\n- **Owner**: nobody\n- **Status**: backlog\n"), + "the description was rewritten:\n{updated}" + ); + assert_eq!(parse_ticket_field(&updated, "Status"), "committed"); + assert_eq!(updated.matches("**Status**: committed").count(), 1); + } + + #[test] + fn setting_the_status_needs_the_field_in_the_metadata_section() { + // A Status line above the section is prose, so this is a ticket without the field. + let content = "# T\n\n## Description\n- **Status**: backlog\n\n## Metadata\n\n- **ID**: x\n"; + assert!(set_status_field(content, "committed", "T").is_err()); + } + #[test] fn a_level_in_range_parses() { for level in 1..=5u8 {