kanbus 0.14.0

High-performance CLI and web console for the Kanbus issue tracker. Includes kanbus (CLI) and kanbus-console (web UI server).
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
use std::fs;
use std::path::PathBuf;

use chrono::Utc;
use cucumber::{given, then, when};

use kanbus::cli::run_from_args_with_output;
use kanbus::file_io::load_project_directory;
use kanbus::models::{IssueComment, IssueData};

use crate::step_definitions::initialization_steps::KanbusWorld;

fn load_project_dir(world: &KanbusWorld) -> PathBuf {
    let cwd = world.working_directory.as_ref().expect("cwd");
    load_project_directory(cwd).expect("project dir")
}

fn load_issue(project_dir: &PathBuf, identifier: &str) -> IssueData {
    let issue_path = project_dir
        .join("issues")
        .join(format!("{identifier}.json"));
    let contents = fs::read_to_string(issue_path).expect("read issue");
    serde_json::from_str(&contents).expect("parse issue")
}

fn save_issue(project_dir: &PathBuf, issue: &IssueData) {
    let issue_path = project_dir
        .join("issues")
        .join(format!("{}.json", issue.identifier));
    let contents = serde_json::to_string_pretty(issue).expect("serialize issue");
    fs::write(issue_path, contents).expect("write issue");
}

#[given("the current user is \"dev@example.com\"")]
fn given_current_user(_world: &mut KanbusWorld) {
    std::env::set_var("KANBUS_USER", "dev@example.com");
}

#[then("issue \"kanbus-aaa\" should have 1 comment")]
fn then_issue_has_one_comment(world: &mut KanbusWorld) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, "kanbus-aaa");
    assert_eq!(issue.comments.len(), 1);
}

#[then("the latest comment should have author \"dev@example.com\"")]
fn then_latest_author(world: &mut KanbusWorld) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, "kanbus-aaa");
    let latest = issue.comments.last().expect("comment");
    assert_eq!(latest.author, "dev@example.com");
}

#[then("the latest comment should have text \"First comment\"")]
fn then_latest_text(world: &mut KanbusWorld) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, "kanbus-aaa");
    let latest = issue.comments.last().expect("comment");
    assert_eq!(latest.text, "First comment");
}

#[then("the latest comment should have a created_at timestamp")]
fn then_latest_timestamp(world: &mut KanbusWorld) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, "kanbus-aaa");
    let latest = issue.comments.last().expect("comment");
    assert!(latest.created_at.timestamp() > 0);
}

#[then("issue \"kanbus-aaa\" should have comments in order \"First comment\", \"Second comment\"")]
fn then_comments_order(world: &mut KanbusWorld) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, "kanbus-aaa");
    let texts: Vec<String> = issue
        .comments
        .iter()
        .map(|comment| comment.text.clone())
        .collect();
    assert_eq!(
        texts,
        vec!["First comment".to_string(), "Second comment".to_string()]
    );
}

#[then(expr = "issue {string} has a comment from {string} with text {string} and id {string}")]
fn then_issue_has_comment_with_id(
    world: &mut KanbusWorld,
    identifier: String,
    author: String,
    text: String,
    comment_id: String,
) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, &identifier);
    let found = issue.comments.iter().any(|comment| {
        comment.author == author
            && comment.text == text
            && comment.id.as_deref() == Some(comment_id.as_str())
    });
    assert!(found, "expected comment not found");
}

#[given(expr = "issue {string} has a comment from {string} with text {string} and id {string}")]
fn given_issue_has_comment_with_id(
    world: &mut KanbusWorld,
    identifier: String,
    author: String,
    text: String,
    comment_id: String,
) {
    let project_dir = load_project_dir(world);
    let mut issue = load_issue(&project_dir, &identifier);
    issue.comments.push(IssueComment {
        id: Some(comment_id),
        author,
        text,
        created_at: Utc::now(),
    });
    save_issue(&project_dir, &issue);
}

#[then(expr = "issue {string} has a comment from {string} with text {string} and no id")]
fn then_issue_has_comment_without_id(
    world: &mut KanbusWorld,
    identifier: String,
    author: String,
    text: String,
) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, &identifier);
    let found = issue
        .comments
        .iter()
        .any(|comment| comment.author == author && comment.text == text && comment.id.is_none());
    assert!(found, "expected comment not found");
}

#[given(expr = "issue {string} has a comment from {string} with text {string} and no id")]
fn given_issue_has_comment_without_id(
    world: &mut KanbusWorld,
    identifier: String,
    author: String,
    text: String,
) {
    let project_dir = load_project_dir(world);
    let mut issue = load_issue(&project_dir, &identifier);
    issue.comments.push(IssueComment {
        id: None,
        author,
        text,
        created_at: Utc::now(),
    });
    save_issue(&project_dir, &issue);
}

