blib/lib/App/karr/Cmd/Show.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/Pick.pm ↑ Summary blib/lib/App/karr/Cmd/Unlock.pm »
1 # ABSTRACT: Show full details of a task
2  
3 package App::karr::Cmd::Show;
4 our $VERSION = '0.602';
5 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooX::Options (
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 usage_string => 'USAGE: karr show [ID[,ID,...]] [--me] [--agent NAME] [--last N] [--json] [--compact]',
9 1 );
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::CompactOutput;
13 use App::karr::Task;
14 use App::karr::CrossBoard;
15 use App::karr::Error qw( command_hint user_error );
16  
17 with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output',
18 'App::karr::Role::CompactOutput';
19  
20  
21 option last => (
22 is => 'ro',
23 format => 'i',
24 default => sub { 1 },
25 doc => 'Number of recent tasks to show (default: 1)',
26 );
27  
28 option me => (
29 is => 'ro',
30 doc => 'Show the task(s) my identity most recently acted on',
31 );
32  
33 option agent => (
34 is => 'ro',
35 format => 's',
36 doc => 'Show the task(s) most recently claimed by this agent name',
37 );
38  
39 sub _show_task {
40 my ($self, $task) = @_;
41  
42 if ($self->json) {
43 $self->print_json($task->to_json_hash);
44 return;
45 }
46  
47 printf "Task #%d: %s\n", $task->id, $task->title;
48 printf "Status: %s\n", $task->status;
49 printf "Priority: %s\n", $task->priority;
50 printf "Class: %s\n", $task->class;
51 printf "Assignee: %s\n", $task->assignee if $task->has_assignee;
52 printf "Tags: %s\n", join(', ', @{$task->tags}) if @{$task->tags};
53 printf "Due: %s\n", $task->due if $task->has_due;
54 printf "Estimate: %s\n", $task->estimate if $task->has_estimate;
55 printf "Depends: %s\n",
56 join( ', ', map { $self->_dependency_label($_) } @{$task->depends_on} )
57 if @{$task->depends_on};
58 # The other board's cards cannot be labelled with a status the way local
59 # dependencies are: reading one needs a path this command does not have and
60 # must not invent (App::karr::CrossBoard). The reference is printed and
61 # `karr needs` is where the state comes from.
62 my @needs = App::karr::CrossBoard->needs_of($task);
63 printf "Needs: %s\n",
64 join( ', ', map { App::karr::CrossBoard->format_ref($_) } @needs ) if @needs;
65 my @from = App::karr::CrossBoard->escalated_from_of($task);
66 printf "From: %s\n",
67 join( ', ', map { App::karr::CrossBoard->format_ref($_) } @from ) if @from;
68 printf "Claimed: %s\n", $task->claimed_by if $task->has_claimed_by;
69 printf "Blocked: %s\n", $task->has_block_reason ? $task->block_reason : 'yes'
70 if $task->has_blocked;
71 printf "Created: %s\n", $task->created;
72 printf "Updated: %s\n", $task->updated;
73 if (defined $task->body && length $task->body) {
74 print "\n" . $task->body . "\n";
75 }
76 }
77  
78 # A bare id list answers the wrong question: `depends_on: [5]` tells the reader
79 # nothing about whether 5 is finished, which is the only thing they wanted to
80 # know. Half of what made ticket #123 a trap was that `show` did not print the
81 # field at all, so a dependency recorded on a card was invisible to the one
82 # reader who could have acted on it. An id the board does not have is called
83 # unknown rather than left to look like a status.
84 sub _dependency_label {
85 my ($self, $dep_id) = @_;
86 my $dep = $self->find_task($dep_id);
87 return sprintf '%s (unknown)', $dep_id unless $dep;
88 return sprintf '%s (%s)', $dep_id, $dep->status;
89 }
90  
91 # Tasks sorted most-recently-updated first.
92 sub _by_updated {
93 my ($self, @tasks) = @_;
94 return sort { ($b->updated // '') cmp ($a->updated // '') } @tasks;
95 }
96  
97 # Task ids the current identity most recently acted on, newest first, deduped.
98 sub _my_recent_ids {
99 my ($self, $limit) = @_;
100 my @ids;
101 my %seen;
102 for my $entry (reverse $self->activity_log->entries) {
103 my $tid = $entry->{task_id};
104 next unless defined $tid;
105 next if $seen{$tid}++;
106 push @ids, $tid;
107 last if @ids >= $limit;
108 }
109 return @ids;
110 }
111  
112 # The id names no card, so there is nothing to show -- end on the command
113 # that lists the ids that do exist, the same spelling the mutation commands
114 # raise through App::karr::Role::TaskMutation/task_not_found (ticket k264).
115 # Show is read-only and composes no mutation role, so the line is inlined.
116 # The single-id path dies with it (the hint is the very last line, pinned by
117 # t/264); the batch path warns it per missing id and reports the failure at
118 # the end.
119 sub _not_found {
120 my ($self, $id) = @_;
121 return "Task $id not found on this board:\n"
122 . command_hint('list', '--compact') . "\n";
123 }
124  
125 sub _select_tasks {
126 my ($self, $id) = @_;
127  
128 # Explicit id always wins.
129 if (defined $id) {
130 my $task = $self->find_task($id);
131 die $self->_not_found($id) unless $task;
132 return ($task);
133 }
134  
135 my $limit = $self->last;
136  
137 if ($self->me) {
138 my @tasks = grep { defined } map { $self->find_task($_) } $self->_my_recent_ids($limit);
139 return @tasks;
140 }
141  
142 if (defined $self->agent) {
143 my @claimed = grep { $_->has_claimed_by && $_->claimed_by eq $self->agent } $self->load_tasks;
144 my @sorted = $self->_by_updated(@claimed);
145 return splice(@sorted, 0, $limit);
146 }
147  
148 my @sorted = $self->_by_updated($self->load_tasks);
149 return splice(@sorted, 0, $limit);
150 }
151  
152 sub execute {
153 my ($self, $args_ref, $chain_ref) = @_;
154  
155 $self->check_positional_args($args_ref, 1);
156  
157 # --last is a count, so 0 and negatives are invalid values, not requests for
158 # a smaller board. They used to be clamped silently to 1, so `--last 0`
159 # answered with one task and exit 0 -- indistinguishable from a correct call
160 # (ticket #76). ADR 0002 classifies an invalid option value as a usage error.
161 $self->usage_error( sprintf '--last must be 1 or greater (got %d)', $self->last )
162 if $self->last < 1;
163  
164 # After the usage checks, before any lookup: "No tasks found." / `[]` for an
165 # id that was never loaded is the wrong answer, and "Task N not found" is a
166 # worse one -- neither says the board itself is not here (#135).
167 $self->require_local_board;
168  
169 my @pos = $self->positional_args($args_ref);
170 my @tasks;
171 my $failed = 0;
172 my @ids;
173  
174 if (defined $pos[0] && length $pos[0]) {
175 # The batch form, split by the shared ID[,ID,...] splitter of the batch
176 # commands (App::karr::Role::BoardAccess/parse_ids). A comma with no ids
177 # around it splits to nothing and is a usage error, the same guard move,
178 # delete and archive raise.
179 @ids = $self->parse_ids($pos[0]);
180 die "Usage: karr show [ID[,ID,...]] [--me] [--agent NAME] [--last N] [--json] [--compact]\n"
181 unless @ids;
182  
183 if (@ids == 1) {
184 # A single id keeps the hard not-found answer: the message ends on the
185 # working command, with nothing after it (ticket k264).
186 my $task = $self->find_task($ids[0]);
187 die $self->_not_found($ids[0]) unless $task;
188 @tasks = ($task);
189 } else {
190 # The batch rule of ADR 0002: every id is attempted, a missing one is
191 # reported and the ids after it are still shown, and the exit code
192 # reports the failure at the end.
193 for my $id (@ids) {
194 my $task = $self->find_task($id);
195 if ($task) {
196 push @tasks, $task;
197 } else {
198 $failed++;
199 warn $self->_not_found($id);
200 }
201 }
202 }
203 } else {
204 @tasks = $self->_select_tasks(undef);
205 }
206  
207 # "No tasks found." stands under --compact too. It is one line already, and
208 # printing nothing at all would make an empty selection indistinguishable
209 # from a card whose line went missing -- `list --compact` can afford silence
210 # because its table says "0 task(s)", this command has no second half to say
211 # it in. A batch that reported every id missing has its second half on
212 # STDERR already, so the line is left out there.
213 unless (@tasks) {
214 print "No tasks found.\n" unless $self->json || $failed;
215 $self->print_json([]) if $self->json;
216 } elsif ($self->json) {
217 my @data = map { $_->to_json_hash } @tasks;
218 # A single explicit lookup stays a bare object for backward compatibility;
219 # the batch form is always an array, the shape `list --json` uses.
220 $self->print_json( @ids > 1 ? \@data : ( @data == 1 ? $data[0] : \@data ) );
221 } elsif ($self->compact) {
222 # Below --json and above the detail view, the same place `pick` cuts
223 # (#251): --compact shapes the plaintext rendering and never the payload.
224 # The line is App::karr::Task's, shared with `list --compact` so the two
225 # renderings of one card cannot drift apart (#254).
226 for my $task (@tasks) {
227 print $task->compact_line . "\n";
228 }
229 } else {
230 for my $i (0 .. $#tasks) {
231 $self->_show_task($tasks[$i]);
232 print "\n" . ('=' x 60) . "\n\n" if $i < $#tasks;
233 }
234 }
235  
236 # The batch rule of ADR 0002, the exit-code half: partial success is
237 # committed, the exit code reports the failure (1). The same summary the
238 # mutation commands raise through App::karr::Role::TaskMutation/
239 # report_batch_failure.
240 user_error( sprintf '%d of %d ids failed', $failed, scalar @ids ) if $failed;
241 }
242  
243 1;
244  
245 __END__
« blib/lib/App/karr/Cmd/Pick.pm ↑ Summary blib/lib/App/karr/Cmd/Unlock.pm »