| 1 | # ABSTRACT: Archive a task (soft-delete) | ||
| 2 | |||
| 3 | package App::karr::Cmd::Archive; | ||
| 4 | our $VERSION = '0.602'; | ||
| 5 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use MooX::Cmd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use MooX::Options ( |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | usage_string => 'USAGE: karr archive ID[,ID,...] [--claim NAME] [--json]', | |
| 9 | 2 | ); | |
| 10 | 0 | ▶ | use App::karr::Role::BoardAccess; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use App::karr::Role::Output; | ||
| 12 | use App::karr::Role::TaskMutation; | ||
| 13 | use App::karr::Task; | ||
| 14 | |||
| 15 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output', | ||
| 16 | 'App::karr::Role::TaskMutation'; | ||
| 17 | |||
| 18 | option claim => ( | ||
| 19 | is => 'ro', | ||
| 20 | format => 's', | ||
| 21 | doc => 'Archive a task claimed by this agent', | ||
| 22 | ); | ||
| 23 | |||
| 24 | |||
| 25 | sub execute { | ||
| 26 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 27 | |||
| 28 | $self->check_positional_args($args_ref, 1); | ||
| 29 | |||
| 30 | $self->sync_before; | ||
| 31 | $self->require_board; | ||
| 32 | |||
| 33 | my @pos = $self->positional_args($args_ref); | ||
| 34 | my $id_str = $pos[0]; | ||
| 35 | # See the note in Cmd::Move: length, not truth, or the id "0" is read as no | ||
| 36 | # id at all and answered with a usage error instead of "not found" (#239). | ||
| 37 | die "Usage: karr archive ID[,ID,...] [--claim NAME]\n" | ||
| 38 | unless defined $id_str && length $id_str; | ||
| 39 | # And a comma with no ids around it passes that guard and | ||
| 40 | # splits to nothing, so the command used to exit 0 having done nothing | ||
| 41 | # (ticket #152). | ||
| 42 | my @ids = $self->parse_ids($id_str); | ||
| 43 | die "Usage: karr archive ID[,ID,...] [--claim NAME]\n" unless @ids; | ||
| 44 | |||
| 45 | # This loop was already the shape ADR 0002 asks for -- warn on a bad id, keep | ||
| 46 | # going, report failure at the end -- and is now the shared one, so move, edit | ||
| 47 | # and delete behave the same way (ticket #61). | ||
| 48 | my ($results, $failed) = $self->run_batch(\@ids, sub { | ||
| 49 | my ($id) = @_; | ||
| 50 | |||
| 51 | # The already-archived question is answered on an unguarded read on purpose: | ||
| 52 | # it only decides whether there is any work to do, and losing that race | ||
| 53 | # costs one idempotent re-archive. The claim rule and the status change both | ||
| 54 | # sit inside update_task_guarded's callback below, applied to the very | ||
| 55 | # revision that gets written. | ||
| 56 | my $found = $self->find_task($id); | ||
| 57 | # The unguarded pre-read fires before update_task_guarded below, so this is | ||
| 58 | # the not-found a caller normally meets; the guard raises the same line only | ||
| 59 | # on the race where the card vanishes in the window. One spelling for both, | ||
| 60 | # and for every other command on the mutation path (ticket k264). | ||
| 61 | die $self->task_not_found($id) unless $found; | ||
| 62 | |||
| 63 | if ($found->status eq 'archived') { | ||
| 64 | printf "Task %d is already archived: %s\n", $found->id, $found->title | ||
| 65 | unless $self->json; | ||
| 66 | return { | ||
| 67 | id => $found->id, | ||
| 68 | title => $found->title, | ||
| 69 | status => 'archived', | ||
| 70 | note => 'already archived', | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | my $old_status; | ||
| 75 | my $task = $self->update_task_guarded($id, sub { | ||
| 76 | my ($task) = @_; | ||
| 77 | |||
| 78 | # Archive used to be a third door into a status change, beside move and | ||
| 79 | # edit --status, and the only one with no claim check at all: it could | ||
| 80 | # take a card off an agent who was still holding it. `archived` carries no | ||
| 81 | # require_claim, so #55 did not reach it, but the claim rule does -- and | ||
| 82 | # it is the same rule, from the same place, applied under the same guard | ||
| 83 | # (ticket #97). The claimant is the caller's --claim, the key the refusal | ||
| 84 | # message now offers (ticket #269), exactly as kanban-md's archive takes | ||
| 85 | # one. | ||
| 86 | $self->check_claim($task, $self->claim); | ||
| 87 | |||
| 88 | # apply_status_change is where `archived` is validated against the board's | ||
| 89 | # configured statuses and where the terminal-status stamps are maintained, | ||
| 90 | # so a task archived straight out of the backlog still gets its | ||
| 91 | # started/completed stamps (ticket #68). | ||
| 92 | $old_status = $self->apply_status_change($task, 'archived', undef); | ||
| 93 | }); | ||
| 94 | |||
| 95 | printf "Archived task %d: %s\n", $task->id, $task->title unless $self->json; | ||
| 96 | # The claimant passed above is the caller's --claim, so a claimed card | ||
| 97 | # reaches here either because the claim expired (reported below, #177) or | ||
| 98 | # because the caller named the holder. The claim itself is not re-stamped: | ||
| 99 | # the name on an archived card is provenance, and kanban-md's Archive | ||
| 100 | # leaves it the same way. | ||
| 101 | return { | ||
| 102 | id => $task->id, | ||
| 103 | title => $task->title, | ||
| 104 | status => 'archived', | ||
| 105 | old_status => $old_status, | ||
| 106 | $self->expired_claim_report( $task->id ), | ||
| 107 | }; | ||
| 108 | }); | ||
| 109 | |||
| 110 | $self->sync_after; | ||
| 111 | |||
| 112 | $self->print_json_results(@$results); | ||
| 113 | |||
| 114 | $self->report_batch_failure($failed, scalar @ids); | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ | ||