SD/CLI/Command/Ticket/Show.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 159 CC 8 SCAR 19.0
CC
8
Cov
0%
by_creation_date
12.0
show_history_entry
12.0
usage_msg
6.0
CRAP 6.7
1 package App::SD::CLI::Command::Ticket::Show;
2 use Any::Moose;
3 extends 'Prophet::CLI::Command::Show';
4 with 'App::SD::CLI::Command';
5 with 'App::SD::CLI::Model::Ticket';
6  
7 sub ARG_TRANSLATIONS {
8 shift->SUPER::ARG_TRANSLATIONS(),
9 a => 'all-props',
10 s => 'skip-history',
11 h => 'with-history',
12 b => 'batch';
13 }
14  
15 sub by_creation_date {
16 ($a->can('created') ? $a->created : $a->prop('created') )
17 cmp
18 ($b->can('created') ? $b->created : $b->prop('created') )
19 }
20  
21 sub usage_msg {
22 my $self = shift;
23 my $cmd = shift || 'show';
24 my $script = $self->cli->get_script_name;
25 my $type = $self->type ? $self->type . q{ } : q{};
26  
27 return <<"END_USAGE";
28 usage: ${script}${type}${cmd} <record-id> [options]
29  
30 Options are:
31 -a|--all-props Show props even if they aren't common
32 -s|--skip-history Don't show ticket history
33 -h|--with-history Show ticket history even if disabled in config
34 -b|--batch
35 END_USAGE
36 }
37  
38 override run => sub {
39 my $self = shift;
40  
41 $self->print_usage if $self->has_arg('h');
42  
43 $self->require_uuid;
44 my $record = $self->_load_record;
45  
46 # prophet uses --verbose to decide whether to show all declared props
47 # or not (rather than just the ones returned by props_to_show),
48 # but --all-props is more consistent with sd's behaviour in update/create
49 if ( $self->has_arg('all-props') ) {
50 $self->set_arg( 'verbose' => 1 );
51 }
52  
53 print "\n= METADATA\n\n";
54 super();
55 my @history = sort by_creation_date ( @{ $record->comments }, $record->changesets );
56  
57 my @attachments = sort by_creation_date @{ $record->attachments };
58 if (@attachments) {
59 print "\n= ATTACHMENTS\n\n";
60 $self->show_attachment($_) for @attachments;
61 }
62  
63 # allow user to not display history by specifying the --skip-history
64 # arg or setting ticket.no-implicit-history-display config item to a
65 # true value (can be overridden with --with-history)
66 if (!$self->has_arg('skip-history')
67 && ( !$self->app_handle->config->get(
68 key => 'ticket.no-implicit-history-display',
69 as => 'bool',
70 ) || $self->has_arg('with-history') )
71 )
72 {
73 print "\n= HISTORY\n\n";
74 foreach my $item (@history) {
75 if ( $item->isa('Prophet::ChangeSet') ) {
76 $self->show_history_entry( $record, $item );
77 } elsif ( $item->isa('App::SD::Model::Comment') ) {
78 $self->show_comment($item);
79 }
80 }
81 }
82 };
83  
84  
85 sub format_prop {
86 my $self = shift;
87 my $field = shift;
88 my $value = shift;
89 if ($self->has_arg('batch')) {
90 return "$field: $value\n";
91 } else {
92 return sprintf("%18.18s: %s\n",$field, $value);
93 }
94 }
95  
96 sub show_history_entry {
97 my $self = shift;
98 my $ticket = shift;
99 my $changeset = shift;
100 my $body = '';
101
102 for my $change ( $changeset->changes ) {
103 next if $change->record_uuid ne $ticket->uuid;
104  
105 $body .= App::SD::CLI->format_change(change => $change) || next;
106 $body .= "\n";
107 }
108  
109 return '' if !$body;
110  
111 $self->history_entry_header(
112 $changeset->creator,
113 $changeset->created,
114 $changeset->original_sequence_no,
115 $self->app_handle->display_name_for_replica($changeset->original_source_uuid),
116
117 );
118  
119 print $body;
120 }
121  
122 sub show_attachment {
123 my $self = shift;
124 my $attachment = shift;
125 print $attachment->format_summary . "\n";
126 }
127  
128 sub show_comment {
129 my $self = shift;
130 my $comment = shift;
131 my $creator = $comment->prop('creator');
132 my $created = $comment->prop('created');
133 my $content_type = $comment->prop('content_type') || 'text/plain';
134 my $content = $comment->prop('content') || '';
135  
136  
137 my ($creation) = $comment->changesets(limit => 1);
138  
139 $self->history_entry_header($creator,
140 $created,$creation->original_sequence_no, $self->app_handle->display_name_for_replica($creation->original_source_uuid));
141  
142 print $self->format_comment($content_type, $content);
143 print "\n\n";
144 }
145  
146 sub format_comment {
147 my $self = shift;
148 my $content_type = shift;
149 my $content = shift;
150  
151 if ( $content_type =~ m{text/html}i ) {
152  
153 $content =~ s|<p.*?>|\n|gismx;
154 $content =~ s|</?pre.*?>|\n|gismx;
155 $content =~ s|</?b\s*>|*|gismx;
156 $content =~ s|</?i\s*>|_|gismx;
157 $content =~ s|<a(?:.*?)href="(.*?)".*?>(.*?)</a.*?>|$2 [link: $1 ]|gismx;
158 $content =~ s|<.*?>||gismx;
159 $content =~ s|\n\n|\n|gismx;
160 }
161  
162 return $content;
163 }
164  
165  
166 sub history_entry_header {
167 my $self = shift;
168 my ($creator, $created, $sequence, $source) = (@_);
169 print "="x80;
170 print "\n";
171 printf "%s at %s\t\(%d@%s)\n",
172 ( $creator || '(unknown)' ),
173 $created,
174 $sequence,
175 $source;
176 print "-"x80;
177 print "\n";
178 }
179  
180 __PACKAGE__->meta->make_immutable;
181 no Any::Moose;
182  
183 1;