grite 0.3.0

Git-backed issue tracker with CRDT merging, designed for AI coding agents
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
use libgrite_core::{
    hash::compute_event_id,
    lock::LockCheckResult,
    types::event::{Event, EventKind, IssueState},
    types::ids::{generate_issue_id, id_to_hex, hex_to_id, parse_issue_id},
    types::issue::IssueSummary,
    store::IssueFilter,
    GriteError,
};
use libgrite_git;
use serde::Serialize;
use crate::cli::{Cli, IssueCommand, LabelCommand, AssigneeCommand, LinkCommand, AttachmentCommand};
use crate::context::GriteContext;
use crate::output::output_success;
use crate::event_helper::insert_and_append;

/// Check lock for an issue operation
///
/// Returns Ok(()) if operation can proceed, with warnings printed to stderr if applicable.
/// Returns Err if blocked by lock policy.
fn check_issue_lock(cli: &Cli, ctx: &GriteContext, issue_id_hex: &str) -> Result<(), GriteError> {
    let resource = format!("issue:{}", issue_id_hex);
    match ctx.check_lock(&resource)? {
        LockCheckResult::Clear => Ok(()),
        LockCheckResult::Warning(conflicts) => {
            if !cli.quiet {
                for lock in &conflicts {
                    eprintln!(
                        "Warning: {} is locked by {} (expires in {}s)",
                        lock.resource,
                        lock.owner,
                        lock.time_remaining_ms() / 1000
                    );
                }
            }
            Ok(())
        }
        LockCheckResult::Blocked(_) => {
            // This case is handled by ctx.check_lock returning Err
            unreachable!()
        }
    }
}

/// Check repo-level lock (for issue creation)
fn check_repo_lock(cli: &Cli, ctx: &GriteContext) -> Result<(), GriteError> {
    match ctx.check_lock("repo:global")? {
        LockCheckResult::Clear => Ok(()),
        LockCheckResult::Warning(conflicts) => {
            if !cli.quiet {
                for lock in &conflicts {
                    eprintln!(
                        "Warning: {} is locked by {} (expires in {}s)",
                        lock.resource,
                        lock.owner,
                        lock.time_remaining_ms() / 1000
                    );
                }
            }
            Ok(())
        }
        LockCheckResult::Blocked(_) => unreachable!(),
    }
}

/// RAII guard for auto-releasing locks
struct LockGuard<'a> {
    ctx: &'a GriteContext,
    resource: String,
    acquired: bool,
}

impl<'a> LockGuard<'a> {
    /// Acquire a lock if requested
    fn acquire(ctx: &'a GriteContext, issue_id_hex: &str, should_lock: bool) -> Result<Self, GriteError> {
        let resource = format!("issue:{}", issue_id_hex);
        if should_lock {
            let lock_manager = ctx.open_lock_manager()
                .map_err(|e| GriteError::Internal(e.to_string()))?;
            lock_manager.acquire(&resource, &ctx.actor_id, None)
                .map_err(|e| match e {
                    libgrite_git::GitError::LockConflict { resource, owner, expires_in_ms } => {
                        GriteError::Conflict(format!(
                            "Cannot acquire lock on {} - held by {} (expires in {}s)",
                            resource, owner, expires_in_ms / 1000
                        ))
                    }
                    _ => GriteError::Internal(e.to_string()),
                })?;
            Ok(Self { ctx, resource, acquired: true })
        } else {
            Ok(Self { ctx, resource, acquired: false })
        }
    }
}

impl<'a> Drop for LockGuard<'a> {
    fn drop(&mut self) {
        if self.acquired {
            if let Ok(lock_manager) = self.ctx.open_lock_manager() {
                let _ = lock_manager.release(&self.resource, &self.ctx.actor_id);
            }
        }
    }
}

#[derive(Serialize)]
struct IssueCreateOutput {
    issue_id: String,
    event_id: String,
    wal_head: Option<String>,
}

#[derive(Serialize)]
struct IssueListOutput {
    issues: Vec<IssueSummaryJson>,
    total: usize,
}

