| 1 | # ABSTRACT: Show activity log | ||
| 2 | |||
| 3 | package App::karr::Cmd::Log; | ||
| 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 log [--agent NAME] [--task ID] [--last N] [--since DATE] [--action KIND] [--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::ActivityLog; | ||
| 14 | use App::karr::Config; | ||
| 15 | use App::karr::Encoding qw( json_decode ); | ||
| 16 | |||
| 17 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output', | ||
| 18 | 'App::karr::Role::CompactOutput'; | ||
| 19 | |||
| 20 | |||
| 21 | option agent => ( | ||
| 22 | is => 'ro', | ||
| 23 | format => 's', | ||
| 24 | doc => 'Filter by agent name', | ||
| 25 | ); | ||
| 26 | |||
| 27 | # A string, not `format => 'i'`: Getopt::Long refuses `k5` before karr sees it, | ||
| 28 | # and the house kNNN spelling has to work here as it does for every other local | ||
| 29 | # id (ticket k310). Normalized and validated in execute. | ||
| 30 | option task => ( | ||
| 31 | is => 'ro', | ||
| 32 | format => 's', | ||
| 33 | doc => 'Filter by task ID', | ||
| 34 | ); | ||
| 35 | |||
| 36 | option last => ( | ||
| 37 | is => 'ro', | ||
| 38 | format => 'i', | ||
| 39 | default => sub { 20 }, | ||
| 40 | doc => 'Number of entries to show (default: 20)', | ||
| 41 | ); | ||
| 42 | |||
| 43 | option since => ( | ||
| 44 | is => 'ro', | ||
| 45 | format => 's', | ||
| 46 | doc => 'Only show entries on or after this date (YYYY-MM-DD)', | ||
| 47 | ); | ||
| 48 | |||
| 49 | option action => ( | ||
| 50 | is => 'ro', | ||
| 51 | format => 's', | ||
| 52 | doc => 'Only show entries with this action kind', | ||
| 53 | ); | ||
| 54 | |||
| 55 | sub execute { | ||
| 56 | my ($self, $args_ref, $chain_ref) = @_; | ||
| 57 | |||
| 58 | # --last is a count, so 0 and negatives are invalid values, not requests | ||
| 59 | # for a smaller log. The pre-fix truthiness guard read 0 as "no bound | ||
| 60 | # at all" and dumped the full log, while a negative passed the guard and | ||
| 61 | # sliced an empty range, so the command reported an empty log and exited | ||
| 62 | # 0 -- indistinguishable from "the board has no activity" (ticket #151). | ||
| 63 | # Same rule and same reason as `show --last` (ticket #76, ADR 0002). | ||
| 64 | $self->usage_error( | ||
| 65 | sprintf '--last must be 1 or greater (got %d)', $self->last ) | ||
| 66 | if $self->last < 1; | ||
| 67 | |||
| 68 | # Option validation first, so a bad --since or --action still exits 2 on a | ||
| 69 | # repository that has no board (ADR 0002, the ordering require_local_board | ||
| 70 | # documents -- the same rule `metrics --since` follows). | ||
| 71 | # | ||
| 72 | # --since is a date, validated the way every other date in karr is | ||
| 73 | # (App::karr::Config/validate_due): calendar-correct YYYY-MM-DD, and a | ||
| 74 | # usage error otherwise. A typo used to be answered with "No log entries." | ||
| 75 | # and exit 0, which reads as "no activity" when the truth is "no such | ||
| 76 | # date" (ticket #278). | ||
| 77 | if ( defined $self->since && length $self->since ) { | ||
| 78 | eval { App::karr::Config->validate_due( $self->since ); 1 } | ||
| 79 | or $self->usage_error( | ||
| 80 | sprintf 'invalid --since date "%s" (expected YYYY-MM-DD)', $self->since ); | ||
| 81 | } | ||
| 82 | |||
| 83 | # --action is one of the actions the board log can actually hold, and the | ||
| 84 | # vocabulary comes from App::karr::ActivityLog/ACTIONS -- the same constant | ||
| 85 | # the writers' actions are checked against -- rather than a second list | ||
| 86 | # that drifts from what the commands record (ticket #278). A typo used to | ||
| 87 | # be answered with "No log entries." and exit 0, which reads as "no | ||
| 88 | # activity" when the truth is "no such action". | ||
| 89 | if ( defined $self->action ) { | ||
| 90 | my @valid = App::karr::ActivityLog->ACTIONS; | ||
| 91 | $self->usage_error( | ||
| 92 | sprintf 'invalid --action "%s" (valid: %s)', $self->action, join(', ', @valid) ) | ||
| 93 | unless grep { $_ eq $self->action } @valid; | ||
| 94 | } | ||
| 95 | |||
| 96 | # --task is a local task id, so it takes the house kNNN spelling through | ||
| 97 | # the same normalize_task_id every other id argument goes through (k5 == | ||
| 98 | # 5). Anything that is still not a number afterwards -- abc, a lone k, k5x | ||
| 99 | # -- stays a usage error naming the value as typed, which is what the old | ||
| 100 | # `format => 'i'` answered it with (ticket k310). | ||
| 101 | my $task_id; | ||
| 102 | if ( defined $self->task ) { | ||
| 103 | $task_id = $self->normalize_task_id( $self->task ); | ||
| 104 | $self->usage_error( | ||
| 105 | sprintf 'invalid --task id "%s" (ids are numbers or kNNN)', $self->task ) | ||
| 106 | unless $task_id =~ /\A[0-9]+\z/; | ||
| 107 | # Task ids start at 1, so 0 in any spelling -- 0, k0, 00 -- names no | ||
| 108 | # task. The filter below used to test the id for truth, which read 0 as | ||
| 109 | # "no filter" and printed the whole log, while "00" (a true string) | ||
| 110 | # filtered for task 0 and printed none (ticket k312). | ||
| 111 | $self->usage_error( | ||
| 112 | sprintf 'invalid --task id "%s" (ids start at 1)', $self->task ) | ||
| 113 | if $task_id == 0; | ||
| 114 | } | ||
| 115 | |||
| 116 | # This is where the empty answers are told apart, and all three are | ||
| 117 | # settled before a single ref is read. "No log entries." is what a board | ||
| 118 | # with no activity says; a repository with no board says something else | ||
| 119 | # and exits 1 (#135), and a directory that is no repository at all never | ||
| 120 | # gets this far, because $self->store builds from git_root and git_root | ||
| 121 | # answers "Not a git repository. karr requires Git." itself. | ||
| 122 | # | ||
| 123 | # That last one used to be answered here, with a local `unless | ||
| 124 | # ($git->is_repo)` printing "Not a git repository. No log available." It | ||
| 125 | # was live only while this command built its own Git handle on an | ||
| 126 | # arbitrary directory; the refs-first refactor made $self->git come from | ||
| 127 | # git_root, which cannot return a non-repository, and the branch has been | ||
| 128 | # unreachable ever since. Removed with #253 -- it was also the one line in | ||
| 129 | # any --json-capable command that would have put plain text on STDOUT | ||
| 130 | # under --json (#248), so anything re-added here belongs on STDERR or in | ||
| 131 | # the payload, not in a bare print. | ||
| 132 | $self->require_local_board; | ||
| 133 | |||
| 134 | my $git = $self->git; | ||
| 135 | |||
| 136 | # Read all log refs (refs/karr/log/*) natively via Git::Native. | ||
| 137 | my @entries; | ||
| 138 | for my $ref ($git->list_refs('refs/karr/log/')) { | ||
| 139 | my $content = $git->read_ref($ref); | ||
| 140 | next unless $content; | ||
| 141 | for my $line (split /\n/, $content) { | ||
| 142 | my $entry = eval { json_decode($line) }; | ||
| 143 | push @entries, $git->maybe_repair_legacy($entry) if $entry; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | # Sort by timestamp | ||
| 148 | @entries = sort { $a->{ts} cmp $b->{ts} } @entries; | ||
| 149 | |||
| 150 | # Apply filters | ||
| 151 | if ($self->agent) { | ||
| 152 | @entries = grep { ($_->{agent} // '') eq $self->agent } @entries; | ||
| 153 | } | ||
| 154 | if (defined $task_id) { | ||
| 155 | @entries = grep { ($_->{task_id} // 0) == $task_id } @entries; | ||
| 156 | } | ||
| 157 | if ( defined $self->since && length $self->since ) { | ||
| 158 | # String comparison against the RFC3339 timestamp: an entry from the | ||
| 159 | # --since day itself is kept, matching kanban-md's | ||
| 160 | # entry.Timestamp.Before(opts.Since). | ||
| 161 | @entries = grep { ($_->{ts} // '') ge $self->since } @entries; | ||
| 162 | } | ||
| 163 | if ( defined $self->action ) { | ||
| 164 | @entries = grep { ($_->{action} // '') eq $self->action } @entries; | ||
| 165 | } | ||
| 166 | |||
| 167 | # Limit | ||
| 168 | if (@entries > $self->last) { | ||
| 169 | @entries = @entries[-$self->last .. -1]; | ||
| 170 | } | ||
| 171 | |||
| 172 | if ($self->json) { | ||
| 173 | $self->print_json(\@entries); | ||
| 174 | return; | ||
| 175 | } | ||
| 176 | |||
| 177 | # One line, so --compact has nothing to shorten here and says the same | ||
| 178 | # thing. Silence would be a worse compact answer than a short sentence. | ||
| 179 | unless (@entries) { | ||
| 180 | print "No log entries.\n"; | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | # No column padding: single spaces, `#12` for the task the way `list | ||
| 185 | # --compact` spells an id, and no trailing space when an entry has no | ||
| 186 | # detail. Until #254 this command took --compact from | ||
| 187 | # App::karr::Role::Output and printed the padded table for it regardless. | ||
| 188 | if ($self->compact) { | ||
| 189 | for my $e (@entries) { | ||
| 190 | my $line = sprintf '%s %s %s #%s', | ||
| 191 | $e->{ts} // '?', | ||
| 192 | $e->{agent} // '?', | ||
| 193 | $e->{action} // '?', | ||
| 194 | $e->{task_id} // '?'; | ||
| 195 | $line .= ' ' . $e->{detail} | ||
| 196 | if defined $e->{detail} && length $e->{detail}; | ||
| 197 | print $line . "\n"; | ||
| 198 | } | ||
| 199 | return; | ||
| 200 | } | ||
| 201 | |||
| 202 | for my $e (@entries) { | ||
| 203 | printf "%s %-15s %-10s task#%s %s\n", | ||
| 204 | $e->{ts} // '?', | ||
| 205 | $e->{agent} // '?', | ||
| 206 | $e->{action} // '?', | ||
| 207 | $e->{task_id} // '?', | ||
| 208 | $e->{detail} // ''; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | 1; | ||
| 213 | |||
| 214 | __END__ | ||