Skip to end of metadata
Go to start of metadata

This tutorial will write a Perl script which removes a user from all open reviews – this might be useful if a reviewer is no longer available, and is holding up the completed status of all their reviews.

First install the REST::Client and JSON packages from CPAN – the details of doing this will depend on your platform.

Icon

Data::Dumper is very useful when developing Perl REST clients:

Our script will retrieve a list of all open reviews, get the uncompleted reviewers for each review, and if one of these matches the user passed as a command line parameter we will complete the reviewer.

  • Get all open reviews: GET /reviews-v1/filter/{filter}, setting {filter} to allOpenReviews.
  • Get the incomplete reviewers for each of these reviews: GET /reviews-v1/{id}/reviewers/uncompleted
  • Remove a reviewer: POST /reviews-v1/{id}/reviewers/{username}
Icon

When JSON produces lists of objects, the structures produced depend on the number of items in the list.
If the /reviews-v1/filter URL returns a single review, the JSON will look like this:

but if several reviews are returned the JSON will be:

and if there are no reviews it will simply be:

The toList function in the code below handles the three cases above.

  • No labels

4 Comments

  1. Anonymous

    When I run the example perl script, it give me "malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "<!DOCTYPE HTML PUBLI...") at test.pl line 30.

    Appreciated if any help.

    1. Anonymous

      verify the url's you are requesting:

      '/rest-service/reviews-v1/filter/allOpenReviews', vs '/fecru/rest-service/reviews-v1/filter/allOpenReviews',

  2. Anonymous

    could you please tell me how to attach file to JIRA issue using rest::cient in perl ?

  3. Anonymous

    > could you please tell me how to attach file to JIRA issue using rest::cient in perl ?

     

    here is what i came up with... 

     

    =pod
    ----------------------------------------------------------------------------
    # rest_add_jira_attachment ($rest_hash, $key, $filename)
    $rest_hash = {url=> 'http://jira.mycompany.com', user=> 'jira_admin', password=> 'password_for_jira_admin'}

    (c) Max Uzunov
    ----------------------------------------------------------------------------
    =cut
    sub rest_add_jira_attachment {
    my ($rest_hash, $key, $filename) = (@_);

    require Data::Dumper;
    require MIME::Base64;
    require URI;
    require URI::Escape;

    my (undef, undef, $strip_fn) = File::Spec->splitpath ($filename);
    my $rest_api_suffix = '/rest/api/latest';
    my $uri = URI->new( $rest_hash->{url} );
    $uri->path( $uri->path . $rest_api_suffix );

    my $rest = REST::Client->new();
    $rest->setHost( $uri );
    $rest->setFollow(1);
    $rest->addHeader(Authorization => 'Basic ' . MIME::Base64::encode_base64($rest_hash->{user} . ':' . $rest_hash->{password}));
     $rest->getUseragent->agent('Adding JIRA attachment using REST::Client');

    my $jira_path = "/issue/$key/attachments";
    my $jira_uri = $uri->path;

    my $request =
    POST ( $uri->path
    ,Content_Type => 'form-data'
    ,'X-Atlassian-Token' => 'nocheck'
    ,Content => [file => [$filename, $strip_fn],]
    );
    print "URI:\t" . $uri->path . "\n";
    print Dumper($request) . "\n";

    my $headers = {'Content-Type' => $request->header('Content-Type')};
    $headers->{'X-Atlassian-Token'} //= 'nocheck';

    my $response =
    $rest->POST(
    $jira_path
    ,$request->content
    ,$headers
    );
    print Dumper($response) . "\n";

    return $rest;
    }

    #-------------------------------------------------------

     

     

     

    usage:

      my $filename = 'a.sql';
      my $rest_hash = { 'https://jira.mycompany.com', 'jira_admin', 'jira_password' };
      my $rest = rest_add_jira_attachment ($rest_hash, $RELEASE_TAG, $filename);
      print "=" x 80 . Dumper($rest) . "\n";