blib/lib/App/karr/Cmd/Move.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/App/karr/Cmd/Log.pm ↑ Summary blib/lib/App/karr/Cmd/Needs.pm »
1 # ABSTRACT: Change a task's status
2  
3 package App::karr::Cmd::Move;
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 move ID[,ID,...] STATUS [--claim NAME] [--next|--prev]',
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::Role::ClaimDefault;
14 use App::karr::Task;
15 use App::karr::Config;
16 use Time::Piece;
17 # Loaded without importing: this class composes no namespace::clean (MooX::Options
18 # forbids it), so an imported `JSON` would become a method on the command. The
19 # two booleans are wanted as functions anyway, the way App::karr::Task calls them.
20 use JSON::MaybeXS ();
21 use App::karr::Error qw( command_hint );
22  
23 with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output',
24 'App::karr::Role::TaskMutation', 'App::karr::Role::ClaimDefault';
25  
26  
27 option next => (
28 is => 'ro',
29 doc => 'Advance to next status',
30 );
31  
32 option prev => (
33 is => 'ro',
34 doc => 'Move to previous status',
35 );
36  
37 option claim => (
38 is => 'ro',
39 format => 's',
40 doc => 'Claim task for an agent',
41 );
42  
43 sub execute {
44 my ($self, $args_ref, $chain_ref) = @_;
45  
46 $self->check_positional_args($args_ref, 2);
47  
48 $self->sync_before;
49 $self->require_board;
50  
51 my @pos = $self->positional_args($args_ref);
52 my $id_str = $pos[0];
53 # Read before the id guards below rather than after them, because the guards
54 # now quote it back: `karr move , todo` is missing an id, not a status, and
55 # the line that would have worked says `todo` where it knows it (ticket k263).
56 my $new_status = $pos[1];
57 # The suggestion goes after the "Usage:" line, never in front of it: the
58 # marker has to stay at the start of the first line for bin/karr's handler to
59 # read it as a usage error (ADR 0002), and the actionable line is what a
60 # `tail -n` has to be left holding.
61 #
62 # And it is appended only when it can carry a word the caller really typed.
63 # `karr move , todo` keeps it -- `todo` is theirs, so the line shows which
64 # half was wrong. `karr move` with nothing at all has nothing to quote back,
65 # and a suggestion of pure placeholders would only spell the "Usage:" line a
66 # second time: that is the generic example with a placeholder id that k263
67 # forbids, and it is exactly what an agent reading `tail -1` would be left
68 # holding. With no suggestion the "Usage:" line is itself the actionable
69 # line, and it is last on its own.
70 my $usage = "Usage: karr move ID[,ID,...] [STATUS]\n"
71 . ( defined $new_status && length $new_status
72 ? command_hint( 'move', 'ID', $new_status ) . "\n"
73 : '' );
74 # length, not truth: the id "0" is an argument that was given, just a false
75 # one, so `or` sent it down the branch meant for "no argument at all". That
76 # made `karr move 0 todo` a usage error (2) where `karr show 0` and even
77 # `karr move 0,1 todo` already answered "not found" (1) -- card numbers start
78 # at 1, so no real card was unreachable, but the exit-code contract ADR 0002
79 # promises to agents scripting this CLI fell on the wrong side (ticket #239,
80 # the same root as #153 and #230).
81 die $usage
82 unless defined $id_str && length $id_str;
83 # `karr move , todo` passes that guard -- a comma is one character long --
84 # and then splits to an empty list, so the loop below never ran: no ids, no
85 # output, no die, exit 0. A command that silently did nothing is the one
86 # answer the exit-code contract (ADR 0002) cannot express. The "Usage:"
87 # prefix is what bin/karr keys on to make both of these guards a usage error
88 # (2) rather than a runtime failure (1).
89 my @ids = $self->parse_ids($id_str);
90 die $usage unless @ids;
91  
92 # A target status and a relative move are two answers to the same question,
93 # and a caller who gives both has contradicted themselves -- so the invocation
94 # is refused before any card is read (ticket #235).
95 #
96 # This deviates from kanban-md, deliberately and on karr's own contract.
97 # kanban-md resolves the pair silently (resolveTargetStatus, cmd/move.go:135-159):
98 # its `case len(args) == 2:` is the first arm of the switch, so the positional
99 # wins and --next is dropped. karr resolved it silently the other way round --
100 # the block below overwrote the positional target unconditionally, so
101 # `move 7 archived --next` put a backlog card on todo. Two tools, one command
102 # line, two different columns, exit 0 and no message either way: that
103 # disagreement is the argument against picking a winner at all, because
104 # whichever side one picks, the caller who typed both has no way to learn that
105 # half of what they typed was thrown away.
106 #
107 # Adopting kanban-md's precedence would be the worse of the two silences here:
108 # it would move cards to a different column than this karr does today, with
109 # nothing in the output saying so. Refusing says it out loud, and karr can say
110 # it where kanban-md cannot -- cobra exits 1 for everything, so "you called
111 # this wrong" is not expressible there, while ADR 0002 reserves exit 2 for
112 # exactly this and karr's callers are agents for whom $? is API.
113 #
114 # length, not truth, on the positional, the same rule as the id guard above:
115 # an empty status names no target, so it is not a target for --next to
116 # contradict.
117 $self->usage_error('cannot use a target status and --next/--prev together')
118 if ( defined $new_status && length $new_status )
119 && ( $self->next || $self->prev );
120  
121 # And the relative pair against itself, which was silent in the same way: the
122 # if/elsif below let --next win and dropped --prev, as kanban-md's switch does
123 # in the same order. Same contradiction, same answer.
124 $self->usage_error('cannot use --next and --prev together')
125 if $self->next && $self->prev;
126  
127 my @statuses = $self->store->all_status_names;
128  
129 # Every id is attempted, whatever the ones before it did: a missing id used to
130 # die from inside this loop and take the rest of the batch with it, so the
131 # result depended on where the bad id sat in the list (ticket #61).
132 my ($results, $failed) = $self->run_batch(\@ids, sub {
133 my ($id) = @_;
134  
135 # Everything that reads the task happens inside the guard, --next/--prev
136 # included: the target status is derived from the task's current status, so
137 # deciding it outside the loop would decide it against a revision another
138 # agent may already have replaced.
139 my $old_status;
140 my $unchanged;
141 my $task = $self->update_task_guarded($id, sub {
142 my ($task) = @_;
143  
144 # Who the caller is: --claim, else KARR_CLAIM (ADR 0005). That is an
145 # identity for the ownership check, so a card the caller already holds
146 # moves whatever the destination. Whether that name is then *written*
147 # is decided below, once the destination is known.
148 $self->check_claim($task, $self->resolved_claim);
149  
150 my $task_new_status = $new_status;
151  
152 # Every message from here on names the card and ends in the invocation
153 # that would have worked, and stays ONE line of prose above it: these are
154 # raised inside run_batch, whose clean_error keeps the first line and the
155 # suggestion block and drops anything in between (ticket k263).
156 if ($self->next) {
157 my $idx = $self->_status_index(\@statuses, $task->status, $id);
158 die "Task $id is already at the last status '" . $task->status
159 . "', so --next has nowhere to go:\n"
160 . command_hint( 'move', $id, 'STATUS' ) . "\n"
161 if $idx >= $#statuses;
162 $task_new_status = $statuses[$idx + 1];
163 } elsif ($self->prev) {
164 my $idx = $self->_status_index(\@statuses, $task->status, $id);
165 die "Task $id is already at the first status '" . $task->status
166 . "', so --prev has nowhere to go:\n"
167 . command_hint( 'move', $id, 'STATUS' ) . "\n"
168 if $idx <= 0;
169 $task_new_status = $statuses[$idx - 1];
170 }
171  
172 # The valid list rides along wherever the suggestion has to fall back to
173 # the STATUS placeholder: the caller is being asked for a value, and the
174 # board is the only place that vocabulary exists. Same shape
175 # App::karr::Config/_usage_error prints for a rejected one.
176 die 'New status required (valid: ' . join( ', ', @statuses ) . "):\n"
177 . command_hint( 'move', $id, 'STATUS' ) . "\n"
178 unless $task_new_status;
179  
180 # The claim this move writes: an explicit --claim on any destination,
181 # KARR_CLAIM only into a require_claim column (ticket #304, the #286
182 # rule create follows). Promoting a card from backlog to todo puts it
183 # in the pool for someone else to pick up. With the env name stamped on
184 # every move, the promoter held every card it promoted for
185 # claim_timeout, and `pick` and `list --unclaimed` did not see it.
186 my $claim = $self->resolved_claim_for($task_new_status);
187  
188 # A move to the status the card already has, with no claim to hand over,
189 # changes nothing -- so it writes nothing: the write is what stamps
190 # `updated` and appends the activity-log entry, and both were saying a
191 # move happened when none did (#231). Assigned on every attempt rather
192 # than only in the branch where it is true: the callback re-runs on
193 # contention, and this answer belongs to the revision that attempt read.
194 #
195 # --claim is what makes it not this case: `move ID <same status> --claim
196 # NAME` writes claimed_by and a fresh claimed_at, which is how an agent
197 # takes over a card whose claim ran out without moving it, and dropping
198 # that silently would be this ticket's own bug pointing the other way.
199 # kanban-md short-circuits in front of its claim handling and does drop
200 # it; karr's claims expire and gate `pick`, so here the claim wins.
201 #
202 # After check_claim, not before it: whether somebody else's live claim
203 # blocks this command is a question about the card, not about the work,
204 # and kanban-md asks it in the same order.
205 $unchanged = $task->status eq $task_new_status
206 && !( defined $claim && length $claim );
207 return $self->no_change if $unchanged;
208  
209 if ( defined $claim && length $claim ) {
210 $task->claimed_by($claim);
211 $task->claimed_at(gmtime->datetime . 'Z');
212 }
213  
214 $old_status = $self->apply_status_change($task, $task_new_status, $claim);
215 });
216  
217 if ($unchanged) {
218 # The wording `karr archive` already uses for the same answer, and ADR
219 # 0002's exit 0: the card is where it was asked to be, which is success
220 # and not a failure to report.
221 printf "Task %d is already at %s: %s\n", $task->id, $task->status, $task->title
222 unless $self->json;
223 # `changed` is the field a --json reader keys on, and it is on both
224 # answers rather than only on this one: a key that appears only when
225 # nothing happened has to be tested for existence instead of for its
226 # value, and tells a reader nothing at all on the karr that never wrote
227 # it. old_status/new_status stay, holding the one status the card has, so
228 # the shape of a move result does not change with its outcome.
229 #
230 # Neither report is called here. Nothing was written, so nothing stepped
231 # over the expired claim this card may carry, and nothing took up work
232 # that its dependencies could still be waiting on -- and
233 # apply_status_change did not record either of them for this id.
234 return { id => $task->id, title => $task->title, old_status => $task->status,
235 new_status => $task->status, changed => JSON::MaybeXS::false() };
236 }
237  
238 printf "Moved task %d: %s -> %s\n", $task->id, $old_status, $task->status unless $self->json;
239 # After the write, not inside the guarded callback that decided it: see
240 # App::karr::Role::DependencyCheck/dependency_report. Under --json the pair
241 # it returns lands in this hash instead of on STDERR. Same for the expired
242 # claim this move may have stepped over
243 # (App::karr::Role::ClaimTimeout/expired_claim_report, #177), which is
244 # reported first because it is about who held the card, not about the work.
245 return { id => $task->id, title => $task->title, old_status => $old_status,
246 new_status => $task->status, changed => JSON::MaybeXS::true(),
247 $self->expired_claim_report( $task->id ),
248 $self->dependency_report( $task->id ) };
249 });
250  
251 $self->sync_after;
252  
253 $self->print_json_results(@$results);
254  
255 $self->report_batch_failure($failed, scalar @ids);
256 }
257  
258 # The status looked up here is the card's own, not one the caller typed -- only
259 # --next/--prev come through -- so a miss means the card sits in a column this
260 # board does not configure and no relative move can be computed from it. The
261 # card is named, the board's vocabulary is printed, and the way out is the
262 # explicit form (ticket k263).
263 sub _status_index {
264 my ($self, $statuses, $status, $id) = @_;
265 for my $i (0..$#$statuses) {
266 return $i if $statuses->[$i] eq $status;
267 }
268 die "Task $id is at '$status', which this board does not configure (valid: "
269 . join( ', ', @$statuses ) . "):\n"
270 . command_hint( 'move', $id, 'STATUS' ) . "\n";
271 }
272  
273 # App::karr::Role::TaskMutation raises the require_claim message and cannot know
274 # which command is running; its default names `karr edit ID --status STATUS`.
275 # Here the status is a positional and the caller has just typed it, so the
276 # suggestion is the caller's own command line with the one missing piece added.
277 sub claim_hint_tokens {
278 my ( $self, $task, $status ) = @_;
279 return ( 'move', $task->id, $status, '--claim', 'NAME' );
280 }
281  
282 1;
283  
284 __END__
« blib/lib/App/karr/Cmd/Log.pm ↑ Summary blib/lib/App/karr/Cmd/Needs.pm »