#[derive(Serialize)]
struct IssueSummaryJson {
    issue_id: String,
    title: String,
    state: String,
    labels: Vec<String>,
    assignees: Vec<String>,
    updated_ts: u64,
    comment_count: usize,
}

impl From<&IssueSummary> for IssueSummaryJson {
    fn from(s: &IssueSummary) -> Self {
        Self {
            issue_id: id_to_hex(&s.issue_id),
            title: s.title.clone(),
            state: format!("{:?}", s.state).to_lowercase(),
            labels: s.labels.clone(),
            assignees: s.assignees.clone(),
            updated_ts: s.updated_ts,
            comment_count: s.comment_count,
        }
    }
}

#[derive(Serialize)]
struct IssueShowOutput {
    issue: IssueSummaryJson,
    events: Vec<EventJson>,
}

#[derive(Serialize)]
struct EventJson {
    event_id: String,
    issue_id: String,
    actor: String,
    ts_unix_ms: u64,
    parent: Option<String>,
    kind: serde_json::Value,
}

#[derive(Serialize)]
struct IssueUpdateOutput {
    issue_id: String,
    event_id: String,
    wal_head: Option<String>,
}

#[derive(Serialize)]
struct IssueStateOutput {
    issue_id: String,
    event_id: String,
    state: String,
    wal_head: Option<String>,
}

pub fn run(cli: &Cli, cmd: IssueCommand) -> Result<(), GriteError> {
    match cmd {
        IssueCommand::Create { title, body, label } => run_create(cli, title, body, label),
        IssueCommand::List { state, label } => run_list(cli, state, label),
        IssueCommand::Show { id } => run_show(cli, id),
        IssueCommand::Update { id, title, body, lock } => run_update(cli, id, title, body, lock),
        IssueCommand::Comment { id, body, lock } => run_comment(cli, id, body, lock),
        IssueCommand::Close { id, lock } => run_close(cli, id, lock),
        IssueCommand::Reopen { id, lock } => run_reopen(cli, id, lock),
        IssueCommand::Label { cmd } => run_label(cli, cmd),
        IssueCommand::Assignee { cmd } => run_assignee(cli, cmd),
        IssueCommand::Link { cmd } => run_link(cli, cmd),
        IssueCommand::Attachment { cmd } => run_attachment(cli, cmd),
        IssueCommand::Dep { cmd } => super::dep::run(cli, cmd),
    }
}

fn current_ts() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_millis() as u64
}

fn run_create(cli: &Cli, title: String, body: String, labels: Vec<String>) -> Result<(), GriteError> {
    let ctx = GriteContext::resolve(cli)?;

    // Check for repo-level locks before creating
    check_repo_lock(cli, &ctx)?;

    let store = ctx.open_store()?;
    let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
    let actor = ctx.actor_config.actor_id_bytes()?;

    let issue_id = generate_issue_id();
    let ts = current_ts();
    let kind = EventKind::IssueCreated { title, body, labels };
    let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
    let event = Event::new(event_id, issue_id, actor, ts, None, kind);
    let event = ctx.sign_event(event);

    let result = insert_and_append(&store, &wal, &actor, &event)?;

    output_success(cli, IssueCreateOutput {
        issue_id: id_to_hex(&issue_id),
        event_id: id_to_hex(&event_id),
        wal_head: result.wal_head,
    });

    Ok(())
}

fn run_list(cli: &Cli, state: Option<String>, label: Option<String>) -> Result<(), GriteError> {
    let ctx = GriteContext::resolve(cli)?;
    let store = ctx.open_store()?;

    let state_filter = state.map(|s| {
        match s.to_lowercase().as_str() {
            "open" => IssueState::Open,
            "closed" => IssueState::Closed,
            _ => IssueState::Open,
        }
    });

    let filter = IssueFilter {
        state: state_filter,
        label,
    };

    let issues = store.list_issues(&filter)?;
    let total = issues.len();
    let issue_jsons: Vec<IssueSummaryJson> = issues.iter().map(IssueSummaryJson::from).collect();

    output_success(cli, IssueListOutput { issues: issue_jsons, total });

    Ok(())
}

fn run_show(cli: &Cli, id: String) -> Result<(), GriteError> {
    let ctx = GriteContext::resolve(cli)?;
    let store = ctx.open_store()?;

    let issue_id = parse_issue_id(&id)?;
    let proj = store.get_issue(&issue_id)?
        .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

    let events = store.get_issue_events(&issue_id)?;
    let event_jsons: Vec<EventJson> = events.iter().map(|e| {
        EventJson {
            event_id: id_to_hex(&e.event_id),
            issue_id: id_to_hex(&e.issue_id),
            actor: id_to_hex(&e.actor),
            ts_unix_ms: e.ts_unix_ms,
            parent: e.parent.as_ref().map(id_to_hex),
            kind: serde_json::to_value(&e.kind).unwrap_or(serde_json::Value::Null),
        }
    }).collect();

    let summary = IssueSummary::from(&proj);

    output_success(cli, IssueShowOutput {
        issue: IssueSummaryJson::from(&summary),
        events: event_jsons,
    });

    Ok(())
}

fn run_update(cli: &Cli, id: String, title: Option<String>, body: Option<String>, lock: bool) -> Result<(), GriteError> {
    if title.is_none() && body.is_none() {
        return Err(GriteError::InvalidArgs("At least one of --title or --body must be provided".to_string()));
    }

    let ctx = GriteContext::resolve(cli)?;

    // Acquire lock if requested (or just check for conflicts)
    let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
    if !lock {
        check_issue_lock(cli, &ctx, &id)?;
    }

    let store = ctx.open_store()?;
    let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
    let actor = ctx.actor_config.actor_id_bytes()?;

    let issue_id = parse_issue_id(&id)?;

    // Verify issue exists
    store.get_issue(&issue_id)?
        .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

    let ts = current_ts();
    let kind = EventKind::IssueUpdated { title, body };
    let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
    let event = Event::new(event_id, issue_id, actor, ts, None, kind);
    let event = ctx.sign_event(event);

    let result = insert_and_append(&store, &wal, &actor, &event)?;

    output_success(cli, IssueUpdateOutput {
        issue_id: id_to_hex(&issue_id),
        event_id: id_to_hex(&event_id),
        wal_head: result.wal_head,
    });

    Ok(())
}

fn run_comment(cli: &Cli, id: String, body: String, lock: bool) -> Result<(), GriteError> {
    let ctx = GriteContext::resolve(cli)?;

    // Acquire lock if requested (or just check for conflicts)
    let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
    if !lock {
        check_issue_lock(cli, &ctx, &id)?;
    }

    let store = ctx.open_store()?;
    let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
    let actor = ctx.actor_config.actor_id_bytes()?;

    let issue_id = parse_issue_id(&id)?;

    // Verify issue exists
    store.get_issue(&issue_id)?
        .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

    let ts = current_ts();
    let kind = EventKind::CommentAdded { body };
    let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
    let event = Event::new(event_id, issue_id, actor, ts, None, kind);
    let event = ctx.sign_event(event);

    let result = insert_and_append(&store, &wal, &actor, &event)?;

    output_success(cli, IssueUpdateOutput {
        issue_id: id_to_hex(&issue_id),
        event_id: id_to_hex(&event_id),
        wal_head: result.wal_head,
    });

    Ok(())
}

fn run_close(cli: &Cli, id: String, lock: bool) -> Result<(), GriteError> {
    let ctx = GriteContext::resolve(cli)?;

    // Acquire lock if requested (or just check for conflicts)
    let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
    if !lock {
        check_issue_lock(cli, &ctx, &id)?;
    }

    let store = ctx.open_store()?;
    let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
    let actor = ctx.actor_config.actor_id_bytes()?;

    let issue_id = parse_issue_id(&id)?;

    // Verify issue exists
    store.get_issue(&issue_id)?
        .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

    let ts = current_ts();
    let kind = EventKind::StateChanged { state: IssueState::Closed };
    let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
    let event = Event::new(event_id, issue_id, actor, ts, None, kind);
    let event = ctx.sign_event(event);

    let result = insert_and_append(&store, &wal, &actor, &event)?;

    output_success(cli, IssueStateOutput {
        issue_id: id_to_hex(&issue_id),
        event_id: id_to_hex(&event_id),
        state: "closed".to_string(),
        wal_head: result.wal_head,
    });

    Ok(())
}