// Additional Given steps for comment manipulation tests

#[given(expr = "an issue {string} exists with a comment missing an id")]
fn given_issue_with_comment_missing_id(world: &mut KanbusWorld, identifier: String) {
    let project_dir = load_project_dir(world);
    let timestamp = Utc::now();
    let issue = IssueData {
        identifier: identifier.clone(),
        title: "Test Issue".to_string(),
        description: String::new(),
        issue_type: "task".to_string(),
        status: "open".to_string(),
        priority: 2,
        assignee: None,
        creator: None,
        parent: None,
        labels: Vec::new(),
        dependencies: Vec::new(),
        comments: vec![IssueComment {
            id: None, // missing id
            author: "user@example.com".to_string(),
            text: "Legacy comment".to_string(),
            created_at: timestamp,
        }],
        created_at: timestamp,
        updated_at: timestamp,
        closed_at: None,
        custom: std::collections::BTreeMap::new(),
    };
    save_issue(&project_dir, &issue);
}

#[given(expr = "an issue {string} exists with comment id {string} and text {string}")]
fn given_issue_with_comment_id_and_text(
    world: &mut KanbusWorld,
    identifier: String,
    comment_id: String,
    text: String,
) {
    let project_dir = load_project_dir(world);
    let timestamp = Utc::now();
    let issue = IssueData {
        identifier: identifier.clone(),
        title: "Test Issue".to_string(),
        description: String::new(),
        issue_type: "task".to_string(),
        status: "open".to_string(),
        priority: 2,
        assignee: None,
        creator: None,
        parent: None,
        labels: Vec::new(),
        dependencies: Vec::new(),
        comments: vec![IssueComment {
            id: Some(comment_id),
            author: "user@example.com".to_string(),
            text,
            created_at: timestamp,
        }],
        created_at: timestamp,
        updated_at: timestamp,
        closed_at: None,
        custom: std::collections::BTreeMap::new(),
    };
    save_issue(&project_dir, &issue);
}

#[given(expr = "an issue {string} exists with comment ids {string} and {string}")]
fn given_issue_with_two_comment_ids(
    world: &mut KanbusWorld,
    identifier: String,
    id1: String,
    id2: String,
) {
    let project_dir = load_project_dir(world);
    let timestamp = Utc::now();
    let issue = IssueData {
        identifier: identifier.clone(),
        title: "Test Issue".to_string(),
        description: String::new(),
        issue_type: "task".to_string(),
        status: "open".to_string(),
        priority: 2,
        assignee: None,
        creator: None,
        parent: None,
        labels: Vec::new(),
        dependencies: Vec::new(),
        comments: vec![
            IssueComment {
                id: Some(id1),
                author: "user@example.com".to_string(),
                text: "First".to_string(),
                created_at: timestamp,
            },
            IssueComment {
                id: Some(id2),
                author: "user@example.com".to_string(),
                text: "Second".to_string(),
                created_at: timestamp,
            },
        ],
        created_at: timestamp,
        updated_at: timestamp,
        closed_at: None,
        custom: std::collections::BTreeMap::new(),
    };
    save_issue(&project_dir, &issue);
}

// When steps for comment operations

#[when(expr = "I ensure comment ids for {string}")]
fn when_ensure_comment_ids(world: &mut KanbusWorld, identifier: String) {
    let cwd = world
        .working_directory
        .as_ref()
        .expect("working directory not set");
    let argv = vec![
        "kanbus".to_string(),
        "comment".to_string(),
        "ensure-ids".to_string(),
        identifier,
    ];

    match run_from_args_with_output(argv, cwd.as_path()) {
        Ok(output) => {
            world.exit_code = Some(0);
            world.stdout = Some(output.stdout);
            world.stderr = Some(String::new());
        }
        Err(error) => {
            world.exit_code = Some(1);
            world.stdout = Some(String::new());
            world.stderr = Some(error.to_string());
        }
    }
}

#[when(expr = "I update comment {string} on {string} to {string}")]
fn when_update_comment(
    world: &mut KanbusWorld,
    comment_prefix: String,
    identifier: String,
    new_text: String,
) {
    let cwd = world
        .working_directory
        .as_ref()
        .expect("working directory not set");
    let argv = vec![
        "kanbus".to_string(),
        "comment".to_string(),
        "update".to_string(),
        identifier,
        comment_prefix,
        new_text,
    ];

    match run_from_args_with_output(argv, cwd.as_path()) {
        Ok(output) => {
            world.exit_code = Some(0);
            world.stdout = Some(output.stdout);
            world.stderr = Some(String::new());
        }
        Err(error) => {
            world.exit_code = Some(1);
            world.stdout = Some(String::new());
            world.stderr = Some(error.to_string());
        }
    }
}

