lib/App/TimeTracker/Command/Jira.pm

filter: Statementstmt 22.6% 41 / 181 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 33.3% 11 / 33 Podpod 0.0% 0 / 1 totaltotal 15.8% 52 / 329 CC 52 SCAR 43.9
CC
52
Cov
15%
__ANON__
240.0
__ANON__
110.0
__ANON__
90.0
CRAP 80.3
↑ Summary
1 package App::TimeTracker::Command::Jira;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: App::TimeTracker Jira plugin
7 1 ▶use App::TimeTracker::Utils qw(error_message warning_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '1.1';
10  
11 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JIRA::REST ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON::XS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Unicode::Normalize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 has 'jira_client' => (
19 is => 'ro',
20 isa => 'Maybe[JIRA::REST]',
21 lazy_build => 1,
22 traits => ['NoGetopt'],
23 predicate => 'has_jira_client'
24 );
25 1 has 'jira_ticket' => (
26 is => 'ro',
27 isa => 'Maybe[HashRef]',
28 lazy_build => 1,
29 traits => ['NoGetopt'],
30 );
31 1 has 'jira_ticket_transitions' => (
32 is => 'rw',
33 isa => 'Maybe[ArrayRef]',
34 traits => ['NoGetopt'],
35 );
36  
37 0 ▶sub _build_jira_ticket {
Subroutine _build_jira_ticket: not called CC=2 SCAR=17.9
38 0 my ($self) = @_;
39  
40 0 ▶ if ( my $ticket = $self->_init_jira_ticket( $self->_current_task ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ticket = $self->_init_jira_ticket($self->_current_task)) 0 0 0
41 0 return $ticket;
42 }
43 }
44  
45 0 ▶sub _build_jira_client {
Subroutine _build_jira_client: not called CC=2 SCAR=17.9
46 0 my $self = shift;
47 0 my $config = $self->config->{jira};
48  
49 0 ▶ unless ($config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 0 0 0
50 0 error_message('Please configure Jira in your TimeTracker config');
51 0 return;
52 }
53  
54 0 my $jira_client;
55 0 ▶ try {
Subroutine __ANON__: not called CC=4 SCAR=30.0
56 0 my %auth_cfg = ( url => $config->{server_url}, );
57 0 ▶ if ( my $token = $config->{token} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $token = $config->{'token'}) { } 0 0 0
elsif (defined $config->{'username'}) { } 0 0 0
elsif ($config->{'anonymous'}) { } 0 0 0
58 0 $auth_cfg{pat} = $token;
59 }
60 elsif ( defined $config->{username} ) {
61 0 $auth_cfg{username} = $config->{username};
62 0 $auth_cfg{password} = $config->{password};
63 }
64 elsif ( $config->{anonymous} ) {
65 0 $auth_cfg{anonymous} = 1;
66 }
67 0 $jira_client = JIRA::REST->new( \%auth_cfg );
68 }
69 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 0 error_message("Could not build JIRA client.\nEither configure token, anonymous, username/password in your tracker config, .netrc or via Config::Identity, see perldoc JIRA::REST.\nError was:\n'%s'", $_ );
71 0 return;
72 0 };
73  
74 0 return $jira_client;
75 }
76  
77 0 ▶after ['_load_attribs_start','_load_attribs_continue','_load_attribs_append'] => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 my ($class,$meta) = @_;
79  
80 0 $meta->add_attribute(
81 'jira' => {
82 is => 'rw',
83 isa => 'Str',
84 documentation => 'JIRA ticket ID',
85 predicate => 'has_jira'
86 }
87 );
88 0 return;
89 1 };
90  
91 0 ▶before [ 'cmd_start', 'cmd_continue', 'cmd_append' ] => sub {
Subroutine __ANON__: not called CC=15 SCAR=54.8
92 0 my $self = shift;
93 0 ▶ return unless $self->has_jira;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_jira 0 0 0
94  
95 0 $self->insert_tag('JIRA:' . $self->jira);
96  
97 0 my $ticket;
98 0 ▶ if ( $self->jira_client ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->jira_client) 0 0 0
99 0 $ticket = $self->jira_ticket;
100 0 ▶ return unless defined $ticket;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ticket 0 0 0
101 0 ▶ if ( defined $self->description ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->description) { } 0 0 0
102 $self->description(
103 sprintf(
104 '%s (%s)', $self->description, $ticket->{fields}->{summary}
105 0 ) );
106 }
107 else {
108 0 ▶ $self->description( $ticket->{fields}->{summary} // '' );
Condition0%0/2
$ticket->{"fields"}{"summary"} // ""
l!l
00
MC/DC0%0/1
$ticket->{"fields"}{"summary"} // ""
$ticket->{"fields"}{"summary"}
Truth table0%0/2
A
A= $ticket->{"fields"}{"summary"}
Aresult
11
00
109 }
110 }
111  
112 0 ▶ if ( $self->meta->does_role('App::TimeTracker::Command::Git') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->meta->does_role("App::TimeTracker::Command::Git")) 0 0 0
113 0 my $branch = $self->jira;
114 0 ▶ if ($ticket) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ticket) 0 0 0
115 0 ▶ my $subject = $self->_safe_ticket_subject( $ticket->{fields}->{summary} // '' );
Condition0%0/2
$ticket->{"fields"}{"summary"} // ""
l!l
00
MC/DC0%0/1
$ticket->{"fields"}{"summary"} // ""
$ticket->{"fields"}{"summary"}
Truth table0%0/2
A
A= $ticket->{"fields"}{"summary"}
Aresult
11
00
116 0 $branch .= '_' . $subject;
117 }
118  
119 # Get existing branches matching the ticket number
120 0 my @branches = map { s/^\*?\s+//r }
121 $self->repository->run('branch','--list',$self->jira.'*');
122 0 ▶ if (scalar @branches == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @branches == 0) { } 0 0 0
elsif (scalar @branches == 1) { } 0 0 0
123 0 ▶ say 'Creating new branch "'.$branch.'".'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->branch or $self->no_branch 0 0 0
Condition0%0/3
$self->branch or $self->no_branch
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->branch or $self->no_branch
$self->branch $self->no_branch
Truth table0%0/3
A or B
A= $self->branchB= $self->no_branch
ABresult
1X1
011
000
124 unless $self->branch || $self->no_branch;
125 } elsif (scalar @branches == 1) {
126 0 $branch = $branches[0];
127 } else {
128 0 say 'More than one branch for '.$self->jira.'? I don\'t know what to do!';
129 0 foreach (@branches) {
130 0 say " * $_";
131 }
132 0 return;
133 }
134  
135 0 ▶ $self->branch($branch)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->branch 0 0 0
136 unless $self->branch;
137 }
138 1 };
139  
140 0 ▶after [ 'cmd_start', 'cmd_continue', 'cmd_append' ] => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
141 0 my $self = shift;
142 0 ▶ return unless $self->has_jira && $self->jira_client;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_jira and $self->jira_client 0 0 0
Condition0%0/3
$self->has_jira and $self->jira_client
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_jira and $self->jira_client
$self->has_jira $self->jira_client
Truth table0%0/3
A and B
A= $self->has_jiraB= $self->jira_client
ABresult
0X0
100
111
143  
144 0 my $ticket = $self->jira_ticket;
145 0 ▶ return unless defined $ticket;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ticket 0 0 0
146  
147 0 ▶ if ( $self->config->{jira}->{set_status}{start}->{transition}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"jira"}{"set_status"}{"start"}{"transition"} and $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}) 0 0 0
Condition0%0/3
$self->config->{"jira"}{"set_status"}{"start"}{"transition"} and $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->config->{"jira"}{"set_status"}{"start"}{"transition"} and $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}
$self->config->{"jira"}{"set_status"}{"start"}{"transition"} $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}
Truth table0%0/3
A and B
A= $self->config->{"jira"}{"set_status"}{"start"}{"transition"}B= $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}
ABresult
0X0
100
111
148 and $self->config->{jira}->{set_status}{start}->{target_state} ) {
149 0 my $ticket_update_data;
150  
151 0 my $status = $self->config->{jira}->{set_status}{start}->{target_state};
152 0 ▶ if ( $status and $status ne $ticket->{fields}->{status}->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status and $status ne $ticket->{"fields"}{"status"}{"name"}) 0 0 0
Condition0%0/3
$status and $status ne $ticket->{"fields"}{"status"}{"name"}
!ll&&!rl&&r
000
MC/DC0%0/2
$status and $status ne $ticket->{"fields"}{"status"}{"name"}
$status $status ne $ticket->{"fields"}{"status"}{"name"}
Truth table0%0/3
A and B
A= $statusB= $status ne $ticket->{"fields"}{"status"}{"name"}
ABresult
0X0
100
111
153 0 ▶ if ( my $transition_id = $self->_check_resolve_ticket_transition(
Branch: 1 branch
BranchTrueFalseTotal
if (my $transition_id = $self->_check_resolve_ticket_transition($self->config->{"jira"}{"set_status"}{"start"}{"transition"})) 0 0 0
154 $self->config->{jira}->{set_status}{start}->{transition}
155 ) ) {
156 0 $ticket_update_data->{transition}->{id} = $transition_id;
157 }
158 }
159  
160 0 ▶ if ( defined $ticket_update_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ticket_update_data) 0 0 0
161 0 my $result;
162 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 $result = $self->jira_client->POST(
164 sprintf('/issue/%s/transitions', $self->jira),
165 undef,
166 $ticket_update_data,
167 );
168 }
169 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
170 0 error_message( 'Could not set JIRA ticket ticket status: "%s"', $_ );
171 0 };
172 }
173 }
174 1 };
175  
176 0 ▶after 'cmd_stop' => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
177 0 my $self = shift;
178 0 ▶ return unless $self->jira_client;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->jira_client 0 0 0
179  
180 0 my $task = $self->_previous_task;
181 0 ▶ return unless $task;
Branch: 1 branch
BranchTrueFalseTotal
unless $task 0 0 0
182 0 my $task_rounded_minutes = $task->rounded_minutes;
183 0 ▶ return unless $task_rounded_minutes > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $task_rounded_minutes > 0 0 0 0
184  
185 0 my $ticket = $self->_init_jira_ticket($task);
186 0 ▶ if ( not defined $ticket ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ticket) 0 0 0
187 0 say
188 'Last task did not contain a JIRA ticket id, not updating TimeWorked or Status.';
189 0 return;
190 }
191  
192 0 my $do_store = 0;
193 0 ▶ if ( $self->config->{jira}->{log_time_spent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"jira"}{"log_time_spent"}) 0 0 0
194 0 my $result;
195 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
196 0 $result = $self->jira_client->POST(sprintf('/issue/%s/worklog', $task->jira_id), undef, { timeSpent => sprintf('%sm', $task_rounded_minutes) });
197 }
198 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 error_message( 'Could not log JIRA time spent: "%s"', $@ );
200 0 };
201 }
202  
203 0 my $status = $self->config->{jira}->{set_status}{stop}->{transition};
204 # Do not change the configured stop status if it has been changed since starting the ticket
205 0 ▶ if ( defined $status
Branch: 1 branch
BranchTrueFalseTotal
if (defined $status and $ticket->{"fields"}{"status"}{"name"} eq $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}) 0 0 0
Condition0%0/3
defined $status and $ticket->{"fields"}{"status"}{"name"} eq $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $status and $ticket->{"fields"}{"status"}{"name"} eq $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}
defined $status $ticket->{"fields"}{"status"}{"name"} eq $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}
Truth table0%0/3
A and B
A= defined $statusB= $ticket->{"fields"}{"status"}{"name"} eq $self->config->{"jira"}{"set_status"}{"start"}{"target_state"}
ABresult
0X0
100
111
206 and $ticket->{fields}->{status}->{name} eq
207 $self->config->{jira}->{set_status}{start}->{target_state} )
208 {
209 0 ▶ if ( my $transition_id = $self->_check_resolve_ticket_transition( $status ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $transition_id = $self->_check_resolve_ticket_transition($status)) 0 0 0
210 0 my $ticket_update_data;
211 0 $ticket_update_data->{transition}->{id} = $transition_id;
212  
213 0 my $result;
214 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 $result = $self->jira_client->POST(
216 sprintf('/issue/%s/transitions', $task->jira_id),
217 undef,
218 $ticket_update_data,
219 );
220 }
221 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 error_message( 'Could not set JIRA ticket status: "%s"', $@ );
223 0 };
224 }
225 }
226 1 };
227  
228 0 ▶sub _init_jira_ticket {
Subroutine _init_jira_ticket: not called CC=6 SCAR=37.4
229 0 my ( $self, $task ) = @_;
230 0 my $id;
231 0 ▶ if ($task) {
Branch: 2 branches
BranchTrueFalseTotal
if ($task) { } 0 0 0
elsif ($self->jira) { } 0 0 0
232 0 $id = $task->jira_id;
233 }
234 elsif ( $self->jira ) {
235 0 $id = $self->jira;
236 }
237 0 ▶ return unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
238  
239 0 my $ticket;
240 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
241 0 $ticket = $self->jira_client->GET(sprintf('/issue/%s',$id), { fields => '-comment' });
242 }
243 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
244 0 error_message( 'Could not fetch JIRA ticket %s: "%s"', $id, $_ );
245 0 };
246 return
247 0 ▶ unless defined $ticket;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ticket 0 0 0
248  
249 0 my $transitions;
250 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
251 0 $transitions = $self->jira_client->GET(sprintf('/issue/%s/transitions',$id));
252 }
253 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 0 require Data::Dumper;
255 0 error_message( 'Could not fetch JIRA transitions for %s: %s', $id, Data::Dumper::Dumper $transitions );
256 0 };
257 return
258 0 ▶ unless defined $transitions;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $transitions 0 0 0
259  
260 0 $self->jira_ticket_transitions( $transitions->{transitions} );
261  
262 0 return $ticket;
263 }
264  
265 0 ▶sub _check_resolve_ticket_transition {
Subroutine _check_resolve_ticket_transition: not called CC=9 SCAR=45.0
266 0 my ( $self, $status_name ) = @_;
267 0 my $transition_id;
268  
269 0 foreach my $transition ( @{$self->jira_ticket_transitions} ) {
270 0 ▶ if ( ref $status_name and ref $status_name eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $status_name and ref $status_name eq 'ARRAY') { } 0 0 0
elsif ($transition->{'name'} eq $status_name) { } 0 0 0
Condition0%0/3
ref $status_name and ref $status_name eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $status_name and ref $status_name eq "ARRAY"
ref $status_name ref $status_name eq "ARRAY"
Truth table0%0/3
A and B
A= ref $status_nameB= ref $status_name eq "ARRAY"
ABresult
0X0
100
111
271 0 foreach my $name ( @$status_name ) {
272 0 ▶ if ( $transition->{name} eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($transition->{"name"} eq $name) 0 0 0
273 0 $transition_id = $transition->{id};
274 0 last;
275 }
276 }
277 }
278 elsif ( $transition->{name} eq $status_name ) {
279 0 $transition_id = $transition->{id};
280 0 last;
281 }
282 }
283 0 ▶ if ( not defined $transition_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $transition_id) 0 0 0
284 0 require Data::Dumper;
285 error_message( 'None of the configured ticket transitions (%s) did match the ones valid for this JIRA ticket\'s workflow-state: %s',
286 0 ref $status_name ? join(',', map { '"'.$_.'"' } @$status_name) : $status_name,
287 0 ▶ join(',', map { '"'.$_->{name}.'"' } @{$self->jira_ticket_transitions} ),
Branch: 1 branch
BranchTrueFalseTotal
ref $status_name ? : 0 0 0
288 );
289 0 return;
290 }
291 0 return $transition_id;
292 }
293  
294 0 ▶sub App::TimeTracker::Data::Task::jira_id {
Subroutine jira_id: not called CC=3 SCAR=24.8
Pod undocumented
295 0 my $self = shift;
296 0 foreach my $tag ( @{ $self->tags } ) {
297 0 ▶ next unless $tag =~ /^JIRA:(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /^JIRA:(.+)/ 0 0 0
298 0 return $1;
299 }
300 0 return;
301 }
302  
303 0 ▶sub _safe_ticket_subject {
Subroutine _safe_ticket_subject: not called CC=1 SCAR=6.9
304 0 my ( $self, $subject ) = @_;
305  
306 0 $subject = Unicode::Normalize::NFKD($subject);
307 0 $subject =~ s/\p{NonspacingMark}//g;
308 0 $subject =~ s/\W/_/g;
309 0 $subject =~ s/_+/_/g;
310 0 $subject =~ s/^_//;
311 0 $subject =~ s/_$//;
312 0 return $subject;
313 }
314  
315 1 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
316 1 1;
317  
318 __END__
↑ Summary