File Coverage

blib/lib/App/bif/show/change.pm
Criterion Covered Total %
statement 21 60 35.0
branch 0 10 0.0
condition 0 6 0.0
subroutine 7 7 100.0
pod 1 1 100.0
total 29 84 34.5


line stmt bran cond sub pod time code
1             package App::bif::show::change;
2 1     1   5155 use strict;
  1         2  
  1         26  
3 1     1   4 use warnings;
  1         2  
  1         34  
4 1     1   439 use App::bif::log;
  1         4  
  1         34  
5 1     1   5 use Bif::Mo;
  1         2  
  1         8  
6 1     1   833 use DBIx::ThinSQL qw/sq qv/;
  1         26567  
  1         9  
7 1     1   101 use Encode;
  1         3  
  1         810  
8              
9             our $VERSION = '0.1.5_7';
10             extends 'App::bif::show';
11              
12             sub run {
13 1     1 1 3 my $self = shift;
14 1         4 my $opts = $self->opts;
15 1         8 my $dbw = $self->dbw;
16 0           my $info = $self->get_change( $opts->{uid} );
17 0           my $now = $self->now;
18 0           my $devel = eval { require YAML::Tiny };
  0            
19              
20             my $ref = $dbw->xhashref(
21             select => [
22             'c.id',
23             'c.mtime AS mtime',
24             'c.mtimetz AS mtimetz',
25             'c.mtimetzhm AS mtimetzhm',
26             "$now - c.mtime AS mtime_age",
27             'c.uuid AS uuid',
28             'c.action',
29             'e.name',
30             'COALESCE(c.author,e.name) AS author',
31             'COALESCE(c.author_contact,ecm.mvalue) AS contact',
32             'c.message',
33             ],
34             from => 'changes c',
35             inner_join => 'identities id',
36             on => 'id.id = c.identity_id',
37             inner_join => 'entities e',
38             on => 'e.id = c.identity_id',
39             inner_join => 'entity_contact_methods ecm',
40             on => 'ecm.id = e.default_contact_method_id',
41             left_join => 'change_deltas cd',
42             on => 'cd.change_id = c.id',
43             where => { 'c.id' => $info->{id} },
44 0           );
45              
46             # YAML::Tiny only installed by cpanm --with-develop
47 0           my $yaml = '';
48 0           my $ref_uuid = $ref->{uuid};
49              
50 0 0         if ($devel) {
51 0           require Bif::DB::Plugin::ChangeUUIDv1;
52 0           require Digest::SHA;
53              
54 0           my $changeset = $dbw->uchangeset_v1( $info->{id} );
55              
56 0           my $begin = shift @$changeset;
57 0           my $end = pop @$changeset;
58 0           my $uuid = delete $end->{uuid};
59              
60 0           foreach my $x (@$changeset) {
61             next
62             unless ref $x eq 'HASH'
63             && exists $x->{_delta}
64 0 0 0       && $x->{_delta} =~ m/^new_/;
      0        
65 0           delete $x->{uuid};
66             }
67              
68 0           $yaml = YAML::Tiny::Dump( [ $begin, @$changeset, $end ] );
69              
70 0           my ( $green, $red, $reset ) = $self->colours(qw/green red reset/);
71              
72             $ref_uuid = $reset . $red . 'INVALID' . $reset
73             unless Digest::SHA::sha1_hex( Encode::encode( 'UTF-8', $yaml ) ) eq
74 0 0         $ref->{uuid};
75              
76 0 0         if ( $opts->{diff} ) {
77 0           require Text::Diff;
78             my $terms = $dbw->xval(
79             select => 'c.yaml',
80             from => 'changes c',
81             where => { 'c.id' => $info->{id} },
82 0           );
83              
84 0           my $diff =
85             Text::Diff::diff( \$terms, \$yaml, { CONTEXT => length $yaml } );
86              
87 0 0         if ($diff) {
88 0           $diff =~ s/^\-(.*)/$red-$1$reset/mg;
89 0           $diff =~ s/^\+(.*)/$green+$1$reset/mg;
90 0           $diff =~ s/.*//;
91 0           $yaml = $diff;
92             }
93             else {
94 0           $yaml =~ s/^/ /mg;
95 0           $yaml = "\n" . $yaml;
96             }
97             }
98             else {
99 0           $yaml =~ s/^/ /mg;
100 0           $yaml = "\n" . $yaml;
101             }
102             }
103             else {
104 0           $yaml = "Details only shown when developer libraries installed\n";
105             }
106              
107 0           my @data;
108             push( @data,
109             $self->header( ' From', $ref->{author}, $ref->{contact} ),
110             $self->header( ' When', $self->mtime_ago($ref) ),
111 0           $self->header( ' Action', $ref->{action} ),
112             );
113              
114 0           $self->start_pager;
115              
116             print $self->render_table( 'l l',
117 0           $self->header( 'change', $ref->{id}, $ref_uuid ),
118             \@data, 1 ),
119             $yaml;
120              
121 0           return $self->ok('ShowChange');
122             }
123              
124             1;
125             __END__