line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::ReadPath; |
2
|
|
|
|
|
|
|
$JSON::ReadPath::VERSION = '2'; |
3
|
2
|
|
|
2
|
|
46490
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
78
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
66
|
|
5
|
2
|
|
|
2
|
|
2353
|
use JSON::XS qw( encode_json decode_json ); |
|
2
|
|
|
|
|
24425
|
|
|
2
|
|
|
|
|
183
|
|
6
|
2
|
|
|
2
|
|
1724
|
use Mouse; |
|
2
|
|
|
|
|
75149
|
|
|
2
|
|
|
|
|
13
|
|
7
|
2
|
|
|
2
|
|
3021
|
use Template; |
|
2
|
|
|
|
|
113482
|
|
|
2
|
|
|
|
|
1066
|
|
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
|
|
|
5
|
my $json_str = $self->from_file || $self->string |
43
|
|
|
|
|
|
|
or die "No data"; |
44
|
2
|
|
|
|
|
70
|
return decode_json($json_str); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub from_file { |
48
|
2
|
|
|
2
|
0
|
3
|
my $self = shift; |
49
|
2
|
50
|
|
|
|
26
|
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
|
865
|
my $self = shift; |
62
|
2
|
|
|
|
|
15
|
my $config = $self->config; |
63
|
1
|
50
|
|
|
|
6
|
my $path = shift |
64
|
|
|
|
|
|
|
or return $config; |
65
|
1
|
|
|
|
|
18
|
my $tt = Template->new; |
66
|
1
|
|
|
|
|
46980
|
my $value = q{}; |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
6
|
$config->{json} = \&encode_json; |
69
|
1
|
|
|
|
|
5
|
$config->{decode_json} = \&decode_json; |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
9
|
$tt->process( \"[%$path%]", $config, \$value ); |
72
|
1
|
|
|
|
|
38357
|
return $value; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
1; |