fn run_reopen(cli: &Cli, id: String, lock: bool) -> Result<(), GriteError> {
    let ctx = GriteContext::resolve(cli)?;

    // Acquire lock if requested (or just check for conflicts)
    let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
    if !lock {
        check_issue_lock(cli, &ctx, &id)?;
    }

    let store = ctx.open_store()?;
    let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
    let actor = ctx.actor_config.actor_id_bytes()?;

    let issue_id = parse_issue_id(&id)?;

    // Verify issue exists
    store.get_issue(&issue_id)?
        .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

    let ts = current_ts();
    let kind = EventKind::StateChanged { state: IssueState::Open };
    let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
    let event = Event::new(event_id, issue_id, actor, ts, None, kind);
    let event = ctx.sign_event(event);

    let result = insert_and_append(&store, &wal, &actor, &event)?;

    output_success(cli, IssueStateOutput {
        issue_id: id_to_hex(&issue_id),
        event_id: id_to_hex(&event_id),
        state: "open".to_string(),
        wal_head: result.wal_head,
    });

    Ok(())
}

fn run_label(cli: &Cli, cmd: LabelCommand) -> Result<(), GriteError> {
    match cmd {
        LabelCommand::Add { id, label, lock } => {
            let ctx = GriteContext::resolve(cli)?;
            let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
            if !lock {
                check_issue_lock(cli, &ctx, &id)?;
            }
            let store = ctx.open_store()?;
            let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
            let actor = ctx.actor_config.actor_id_bytes()?;

            let issue_id = parse_issue_id(&id)?;
            store.get_issue(&issue_id)?
                .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

            let ts = current_ts();
            let kind = EventKind::LabelAdded { label };
            let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
            let event = Event::new(event_id, issue_id, actor, ts, None, kind);
            let event = ctx.sign_event(event);

            let result = insert_and_append(&store, &wal, &actor, &event)?;

            output_success(cli, IssueUpdateOutput {
                issue_id: id_to_hex(&issue_id),
                event_id: id_to_hex(&event_id),
                wal_head: result.wal_head,
            });
        }
        LabelCommand::Remove { id, label, lock } => {
            let ctx = GriteContext::resolve(cli)?;
            let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
            if !lock {
                check_issue_lock(cli, &ctx, &id)?;
            }
            let store = ctx.open_store()?;
            let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
            let actor = ctx.actor_config.actor_id_bytes()?;

            let issue_id = parse_issue_id(&id)?;
            store.get_issue(&issue_id)?
                .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

            let ts = current_ts();
            let kind = EventKind::LabelRemoved { label };
            let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
            let event = Event::new(event_id, issue_id, actor, ts, None, kind);
            let event = ctx.sign_event(event);

            let result = insert_and_append(&store, &wal, &actor, &event)?;

            output_success(cli, IssueUpdateOutput {
                issue_id: id_to_hex(&issue_id),
                event_id: id_to_hex(&event_id),
                wal_head: result.wal_head,
            });
        }
    }
    Ok(())
}

