blib/lib/App/karr/Role/BoardAccess.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« bin/karr ↑ Summary blib/lib/App/karr/AgentName.pm »
1 # ABSTRACT: Role providing board discovery, sync lifecycle, and task access
2  
3 package App::karr::Role::BoardAccess;
4 our $VERSION = '0.602';
5 26 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use App::karr::Role::CliArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use App::karr::ActivityLog;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 with 'App::karr::Role::BoardDiscovery';
10 with 'App::karr::Role::SyncLifecycle';
11 with 'App::karr::Role::CliArgs';
12  
13  
14 # Guards against logging one mutation twice within a single command run: pick
15 # saves the task (logged here) and then calls append_log itself.
16 has _logged_writes => (
17 is => 'ro',
18 default => sub { {} },
19 );
20  
21 sub load_tasks {
22 my ($self) = @_;
23 return $self->store->load_tasks;
24 }
25  
26  
27 sub find_task {
28 my ($self, $id) = @_;
29 return $self->store->find_task($id);
30 }
31  
32  
33 sub save_task {
34 my ( $self, $task, $expected_oid ) = @_;
35 # One door, guarded or not, so the activity log has exactly one place to
36 # hang off. Handed the OID the card was read from (find_task_with_oid /
37 # read_ref_with_oid) this is a compare-and-swap and returns false when
38 # another agent got there first -- the caller re-reads and decides again,
39 # and a write that never landed is never logged. Splitting the guarded path
40 # off into its own method is what let move/edit/pick slip out of the log
41 # once App::karr::Role::TaskMutation arrived (#64 again).
42 my $wrote = @_ > 2
43 ? $self->store->save_task_cas( $task, $expected_oid )
44 : $self->store->save_task($task);
45 return $wrote unless $wrote;
46 $self->log_task_write( $task->id, $task->status, $task );
47 return $wrote;
48 }
49  
50  
51 sub delete_task {
52 my ($self, $id) = @_;
53 # The same guard save_task has, for the same reason. The log answers what
54 # happened to this board, and App::karr::BoardStore::delete_task returns
55 # false when nothing was removed -- the id was never there, or the delete
56 # itself failed. Logging that wrote an entry `karr log` and `karr show --me`
57 # then reported as a delete that happened (#64 gave them this log, #120
58 # found the two doors disagreeing). An entry cannot say "attempted": it
59 # carries agent, action and task id, so an attempt is indistinguishable from
60 # a real delete, and a false entry is worse than a missing one. The guarded
61 # twin, App::karr::Role::TaskMutation::delete_task_guarded, already dies on
62 # a missing id before it logs, so all three write paths agree now.
63 my $result = $self->store->delete_task($id);
64 return $result unless $result;
65 $self->log_task_write($id);
66 return $result;
67 }
68  
69  
70 sub allocate_next_id {
71 my ($self) = @_;
72 return $self->store->allocate_next_id;
73 }
74  
75  
76 sub normalize_task_id {
77 my ($self, $id) = @_;
78 return defined $id && $id =~ /\A[kK]([0-9]+)\z/ ? $1 : $id;
79 }
80  
81  
82 sub parse_ids {
83 my ($self, $id_str) = @_;
84 return map { $self->normalize_task_id($_) } split /,/, $id_str;
85 }
86  
87  
88 sub activity_log {
89 my ($self, $git) = @_;
90 $git //= $self->git;
91 return App::karr::ActivityLog->new(git => $git, role => $self->role);
92 }
93  
94  
95 sub append_log {
96 my ($self, $git, %entry) = @_;
97 my $key = ($entry{action} // '') . ':' . ($entry{task_id} // '');
98 return 0 if $self->_logged_writes->{$key}++;
99 return $self->activity_log($git)->log_entry(%entry);
100 }
101  
102  
103  
104 sub log_action {
105 my ($self) = @_;
106 my $name = ref($self) || $self;
107 $name =~ s/.*:://;
108 $name =~ s/(?<=[a-z0-9])([A-Z])/-$1/g;
109 return lc $name;
110 }
111  
112  
113 sub log_agent {
114 my ($self, $task) = @_;
115 if ($self->can('claim')) {
116 my $claim = $self->claim;
117 return $claim if defined $claim && length $claim;
118 }
119 return $task->claimed_by if $task && $task->has_claimed_by;
120 my $git = $self->git;
121 return $git->git_user_name || $git->git_user_email || 'unknown';
122 }
123  
124  
125 sub log_task_write {
126 my ($self, $task_id, $detail, $task) = @_;
127 my $action = $self->log_action;
128 return 0 if $self->_logged_writes->{"$action:$task_id"}++;
129 return $self->activity_log->log_entry(
130 agent => $self->log_agent($task),
131 action => $action,
132 task_id => $task_id + 0,
133 ( defined $detail ? ( detail => $detail ) : () ),
134 );
135 }
136  
137  
138 1;
139  
140 __END__
« bin/karr ↑ Summary blib/lib/App/karr/AgentName.pm »