#[when(expr = "I delete comment {string} on {string}")]
fn when_delete_comment(world: &mut KanbusWorld, comment_prefix: String, identifier: String) {
    let cwd = world
        .working_directory
        .as_ref()
        .expect("working directory not set");
    let argv = vec![
        "kanbus".to_string(),
        "comment".to_string(),
        "delete".to_string(),
        identifier,
        comment_prefix,
    ];

    match run_from_args_with_output(argv, cwd.as_path()) {
        Ok(output) => {
            world.exit_code = Some(0);
            world.stdout = Some(output.stdout);
            world.stderr = Some(String::new());
        }
        Err(error) => {
            world.exit_code = Some(1);
            world.stdout = Some(String::new());
            world.stderr = Some(error.to_string());
        }
    }
}

#[when(expr = "I attempt to update comment {string} on {string} to {string}")]
fn when_attempt_update_comment(
    world: &mut KanbusWorld,
    comment_prefix: String,
    identifier: String,
    new_text: String,
) {
    // Replace <empty> with empty string
    let prefix = if comment_prefix == "<empty>" {
        String::new()
    } else {
        comment_prefix
    };
    let cwd = world
        .working_directory
        .as_ref()
        .expect("working directory not set");
    let argv = vec![
        "kanbus".to_string(),
        "comment".to_string(),
        "update".to_string(),
        identifier,
        prefix,
        new_text,
    ];

    match run_from_args_with_output(argv, cwd.as_path()) {
        Ok(output) => {
            world.exit_code = Some(0);
            world.stdout = Some(output.stdout);
            world.stderr = Some(String::new());
        }
        Err(error) => {
            world.exit_code = Some(1);
            world.stdout = Some(String::new());
            world.stderr = Some(error.to_string());
        }
    }
}

#[when(expr = "I attempt to delete comment {string} on {string}")]
fn when_attempt_delete_comment(
    world: &mut KanbusWorld,
    comment_prefix: String,
    identifier: String,
) {
    let cwd = world
        .working_directory
        .as_ref()
        .expect("working directory not set");
    let argv = vec![
        "kanbus".to_string(),
        "comment".to_string(),
        "delete".to_string(),
        identifier,
        comment_prefix,
    ];

    match run_from_args_with_output(argv, cwd.as_path()) {
        Ok(output) => {
            world.exit_code = Some(0);
            world.stdout = Some(output.stdout);
            world.stderr = Some(String::new());
        }
        Err(error) => {
            world.exit_code = Some(1);
            world.stdout = Some(String::new());
            world.stderr = Some(error.to_string());
        }
    }
}

#[when(expr = "I attempt to ensure comment ids for {string}")]
fn when_attempt_ensure_comment_ids(world: &mut KanbusWorld, identifier: String) {
    let cwd = world
        .working_directory
        .as_ref()
        .expect("working directory not set");
    let argv = vec![
        "kanbus".to_string(),
        "comment".to_string(),
        "ensure-ids".to_string(),
        identifier,
    ];

    match run_from_args_with_output(argv, cwd.as_path()) {
        Ok(output) => {
            world.exit_code = Some(0);
            world.stdout = Some(output.stdout);
            world.stderr = Some(String::new());
        }
        Err(error) => {
            world.exit_code = Some(1);
            world.stdout = Some(String::new());
            world.stderr = Some(error.to_string());
        }
    }
}

// Then steps for comment assertions

#[then(expr = "issue {string} should have comment ids assigned")]
fn then_issue_has_comment_ids(world: &mut KanbusWorld, identifier: String) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, &identifier);
    for comment in &issue.comments {
        assert!(comment.id.is_some(), "Comment missing id");
    }
}

#[then(expr = "issue {string} should have comment text {string}")]
fn then_issue_has_comment_text(world: &mut KanbusWorld, identifier: String, expected_text: String) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, &identifier);
    assert_eq!(issue.comments.len(), 1);
    assert_eq!(issue.comments[0].text, expected_text);
}

#[then(expr = "issue {string} should have {int} comments")]
fn then_issue_has_comment_count(world: &mut KanbusWorld, identifier: String, count: usize) {
    let project_dir = load_project_dir(world);
    let issue = load_issue(&project_dir, &identifier);
    assert_eq!(issue.comments.len(), count);
}

#[then(expr = "the last comment operation should fail with {string}")]
fn then_last_comment_op_fails(world: &mut KanbusWorld, expected_error: String) {
    assert_eq!(world.exit_code, Some(1), "Expected operation to fail");
    let stderr = world.stderr.as_ref().expect("stderr");
    assert!(
        stderr.contains(&expected_error),
        "Expected error containing '{}', got: {}",
        expected_error,
        stderr
    );
}