blib/lib/App/karr/CrossBoard.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« blib/lib/App/karr/Cmd/SetRefs.pm ↑ Summary lib/App/karr/SyncGuard.pm »
1 # ABSTRACT: Cross-board dependencies -- a link from a card here to a card on another board
2  
3 package App::karr::CrossBoard;
4 our $VERSION = '0.602';
5 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # Imported, not called through the package: App::karr::Encoding owns every
9 # character/octet crossing, and the fleet config file is one of them.
10 5 ▶use App::karr::Encoding qw( yaml_load );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use App::karr::Git;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use App::karr::BoardStore;
13  
14  
15 use constant NEEDS_PREFIX => 'needs:';
16 use constant ESCALATED_FROM_PREFIX => 'escalated-from:';
17  
18 # A board name may not contain a path separator, a '#', a ',', a ':' or
19 # whitespace: the first is what keeps a machine-local path off a card, the rest
20 # are the characters the reference syntax and the tag list are made of. It may
21 # not start with '-' or '.' either, so a name can never be mistaken for an
22 # option or for a relative path.
23 my $BOARD_RE = qr/[A-Za-z0-9_][A-Za-z0-9._-]*/;
24 # The id side accepts the house kNNN spelling (foo#k30 == foo#30): an optional
25 # leading k/K in front of the digits, stripped by capturing only the digits, so
26 # what lands on the card and comes back out (format_ref) stays numeric. The same
27 # strip App::karr::Role::BoardAccess/normalize_task_id makes for a local id.
28 my $REF_RE = qr/\A($BOARD_RE)\#[kK]?([0-9]+)\z/;
29  
30 sub parse_ref {
31 my ( $class, $flag, $raw ) = @_;
32 $raw = '' unless defined $raw;
33 my ( $board, $id ) = $raw =~ $REF_RE;
34 # The same "Usage error:" marker App::karr::Role::ExitCodes emits and
35 # F<bin/karr> maps to exit 2, raised from a plain class so the commands do
36 # not each repeat the sentence -- the arrangement App::karr::Config's
37 # validate_* helpers already use.
38 die sprintf
39 "Usage error: invalid %s reference \"%s\" (expected BOARD#ID -- the "
40 . "other board's name and a task id on it; a path is not a board name)\n",
41 $flag, $raw
42 unless defined $board;
43 return { board => $board, id => $id + 0 };
44 }
45  
46  
47 sub parse_refs {
48 my ( $class, $flag, $value ) = @_;
49 my ( @refs, %seen );
50 for my $raw ( split /,/, ( defined $value ? $value : '' ) ) {
51 my $ref = $class->parse_ref( $flag, $raw );
52 push @refs, $ref unless $seen{ $class->format_ref($ref) }++;
53 }
54 die "Usage error: $flag requires at least one BOARD#ID reference\n"
55 unless @refs;
56 return \@refs;
57 }
58  
59  
60 sub format_ref {
61 my ( $class, $ref ) = @_;
62 return sprintf '%s#%d', $ref->{board}, $ref->{id};
63 }
64  
65  
66 sub needs_tag {
67 my ( $class, $ref ) = @_;
68 return NEEDS_PREFIX . $class->format_ref($ref);
69 }
70  
71 sub escalated_from_tag {
72 my ( $class, $ref ) = @_;
73 return ESCALATED_FROM_PREFIX . $class->format_ref($ref);
74 }
75  
76  
77 sub _refs_from_tags {
78 my ( $class, $task, $prefix ) = @_;
79 my @refs;
80 for my $tag ( @{ $task->tags } ) {
81 next unless index( $tag, $prefix ) == 0;
82 my $raw = substr $tag, length $prefix;
83 my ( $board, $id ) = $raw =~ $REF_RE or next;
84 push @refs, { board => $board, id => $id + 0 };
85 }
86 return @refs;
87 }
88  
89 sub needs_of {
90 my ( $class, $task ) = @_;
91 return $class->_refs_from_tags( $task, NEEDS_PREFIX );
92 }
93  
94 sub escalated_from_of {
95 my ( $class, $task ) = @_;
96 return $class->_refs_from_tags( $task, ESCALATED_FROM_PREFIX );
97 }
98  
99  
100 sub add_needs {
101 my ( $class, $task, $refs ) = @_;
102 my %have = map { $_ => 1 } @{ $task->tags };
103 my @new = grep { !$have{$_} } map { $class->needs_tag($_) } @$refs;
104 push @{ $task->tags }, @new;
105 return scalar @new;
106 }
107  
108 sub remove_needs {
109 my ( $class, $task, $refs ) = @_;
110 my %drop = map { $class->needs_tag($_) => 1 } @$refs;
111 my $before = @{ $task->tags };
112 $task->tags( [ grep { !$drop{$_} } @{ $task->tags } ] );
113 return $before - @{ $task->tags };
114 }
115  
116  
117 # ---------------------------------------------------------------------------
118 # Resolution: board name -> directory on this machine -> the far card
119 # ---------------------------------------------------------------------------
120  
121 has overrides => (
122 is => 'ro',
123 default => sub { {} },
124 );
125  
126  
127 has config_file => (
128 is => 'ro',
129 predicate => 1,
130 );
131  
132  
133 has config_data => (
134 is => 'ro',
135 predicate => 1,
136 );
137  
138  
139 has _candidates => ( is => 'lazy' );
140  
141 has _stores => (
142 is => 'ro',
143 default => sub { {} },
144 );
145  
146 sub _config_data {
147 my ($self) = @_;
148  
149 if ( $self->has_config_data ) {
150 my $data = $self->config_data;
151 return ref $data eq 'HASH' ? $data : {};
152 }
153  
154 my $file = $self->config_file;
155 my $explicit = $self->has_config_file && defined $file && length $file;
156 $file = path( $ENV{HOME} // '' )->child(qw( .config karr-foundation config.yml ))
157 unless $explicit;
158 $file = path($file);
159  
160 unless ( $file->is_file ) {
161 die "Fleet config not found: $file\n" if $explicit;
162 return {};
163 }
164  
165 my $data = try { yaml_load( $file->slurp_utf8 ) }
166 catch { die "Cannot parse fleet config $file: $_" };
167 return ref $data eq 'HASH' ? $data : {};
168 }
169  
170 # Board name -> the directories on this machine that could be it. Both of
171 # karr-foundation's discovery keys feed it: `dirs:` names repository roots
172 # outright, `scan:` names parents whose direct children are candidates. Nothing
173 # here asks whether a candidate actually holds a board -- that question is
174 # answered once, later, by the board itself in _store_for, so a `scan:` parent
175 # with fifty unrelated subdirectories costs a hash and not fifty git opens.
176 sub _build__candidates {
177 my ($self) = @_;
178 my $data = $self->_config_data;
179  
180 my %by_name;
181 my $add = sub {
182 my ($dir) = @_;
183 push @{ $by_name{ $dir->basename } }, $dir;
184 };
185  
186 for my $dir ( @{ $data->{dirs} // [] } ) {
187 my $p = path($dir);
188 $add->($p) if $p->is_dir;
189 }
190 for my $scan ( @{ $data->{scan} // [] } ) {
191 my $p = path($scan);
192 next unless $p->is_dir;
193 for my $child ( $p->children ) {
194 $add->($child) if $child->is_dir;
195 }
196 }
197  
198 # A repository reachable through both keys is one candidate, not an
199 # ambiguity -- the same realpath collapse App::karr::Foundation makes for
200 # the same reason (#166).
201 for my $name ( keys %by_name ) {
202 my ( %seen, @uniq );
203 for my $dir ( @{ $by_name{$name} } ) {
204 my $key = try { $dir->realpath } catch { $dir->absolute };
205 push @uniq, $dir unless $seen{"$key"}++;
206 }
207 $by_name{$name} = \@uniq;
208 }
209 return \%by_name;
210 }
211  
212 sub resolve {
213 my ( $self, $name ) = @_;
214  
215 my $override = $self->overrides->{$name};
216 return ( path($override), undef ) if defined $override && length $override;
217  
218 my $found = $self->_candidates->{$name} // [];
219 return ( undef,
220 "unknown board '$name' (no repository of that name in this machine's "
221 . "fleet config; name it with --board $name=PATH)" )
222 unless @$found;
223 return ( undef,
224 "ambiguous board '$name' (" . join( ', ', map { "$_" } @$found )
225 . "; name the one you mean with --board $name=PATH)" )
226 if @$found > 1;
227 return ( $found->[0], undef );
228 }
229  
230  
231 # The far board, or undef when that directory holds no karr board. Cached per
232 # directory: a report over a dozen cards waiting on the same board opens it
233 # once.
234 sub _store_for {
235 my ( $self, $dir ) = @_;
236 my $key = "$dir";
237 return $self->_stores->{$key} if exists $self->_stores->{$key};
238  
239 my $store = try {
240 return undef unless $dir->is_dir;
241 my $s = App::karr::BoardStore->new(
242 git => App::karr::Git->new( dir => "$dir" ) );
243 return $s->board_exists ? $s : undef;
244 }
245 catch { undef };
246 return $self->_stores->{$key} = $store;
247 }
248  
249 sub link_state {
250 my ( $self, $ref, %opt ) = @_;
251  
252 my %state = (
253 ref => $self->format_ref($ref),
254 board => $ref->{board},
255 task => $ref->{id},
256 verified => 0,
257 );
258  
259 my ( $dir, $why ) = $self->resolve( $ref->{board} );
260 return { %state, state => 'unknown-board', detail => $why } unless $dir;
261  
262 my $store = $self->_store_for($dir);
263 return {
264 %state,
265 state => 'no-board',
266 detail => "no karr board in $dir",
267 } unless $store;
268  
269 my $far = try { $store->find_task( $ref->{id} ) } catch { undef };
270  
271 # kanban-md treats a dependency on an id its board does not have as
272 # satisfied, so a dependent card is recoverable after a hard delete
273 # (internal/board/filter.go:151). karr already declines that for
274 # depends_on (App::karr::Role::DependencyCheck, #123) and declines it
275 # harder here: settling a link because the ticket somebody was waiting for
276 # cannot be found would unblock a card on the strength of a card nobody can
277 # read. It is reported and left alone.
278 return {
279 %state,
280 state => 'missing',
281 detail => sprintf( 'task %d does not exist on board %s', $ref->{id}, $ref->{board} ),
282 } unless $far;
283  
284 # The far board's own terminal statuses, never a hardcoded `done`: a fleet
285 # member imported from kanban-md may call its final column anything (#67).
286 my $settled = $store->is_terminal_status( $far->status );
287  
288 my @back = $self->escalated_from_of($far);
289 my $origin = $opt{origin};
290 my $verified = 0;
291 if ($origin) {
292 my $want = $self->format_ref($origin);
293 $verified = ( grep { $self->format_ref($_) eq $want } @back ) ? 1 : 0;
294 }
295  
296 return {
297 %state,
298 state => $settled ? 'settled' : 'open',
299 status => $far->status,
300 title => $far->title,
301 verified => $verified,
302 ( @back ? ( back_refs => [ map { $self->format_ref($_) } @back ] ) : () ),
303 detail => sprintf( '%s (%s)', $far->status, $settled ? 'settled' : 'open' ),
304 };
305 }
306  
307  
308 1;
309  
310 __END__
« blib/lib/App/karr/Cmd/SetRefs.pm ↑ Summary lib/App/karr/SyncGuard.pm »