NAME

Gerrit::REST - A thin wrapper around Gerrit's REST API

VERSION

version 0.009

SYNOPSIS

use Gerrit::REST;

my $gerrit = Gerrit::REST->new('https://review.example.net', 'myuser', 'mypass');

# Get a specific project description
my $project = $gerrit->GET('/projects/myproject');
print "Name: $project->{name}\n";
print "Description: $project->{description}\n";

# Create a new group belonging to the Administrators group
my $admin_group = $gerrit->GET('/groups/Administrators');
my $newgroup = $gerrit->PUT('/groups/newgroup', {
    description    => 'New group description.',
    visible_to_all => 'true',
    owner          => $admin_group->{name},
    owner_id       => $admin_group->{group_id},
});

# Add an account to the new group
my $account = $gerrit->GET('/accounts/someuser');
$gerrit->PUT("/groups/$newgroup->{id}/members/$account->{name}");

# Review change-id #100, patch-set 3
$gerrit->POST("/changes/100/revisions/3/review", {
    message => 'Some nits need to be fixed.',
    labels  => {'Code-Review' => -1},
});

# How to deal with errors easily
my $project = eval { $gerrit->GET('/projects/myproject') };
die $@->as_text if $@;

# How to deal with errors thoroughly
my $project = eval { $gerrit->GET('/projects/myproject') };
if ($@) {
    my ($code, $type, $content) = @{$@}{qw/code type content/};
    # ...
}

DESCRIPTION

"Gerrit is a web based code review system, facilitating online code reviews for projects using the Git version control system."

This module is a thin wrapper around Gerrit's REST API, which is superseding it's old SSH API, for which there is another Perl module called Gerrit::Client.

CONSTRUCTOR

new URL, USERNAME, PASSWORD [, REST_CLIENT_CONFIG]

The constructor needs up to four arguments:

  • URL

    A string or a URI object denoting the base URL of the Gerrit server. This is a required argument.

  • USERNAME

    The username of a Gerrit user.

    It can be undefined if PASSWORD is also undefined. In such a case the user credentials are looked up in the .netrc file.

  • PASSWORD

    The HTTP password of the user. (This is the password the user uses to log in to Gerrit's web interface.)

    It can be undefined, in which case the user credentials are looked up in the .netrc file.

  • REST_CLIENT_CONFIG

    A Gerrit::REST object uses a REST::Client object to make the REST invocations. This optional argument must be a hash-ref that can be fed to the REST::Client constructor. Note that the URL argument overwrites any value associated with the host key in this hash.

METHODS

Gerrit's REST API documentation lists dozens of "endpoints" which can be operated via the standard HTTP requests: GET, DELETE, PUT, and POST. Gerrit::REST objects implement four methods called GET, DELETE, PUT, and POST to make it easier to invoke and get results from Gerrit's REST endpoints.

All four methods need a RESOURCE argument which is simply a string denoting the endpoint URL's path, as indicated in the documentation.

PUT and POST need a second argument which is the VALUE that's a Perl data structure (usually a hash-ref, but sometimes a simple string) which is encoded using the encode method of a JSON object and passed as contents of the underlying associated HTTP method.

All four methods return the value returned by the associated endpoint's method, as specified in the documentation, decoded according to its content type as follows:

  • application/json

    The majority of the API's endpoints return JSON values. Those are decoded using the decode method of a JSON object. Most of the endpoints return hashes, which are returned as a Perl hash-ref.

  • text/plain

    Those values are returned as simple strings.

Some endpoints don't return anything. In those cases, the methods return undef. The methods croak if they get any other type of values in return.

In case of errors (i.e., if the underlying HTTP method return an error code different from 2xx) the method dies throwing a Gerrit::REST::Exception object. These objects are simple hash-refs containing the code, the type, and the content of the HTTP error response. So, in order to treat errors you must invoke the methods in an eval block and test $@ or use any of the exception handling Perl modules, such as Try::Tiny and Try::Catch. The "SYNOPSIS" section above shows some examples.

GET RESOURCE

Returns the RESOURCE as a Perl data structure.

DELETE RESOURCE

Deletes the RESOURCE.

PUT RESOURCE, VALUE

Creates RESOURCE based on VALUE.

POST RESOURCE, VALUE

Updates RESOURCE based on VALUE.

SEE ALSO

  • REST::Client

    Gerrit::REST uses a REST::Client object to perform the low-level interactions.

  • Gerrit::Client

    Gerrit::Client is another Perl module implementing the other Gerrit API based on SSH.

AUTHOR

Gustavo L. de M. Chaves <gnustavo@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by CPqD <www.cpqd.com.br>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.