fn run_assignee(cli: &Cli, cmd: AssigneeCommand) -> Result<(), GriteError> {
    match cmd {
        AssigneeCommand::Add { id, user, lock } => {
            let ctx = GriteContext::resolve(cli)?;
            let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
            if !lock {
                check_issue_lock(cli, &ctx, &id)?;
            }
            let store = ctx.open_store()?;
            let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
            let actor = ctx.actor_config.actor_id_bytes()?;

            let issue_id = parse_issue_id(&id)?;
            store.get_issue(&issue_id)?
                .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

            let ts = current_ts();
            let kind = EventKind::AssigneeAdded { user };
            let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
            let event = Event::new(event_id, issue_id, actor, ts, None, kind);
            let event = ctx.sign_event(event);

            let result = insert_and_append(&store, &wal, &actor, &event)?;

            output_success(cli, IssueUpdateOutput {
                issue_id: id_to_hex(&issue_id),
                event_id: id_to_hex(&event_id),
                wal_head: result.wal_head,
            });
        }
        AssigneeCommand::Remove { id, user, lock } => {
            let ctx = GriteContext::resolve(cli)?;
            let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
            if !lock {
                check_issue_lock(cli, &ctx, &id)?;
            }
            let store = ctx.open_store()?;
            let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
            let actor = ctx.actor_config.actor_id_bytes()?;

            let issue_id = parse_issue_id(&id)?;
            store.get_issue(&issue_id)?
                .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

            let ts = current_ts();
            let kind = EventKind::AssigneeRemoved { user };
            let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
            let event = Event::new(event_id, issue_id, actor, ts, None, kind);
            let event = ctx.sign_event(event);

            let result = insert_and_append(&store, &wal, &actor, &event)?;

            output_success(cli, IssueUpdateOutput {
                issue_id: id_to_hex(&issue_id),
                event_id: id_to_hex(&event_id),
                wal_head: result.wal_head,
            });
        }
    }
    Ok(())
}

fn run_link(cli: &Cli, cmd: LinkCommand) -> Result<(), GriteError> {
    match cmd {
        LinkCommand::Add { id, url, note, lock } => {
            let ctx = GriteContext::resolve(cli)?;
            let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
            if !lock {
                check_issue_lock(cli, &ctx, &id)?;
            }
            let store = ctx.open_store()?;
            let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
            let actor = ctx.actor_config.actor_id_bytes()?;

            let issue_id = parse_issue_id(&id)?;
            store.get_issue(&issue_id)?
                .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

            let ts = current_ts();
            let kind = EventKind::LinkAdded { url, note };
            let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
            let event = Event::new(event_id, issue_id, actor, ts, None, kind);
            let event = ctx.sign_event(event);

            let result = insert_and_append(&store, &wal, &actor, &event)?;

            output_success(cli, IssueUpdateOutput {
                issue_id: id_to_hex(&issue_id),
                event_id: id_to_hex(&event_id),
                wal_head: result.wal_head,
            });
        }
    }
    Ok(())
}

fn run_attachment(cli: &Cli, cmd: AttachmentCommand) -> Result<(), GriteError> {
    match cmd {
        AttachmentCommand::Add { id, name, sha256, mime, lock } => {
            let ctx = GriteContext::resolve(cli)?;
            let _lock_guard = LockGuard::acquire(&ctx, &id, lock)?;
            if !lock {
                check_issue_lock(cli, &ctx, &id)?;
            }
            let store = ctx.open_store()?;
            let wal = ctx.open_wal().map_err(|e| GriteError::Internal(e.to_string()))?;
            let actor = ctx.actor_config.actor_id_bytes()?;

            let issue_id = parse_issue_id(&id)?;
            store.get_issue(&issue_id)?
                .ok_or_else(|| GriteError::NotFound(format!("Issue {} not found", id)))?;

            let sha256_bytes: [u8; 32] = hex_to_id(&sha256)?;

            let ts = current_ts();
            let kind = EventKind::AttachmentAdded { name, sha256: sha256_bytes, mime };
            let event_id = compute_event_id(&issue_id, &actor, ts, None, &kind);
            let event = Event::new(event_id, issue_id, actor, ts, None, kind);
            let event = ctx.sign_event(event);

            let result = insert_and_append(&store, &wal, &actor, &event)?;

            output_success(cli, IssueUpdateOutput {
                issue_id: id_to_hex(&issue_id),
                event_id: id_to_hex(&event_id),
                wal_head: result.wal_head,
            });
        }
    }
    Ok(())
}