line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::ReadPath; |
2
|
|
|
|
|
|
|
$JSON::ReadPath::VERSION = '3.01'; |
3
|
2
|
|
|
2
|
|
26449
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
87
|
|
4
|
2
|
|
|
2
|
|
15
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
94
|
|
5
|
2
|
|
|
2
|
|
3142
|
use JSON::XS qw( encode_json decode_json ); |
|
2
|
|
|
|
|
17523
|
|
|
2
|
|
|
|
|
244
|
|
6
|
2
|
|
|
2
|
|
3254
|
use Mouse; |
|
2
|
|
|
|
|
67499
|
|
|
2
|
|
|
|
|
12
|
|
7
|
2
|
|
|
2
|
|
4845
|
use Template; |
|
2
|
|
|
|
|
48986
|
|
|
2
|
|
|
|
|
1152
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
JSON::ReadPath - In Jenkins grep payload json data and assign to an environment |
12
|
|
|
|
|
|
|
variable. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 USAGE |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Let's say Bitbucket pushed a payload '{"commits":{ "branch": "FooBar" }}' |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
And we want to grab the branch name from the payload. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
BRANCH=$(read_json.pl --env payload --path commits.branch) |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has file => ( |
25
|
|
|
|
|
|
|
is => "ro", |
26
|
|
|
|
|
|
|
isa => "Str", |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has string => ( |
30
|
|
|
|
|
|
|
is => "ro", |
31
|
|
|
|
|
|
|
isa => "Str", |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
has config => ( |
35
|
|
|
|
|
|
|
is => "ro", |
36
|
|
|
|
|
|
|
isa => "HashRef", |
37
|
|
|
|
|
|
|
lazy_build => 1, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _build_config { |
41
|
2
|
|
|
2
|
|
5
|
my $self = shift; |
42
|
2
|
50
|
33
|
|
|
6
|
my $json_str = $self->from_file || $self->string |
43
|
|
|
|
|
|
|
or die "No data"; |
44
|
2
|
|
|
|
|
79
|
return decode_json($json_str); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub from_file { |
48
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
49
|
2
|
50
|
|
|
|
27
|
my $file = $self->file |
50
|
|
|
|
|
|
|
or return; |
51
|
0
|
0
|
|
|
|
0
|
return if !-f $file; |
52
|
0
|
0
|
|
|
|
0
|
open my $FH, "<", $file |
53
|
|
|
|
|
|
|
or return; |
54
|
0
|
|
|
|
|
0
|
local $/; |
55
|
0
|
|
|
|
|
0
|
my $string = <$FH>; |
56
|
0
|
|
|
|
|
0
|
close $FH; |
57
|
0
|
|
|
|
|
0
|
return $string; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub get { |
61
|
2
|
|
|
2
|
0
|
766
|
my $self = shift; |
62
|
2
|
|
|
|
|
13
|
my $config = $self->config; |
63
|
1
|
50
|
|
|
|
4
|
my $path = shift |
64
|
|
|
|
|
|
|
or return $config; |
65
|
1
|
|
|
|
|
19
|
my $tt = Template->new; |
66
|
1
|
|
|
|
|
30710
|
my $value = q{}; |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
5
|
$config->{json} = \&encode_json; |
69
|
1
|
|
|
|
|
4
|
$config->{decode_json} = \&decode_json; |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
9
|
$tt->process( \"[%$path%]", $config, \$value ); |
72
|
1
|
|
|
|
|
41499
|
return $value; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub flat_list { |
76
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
77
|
0
|
|
|
|
|
|
my $data = shift; |
78
|
0
|
|
0
|
|
|
|
my $base_key = shift || qq{}; |
79
|
0
|
|
|
|
|
|
my @lines = (); |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
if ( ref $data ) { |
82
|
0
|
0
|
|
|
|
|
$base_key .= "_" |
83
|
|
|
|
|
|
|
if $base_key; |
84
|
0
|
0
|
|
|
|
|
if ( UNIVERSAL::isa( $data, "HASH" ) ) { |
|
|
0
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
push @lines, $base_key . "__KEYS", join ",", map { uc( $base_key . $_ ) } keys %$data |
86
|
0
|
0
|
|
|
|
|
if !$data->{$base_key . "__KEYS"}; |
87
|
0
|
|
|
|
|
|
foreach my $key ( keys %$data ) { |
88
|
0
|
|
|
|
|
|
my $val = $data->{$key}; |
89
|
0
|
|
|
|
|
|
push @lines, $self->flat_list( $val, uc( $base_key . $key ) ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
elsif ( UNIVERSAL::isa( $data, "ARRAY" ) ) { |
93
|
0
|
|
|
|
|
|
push @lines, $base_key . "__SIZE", $#$data; |
94
|
0
|
|
|
|
|
|
foreach my $i ( 0 .. $#$data ) { |
95
|
0
|
|
|
|
|
|
push @lines, $self->flat_list( $data->[$i], uc( $base_key . $i ) ); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
else { |
100
|
0
|
|
|
|
|
|
push @lines, uc($base_key), $data; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
return @lines; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |