cliban 0.4.0

Self-hosted, agent-first kanban for the terminal: flat CLI, ratatui board, SQLite storage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//! `cliban milestone` subcommands. Output is byte-for-byte parity with the Go
//! oracle (`internal/cli/milestone.go`).

use chrono::NaiveDate;
use serde_json::{json, Map, Value};

use cliban_core::contexts::issues::ListOpts;
use cliban_core::contexts::milestones::{CreateMilestone, UpdateMilestone};
use cliban_core::contexts::{issues, milestones};
use cliban_core::schema::Milestone;
use cliban_core::time::{format_date, format_usec, parse_date};
use cliban_core::Store;

use crate::cmd::issue::issue_json_inputs;
use crate::errors::{CliError, CliResult};
use crate::output::{build_issue_json, write_issue_table, Detail, IssueRow};
use crate::store_open;

#[derive(clap::Args)]
pub struct MilestoneArgs {
    #[command(subcommand)]
    pub cmd: MilestoneCmd,
}

#[derive(clap::Subcommand)]
pub enum MilestoneCmd {
    /// Partition a milestone's open issues into dependency waves (wave N is
    /// safe to start once waves 1..N-1 are done)
    Waves {
        /// project key
        #[arg(long)]
        project: String,
        /// milestone name
        name: String,
        #[arg(long)]
        json: bool,
    },
    /// Add a milestone
    Add {
        #[arg(long)]
        project: String,
        #[arg(long)]
        name: String,
        #[arg(long, allow_hyphen_values = true)]
        description: Option<String>,
        #[arg(long = "description-file")]
        description_file: Option<String>,
        /// target date YYYY-MM-DD
        #[arg(long)]
        target: Option<String>,
        #[arg(long)]
        json: bool,
    },
    /// List milestones
    Ls {
        /// project key; omit to list milestones across every project
        #[arg(long)]
        project: Option<String>,
        /// filter by status (open|completed|cancelled)
        #[arg(long)]
        status: Option<String>,
        /// order: activity (most recently worked on) | name | target
        #[arg(long, default_value = "name")]
        sort: String,
        /// add done/total progress and last-activity columns
        #[arg(long)]
        stats: bool,
        #[arg(long)]
        json: bool,
        /// include each milestone's `description` body in --json output
        #[arg(long)]
        full: bool,
    },
    /// Show a milestone (accepts positional NAME or --name)
    Show {
        /// milestone name (or pass via --name)
        name: Option<String>,
        #[arg(long)]
        project: String,
        #[arg(long = "name")]
        name_flag: Option<String>,
        #[arg(long)]
        json: bool,
        #[arg(long = "with-issues")]
        with_issues: bool,
        /// include each listed issue's `description` body (--with-issues)
        #[arg(long)]
        full: bool,
    },
    /// Edit a milestone
    Edit {
        #[arg(long)]
        project: String,
        #[arg(long)]
        name: String,
        /// new name
        #[arg(long)]
        rename: Option<String>,
        #[arg(long, allow_hyphen_values = true)]
        description: Option<String>,
        #[arg(long = "description-file")]
        description_file: Option<String>,
        /// new status (open|completed|cancelled)
        #[arg(long)]
        status: Option<String>,
        /// new target date YYYY-MM-DD
        #[arg(long)]
        target: Option<String>,
        /// clear target date
        #[arg(long = "clear-target")]
        clear_target: bool,
    },
    /// Cancels instead of deleting (kept for muscle memory; hidden)
    #[command(hide = true)]
    Rm {
        #[arg(long)]
        project: String,
        #[arg(long)]
        name: String,
    },
}

pub async fn run(db: &Option<String>, args: MilestoneArgs) -> CliResult<()> {
    match args.cmd {
        MilestoneCmd::Waves {
            project,
            name,
            json,
        } => waves(db, project, name, json).await,
        MilestoneCmd::Add {
            project,
            name,
            description,
            description_file,
            target,
            json,
        } => {
            add(
                db,
                project,
                name,
                description,
                description_file,
                target,
                json,
            )
            .await
        }
        MilestoneCmd::Ls {
            project,
            status,
            sort,
            stats,
            json,
            full,
        } => ls(db, project, status, sort, stats, json, full).await,
        MilestoneCmd::Show {
            name,
            project,
            name_flag,
            json,
            with_issues,
            full,
        } => show(db, name, project, name_flag, json, with_issues, full).await,
        MilestoneCmd::Edit {
            project,
            name,
            rename,
            description,
            description_file,
            status,
            target,
            clear_target,
        } => {
            edit(
                db,
                project,
                name,
                rename,
                description,
                description_file,
                status,
                target,
                clear_target,
            )
            .await
        }
        MilestoneCmd::Rm { project, name } => {
            // `cancelled` is a milestone's archived state, so that is what a
            // delete becomes.
            let project_key = project.to_uppercase();
            edit(
                db,
                project_key.clone(),
                name.clone(),
                None,
                None,
                None,
                Some("cancelled".to_string()),
                None,
                false,
            )
            .await?;
            println!(
                "cancelled milestone {name} in {project_key} — cliban cancels instead of \
                 deleting (undo: cliban milestone edit --project {project_key} --name {name} \
                 --status open)"
            );
            Ok(())
        }
    }
}

/// `parseTarget`: empty → None; otherwise parse `YYYY-MM-DD`. A parse failure is
/// a plain (exit-3) error in Go (NOT wrapped in ErrValidation).
fn parse_target(s: &str) -> CliResult<Option<NaiveDate>> {
    if s.is_empty() {
        return Ok(None);
    }
    match parse_date(s) {
        Some(d) => Ok(Some(d)),
        None => Err(CliError::other(format!(
            "invalid --target {s:?} (want YYYY-MM-DD)"
        ))),
    }
}

/// `resolveDescription`: returns `(content, was_set)`. `--description` and
/// `--description-file` are mutually exclusive; `-` reads stdin.
fn resolve_description(
    description: Option<String>,
    description_file: Option<String>,
) -> CliResult<(String, bool)> {
    use std::io::Read;
    if let Some(file) = description_file {
        if description.is_some() {
            return Err(CliError::validation(
                "--description and --description-file are mutually exclusive",
            ));
        }
        if file == "-" {
            let mut buf = String::new();
            std::io::stdin()
                .read_to_string(&mut buf)
                .map_err(|e| CliError::other(e.to_string()))?;
            return Ok((buf, true));
        }
        match std::fs::read_to_string(&file) {
            Ok(s) => Ok((s, true)),
            Err(e) => Err(CliError::validation(e.to_string())),
        }
    } else if let Some(desc) = description {
        if desc == "-" {
            let mut buf = String::new();
            std::io::stdin()
                .read_to_string(&mut buf)
                .map_err(|e| CliError::other(e.to_string()))?;
            Ok((buf, true))
        } else {
            Ok((desc, true))
        }
    } else {
        Ok((String::new(), false))
    }
}

/// Count non-archived issues with the given milestone name in the given project
/// (mirrors `milestoneToJSON`'s issue_count).
async fn issue_count(store: &Store, project: String, milestone: String) -> CliResult<i64> {
    let n = store
        .call(move |conn| {
            let list = issues::list(
                conn,
                ListOpts {
                    project: Some(&project),
                    milestone: Some(&milestone),
                    archived: false,
                    ..Default::default()
                },
            )?;
            Ok(list.len() as i64)
        })
        .await?;
    Ok(n)
}

fn milestone_json(m: &Milestone, project: &str, count: i64, detail: Detail) -> Value {
    crate::output::build_milestone_json(
        &m.name,
        Some(project.to_string()),
        &m.description,
        m.target_date.map(format_date),
        &m.status,
        &format_usec(m.inserted_at),
        &format_usec(m.updated_at),
        count,
        detail,
    )
}

async fn add(
    db: &Option<String>,
    project: String,
    name: String,
    description: Option<String>,
    description_file: Option<String>,
    target: Option<String>,
    json: bool,
) -> CliResult<()> {
    let project_key = project.to_uppercase();
    let target_date = parse_target(target.as_deref().unwrap_or(""))?;
    let (description, _set) = resolve_description(description, description_file)?;

    let store = store_open::open(db).await?;
    let create_project = project_key.clone();
    let create_name = name.clone();
    let m = store
        .call(move |conn| {
            milestones::create(
                conn,
                CreateMilestone {
                    project: create_project,
                    name: create_name,
                    description: Some(description),
                    target_date,
                    status: None,
                },
            )
        })
        .await?;

    if json {
        let count = issue_count(&store, project_key.clone(), m.name.clone()).await?;
        let v = milestone_json(&m, &project_key, count, Detail::Full);
        println!("{}", serde_json::to_string_pretty(&v).unwrap());
    } else {
        println!("created milestone {} in {}", m.name, project_key);
    }
    Ok(())
}

async fn ls(
    db: &Option<String>,
    project: Option<String>,
    status: Option<String>,
    sort: String,
    stats: bool,
    json: bool,
    full: bool,
) -> CliResult<()> {
    let project_key = project.map(|p| p.to_uppercase());
    let sort = milestones::Sort::parse(&sort).ok_or_else(|| {
        CliError::validation(format!(
            "invalid --sort {sort:?} (want activity, name or target)"
        ))
    })?;
    let status = status.filter(|s| !s.is_empty());

    let store = store_open::open(db).await?;
    let (key, st) = (project_key.clone(), status.clone());
    let ms = store
        .call(move |conn| {
            milestones::summaries(
                conn,
                milestones::SummaryOpts {
                    project: key.as_deref(),
                    status: st.as_deref(),
                    sort,
                },
            )
        })
        .await?;

    let now = cliban_core::time::now_usec();
    if json {
        for s in &ms {
            let v = if stats {
                milestone_stats_json(s, now)
            } else {
                milestone_json(
                    &s.milestone,
                    &s.project_key,
                    s.total,
                    Detail::from_full_flag(full),
                )
            };
            println!("{}", serde_json::to_string(&v).unwrap());
        }
        return Ok(());
    }

    // The scoped, no-stats form stays byte-identical to the original three
    // columns; the project column only appears when the listing spans
    // projects, and the rollups only when asked for. With `--stats` the name
    // column is sized to the data (real milestone names run well past 15
    // chars) so the numeric columns stay in line.
    let name_width = ms
        .iter()
        .map(|s| s.milestone.name.chars().count())
        .max()
        .unwrap_or(15)
        .clamp(15, 44);
    for s in &ms {
        let m = &s.milestone;
        let tgt = m
            .target_date
            .map(format_date)
            .unwrap_or_else(|| "-".to_string());
        let mut line = String::new();
        if project_key.is_none() {
            line.push_str(&format!("{:<8} ", s.project_key));
        }
        if stats {
            // Pad the (variable-width) target so the rollup columns line up.
            line.push_str(&format!(
                "{:<name_width$} {:<10} {:<12} {:>7}  {}",
                m.name,
                m.status,
                tgt,
                format!("{}/{}", s.done, s.total),
                cliban_core::time::relative(s.last_activity, now)
            ));
        } else {
            line.push_str(&format!("{:<15} {:<10} {}", m.name, m.status, tgt));
        }
        println!("{}", line.trim_end());
    }
    Ok(())
}

/// `--stats` JSON: the plain milestone object plus the rollups, keys kept in
/// alphabetical order like the parity form.
fn milestone_stats_json(
    s: &milestones::MilestoneSummary,
    now: chrono::DateTime<chrono::Utc>,
) -> Value {
    let m = &s.milestone;
    let mut map = Map::new();
    map.insert("created_at".into(), json!(format_usec(m.inserted_at)));
    map.insert("description".into(), json!(m.description));
    map.insert("done_count".into(), json!(s.done));
    map.insert("issue_count".into(), json!(s.total));
    map.insert("last_activity".into(), json!(format_usec(s.last_activity)));
    map.insert(
        "last_activity_human".into(),
        json!(cliban_core::time::relative(s.last_activity, now)),
    );
    map.insert("name".into(), json!(m.name));
    map.insert("project".into(), json!(s.project_key));
    map.insert("status".into(), json!(m.status));
    map.insert(
        "target_date".into(),
        match m.target_date.map(format_date) {
            Some(t) => json!(t),
            None => Value::Null,
        },
    );
    map.insert("updated_at".into(), json!(format_usec(m.updated_at)));
    Value::Object(map)
}

async fn show(
    db: &Option<String>,
    name: Option<String>,
    project: String,
    name_flag: Option<String>,
    json: bool,
    with_issues: bool,
    full: bool,
) -> CliResult<()> {
    // name = positional XOR --name (equal is ok); none → validation error.
    let mut resolved = name_flag.clone().unwrap_or_default();
    if let Some(pos) = &name {
        if !resolved.is_empty() && &resolved != pos {
            return Err(CliError::validation(
                "pass NAME positionally OR via --name, not both",
            ));
        }
        resolved = pos.clone();
    }
    if resolved.is_empty() {
        return Err(CliError::validation(
            "milestone name required (positional or --name)",
        ));
    }
    if project.is_empty() {
        return Err(CliError::validation("--project is required"));
    }
    let project_key = project.to_uppercase();

    let store = store_open::open(db).await?;
    let get_key = project_key.clone();
    let get_name = resolved.clone();
    let m = store
        .call(move |conn| milestones::get(conn, &get_key, &get_name))
        .await?
        .ok_or(cliban_core::Error::NotFound)?;

    // Resolve issue list for the milestone (non-archived, this project).
    let list_key = project_key.clone();
    let list_name = resolved.clone();
    let issue_list = store
        .call(move |conn| {
            issues::list(
                conn,
                ListOpts {
                    project: Some(&list_key),
                    milestone: Some(&list_name),
                    archived: false,
                    ..Default::default()
                },
            )
        })
        .await?;
    let count = issue_list.len() as i64;

    if json {
        // Build alpha-ordered map inline so `issues` lands between issue_count
        // and name (matches Go's map[string]any alphabetical serialization).
        let mut map = Map::new();
        map.insert("created_at".into(), json!(format_usec(m.inserted_at)));
        map.insert("description".into(), json!(m.description));
        map.insert("issue_count".into(), json!(count));
        if with_issues {
            let mut arr = Vec::with_capacity(issue_list.len());
            for i in &issue_list {
                let inputs = issue_json_inputs(&store, i).await?;
                arr.push(build_issue_json(inputs, Detail::from_full_flag(full)));
            }
            map.insert("issues".into(), Value::Array(arr));
        }
        map.insert("name".into(), json!(m.name));
        map.insert("project".into(), json!(project_key));
        map.insert("status".into(), json!(m.status));
        map.insert(
            "target_date".into(),
            match m.target_date.map(format_date) {
                Some(s) => json!(s),
                None => Value::Null,
            },
        );
        map.insert("updated_at".into(), json!(format_usec(m.updated_at)));
        println!(
            "{}",
            serde_json::to_string_pretty(&Value::Object(map)).unwrap()
        );
        return Ok(());
    }

    let tgt = m
        .target_date
        .map(format_date)
        .unwrap_or_else(|| "-".to_string());
    print!(
        "{} — {}\nstatus:  {}\ntarget:  {}\nissues:  {}\n{}\n",
        m.name, project_key, m.status, tgt, count, m.description
    );
    if with_issues {
        println!();
        let mut rows = Vec::with_capacity(issue_list.len());
        for i in &issue_list {
            let (ms_name, parent_key) = resolve_refs(&store, i).await?;
            rows.push(IssueRow {
                key: i.key.clone(),
                title: i.title.clone(),
                status: i.status.clone(),
                priority: i.priority.clone(),
                milestone: ms_name,
                parent: parent_key,
            });
        }
        print!("{}", write_issue_table(&rows));
    }
    Ok(())
}

/// Resolve milestone name + parent key for an issue (empty when unset).
async fn resolve_refs(
    store: &Store,
    issue: &cliban_core::schema::Issue,
) -> CliResult<(String, String)> {
    let milestone_id = issue.milestone_id;
    let parent_id = issue.parent_id;
    let pair = store
        .call(move |conn| {
            let milestone = match milestone_id {
                Some(mid) => milestones::get_by_id(conn, mid)?.map(|m| m.name),
                None => None,
            };
            let parent = match parent_id {
                Some(pid) => issues::get_by_id(conn, pid)?.map(|i| i.key),
                None => None,
            };
            Ok((milestone.unwrap_or_default(), parent.unwrap_or_default()))
        })
        .await?;
    Ok(pair)
}

#[allow(clippy::too_many_arguments)]
async fn edit(
    db: &Option<String>,
    project: String,
    name: String,
    rename: Option<String>,
    description: Option<String>,
    description_file: Option<String>,
    status: Option<String>,
    target: Option<String>,
    clear_target: bool,
) -> CliResult<()> {
    let project_key = project.to_uppercase();
    let (desc_content, desc_set) = resolve_description(description, description_file)?;

    let mut params = UpdateMilestone::default();
    if let Some(r) = rename {
        params.name = Some(r);
    }
    if desc_set {
        params.description = Some(desc_content);
    }
    if let Some(s) = status {
        params.status = Some(s);
    }
    if clear_target {
        params.target_date = Some(None);
    } else if let Some(t) = &target {
        let d = parse_target(t)?;
        params.target_date = Some(d);
    }

    let store = store_open::open(db).await?;
    store
        .call(move |conn| {
            let cur =
                milestones::get(conn, &project_key, &name)?.ok_or(cliban_core::Error::NotFound)?;
            milestones::update(conn, &cur, params)?;
            Ok(())
        })
        .await?;
    Ok(())
}

/// `milestone waves`: print the dependency-wave partition the core computes —
/// the deterministic answer to "what can run in parallel, in what order" that
/// orchestrators previously derived by hand from the blocking graph.
async fn waves(db: &Option<String>, project: String, name: String, json: bool) -> CliResult<()> {
    let project = project.to_uppercase();
    let store = store_open::open(db).await?;
    let w = store
        .call(move |conn| milestones::waves(conn, &project, &name))
        .await?;
    if json {
        println!(
            "{}",
            json!({
                "done": w.done,
                "external_blocked": w.external_blocked,
                "waves": w.waves,
            })
        );
        return Ok(());
    }
    if w.waves.is_empty() && w.external_blocked.is_empty() {
        println!("no open issues");
    }
    for (i, wave) in w.waves.iter().enumerate() {
        println!("wave {}: {}", i + 1, wave.join(", "));
    }
    if !w.external_blocked.is_empty() {
        println!("externally blocked: {}", w.external_blocked.join(", "));
    }
    if !w.done.is_empty() {
        println!("done: {}", w.done.join(", "));
    }
    Ok(())
}