blib/lib/App/karr/Cmd/Create.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/Config.pm ↑ Summary blib/lib/App/karr/Cmd/Edit.pm »
1 # ABSTRACT: Create a new task
2  
3 package App::karr::Cmd::Create;
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 create --title TEXT [--priority LEVEL] [--status STATUS] [options]',
9 1 );
10 0 ▶use App::karr::Role::BoardAccess;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use App::karr::Role::DependencyArgs;
12 use App::karr::Role::Output;
13 use App::karr::Role::ClaimDefault;
14 use App::karr::Task;
15 use App::karr::Config;
16 use App::karr::CrossBoard;
17 use App::karr::Error qw( user_error require_claim_message held_back_claim_message );
18 use Time::Piece;
19  
20 # The set-time half only (ticket #137). A card that does not exist yet cannot be
21 # taken up, so create never has a dependency warning to emit and must not
22 # inherit the emitting half -- the half that would also require --quiet, which
23 # create does not take (ticket #137).
24 with 'App::karr::Role::BoardAccess', 'App::karr::Role::DependencyArgs',
25 'App::karr::Role::Output', 'App::karr::Role::ClaimDefault';
26  
27  
28 option title => (
29 is => 'ro',
30 format => 's',
31 doc => 'Task title',
32 );
33  
34 option status => (
35 is => 'ro',
36 format => 's',
37 doc => 'Initial status',
38 );
39  
40 option priority => (
41 is => 'ro',
42 format => 's',
43 doc => 'Priority level',
44 );
45  
46 option assignee => (
47 is => 'ro',
48 format => 's',
49 doc => 'Person assigned',
50 );
51  
52 option tags => (
53 is => 'ro',
54 format => 's',
55 doc => 'Comma-separated tags',
56 );
57  
58 option due => (
59 is => 'ro',
60 format => 's',
61 doc => 'Due date (YYYY-MM-DD)',
62 );
63  
64 option estimate => (
65 is => 'ro',
66 format => 's',
67 doc => 'Time estimate',
68 );
69  
70 option depends_on => (
71 is => 'ro',
72 format => 's',
73 doc => 'Comma-separated ids of tasks this one depends on',
74 );
75  
76 option needs => (
77 is => 'ro',
78 format => 's',
79 doc => 'Comma-separated BOARD#ID this task waits on in another repository',
80 );
81  
82 option escalated_from => (
83 is => 'ro',
84 format => 's',
85 doc => 'Comma-separated BOARD#ID this task was escalated from',
86 );
87  
88 option class => (
89 is => 'ro',
90 format => 's',
91 doc => 'Class of service',
92 );
93  
94 option body => (
95 is => 'ro',
96 format => 's',
97 doc => 'Task description',
98 );
99  
100 option claim => (
101 is => 'ro',
102 format => 's',
103 doc => 'Claim task for an agent',
104 );
105  
106 sub execute {
107 my ($self, $args_ref, $chain_ref) = @_;
108  
109 $self->check_positional_args($args_ref, 1);
110  
111 $self->sync_before;
112 $self->require_board;
113  
114 my @pos = $self->positional_args($args_ref);
115  
116 # A title given twice is a contradiction, not a preference: the two spellings
117 # can hold different strings, and `create TITLE --title OTHER` used to take
118 # --title and drop the positional without a word (ticket #235). kanban-md
119 # refuses that invocation (resolveCreateTitle, cmd/create.go:113-121,
120 # InvalidInput), and karr answers a self-contradicting invocation that way one
121 # command over already (the --claim/--release guard in App::karr::Cmd::Edit).
122 # Before the required-title guard below, so a caller who named two titles is
123 # told that, not that one is missing.
124 #
125 # length, not truth, on both halves: `--title 0` and a bare `0` are titles
126 # that were given, and an empty one is no title at all -- the same rule the
127 # guard below reads (ticket #230).
128 $self->usage_error('title provided both as an argument and with --title; use one or the other')
129 if ( defined $self->title && length $self->title )
130 && ( defined $pos[0] && length $pos[0] );
131  
132 # length, not truth: "0" is one character long and a perfectly good title,
133 # but the assignment below yields it as a false value, so both `--title 0`
134 # and a bare positional `0` were rejected as "Title is required" -- the one
135 # guard in this file ticket #153 left on truth while converting the options
136 # below. kanban-md's resolveCreateTitle tests the flag against "" and the
137 # args against length, and takes "0" (ticket #230).
138 my $title = $self->title // $pos[0];
139 die "Title is required. Use --title or pass as argument.\n"
140 unless defined $title && length $title;
141  
142 my $ec = $self->store->effective_config;
143 my $defaults = $ec->{defaults} // {};
144 my $config = App::karr::Config->from_merged($ec);
145  
146 # Validate before allocating an id, so a rejected create does not burn one
147 # (ticket #54).
148 $config->validate_status( $self->status ) if defined $self->status;
149 $config->validate_priority( $self->priority ) if defined $self->priority;
150 $config->validate_class( $self->class ) if defined $self->class;
151 App::karr::Config->validate_due( $self->due ) if defined $self->due;
152  
153 # A card that lands in a require_claim column with no owner is the state
154 # `karr move` refuses to create (Role::TaskMutation/apply_status_change), and
155 # create must not be the door that walks around it (ticket #270). The default
156 # status is deliberately not consulted: a board whose default column needs a
157 # claim is a board that says so, and create without --status keeps its
158 # historical behaviour. The suggestion is the caller's own command line with
159 # --claim added, the k263 shape.
160 #
161 # The same test is what lets KARR_CLAIM onto the card at all (ticket #286):
162 # a column that demands a claim is one the card is being started in, and
163 # that is the only create where the remembered claim means what a Claim is.
164 # Held in a local so the guard here and the stamp below cannot disagree --
165 # the env claim satisfies the guard exactly when it is then written. The
166 # rule itself is App::karr::Role::ClaimDefault/resolved_claim_for, shared
167 # with move and edit since tickets #303/#304.
168 my $starts_work = defined $self->status
169 && $self->store->status_requires_claim($self->status);
170 my $claim = $self->resolved_claim_for( $self->status );
171 if ( $starts_work && !( defined $claim && length $claim ) ) {
172 # A local, not "$self->status" inside the string: that would interpolate
173 # the object and leave the literal text "->status" behind it.
174 my $status = $self->status;
175 user_error( require_claim_message(
176 $status, 'create', $title, '--status', $status, '--claim', 'NAME' ) );
177 }
178  
179 # The mirror image (ticket k306): a card filed into backlog -- the default
180 # status included -- is held back and holds no claim, so an explicit --claim
181 # there is refused before an id is allocated. KARR_CLAIM never gets this far:
182 # resolved_claim_for leaves it off a column that needs no claim.
183 my $status = $self->status // $defaults->{status} // 'backlog';
184 user_error( held_back_claim_message( $status,
185 'create', $title, '--status', $config->promotion_status // 'STATUS', '--claim', $claim ) )
186 if defined $claim && length $claim && $config->is_held_back_status($status);
187  
188 # Set-time dependency validation (ticket #124), under the same #54 rule.
189 # A self-reference is not expressible here: the new id does not exist until
190 # it is allocated below, and every dependency must already exist, so no
191 # dependency can equal it. length, not truth (ticket #78).
192 my $depends_on;
193 if ( defined $self->depends_on && length $self->depends_on ) {
194 $depends_on = $self->parse_dependency_ids( '--depends-on', $self->depends_on );
195 $self->assert_dependencies_exist($depends_on);
196 }
197  
198 # The cross-board half (ticket #192). Only the syntax is checked here, and
199 # that is the whole story rather than a shortcut: the far board may not be on
200 # this machine at all, and whether it is is local configuration, so a create
201 # that refused a link it could not look up would refuse it on one machine and
202 # accept it on the next for the same card. Whether the far card exists and
203 # what state it is in is `karr needs`'s question.
204 my @cross;
205 push @cross, map { App::karr::CrossBoard->needs_tag($_) }
206 @{ App::karr::CrossBoard->parse_refs( '--needs', $self->needs ) }
207 if defined $self->needs && length $self->needs;
208 push @cross, map { App::karr::CrossBoard->escalated_from_tag($_) }
209 @{ App::karr::CrossBoard->parse_refs( '--escalated-from', $self->escalated_from ) }
210 if defined $self->escalated_from && length $self->escalated_from;
211  
212 my %task_args = (
213 id => $self->allocate_next_id,
214 title => $title,
215 status => $status,
216 priority => $self->priority // $defaults->{priority} // 'medium',
217 class => $self->class // $defaults->{class} // 'standard',
218 );
219  
220 # length, not truth: a literal "0" is a meaningful assignee, tag, due or
221 # estimate (ticket #153, extending ticket #78's rule from --body to its
222 # siblings). --depends_on was already on the #78 pattern (see above).
223 $task_args{assignee} = $self->assignee if defined $self->assignee && length $self->assignee;
224 $task_args{tags} = [split /,/, $self->tags] if defined $self->tags && length $self->tags;
225 # Cross-board links live in the tag list (App::karr::CrossBoard explains why),
226 # so they are appended to whatever --tags brought rather than replacing it.
227 push @{ $task_args{tags} //= [] }, @cross if @cross;
228 $task_args{depends_on} = $depends_on if $depends_on;
229 $task_args{due} = $self->due if defined $self->due && length $self->due;
230 $task_args{estimate} = $self->estimate if defined $self->estimate && length $self->estimate;
231 # length, not truth: --body 0 is a body (ticket #78).
232 $task_args{body} = $self->body if defined $self->body && length $self->body;
233  
234 # Claim stamping, the same two fields and the same UTC instant `karr move
235 # --claim` writes (ticket #270). There is no shared helper to call: move,
236 # handoff, pick and edit all inline these two lines, and a single-use
237 # abstraction would be worse than the copy.
238 #
239 # Which claim: an explicit --claim on any status; KARR_CLAIM only when the
240 # card is being started in a require_claim column, the case the guard above
241 # just let through on its strength (ticket #286). A Claim is an active lease
242 # held while working a card (CONTEXT.md), and a card filed into the backlog
243 # for whoever picks it next is not being worked -- so the filer's remembered
244 # claim, which the skill has every agent export first thing, stays off it.
245 # Before #286 resolved_claim was taken unconditionally, and a bug filed by
246 # one agent was invisible to every other agent's `pick` and `list
247 # --unclaimed` until claim_timeout ran out. $claim is resolved above the
248 # guard, so the two read the same value.
249 if ( defined $claim && length $claim ) {
250 $task_args{claimed_by} = $claim;
251 $task_args{claimed_at} = gmtime->datetime . 'Z';
252 }
253  
254 my $task = App::karr::Task->new(%task_args);
255 $self->save_task($task);
256  
257 $self->sync_after;
258  
259 # --json emits the card in the same shape `karr show --json` uses and nothing
260 # else on stdout, so a caller can pipe the new id into the next step (ticket
261 # #268). Sync progress already goes to STDERR, so the JSON stream stays clean.
262 if ($self->json) {
263 $self->print_json( $task->to_json_hash );
264 } else {
265 printf "Created task %d: %s\n", $task->id, $task->title;
266 }
267 }
268  
269 1;
270  
271 __END__
« blib/lib/App/karr/Cmd/Config.pm ↑ Summary blib/lib/App/karr/Cmd/Edit.pm »