blib/lib/App/karr/Cmd/Edit.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/Create.pm ↑ Summary blib/lib/App/karr/Cmd/GetRefs.pm »
1 # ABSTRACT: Modify an existing task
2  
3 package App::karr::Cmd::Edit;
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 edit ID[,ID,...] [--title TEXT] [--priority LEVEL] [options]',
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::DependencyArgs;
14 use App::karr::Role::ClaimDefault;
15 use App::karr::Task;
16 use App::karr::Config;
17 use App::karr::CrossBoard;
18 use Time::Piece;
19  
20 # Both halves of the dependency pair, and the only command that needs both:
21 # --add-depends-on/--remove-depends-on are parsed by DependencyArgs, and
22 # --status takes the same warning path as move through TaskMutation, which
23 # brings App::karr::Role::DependencyCheck with it. Named here since ticket #137
24 # split the two; before that the set-time helpers arrived through TaskMutation
25 # by accident of them sharing a role.
26 with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output',
27 'App::karr::Role::TaskMutation', 'App::karr::Role::DependencyArgs',
28 'App::karr::Role::ClaimDefault';
29  
30  
31 option title => (
32 is => 'ro',
33 format => 's',
34 doc => 'New title',
35 );
36  
37 option status => (
38 is => 'ro',
39 format => 's',
40 doc => 'New status',
41 );
42  
43 option priority => (
44 is => 'ro',
45 format => 's',
46 doc => 'New priority',
47 );
48  
49 option assignee => (
50 is => 'ro',
51 format => 's',
52 doc => 'New assignee',
53 );
54  
55 option add_tag => (
56 is => 'ro',
57 format => 's',
58 doc => 'Add tags (comma-separated)',
59 );
60  
61 option remove_tag => (
62 is => 'ro',
63 format => 's',
64 doc => 'Remove tags (comma-separated)',
65 );
66  
67 option add_depends_on => (
68 is => 'ro',
69 format => 's',
70 doc => 'Add dependency ids (comma-separated)',
71 );
72  
73 option remove_depends_on => (
74 is => 'ro',
75 format => 's',
76 doc => 'Remove dependency ids (comma-separated)',
77 );
78  
79 option add_needs => (
80 is => 'ro',
81 format => 's',
82 doc => 'Add cross-board dependencies (comma-separated BOARD#ID)',
83 );
84  
85 option remove_needs => (
86 is => 'ro',
87 format => 's',
88 doc => 'Remove cross-board dependencies (comma-separated BOARD#ID)',
89 );
90  
91 option due => (
92 is => 'ro',
93 format => 's',
94 doc => 'New due date',
95 );
96  
97 option clear_due => (
98 is => 'ro',
99 doc => 'Clear due date',
100 );
101  
102 option class => (
103 is => 'ro',
104 format => 's',
105 doc => 'New class of service',
106 );
107  
108 option estimate => (
109 is => 'ro',
110 format => 's',
111 doc => 'New time estimate',
112 );
113  
114 option body => (
115 is => 'ro',
116 format => 's',
117 doc => 'New body text',
118 );
119  
120 option append_body => (
121 is => 'ro',
122 format => 's',
123 short => 'a',
124 doc => 'Append text to body',
125 );
126  
127 # Opt-in, exactly as on karr handoff, and for the same reason it is opt-in
128 # there: --append-body appends arbitrary Markdown, not only notes, and the
129 # stamp is an inline prefix -- forced on every append it would turn
130 # `-a "## Findings"` into a line that is no longer a heading, with no way to
131 # say no. kanban-md's edit has the same flag (cmd/edit.go:38); the format is
132 # handoff's UTC one, not kanban-md's local-time wiki link (ticket #238).
133 option timestamp => (
134 is => 'ro',
135 short => 't',
136 doc => 'Prefix the appended text with the current UTC timestamp',
137 );
138  
139 option claim => (
140 is => 'ro',
141 format => 's',
142 doc => 'Claim task for an agent',
143 );
144  
145 option release => (
146 is => 'ro',
147 doc => 'Release claim',
148 );
149  
150 option block => (
151 is => 'ro',
152 format => 's',
153 doc => 'Mark as blocked with reason',
154 );
155  
156 option unblock => (
157 is => 'ro',
158 doc => 'Clear blocked state',
159 );
160  
161 # Every option above that can change a card, in the order they are declared:
162 # the ones that carry a value, and the three that are flags. --json and --quiet
163 # are deliberately absent -- they decide how the result is printed, not what it
164 # is, so `karr edit 5 --json` asks for a change just as little as `karr edit 5`
165 # does. (--compact stood here too until #254, when this command stopped taking
166 # it: it never rendered one, and an option accepted and thrown away is not a
167 # rendering.) --timestamp is absent for the same reason on the write
168 # side: it decorates the text --append-body appends and appends nothing of its
169 # own, so `karr edit 5 --timestamp` names no change either (ticket #238). A new
170 # field option belongs on one of these lists; leaving it off makes it invisible
171 # to the check below, which would then reject an invocation that does have
172 # something to do.
173 my @FIELD_OPTIONS = qw(
174 title status priority assignee add_tag remove_tag add_depends_on
175 remove_depends_on add_needs remove_needs due class estimate body append_body
176 claim block
177 );
178 my @FIELD_FLAGS = qw( clear_due release unblock );
179  
180 sub _has_field_change {
181 my ($self) = @_;
182 for my $option (@FIELD_OPTIONS) {
183 my $value = $self->$option;
184 return 1 if defined $value && length $value;
185 }
186 for my $flag (@FIELD_FLAGS) {
187 return 1 if $self->$flag;
188 }
189 return 0;
190 }
191  
192 sub execute {
193 my ($self, $args_ref, $chain_ref) = @_;
194  
195 $self->check_positional_args($args_ref, 1);
196  
197 $self->sync_before;
198 $self->require_board;
199  
200 my @pos = $self->positional_args($args_ref);
201 my $id_str = $pos[0];
202 # See the note in Cmd::Move: length, not truth, or the id "0" is read as no
203 # id at all and answered with a usage error instead of "not found" (#239).
204 die "Usage: karr edit ID[,ID,...] [FLAGS]\n"
205 unless defined $id_str && length $id_str;
206 # And a comma with no ids around it passes that guard and
207 # splits to nothing, so the command used to exit 0 having done nothing.
208 my @ids = $self->parse_ids($id_str);
209 die "Usage: karr edit ID[,ID,...] [FLAGS]\n" unless @ids;
210  
211 # An edit that names no field asks for nothing, so there is nothing to do and
212 # nothing to write. It used to run the whole path with an empty callback:
213 # `updated` was stamped (App::karr::BoardStore/save_task_cas), an `edit` entry
214 # was appended to the activity log, and stdout said "Updated task N" about a
215 # card nothing had touched -- the same false signal `karr move` gave on a
216 # same-status move, and the effect t/153-falsy-option-values.t already names
217 # as the bug for a value the length guards below discard (#231).
218 #
219 # A usage error (exit 2), where the same-status move is a success (exit 0),
220 # and the two answers come from opposite sides of ADR 0002. A move to the
221 # status a card already has is a request whose outcome already holds -- "a
222 # no-op like re-archiving an archived task", which the ADR files under 0.
223 # This is not a request at all: no field is named, so there is no state to
224 # reach and nothing that could have held. That is "an argument list that is
225 # syntactically fine but semantically empty", which is what
226 # App::karr::Role::ExitCodes/usage_error exists for, and the same answer
227 # `karr move , todo` gets one command over. kanban-md refuses it too
228 # (internal/board/mutate.go:413-415, "no changes specified"); it exits 1
229 # there only because cobra exits 1 for everything.
230 #
231 # length, not defined, so this asks the same question the write path answers:
232 # every guard inside the callback below is `defined && length` (tickets #78,
233 # #153), so an empty value sets nothing, and an option whose value the write
234 # path discards has not named a change. On the command line MooX::Options
235 # refuses an empty value first ("Option title requires an argument", exit 2
236 # through App::karr::Role::ExitCodes), so this half of the rule is for an
237 # in-process caller -- and for the two lists staying in step rather than
238 # drifting into disagreeing about what counts.
239 $self->usage_error('no changes specified -- karr edit needs at least one field option')
240 unless $self->_has_field_change;
241  
242 # Once, before any task is touched: these are plain option values, so a bad
243 # one must not update the first half of a batch (ticket #54). --status is not
244 # here because it goes through apply_status_change, which is the one place a
245 # status change happens and therefore the one place its name is checked.
246 #
247 # --claim and --release are mutually exclusive: --claim sets a claim --release
248 # is about to discard, so the require_claim guard in apply_status_change would
249 # be satisfied by a claim the same command is clearing and let the task land
250 # in a require_claim column with no claim on it (ticket #150). kanban-md
251 # rejects the pair at the flag layer too (cmd/edit.go:128-130); we match.
252 $self->usage_error('cannot use --claim and --release together')
253 if (defined $self->claim && length $self->claim) && $self->release;
254  
255 # The two pairs kanban-md rejects on the same layer for the same reason, which
256 # this command had left running instead (ticket #235). Both used to take the
257 # write callback below through both branches and let the second one win:
258 # `--block "waiting" --unblock` left the card unblocked, and
259 # `--body neu --append-body note` left it holding "neu\nnote" -- a body the
260 # caller asked for neither half of. A caller naming both halves of a pair has
261 # contradicted themselves, and the guard above already says what karr does
262 # about that. (cmd/edit.go:366-369 and cmd/edit.go:202-206, both
263 # StatusConflict; ADR 0002 makes it exit 2 here.)
264 #
265 # length, not truth, on every value-carrying half, exactly as above: an empty
266 # --block names no reason and sets nothing in the callback, so it is not a
267 # change for --unblock to contradict (tickets #78, #153) -- the same rule
268 # _has_field_change reads one guard up.
269 $self->usage_error('cannot use --block and --unblock together')
270 if (defined $self->block && length $self->block) && $self->unblock;
271  
272 $self->usage_error('cannot use --body and --append-body together')
273 if (defined $self->body && length $self->body)
274 && (defined $self->append_body && length $self->append_body);
275  
276 # The same pair rule, one field over: --due sets a date --clear-due is about
277 # to remove, so naming both halves contradicts the caller (ticket #273).
278 # length, not truth, on the value-carrying half, exactly as above: an empty
279 # --due names no date and sets nothing in the callback, so it is not a change
280 # for --clear-due to contradict (tickets #78, #153).
281 $self->usage_error('cannot use --due and --clear-due together')
282 if (defined $self->due && length $self->due) && $self->clear_due;
283  
284 my $config = App::karr::Config->from_merged( $self->store->effective_config );
285 $config->validate_priority( $self->priority ) if defined $self->priority;
286 $config->validate_class( $self->class ) if defined $self->class;
287 App::karr::Config->validate_due( $self->due ) if defined $self->due;
288  
289 # Same rule for the dependency flags (ticket #124): a malformed or unknown
290 # id is wrong for every id in the batch at once. Only ids being *added* must
291 # exist -- removing an id the board no longer has is how a dependency on a
292 # deleted task is cleaned up. length, not truth (ticket #78).
293 my $add_depends;
294 if ( defined $self->add_depends_on && length $self->add_depends_on ) {
295 $add_depends = $self->parse_dependency_ids( '--add-depends-on', $self->add_depends_on );
296 $self->assert_dependencies_exist($add_depends);
297 }
298 my $remove_depends;
299 if ( defined $self->remove_depends_on && length $self->remove_depends_on ) {
300 $remove_depends = $self->parse_dependency_ids( '--remove-depends-on', $self->remove_depends_on );
301 }
302  
303 # The cross-board pair (ticket #192), under the same rule: a malformed
304 # BOARD#ID is wrong for every id in the batch at once, so it is checked here
305 # and condemns the invocation. Nothing checks whether the far card exists --
306 # that is local configuration's question and `karr needs` asks it.
307 my $add_needs;
308 if ( defined $self->add_needs && length $self->add_needs ) {
309 $add_needs = App::karr::CrossBoard->parse_refs( '--add-needs', $self->add_needs );
310 }
311 my $remove_needs;
312 if ( defined $self->remove_needs && length $self->remove_needs ) {
313 $remove_needs = App::karr::CrossBoard->parse_refs( '--remove-needs', $self->remove_needs );
314 }
315  
316 # Every id is attempted, whatever the ones before it did: a missing id used to
317 # die from inside this loop and take the rest of the batch with it (ticket
318 # #61). The option-value checks above stay outside it, because they condemn
319 # the whole invocation rather than one id.
320 my ($results, $failed) = $self->run_batch(\@ids, sub {
321 my ($id) = @_;
322  
323 # A self-reference is the one dependency error that is per-id rather than
324 # per-invocation: `edit 4,5 --add-depends-on 5` is valid for 4 and wrong
325 # for 5, so it fails this id and lets the batch carry on (ticket #61).
326 # kanban-md rejects it at the same moment (ValidateDependencyIDs). The
327 # numeric guard keeps a non-numeric batch id headed for its own "Task X
328 # not found" instead of a numeric-comparison warning.
329 die "Task $id cannot depend on itself\n"
330 if $add_depends && $id =~ /\A[0-9]+\z/ && grep { $_ == $id } @$add_depends;
331  
332 my $task = $self->update_task_guarded($id, sub {
333 my ($task) = @_;
334  
335 # --release is the one edit that may act on somebody else's claim: it
336 # exists precisely to break a claim a crashed agent left behind, and it
337 # is karr's only way out of one before the timeout. Everything else has
338 # to own the claim, or find it expired. Same carve-out as kanban-md's
339 # validateEditClaim (cmd/edit.go).
340 #
341 # The owner asked about is who the caller is: --claim, else KARR_CLAIM
342 # (ADR 0005). That is an identity, not a stamp, so a note on a card the
343 # caller holds is let through in any column. What gets written is
344 # decided separately below.
345 $self->check_claim($task, $self->resolved_claim) unless $self->release;
346  
347 # Clear the claim BEFORE the status change so the require_claim guard
348 # in apply_status_change sees the post-release state: --release sets up
349 # a claim the guard was about to satisfy, and a status change into a
350 # require_claim column would otherwise walk straight through and leave
351 # the card with no owner (ticket #150). kanban-md's equivalent check
352 # (validateEditPost, internal/board/mutate.go:442) fires after applyFn
353 # regardless of release.
354 if ($self->release) {
355 $task->clear_claimed_by;
356 $task->clear_claimed_at;
357 }
358  
359 # The claim this edit writes (ticket #303, the #286 rule create
360 # follows): an explicit --claim on any status, KARR_CLAIM only when the
361 # card ends up in a require_claim column -- the --status it is given,
362 # else the one it already has. Without the narrowing, every edit stamped
363 # the caller's env name. `edit ID -a note` on a backlog card took it out
364 # of every other agent's `pick` and `list --unclaimed`, and a plain
365 # `--release` put back the claim it had just cleared.
366 #
367 # --release never claims. An explicit --claim with it was refused above,
368 # so what this drops is only the env default. Passed to
369 # apply_status_change as well, so `--release --status in-progress` meets
370 # the #150 refusal instead of being satisfied by the env name.
371 my $ends_in = ( defined $self->status && length $self->status )
372 ? $self->status : $task->status;
373 my $claim = $self->release ? undef : $self->resolved_claim_for($ends_in);
374  
375 # A card that is, or ends up, in backlog cannot gain a claim (ticket
376 # k306). apply_status_change asks the same for --status; this is the
377 # half it never sees -- `edit ID --claim X` on a card already there.
378 # Before any field is touched, so the refusal writes nothing.
379 $self->check_held_back_claim( $task, $ends_in, $claim );
380  
381 # length, not truth: a literal "0" is a meaningful title, status,
382 # priority, assignee, due, class, estimate, body, append, tag or block
383 # reason (ticket #153, extending ticket #78's rule from --body to its
384 # siblings).
385 $task->title($self->title) if defined $self->title && length $self->title;
386 $self->apply_status_change($task, $self->status, $claim) if defined $self->status && length $self->status;
387 $task->priority($self->priority) if defined $self->priority && length $self->priority;
388 $task->assignee($self->assignee) if defined $self->assignee && length $self->assignee;
389 $task->due($self->due) if defined $self->due && length $self->due;
390 $task->clear_due if $self->clear_due;
391 $task->class($self->class) if defined $self->class && length $self->class;
392 $task->estimate($self->estimate) if defined $self->estimate && length $self->estimate;
393 $task->body($self->body) if defined $self->body && length $self->body;
394  
395 # length, not truth: appending to a body of "0" must not replace it
396 # (ticket #78). The outer guard had drifted back to truth while the
397 # comment still read length-not-truth (ticket #153). What the append
398 # itself does -- blank-line separator, no leading separator on an empty
399 # body, optional UTC stamp -- lives in App::karr::Task::append_body,
400 # shared with handoff, which held a copy of it (ticket #238).
401 $task->append_body( $self->append_body, $self->timestamp )
402 if defined $self->append_body && length $self->append_body;
403  
404 if (defined $self->add_tag && length $self->add_tag) {
405 my @new = split /,/, $self->add_tag;
406 my %existing = map { $_ => 1 } @{$task->tags};
407 push @{$task->tags}, grep { !$existing{$_} } @new;
408 }
409  
410 if (defined $self->remove_tag && length $self->remove_tag) {
411 my %remove = map { $_ => 1 } split /,/, $self->remove_tag;
412 $task->tags([grep { !$remove{$_} } @{$task->tags}]);
413 }
414  
415 # The --add-tag/--remove-tag shape: append-unique and remove, so one
416 # rule covers both list fields (ticket #124).
417 if ($add_depends) {
418 my %existing = map { $_ => 1 } @{$task->depends_on};
419 push @{$task->depends_on}, grep { !$existing{$_} } @$add_depends;
420 }
421  
422 if ($remove_depends) {
423 my %remove = map { $_ => 1 } @$remove_depends;
424 $task->depends_on([grep { !$remove{$_} } @{$task->depends_on}]);
425 }
426  
427 # Same shape once more, one field over: a cross-board link is a tag, so
428 # append-unique and remove are literally the tag rules (see
429 # App::karr::CrossBoard for why the link lives there and not in a
430 # frontmatter field of its own).
431 App::karr::CrossBoard->add_needs( $task, $add_needs ) if $add_needs;
432 App::karr::CrossBoard->remove_needs( $task, $remove_needs ) if $remove_needs;
433  
434 if (defined $claim && length $claim) {
435 $task->claimed_by($claim);
436 $task->claimed_at(gmtime->datetime . 'Z');
437 }
438  
439 if (defined $self->block && length $self->block) {
440 $task->block($self->block);
441 }
442  
443 if ($self->unblock) {
444 $task->unblock;
445 }
446 });
447  
448 printf "Updated task %d: %s\n", $task->id, $task->title unless $self->json;
449 # --status goes through apply_status_change, so an edit that takes a card
450 # up gets the same dependency warning `karr move` does, for free and by
451 # construction -- the #55 point again (ticket #123). An edit that changes
452 # anything else records nothing, so this adds no key.
453 # --release skips check_claim entirely, so an edit that breaks a claim on
454 # purpose records nothing and adds no key here either: it is not an override
455 # that went unnoticed, it is the one command whose whole job is saying so
456 # (App::karr::Role::ClaimTimeout/expired_claim_report, #177).
457 return { id => $task->id, title => $task->title,
458 $self->expired_claim_report( $task->id ),
459 $self->dependency_report( $task->id ) };
460 });
461  
462 $self->sync_after;
463  
464 $self->print_json_results(@$results);
465  
466 $self->report_batch_failure($failed, scalar @ids);
467 }
468  
469 1;
470  
471 __END__
« blib/lib/App/karr/Cmd/Create.pm ↑ Summary blib/lib/App/karr/Cmd/GetRefs.pm »