Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab1dae098c |
@@ -51,6 +51,7 @@
|
|||||||
- Colorization functions (`colorize_line`, `colorize_metadata`, `colorize_ordered`, `content_trim_colored`) handle markdown formatting
|
- 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
|
- `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
|
- `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
|
### Ticket Resolution
|
||||||
- `resolve_ticket()` returns `(status, title, content)` tuple
|
- `resolve_ticket()` returns `(status, title, content)` tuple
|
||||||
|
|||||||
Generated
+1
-1
@@ -1029,7 +1029,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "worklane"
|
name = "worklane"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"assert_cmd",
|
"assert_cmd",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "worklane"
|
name = "worklane"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|||||||
+119
-13
@@ -451,33 +451,61 @@ fn is_delimiter(chars: &[char], at: usize) -> bool {
|
|||||||
chars.get(at) == Some(&'*') && chars.get(at + 1) == Some(&'*')
|
chars.get(at) == Some(&'*') && chars.get(at + 1) == Some(&'*')
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_ticket_field(content: &str, field: &str) -> String {
|
const METADATA_HEADING: &str = "## Metadata";
|
||||||
let pattern = format!("**{}**:", field);
|
|
||||||
|
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<Item = &str> {
|
||||||
content
|
content
|
||||||
.lines()
|
.lines()
|
||||||
.find(|line| line.contains(&pattern))
|
.skip_while(|line| !is_metadata_heading(line))
|
||||||
.map(|line| {
|
.skip(1)
|
||||||
line.splitn(2, &pattern)
|
.take_while(|line| !line.starts_with("## "))
|
||||||
.nth(1)
|
}
|
||||||
.unwrap_or("_Not set_")
|
|
||||||
.trim()
|
/// A metadata line without its list marker, so lookup can match on a prefix rather
|
||||||
.to_string()
|
/// than searching the whole line.
|
||||||
})
|
fn metadata_field_body(line: &str) -> &str {
|
||||||
.unwrap_or("_Not set_".to_string())
|
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 {
|
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.
|
// 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.
|
// 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<String> {
|
pub fn set_status_field(content: &str, target_status: &str, title: &str) -> Result<String> {
|
||||||
let mut updated = String::with_capacity(content.len());
|
let mut updated = String::with_capacity(content.len());
|
||||||
|
let mut in_metadata = false;
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
|
|
||||||
for line in content.lines() {
|
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("- **Status**: ");
|
||||||
updated.push_str(target_status);
|
updated.push_str(target_status);
|
||||||
found = true;
|
found = true;
|
||||||
@@ -733,6 +761,84 @@ mod tests {
|
|||||||
format!("# T\n\n## Metadata\n\n- **Priority**: {}\n", raw)
|
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]
|
#[test]
|
||||||
fn a_level_in_range_parses() {
|
fn a_level_in_range_parses() {
|
||||||
for level in 1..=5u8 {
|
for level in 1..=5u8 {
|
||||||
|
|||||||
Reference in New Issue
Block a user