| 1 | # ABSTRACT: Report and resolve cross-board dependencies | ||
| 2 | |||
| 3 | package App::karr::Cmd::Needs; | ||
| 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 needs [ID[,ID,...]] [--resolve] [--board NAME=PATH] [--fleet-config FILE] [--json]', | |
| 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::TaskMutation; | ||
| 13 | use App::karr::CrossBoard; | ||
| 14 | use App::karr::Error qw( command_hint ); | ||
| 15 | |||
| 16 | with 'App::karr::Role::BoardAccess', 'App::karr::Role::Output', | ||
| 17 | 'App::karr::Role::TaskMutation'; | ||
| 18 | |||
| 19 | |||
| 20 | option resolve => ( | ||
| 21 | is => 'ro', | ||
| 22 | doc => 'Drop settled links and unblock a card whose last link settled', | ||
| 23 | ); | ||
| 24 | |||
| 25 | option board => ( | ||
| 26 | is => 'ro', | ||
| 27 | format => 's@', | ||
| 28 | doc => 'Locate a board: NAME=PATH (repeatable)', | ||
| 29 | ); | ||
| 30 | |||
| 31 | # Not --config: App::karr::Role::BoardDiscovery already owns a `config` | ||
| 32 | # accessor (this board's own App::karr::Config), and the name says the more | ||
| 33 | # useful thing anyway -- this is the fleet's local config, not the board's. | ||
| 34 | option fleet_config => ( | ||
| 35 | is => 'ro', | ||
| 36 | format => 's', | ||
| 37 | doc => 'Fleet config file (default: ~/.config/karr-foundation/config.yml)', | ||
| 38 | ); | ||
| 39 | |||
| 40 | # --board NAME=PATH, refused as a usage error before anything is read: it is a | ||
| 41 | # plain option value, so a typo in it is wrong for the whole invocation | ||
| 42 | # (ticket #54's rule). | ||
| 43 | sub _overrides { | ||
| 44 | my ($self) = @_; | ||
| 45 | my %map; | ||
| 46 | for my $entry ( @{ $self->board // [] } ) { | ||
| 47 | my ( $name, $path ) = $entry =~ /\A([^=]+)=(.+)\z/; | ||
| 48 | # The board name is salvaged out of what was typed wherever there is one -- | ||
| 49 | # `--board other-repo` and `--board other-repo=` both name a board and only | ||
| 50 | # miss the path, so the line that would have worked can keep the name and | ||
| 51 | # leave PATH as the one placeholder (ticket k263). | ||
| 52 | unless ( defined $name && length $path ) { | ||
| 53 | my ($typed) = $entry =~ /\A([^=]+)/; | ||
| 54 | $self->usage_error( | ||
| 55 | qq{invalid --board "$entry" (expected NAME=PATH):\n} | ||
| 56 | . command_hint( 'needs', '--board', | ||
| 57 | ( defined $typed && length $typed ? $typed : 'NAME' ) . '=PATH' ) ); | ||
| 58 | } | ||
| 59 | $map{$name} = $path; | ||
| 60 | } | ||
| 61 | return \%map; | ||
| 62 | } | ||
| 63 | |||
| 64 | # This board's own name, which is what the far card has to name back for a link | ||
| 65 | # to count as verified. The repository's directory basename: the same name | ||
| 66 | # `karr-foundation --status` prints and the same name the escalation convention | ||
| 67 | # writes into `escalated-from:<repo>#<id>`. | ||
| 68 | sub _own_board_name { | ||
| 69 | my ($self) = @_; | ||
| 70 | return $self->git_root->basename; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _cards { | ||
| 74 | my ( $self, $args_ref ) = @_; | ||
| 75 | |||
| 76 | my @pos = $self->positional_args($args_ref); | ||
| 77 | if ( defined $pos[0] && length $pos[0] ) { | ||
| 78 | my @tasks; | ||
| 79 | for my $id ( $self->parse_ids( $pos[0] ) ) { | ||
| 80 | # The id names no card, so nothing about this card can be asked -- what | ||
| 81 | # would have worked is the command that shows which ids exist (k263). | ||
| 82 | my $task = $self->find_task($id) | ||
| 83 | or die "Task $id not found on this board:\n" | ||
| 84 | . command_hint( 'list', '--compact' ) . "\n"; | ||
| 85 | push @tasks, $task; | ||
| 86 | } | ||
| 87 | return @tasks; | ||
| 88 | } | ||
| 89 | |||
| 90 | return sort { $a->id <=> $b->id } | ||
| 91 | grep { scalar App::karr::CrossBoard->needs_of($_) } $self->load_tasks; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub execute { | ||
| 95 | my ( $self, $args_ref, $chain_ref ) = @_; | ||
| 96 | |||
| 97 | $self->check_positional_args( $args_ref, 1 ); | ||
| 98 | my $overrides = $self->_overrides; | ||
| 99 | |||
| 100 | # A report pays for no transport, the way `list` and `show` pay for none; | ||
| 101 | # only the writing half syncs. #190 split the fleet namespace the same way | ||
| 102 | # and for the same reason. | ||
| 103 | if ( $self->resolve ) { | ||
| 104 | $self->sync_before; | ||
| 105 | $self->require_board; | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | $self->require_local_board; | ||
| 109 | } | ||
| 110 | |||
| 111 | my $fleet = App::karr::CrossBoard->new( | ||
| 112 | overrides => $overrides, | ||
| 113 | ( defined $self->fleet_config ? ( config_file => $self->fleet_config ) : () ), | ||
| 114 | ); | ||
| 115 | my $me = $self->_own_board_name; | ||
| 116 | |||
| 117 | my @report; | ||
| 118 | for my $task ( $self->_cards($args_ref) ) { | ||
| 119 | my @refs = App::karr::CrossBoard->needs_of($task); | ||
| 120 | next unless @refs; | ||
| 121 | |||
| 122 | my @links = map { | ||
| 123 | $fleet->link_state( $_, origin => { board => $me, id => $task->id } ) | ||
| 124 | } @refs; | ||
| 125 | |||
| 126 | push @report, { | ||
| 127 | id => $task->id, | ||
| 128 | title => $task->title, | ||
| 129 | blocked => ( $task->has_blocked ? 1 : 0 ), | ||
| 130 | needs => \@links, | ||
| 131 | }; | ||
| 132 | } | ||
| 133 | |||
| 134 | $self->_resolve_cards( \@report ) if $self->resolve; | ||
| 135 | |||
| 136 | $self->sync_after if $self->resolve; | ||
| 137 | |||
| 138 | return $self->print_json( \@report ) if $self->json; | ||
| 139 | $self->_print_report( \@report ); | ||
| 140 | } | ||
| 141 | |||
| 142 | # One guarded write per card, and only for cards with something to settle. The | ||
| 143 | # link states were read before the guard, so they are a hint that ages exactly | ||
| 144 | # as every other karr client's view does -- what the callback re-decides | ||
| 145 | # against the fresh card is which of those links the card still carries and | ||
| 146 | # whether anything is left after they go. | ||
| 147 | sub _resolve_cards { | ||
| 148 | my ( $self, $report ) = @_; | ||
| 149 | |||
| 150 | for my $entry (@$report) { | ||
| 151 | my @settled = grep { $_->{state} eq 'settled' } @{ $entry->{needs} }; | ||
| 152 | next unless @settled; | ||
| 153 | |||
| 154 | my @refs = map { App::karr::CrossBoard->parse_ref( 'needs', $_->{ref} ) } @settled; | ||
| 155 | my $unblocked; | ||
| 156 | |||
| 157 | my $task = $self->update_task_guarded( $entry->{id}, sub { | ||
| 158 | my ($task) = @_; | ||
| 159 | undef $unblocked; | ||
| 160 | App::karr::CrossBoard->remove_needs( $task, \@refs ); | ||
| 161 | |||
| 162 | # The card is unblocked only when it waits on nothing else at all. A | ||
| 163 | # link is what says "this card is waiting on another board", so the last | ||
| 164 | # one going is what the escalating agent's `--block` was for; while one | ||
| 165 | # is left the block still has something to point at. | ||
| 166 | return if App::karr::CrossBoard->needs_of($task); | ||
| 167 | return unless $task->has_blocked; | ||
| 168 | $unblocked = $task->has_block_reason ? $task->block_reason : 'no reason given'; | ||
| 169 | $task->unblock; | ||
| 170 | } ); | ||
| 171 | |||
| 172 | $entry->{resolved} = [ map { $_->{ref} } @settled ]; | ||
| 173 | $entry->{unblocked} = defined $unblocked ? 1 : 0; | ||
| 174 | $entry->{was_blocked_because} = $unblocked if defined $unblocked; | ||
| 175 | $entry->{blocked} = $task->has_blocked ? 1 : 0; | ||
| 176 | $_->{state} = 'resolved' for @settled; | ||
| 177 | } | ||
| 178 | return; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub _print_report { | ||
| 182 | my ( $self, $report ) = @_; | ||
| 183 | |||
| 184 | unless (@$report) { | ||
| 185 | print "No cross-board dependencies.\n"; | ||
| 186 | return; | ||
| 187 | } | ||
| 188 | |||
| 189 | for my $entry (@$report) { | ||
| 190 | printf "Task #%d: %s\n", $entry->{id}, $entry->{title}; | ||
| 191 | for my $link ( @{ $entry->{needs} } ) { | ||
| 192 | printf " %s %s -- %s%s\n", | ||
| 193 | ( $link->{state} eq 'resolved' ? 'resolved' : 'needs' ), | ||
| 194 | $link->{ref}, $link->{detail}, $self->_verification_note($link); | ||
| 195 | } | ||
| 196 | printf " unblocked (was: %s)\n", $entry->{was_blocked_because} | ||
| 197 | if defined $entry->{was_blocked_because}; | ||
| 198 | } | ||
| 199 | return; | ||
| 200 | } | ||
| 201 | |||
| 202 | # The half of the escalation protocol nothing checked before: whether the far | ||
| 203 | # card names this one back. A back-reference to a different card is shown | ||
| 204 | # rather than swallowed -- two cards that disagree about which escalation they | ||
| 205 | # are is precisely the mistake worth seeing. | ||
| 206 | sub _verification_note { | ||
| 207 | my ( $self, $link ) = @_; | ||
| 208 | return '' unless exists $link->{status}; | ||
| 209 | return ', back-reference verified' if $link->{verified}; | ||
| 210 | return sprintf ', back-reference names %s', join( ', ', @{ $link->{back_refs} } ) | ||
| 211 | if $link->{back_refs}; | ||
| 212 | return ', no back-reference'; | ||
| 213 | } | ||
| 214 | |||
| 215 | 1; | ||
| 216 | |||
| 217 | __END__ | ||