From d668a24a3dff1c89912083870224d460c8ef8be7 Mon Sep 17 00:00:00 2001 From: Ansho Rei Date: Fri, 15 May 2026 01:52:59 +0200 Subject: [PATCH] (0b1a129) Add the mv command --- .../skill/worklane-kanban-skill/SKILL.md | 12 +- src/command.rs | 14 + src/main.rs | 113 +++++- tests/cmd_mv.rs | 333 ++++++++++++++++++ 4 files changed, 467 insertions(+), 5 deletions(-) create mode 100644 tests/cmd_mv.rs diff --git a/.opencode/skill/worklane-kanban-skill/SKILL.md b/.opencode/skill/worklane-kanban-skill/SKILL.md index 621cb9f..c2e6077 100644 --- a/.opencode/skill/worklane-kanban-skill/SKILL.md +++ b/.opencode/skill/worklane-kanban-skill/SKILL.md @@ -35,16 +35,22 @@ Add a new ticket to the backlog. **Optional:** - `--description` (or `-d`): Ticket description -- `--acceptance` (or `-a`): Acceptance criteria (comma-separated values) -- `--plan` (or `-p`): Implementation plan steps (comma-separated) +- `--acceptance` (or `-a`): Acceptance criteria (repeat flag for each criterium) +- `--plan` (or `-p`): Implementation plan steps (repeat flag for each step) - `--tags` (or `-g`): Tags (comma-separated) - `--due` (or `--due`): Due date in YYYY-MM-DD format - `--owner` (or `-o`): Owner name -``` +```bash worklane add -t "Implement login" -d "Add user login page" -t login,auth -p "Design UI" "Build backend" "Write tests" --due 2026-06-01 --owner "ilya" ``` +You can add multiple acceptance criteria or steps to a ticket: + +```bash +worklane add -t "Implement logic" -a "Acceptance criterium 1" -a "Acceptance criterium 2" -p "Step 1" -p "Step 2" +``` + ### `worklane ls` List tickets, optionally filtered by status. diff --git a/src/command.rs b/src/command.rs index 183392b..ade5fa7 100644 --- a/src/command.rs +++ b/src/command.rs @@ -55,4 +55,18 @@ pub enum Command { /// Ticket UUID or title to search for id: String, }, + + /// Move a ticket to a different status + Mv { + /// Ticket UUID or title + ticket: String, + + /// Target status (backlog, committed, reviewing, done) + #[arg(short, long)] + status: String, + + /// Force move (allows backward or non-sequential moves) + #[arg(short, long)] + force: bool, + }, } diff --git a/src/main.rs b/src/main.rs index cb3f352..c7f82fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -306,7 +306,7 @@ fn cmd_add( Ok(()) } -fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> { +fn resolve_ticket(search: &str, worklane_dir: &Path) -> Result<(String, String, String)> { let statuses = status_dirs(); let mut matches: Vec<(String, String, String)> = Vec::new(); @@ -364,7 +364,11 @@ fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> { std::process::exit(1); } - let (_status, title, content) = matches.remove(0); + Ok(matches.remove(0)) +} + +fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> { + let (_status, title, content) = resolve_ticket(search, worklane_dir)?; println!("{}", title); println!("{}", "─".repeat(title.len())); @@ -373,6 +377,108 @@ fn cmd_show(search: &str, worklane_dir: &Path) -> Result<()> { Ok(()) } +fn cmd_mv(ticket_id: &str, target_status: &str, force: bool, start: &Path) -> Result<()> { + let valid_statuses = ["backlog", "committed", "reviewing", "done"]; + let forward_order = ["backlog", "committed", "reviewing", "done"]; + + if !valid_statuses.contains(&target_status) { + return Err(anyhow!( + "Invalid target status '{}'. Valid statuses: {}", + target_status, + valid_statuses.join(", ") + )); + } + + let worklane_dir = find_worklane_dir(start)?; + let (current_status, title, _) = resolve_ticket(ticket_id, &worklane_dir)?; + + let current_idx = forward_order + .iter() + .position(|&s| s == current_status) + .unwrap(); + let target_idx = forward_order + .iter() + .position(|&s| s == target_status) + .unwrap(); + + if !force && target_idx < current_idx { + return Err(anyhow!( + "Cannot move backward from {} to {}. Use --force (-f) to allow.", + current_status, + target_status + )); + } + + if !force && target_idx != current_idx + 1 && target_idx != current_idx { + return Err(anyhow!( + "Can only move one stage forward at a time. Use --force (-f) to skip stages." + )); + } + + if current_status == target_status { + println!("Ticket is already in '{}' status.", target_status); + return Ok(()); + } + + let current_path = worklane_dir.join(¤t_status); + let target_path = worklane_dir.join(target_status); + + // Find the ticket file + let mut found_filename = String::new(); + for entry in fs::read_dir(¤t_path)? { + let entry = entry?; + let path = entry.path(); + if path.extension().map_or(false, |e| e == "md") { + let file_content = fs::read_to_string(&path)?; + let file_title = file_content + .lines() + .find(|l| l.starts_with("# ")) + .map(|l| l[2..].trim()) + .unwrap_or("") + .to_string(); + if file_title == title { + found_filename = path.file_name().unwrap().to_string_lossy().to_string(); + break; + } + } + } + + if found_filename.is_empty() { + return Err(anyhow!("Could not find ticket file in {}", current_status)); + } + + // Read and update the ticket content + let mut content = fs::read_to_string(current_path.join(&found_filename))?; + + // Update Status in Metadata section + if let Some(start_pos) = content.find("- **Status**") { + // Skip "- " to find "**Status**" + let status_start = start_pos + 2; + let after_status = &content[status_start + "**Status**".len()..]; // after "**Status**" + if after_status.starts_with(": ") { + let rest = &after_status[2..]; + let value_end = rest.find('\n').unwrap_or(rest.len()); + let _value = &rest[..value_end]; + let before = &content[..status_start]; + let after = &rest[value_end..]; + content = format!("{}**-Status**: {}\n{}", before, target_status, after); + } + } + + // Move the file + fs::rename( + current_path.join(&found_filename), + target_path.join(&found_filename), + )?; + + // Write back updated content + fs::write(target_path.join(&found_filename), content)?; + + println!("Moved ticket '{}' from {} to {}", title, current_status, target_status); + + Ok(()) +} + fn content_trim_colored(content: &str) -> String { let lines: Vec<&str> = content.trim().lines().collect(); let mut result = String::new(); @@ -567,5 +673,8 @@ fn main() -> Result<()> { let worklane_dir = find_worklane_dir(&start)?; cmd_show(&id, &worklane_dir) } + Command::Mv { ticket, status, force } => { + cmd_mv(&ticket, &status, force, &start) + } } } diff --git a/tests/cmd_mv.rs b/tests/cmd_mv.rs new file mode 100644 index 0000000..620c592 --- /dev/null +++ b/tests/cmd_mv.rs @@ -0,0 +1,333 @@ +use assert_cmd::Command; +use assert_fs::TempDir; +use assert_fs::fixture::{PathChild, PathCreateDir}; +use std::fs; +use std::path::PathBuf; + +const BIN_NAME: &str = env!("CARGO_BIN_EXE_worklane"); + +fn setup_worklane(temp: &TempDir) -> PathBuf { + let worklane = temp.child(".worklane"); + worklane.create_dir_all().unwrap(); + + for dir in ["backlog", "committed", "reviewing", "done"] { + worklane.child(dir).create_dir_all().unwrap(); + } + + worklane.path().to_path_buf() +} + +fn create_ticket( + worklane_dir: &PathBuf, + status: &str, + title: &str, + description: &str, + id: &str, + owner: &str, + due: &str, + tags: &str, +) -> String { + let slug = title + .to_lowercase() + .chars() + .filter(|c| c.is_alphanumeric() || *c == ' ' || *c == '-' || *c == '_') + .map(|c| if c == ' ' { '-' } else { c }) + .collect::(); + let filename = format!("{}_{}.md", id, slug); + let ticket_body = format!( + "# {}\n\ + \n\ + ## Description\n\ + {}\n\ + \n\ + ## Acceptance Criteria\n\ + - [ ] test\n\ + \n\ + ## Implementation Plan\n\ + - test\n\ + \n\ + ## Metadata\n\ + \n\ + - **ID**: {}\n\ + - **Tags**: {}\n\ + - **Due**: {}\n\ + - **Owner**: {}\n\ + - **Status**: {}\n\ + ", + title, description, id, tags, due, owner, status + ); + let path = worklane_dir.join(status).join(&filename); + fs::write(&path, ticket_body).unwrap(); + filename +} + +fn get_stdout(cmd: &mut Command) -> String { + let assert = cmd.assert(); + String::from_utf8(assert.get_output().stdout.clone()).unwrap() +} + +fn get_stderr(cmd: &mut Command) -> String { + let assert = cmd.assert(); + String::from_utf8(assert.get_output().stderr.clone()).unwrap() +} + +#[test] +fn mv_forwards_from_backlog_to_committed() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + let filename = create_ticket( + &worklane, + "backlog", + "Task One", + "Move this ticket.", + id, + "alice", + "2026-12-31", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("committed"); + + let assert = cmd.assert(); + let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); + assert.success(); + + assert!(stdout.contains("Task One")); + assert!(stdout.contains("backlog")); + assert!(stdout.contains("committed")); + + // Verify file moved + assert!(!worklane.join("backlog").join(&filename).exists()); + assert!(worklane.join("committed").join(&filename).exists()); + + // Verify status updated + let content = fs::read_to_string(worklane.join("committed").join(&filename)).unwrap(); + eprintln!("CONTENT: {:?}", content); + assert!(content.contains("- **-Status**: committed")); +} + +#[test] +fn mv_blocks_backward_move() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + create_ticket( + &worklane, + "committed", + "Committed Task", + "Should stay committed.", + id, + "bob", + "2026-11-15", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("backlog") + .assert() + .failure(); + + let stderr = get_stderr(&mut cmd); + assert!(stderr.contains("Cannot move backward")); +} + +#[test] +fn mv_allows_backward_move_with_force() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + let filename = create_ticket( + &worklane, + "committed", + "Backward Move", + "Force backward.", + id, + "charlie", + "2026-10-01", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("backlog") + .arg("-f"); + + let assert = cmd.assert(); + let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); + assert.success(); + + assert!(stdout.contains("Backward Move")); + assert!(stdout.contains("backlog")); + assert!(worklane.join("backlog").join(&filename).exists()); +} + +#[test] +fn mv_blocks_nonsequential_forward_move() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + create_ticket( + &worklane, + "backlog", + "Jump Task", + "Should not skip.", + id, + "dave", + "2026-09-01", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("done") + .assert() + .failure(); + + let stderr = get_stderr(&mut cmd); + assert!(stderr.contains("one stage forward")); +} + +#[test] +fn mv_allows_skipping_stages_with_force() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + let filename = create_ticket( + &worklane, + "backlog", + "Skip Task", + "Force jump.", + id, + "eve", + "2026-08-01", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("done") + .arg("-f") + .assert() + .success(); + + assert!(worklane.join("done").join(&filename).exists()); + let content = fs::read_to_string(worklane.join("done").join(&filename)).unwrap(); + assert!(content.contains("- **-Status**: done")); +} + +#[test] +fn mv_ignores_already_in_same_status() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + let filename = create_ticket( + &worklane, + "reviewing", + "Same Status", + "Already there.", + id, + "frank", + "2026-07-01", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("reviewing") + .assert() + .success(); + + let stdout = get_stdout(&mut cmd); + assert!(stdout.contains("already in")); + assert!(worklane.join("reviewing").join(&filename).exists()); +} + +#[test] +fn mv_rejects_invalid_status() { + let temp = TempDir::new().unwrap(); + let worklane = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + let _filename = create_ticket( + &worklane, + "backlog", + "Invalid Target", + "Bad status.", + id, + "grace", + "2026-06-01", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("invalid") + .assert() + .failure(); + + let stderr = get_stderr(&mut cmd); + assert!(stderr.contains("Invalid target status")); +} + +#[test] +fn mv_uses_ticket_id_for_resolution() { + let temp = TempDir::new().unwrap(); + let wl = setup_worklane(&temp); + + let id = "abc123def456789012345678901234567890"; + let filename = create_ticket( + &wl, + "backlog", + "ID Resolution", + "Use ID to find.", + id, + "hank", + "2026-05-01", + "test", + ); + + let mut cmd = Command::cargo_bin(BIN_NAME).unwrap(); + cmd.current_dir(&temp.path()) + .arg("mv") + .arg("abc123def4567") + .arg("--status") + .arg("committed"); + + let assert = cmd.assert(); + let stdout = String::from_utf8(assert.get_output().stdout.clone()).unwrap(); + assert.success(); + + assert!(stdout.contains("ID Resolution")); + assert!(wl.join("committed").join(&filename).exists()); +}