| 1 | package Gerrit::REST; | ||||||||||||||||||||
| 2 | { | ||||||||||||||||||||
| 3 | 1 | $Gerrit::REST::VERSION = '0.011'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | # ABSTRACT: Thin wrapper around Gerrit's REST API | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use 5.010; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use Data::Util qw/:check/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use REST::Client; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||
| 19 | 0 | my ($class, $URL, $username, $password, $rest_client_config) = @_; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | $URL = URI->new($URL) if is_string($URL); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | 0 | ▶ | is_instance($URL, 'URI') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | or croak __PACKAGE__ . "::new: URL argument must be a string or a URI object.\n"; | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | # If no password is set we try to lookup the credentials in the .netrc file | ||||||||||||||||||||
| 26 | 0 | ▶ | if (! defined $password) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 0 | ▶ | eval {require Net::Netrc} | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | or croak "Can't require Net::Netrc module. Please, specify the USERNAME and PASSWORD.\n"; | ||||||||||||||||||||
| 29 | 0 | ▶ | if (my $machine = Net::Netrc->lookup($URL->host, $username)) { # $username may be undef | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 0 | $username = $machine->login; | |||||||||||||||||||
| 31 | 0 | $password = $machine->password; | |||||||||||||||||||
| 32 | } else { | ||||||||||||||||||||
| 33 | 0 | croak "No credentials found in the .netrc file.\n"; | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | ▶ | is_string($username) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | or croak __PACKAGE__ . "::new: USERNAME argument must be a string.\n"; | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | is_string($password) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | or croak __PACKAGE__ . "::new: PASSWORD argument must be a string.\n"; | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | ▶ | $rest_client_config = {} unless defined $rest_client_config; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 0 | ▶ | is_hash_ref($rest_client_config) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | or croak __PACKAGE__ . "::new: REST_CLIENT_CONFIG argument must be a hash-ref.\n"; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | my $rest = REST::Client->new($rest_client_config); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | # Set default base URL | ||||||||||||||||||||
| 50 | 0 | $rest->setHost($URL); | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | # Follow redirects/authentication by default | ||||||||||||||||||||
| 53 | 0 | $rest->setFollow(1); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # Request compact JSON by default | ||||||||||||||||||||
| 56 | 0 | $rest->addHeader('Accept' => 'application/json'); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | # Configure UserAgent name and password authentication | ||||||||||||||||||||
| 59 | 0 | for my $ua ($rest->getUseragent) { | |||||||||||||||||||
| 60 | 0 | $ua->agent(__PACKAGE__); | |||||||||||||||||||
| 61 | 0 | $ua->credentials($URL->host_port, 'Gerrit Code Review', $username, $password); | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | return bless { | |||||||||||||||||||
| 65 | rest => $rest, | ||||||||||||||||||||
| 66 | json => JSON->new->utf8->allow_nonref, | ||||||||||||||||||||
| 67 | } => $class; | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | ▶ | sub _error { | ||||||||||||||||||
|
Subroutine
_error: not called CC=8 SCAR=42.8
| |||||||||||||||||||||
| 71 | 0 | my ($self, $content, $type, $code) = @_; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 0 | ▶ | $type = 'text/plain' unless $type; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 0 | ▶ | $code = 500 unless $code; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | my $msg = __PACKAGE__ . " Error[$code"; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | if (eval {require HTTP::Status}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | ▶ | if (my $status = HTTP::Status::status_message($code)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 0 | $msg .= " - $status"; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | $msg .= "]:\n"; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | if ($type =~ m:text/plain:i) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 87 | 0 | $msg .= $content; | |||||||||||||||||||
| 88 | 0 | ▶ | } elsif ($type =~ m:text/html:i && eval {require HTML::TreeBuilder}) { | ||||||||||||||||||
|
Condition0%0/3
$type =~ m[text/html]i and eval {
do {
require HTML::TreeBuilder
}
}
MC/DC0%0/2
$type =~ m[text/html]i and eval {
do {
require HTML::TreeBuilder
}
}
$type =~ m[text/html]i
eval {
do {
require HTML::TreeBuilder
}
}
Truth table0%0/3
A and B
A= $type =~ m[text/html]iB= eval {
do {
require HTML::TreeBuilder
}
}
| |||||||||||||||||||||
| 89 | 0 | $msg .= HTML::TreeBuilder->new_from_content($content)->as_text; | |||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||
| 91 | 0 | $msg .= "<unconvertable Content-Type: '$type'>"; | |||||||||||||||||||
| 92 | }; | ||||||||||||||||||||
| 93 | 0 | $msg =~ s/\n*$/\n/s; # end message with a single newline | |||||||||||||||||||
| 94 | 0 | return $msg; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | sub _content { | ||||||||||||||||||
|
Subroutine
_content: not called CC=6 SCAR=37.4
| |||||||||||||||||||||
| 98 | 0 | my ($self) = @_; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | my $rest = $self->{rest}; | |||||||||||||||||||
| 101 | 0 | my $code = $rest->responseCode(); | |||||||||||||||||||
| 102 | 0 | my $type = $rest->responseHeader('Content-Type'); | |||||||||||||||||||
| 103 | 0 | my $content = $rest->responseContent(); | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | ▶ | $code =~ /^2/ | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | or croak $self->_error($content, $type, $code); | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | ▶ | if (! defined $type) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 109 | 0 | return; | |||||||||||||||||||
| 110 | } elsif ($type =~ m:^application/json:i) { | ||||||||||||||||||||
| 111 | 0 | ▶ | if (substr($content, 0, 4) eq ")]}'") { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | return $self->{json}->decode(substr($content, 5)); | |||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||
| 114 | 0 | croak $self->_error("Missing \")]}'\" prefix for JSON content:\n\n$content"); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | } elsif ($type =~ m:^text/plain:i) { | ||||||||||||||||||||
| 117 | 0 | return $content; | |||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||
| 119 | 0 | croak $self->_error("I don't understand content with Content-Type '$type'"); | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 0 | ▶ | sub GET { | ||||||||||||||||||
|
Subroutine
GET: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 124 | 0 | my ($self, $resource) = @_; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 0 | ▶ | eval { $self->{rest}->GET("/a$resource") } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | or croak $self->_error("Error in GET(/a$resource): $@"); | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | return $self->_content(); | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | ▶ | sub DELETE { | ||||||||||||||||||
|
Subroutine
DELETE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 133 | 0 | my ($self, $resource) = @_; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | ▶ | eval { $self->{rest}->DELETE("/a$resource") } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | or croak $self->_error("Error in DELETE(/a$resource): $@"); | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 0 | return $self->_content(); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 0 | ▶ | sub PUT { | ||||||||||||||||||
|
Subroutine
PUT: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 142 | 0 | my ($self, $resource, $value) = @_; | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | ▶ | eval { $self->{rest}->PUT( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | "/a$resource", | ||||||||||||||||||||
| 146 | 0 | $self->{json}->encode($value), | |||||||||||||||||||
| 147 | {'Content-Type' => 'application/json;charset=UTF-8'}, | ||||||||||||||||||||
| 148 | ) } | ||||||||||||||||||||
| 149 | or croak $self->_error("Error in PUT(/a$resource, ...): $@"); | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | 0 | return $self->_content(); | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | ▶ | sub POST { | ||||||||||||||||||
|
Subroutine
POST: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 155 | 0 | my ($self, $resource, $value) = @_; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 0 | ▶ | eval { $self->{rest}->POST( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | "/a$resource", | ||||||||||||||||||||
| 159 | 0 | $self->{json}->encode($value), | |||||||||||||||||||
| 160 | {'Content-Type' => 'application/json;charset=UTF-8'}, | ||||||||||||||||||||
| 161 | ) } | ||||||||||||||||||||
| 162 | or croak $self->_error("Error in POST(/a$resource, ...): $@"); | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | return $self->_content(); | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 1 | 1; | |||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||