tix-tracker 0.12.0

A terminal issue tracker: markdown + YAML frontmatter issues with a kanban TUI and CLI
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
#[allow(clippy::wildcard_imports)]
use super::*;

pub(super) fn create_epic(_config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let name = req_string(args, "name")?;
    let description = opt_string(args, "description")?;
    let status = opt_string(args, "status")?;
    let owner = opt_string(args, "owner")?;
    // Dates and the name-keyed references are trimmed and a blank one is absent,
    // exactly as `tix epic create` normalizes them; the other optional fields are
    // kept verbatim and the service normalizes a written-but-blank value.
    let start_date = trimmed_optional(opt_string(args, "start_date")?);
    let due_date = trimmed_optional(opt_string(args, "due_date")?);
    let milestone = trimmed_optional(opt_string(args, "milestone")?);
    let cycle = trimmed_optional(opt_string(args, "cycle")?);
    let weight = match epic_create_weight(args, "weight") {
        Ok(weight) => weight,
        Err(outcome) => return outcome,
    };
    let projects = opt_string(args, "projects")?
        .map(|raw| crate::commands::parse_csv_list(&raw))
        .unwrap_or_default();
    let (mut config, path) = match registry_config(store) {
        Ok(loaded) => loaded,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let new = crate::epics::NewEpic {
        name,
        description,
        status,
        owner,
        start_date,
        due_date,
        weight,
        projects,
        milestone,
        cycle,
    };
    // The service allocates the stable id and validates name, dates, weight and
    // the project-id shapes, so an invalid request refuses here with the registry
    // untouched.
    match crate::epics::create_epic(&mut config, &new) {
        Ok(epic) => {
            if let Err(e) = save_registry_config(&mut config, &path) {
                return Ok(error_result(crate::error::render_error(&e)));
            }
            Ok(success_result(
                format!(
                    "Created {}",
                    crate::epics::epic_display(&epic.id, &epic.name)
                ),
                epic_record_value(&epic),
            ))
        }
        Err(error) => Ok(epic_refusal(error)),
    }
}

pub(super) fn delete_epic(_config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let reference = req_string(args, "epic")?;
    // The schema declares `confirm` required, but a schema is documentation a
    // client may ignore; checked before anything is resolved, so a forgotten
    // confirmation writes nothing (the convention `delete_project` follows).
    if opt_bool(args, "confirm")? != Some(true) {
        return Ok(error_result(
            "invalid input: `confirm` must be true to delete an epic — only the registry \
             entry is removed and neither an issue nor a child epic is deleted"
                .to_string(),
        ));
    }
    let (mut config, path) = match registry_config(store) {
        Ok(loaded) => loaded,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let issues = match store.list_all() {
        Ok(issues) => issues,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    // Resolved before the deletion so the impact report describes the epic that
    // still exists — the order `tix epic delete` uses.
    let epic = match crate::epics::resolve(&config, &issues, &reference) {
        Ok(epic) => epic,
        Err(error) => return Ok(epic_refusal(error)),
    };
    // Removes the registry entry **only**; the service reports every affected issue
    // and every child epic, so this response carries the CLI's impact report and
    // neither is ever deleted (C-005, C-006).
    match crate::epics::delete_epic(&mut config, &reference, &issues) {
        Ok(deletion) => {
            if let Err(e) = save_registry_config(&mut config, &path) {
                return Ok(error_result(crate::error::render_error(&e)));
            }
            let affected: Vec<Value> = deletion.affected.iter().map(epic_member_value).collect();
            let children: Vec<Value> = deletion.child_epics.iter().map(epic_ref_value).collect();
            let text = format!(
                "Deleted epic {}\n{}",
                crate::epics::epic_display(&deletion.id, &deletion.name),
                cmd_epic::impact_notice(&epic),
            );
            Ok(success_result(
                text,
                json!({
                    "id": deletion.id.as_str(),
                    "name": deletion.name,
                    "affected": affected,
                    "child_epics": children,
                }),
            ))
        }
        Err(error) => Ok(epic_refusal(error)),
    }
}

pub(super) fn epic_associate_issues(_config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let reference = req_string(args, "epic")?;
    let selectors = crate::commands::parse_csv_list(&req_string(args, "issues")?);
    if selectors.is_empty() {
        return Ok(error_result(
            "invalid input: `issues` must name at least one issue selector".to_string(),
        ));
    }
    let (config, _path) = match registry_config(store) {
        Ok(loaded) => loaded,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let store = store_with_config(store, config);
    // Resolved **once**, up front, so an unknown epic refuses before any write
    // (mirroring `tix epic add`). The id, not the reference, is what the
    // service-backed bulk write receives.
    let id = match crate::epics::resolve_epic_id(&store.config.epics, &reference) {
        Ok(id) => id,
        Err(error) => return Ok(epic_refusal(error)),
    };
    match store.bulk_reference(id.as_str(), &selectors) {
        Ok(outcomes) => {
            let changed = outcomes
                .iter()
                .filter(|outcome| outcome.outcome.as_ref().is_ok_and(|a| a.changed))
                .count();
            let failed = outcomes
                .iter()
                .filter(|outcome| outcome.outcome.is_err())
                .count();
            let rows: Vec<Value> = outcomes.iter().map(epic_reference_outcome_value).collect();
            let changes: Vec<String> = outcomes
                .iter()
                .map(|outcome| match &outcome.outcome {
                    Ok(applied) if applied.changed => {
                        format!("{}: referenced", outcome.selector)
                    }
                    Ok(_) => format!("{}: already referenced", outcome.selector),
                    Err(error) => {
                        format!(
                            "{}: {}",
                            outcome.selector,
                            crate::error::render_error(error)
                        )
                    }
                })
                .collect();
            let text = format!(
                "referenced {changed} of {} issue(s) by {id}\n{}",
                outcomes.len(),
                changes.join("\n")
            );
            Ok(success_result(
                text,
                json!({
                    "epic_id": id.as_str(),
                    "changed": changed,
                    "failed": failed,
                    "total": outcomes.len(),
                    "outcomes": rows,
                }),
            ))
        }
        Err(error) => Ok(error_result(crate::error::render_error(&error))),
    }
}

pub(super) fn epic_dissociate_issues(_config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let reference = req_string(args, "epic")?;
    let selectors = crate::commands::parse_csv_list(&req_string(args, "issues")?);
    if selectors.is_empty() {
        return Ok(error_result(
            "invalid input: `issues` must name at least one issue selector".to_string(),
        ));
    }
    let (config, _path) = match registry_config(store) {
        Ok(loaded) => loaded,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let store = store_with_config(store, config);
    // Resolve when the reference names a live epic; otherwise use it **verbatim**,
    // which is the repair path C-005 promises and exactly what `tix epic remove`
    // does. The one owner of that classification is the service's
    // `epics::dereference_needle` (WP09 item B).
    let needle = crate::epics::dereference_needle(&store.config.epics, &reference);
    let mut rows: Vec<Value> = Vec::with_capacity(selectors.len());
    let mut changes: Vec<String> = Vec::with_capacity(selectors.len());
    let mut changed = 0usize;
    let mut failed = 0usize;
    for selector in &selectors {
        match store.dereference_epic(selector, &needle) {
            Ok(applied) => {
                if applied.changed {
                    changed += 1;
                    changes.push(format!("{selector}: dereferenced"));
                } else {
                    changes.push(format!("{selector}: was not referenced"));
                }
                rows.push(json!({
                    "selector": selector,
                    "changed": applied.changed,
                    "epic_ids": applied.epic_ids,
                }));
            }
            Err(error) => {
                failed += 1;
                changes.push(format!(
                    "{selector}: {}",
                    crate::error::render_error(&error)
                ));
                rows.push(
                    json!({ "selector": selector, "error": crate::error::render_error(&error) }),
                );
            }
        }
    }
    let text = format!(
        "dereferenced {changed} of {} issue(s) from {needle}\n{}",
        selectors.len(),
        changes.join("\n")
    );
    Ok(success_result(
        text,
        json!({
            "epic_id": needle,
            "changed": changed,
            "failed": failed,
            "total": selectors.len(),
            "outcomes": rows,
        }),
    ))
}

pub(super) fn epic_membership(_config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let reference = req_string(args, "epic")?;
    let (config, _path) = match registry_config(store) {
        Ok(loaded) => loaded,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let store = store_with_config(store, config);
    let issues = match store.list_all() {
        Ok(issues) => issues,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    // The service's derived membership — the same value `tix epic show` and the
    // `list_issues {epic}` filter read. Read-only: nothing is written.
    match crate::epics::membership(&store.config, &issues, &reference) {
        Ok(membership) => {
            let members: Vec<Value> = membership.members.iter().map(epic_member_value).collect();
            let text = format!(
                "{}: {} member(s)",
                membership.epic_id, membership.member_count
            );
            Ok(success_result(
                text,
                json!({
                    "epic_id": membership.epic_id.as_str(),
                    "member_count": membership.member_count,
                    "members": members,
                }),
            ))
        }
        Err(error) => Ok(epic_refusal(error)),
    }
}

pub(super) fn get_epic(_config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let reference = req_string(args, "epic")?;
    let (config, _path) = match registry_config(store) {
        Ok(loaded) => loaded,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let store = store_with_config(store, config);
    let issues = match store.list_all() {
        Ok(issues) => issues,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let index = crate::epics::EpicIndex::build(&store.config, &issues);
    let epic = match index.resolve(&reference) {
        Ok(epic) => epic.clone(),
        Err(error) => return Ok(epic_refusal(error)),
    };
    let targets = match epic_reference_targets(&store.config, &store) {
        Ok(targets) => targets,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let (graph_issues, done_column) = match store.graph_source() {
        Ok(source) => source,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let graph = crate::commands::graph_over(&graph_issues, &done_column);
    let rollup = match index.rollup(&store.config, &reference, &targets, &graph) {
        Ok(rollup) => rollup,
        Err(error) => return Ok(epic_refusal(error)),
    };
    let mut value = epic_record_value(&epic);
    value["rollup"] = epic_rollup_value(&rollup);
    Ok(success_result(
        cmd_epic::record_lines(&epic, Some(&rollup)).join("\n"),
        value,
    ))
}

pub(super) fn list_epics(_config: &Config, store: &Store, _args: &Value) -> ToolOutcome {
    let (config, _path) = match registry_config(store) {
        Ok(loaded) => loaded,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    let issues = match store.list_all() {
        Ok(issues) => issues,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    // The service's own listing: ids, names and the derived member and child
    // counts, in declaration order. No count is computed here.
    let index = crate::epics::EpicIndex::build(&config, &issues);
    let structured: Vec<Value> = index.epics().into_iter().map(epic_record_value).collect();
    // The snapshot's **issue-held** unresolvable references (a reference to an
    // epic that no longer exists, or an entry that is not a well-formed epic id),
    // exactly as `tix lint` reports them (C-005). `epic_health` carries the
    // epic-held half for one epic; the two together cover all five reference kinds
    // without re-deriving any of them.
    let missing: Vec<Value> = index.missing().iter().map(epic_missing_value).collect();
    Ok(success_result(
        cmd_epic::listing_lines(&config, &issues).join("\n"),
        json!({ "epics": structured, "missing_references": missing }),
    ))
}

pub(super) fn update_epic(_config: &Config, store: &Store, args: &Value) -> ToolOutcome {
    let reference = req_string(args, "epic")?;
    let name = opt_string(args, "name")?;
    let description = opt_string(args, "description")?;
    let status = opt_string(args, "status")?;
    let owner = opt_string(args, "owner")?;
    let start_date = opt_string(args, "start_date")?;
    let due_date = opt_string(args, "due_date")?;
    let parent = opt_string(args, "parent")?;
    let projects = opt_string(args, "projects")?;
    let milestone = opt_string(args, "milestone")?;
    let cycle = opt_string(args, "cycle")?;
    let weight = match epic_weight_patch(args, "weight") {
        Ok(weight) => weight,
        Err(outcome) => return outcome,
    };
    let has_weight = !matches!(args.get("weight"), None | Some(Value::Null));
    if name.is_none()
        && description.is_none()
        && status.is_none()
        && owner.is_none()
        && start_date.is_none()
        && due_date.is_none()
        && parent.is_none()
        && projects.is_none()
        && milestone.is_none()
        && cycle.is_none()
        && !has_weight
    {
        return Ok(error_result(epic_edit_requires_a_field()));
    }
    // The patch shape `tix epic edit` builds: `name` always sets (a name is never
    // cleared, which the service refuses as a duplicate-name diagnostic), the
    // other text fields follow the blank-clears convention, and `projects`
    // **replaces the whole list** (blank clears it).
    let projects_patch = match projects {
        None => crate::epics::Patch::Unchanged,
        Some(value) if value.trim().is_empty() => crate::epics::Patch::Clear,
        Some(value) => crate::epics::Patch::Set(crate::commands::parse_csv_list(&value)),
    };
    let edit = crate::epics::EpicEdit {
        name: name.map_or(crate::epics::Patch::Unchanged, crate::epics::Patch::Set),
        description: text_patch(description),
        status: text_patch(status),
        owner: text_patch(owner),
        start_date: date_patch(start_date),
        due_date: date_patch(due_date),
        weight,
        parent: text_patch(parent),
        projects: projects_patch,
        milestone: text_patch(milestone),
        cycle: text_patch(cycle),
    };
    let (mut config, path) = match registry_config(store) {
        Ok(loaded) => loaded,
        Err(e) => return Ok(error_result(crate::error::render_error(&e))),
    };
    match crate::epics::edit_epic(&mut config, &reference, &edit) {
        Ok(epic) => {
            if let Err(e) = save_registry_config(&mut config, &path) {
                return Ok(error_result(crate::error::render_error(&e)));
            }
            // Re-resolve through an index so the response carries the derived
            // members and children: a registry-only edit has no issue set to derive
            // from and would report `member_count: 0` for an epic that has members.
            let store = store_with_config(store, config);
            let issues = match store.list_all() {
                Ok(issues) => issues,
                Err(e) => return Ok(error_result(crate::error::render_error(&e))),
            };
            let index = crate::epics::EpicIndex::build(&store.config, &issues);
            match index.resolve(epic.id.as_str()) {
                Ok(epic) => Ok(success_result(
                    format!(
                        "Updated {}",
                        crate::epics::epic_display(&epic.id, &epic.name)
                    ),
                    epic_record_value(epic),
                )),
                Err(error) => Ok(epic_refusal(error)),
            }
        }
        Err(error) => Ok(epic_refusal(error)),
    }
}

pub(super) fn dispatch(
    config: &Config,
    store: &Store,
    name: &str,
    arguments: &Value,
) -> Option<ToolOutcome> {
    match name {
        "list_epics" => Some(list_epics(config, store, arguments)),
        "get_epic" => Some(get_epic(config, store, arguments)),
        "create_epic" => Some(create_epic(config, store, arguments)),
        "update_epic" => Some(update_epic(config, store, arguments)),
        "delete_epic" => Some(delete_epic(config, store, arguments)),
        "epic_membership" => Some(epic_membership(config, store, arguments)),
        "epic_associate_issues" => Some(epic_associate_issues(config, store, arguments)),
        "epic_dissociate_issues" => Some(epic_dissociate_issues(config, store, arguments)),
        _ => None,
    }
}