Updating Crucible comments using REST API

Gopinath Velayudhan June 18, 2012

I am trying to use the REST API using perl to connect to crucible, add comments, update changeset etc. As a start i am trying to add comments to the existing review.

find below the script that i am using to connect

use REST::Client;

use JSON;

use Data::Dumper;

use MIME::Base64;

use LWP::UserAgent;

use HTTP::Request::Common qw(POST);

$dumpfile = '/Users/e13139/source/git_ws/cm_git_scripts/crucible_scripts/reposnsedump.txt';

open (DUMP, >$dumpfile);

#if ($#ARGV ne 0) {

# print usage: $0n;

# exit 1;

#}

#my $reviewerToRemove = $ARGV 0 ;

my $username = 'admin';

my $password = 'xxxxxx';

my $client = REST::Client->new();

my $lwp_client = LWP::UserAgent->new;

$client->setHost('http://qa3382c:8060');

my $headers = { Accept => 'application/json', Content-Type => 'application/json', Authorization => 'Basic ' . encode_base64($username . ':' . $password)};

my $headers_xml = { Accept => 'application/xml', Content-Type => 'application/json', Authorization => 'Basic ' . encode_base64($username . ':' . $password)};

my $id = 'CMTEST-7';

my $com_permid = 'CMT:406';

my $comments->{permaid}->{id}='CMT:406';

$comment = ' this is a test created by rest API';

#### GET the review information

$client->GET( '/rest-service/reviews-v1/' . $id, $headers);

my $response = from_json($client->responseContent());

print Dumper($client->responseContent());

#### GET comment information associated to the review

$client->GET('/rest-service/reviews-v1/' . $id . '/comments', $headers,);

my $response = from_json($client->responseContent());

print Dumper($client->responseContent());

## Gets the commits and loop through to get the right one to update

my $comments_ret = toList($response,'comments');

foreach $comment_ret (@$comments_ret) {

my $com_id = $comment_ret->{'permaId'}->{'id'};

print n the comment id is $com_id **** n message = $comment_ret->{'message'} n;

if ( $com_id eq $com_permid)

{

$comment_ret->{'message'}='updating using rest script';

my $com_message = $comment_ret->{'message'};

print n new message = $com_message n;

my $content ={Content=>'this is a test rest API'};

#####using JSON

my $response1 = $client->POST('/rest-service/reviews-v1/' . $id . '/comments/CM:406' , $headers , $content);

print Dumper($client->responseContent());

my $response1 = from_json($client->responseContent());

print Dumper($client->responseContent());

##### using XML

#my $response1 = $lwp_client->post( 'http://qa3382c:8060/rest-service/reviews-v1/' . $id . '/comments/CM:406', 'admin', 'xxxxx', Accept => 'application/xml', Content_Type => 'application/xml', Authorization => 'Basic ', Content => 'This is test');

#print $response1->status_line;

#print $response1->content;

}

}

I get error when i am trying to do a POST all my GETS works fine. I think i am getting an error 500. my guess is may be i am not passing the variables right to do a post. Can you please help. I have been working on this for a week now with no result. Any help will be greatly appreciated

Find below the error messages that I recieve.

When I use the JSON method I get the following error

$VAR1 = '500 Not a SCALAR reference

';

garbage after JSON object, at character offset 4 (before Not a SCALAR referen...) at /Library/Perl/5.12/JSON.pm line 171.

When i use the XML method i get the follwoing error

500 Internal Server ErrorWebApplicationjavax.xml.bind.UnmarshalException

- with linked exception:

org.xml.sax.SAXParseException: Content is not allowed in prolog.javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException

- with linked exception:

org.xml.sax.SAXParseException: Content is not allowed in prolog.

at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:99)

at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:393)

at com.sun.jersey.server.impl.model.method.dispatch.EntityParamDispatchProvider$EntityInjectable.getValue(EntityParamDispatchProvider.java:139)

at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:43)

5 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
dhandu July 2, 2012

Thanks, Do you have working example of create a review and add changeset ?

dhandu July 8, 2012

we have many users, Do all they neeed to enter thier windows password to connect crucible from command line review ?, is there a way to address the passowrdless review from command line review ?, If I use common account or serviec account , all the reviews are uploaded with the service account name instead of actual author of the review ?

Thanks

Dandu

0 votes
dhandu July 2, 2012

Also looks like I need to enter my windows password for this authentication, how can I override the password authentication?

my $username = 'admin';

my $password = 'xxxxxx';

Thanks

Gopinath Velayudhan July 2, 2012

I think you might be able to use key based authentication The link below provides you information on how you can authenticate

https://developer.atlassian.com/display/FECRUDEV/Authenticating+REST+Requests

thanks
Gopi

0 votes
dhandu July 2, 2012

could you please share the working add changeset code ?, I am not able to find the complete details of using "addFile" or addchangeset options

Thanks

Dandu

0 votes
Gopinath Velayudhan June 20, 2012

Thanks Conor I was able to use create a new comment using the rest API. Although I am still having issue when updating an exisitng commets -- it does not works and throws and error that the author of the review is different from me but it was me who created the comment and the review as well.

As my primary goal i want to attach change set to a review. But i do not see any information on the content that i need to pass to add a changeset. and the REST API doc is incomplete.

can you provide what the input xml content should be for

/rest-service/reviews-v1/{id}/addChangeset

I have attached the working code .

### login to crucible
my $headers = {"Accept" => 'application/json',"Content-Type" => 'application/json', "Authorization" => 'Basic '};
my $headers_xml = { "Accept" => 'application/xml', "Content-Type" => 'application/xml',  "Authorization" => 'Basic ' };
#my $headers = { Accept => 'application/json' , Content-Type => 'application/json', Authorization => 'Basic ' . encode_base64($username . ':' . $password)};

$client->GET('/rest-service/auth-v1/login?userName='.$username.'&password='.$password, $headers );
my $response = from_json($client->responseContent());
my $token = $response->{'token'};
print "printing token again $token\n";

my $id = 'CMTEST-22';
my $com_permid = 'CMT:417';
$comment = ' this is a test created by rest API';

#### GET the review information
$client->GET( '/rest-service/reviews-v1/'.$id.'?FEAUTH='.$token , $headers);
my $response = from_json($client->responseContent());
print  Dumper($client->responseContent());


#### GET comment information associated to the review
$client->GET('/rest-service/reviews-v1/'.$id.'/comments?FEAUTH='.$token, $headers,);
my $response = from_json($client->responseContent());
print  Dumper($client->responseContent());

## Gets the commits and loop through to get the right one to update
my $comments_ret = toList($response,'comments');
foreach $comment_ret (@$comments_ret) {
    my $com_id = $comment_ret->{'permaId'}->{'id'};
    print "\n the comment id is $com_id ****\n message =  $comment_ret->{'message'} \n";

    if ( $com_id eq $com_permid)
       { 
       	 $comment_ret->{'message'}='updating using rest script';
       	 my $com_message = $comment_ret->{'message'};
       	 print "\n new message = $com_message\n";
       
       	 my $content = '{ "generalCommentData":{"message":"this is a test rest API"}';
         $content = '<generalCommentData><message>updating by perl script 123</message></generalCommentData>'       	 ;
#### This does not work ############
my $response1 = $client->POST('/rest-service/reviews-v1/'. $id . '/comments/CMT:417?FEAUTH='.$token, $content,$header_xml );
         print  $client->responseCode();
            print $client->responseContent();
     
#      	   my $response1 = $client->POST('/rest-service/reviews-v1/'. $id . '/comments/CMT:409?FEAUTH='.$token, $headers_xml, $content );
#           print  Dumper($client->responseContent());
#      	   my $response1 = from_json($client->responseContent());
#           print  Dumper($client->responseContent());

#       	 my $response1 = $lwp_client->post( 'http://qa3382c:8060/rest-service/reviews-v1/' . $id . '/comments/CMT:410?FEAUTH='.$token,   Accept => 'application/xml', Content_Type => 'application/xml',  Authorization => 'Basic ', content => $content);
#       	 print $response1->status_line;
#        print $response1->content;
       	 
       }
    
}
#### Add new  comment
print " \n trying to comment \n";
my $content = '<generalCommentData><message>updating by perl script 12345wertwert</message></generalCommentData>'       	 ;
$headers = {"Accept" =>'application/xml', "Content-Type" => 'application/xml', "Authorization" =>'Basic '};
#my $response1 = $client->POST('/rest-service/reviews-v1/CMTEST-22/comments?FEAUTH='.$token, $content, {"Content-Type" => 'application/xml'});
my $response1 = $client->POST('/rest-service/reviews-v1/CMTEST-22/comments?FEAUTH='.$token, $content, $headers);
      print  $client->responseCode();
      print $client->responseContent();

 #my $response1 = $lwp_client->post( 'http://qa3382c:8060/rest-service/reviews-v1/' . $id . '/comments?FEAUTH='.$token,   Accept => 'application/xml', Content_Type => 'application/xml',  Authorization => 'Basic ', content => $content);
  #     	 print $response1->status_line;
   #      print $response1->content;

######## adding Changeset

dhandu July 1, 2012

Hi Gopi, Are you able to attach the changset with the existing review or new review id ? ,

Thanks

Dandu

Gopinath Velayudhan July 1, 2012

Yes I was able to the information is available

http://host:port/context/rest-service/reviews-v1/{id}/addChangeset [P">Adding Changeset to Crucible review using REST API

Thanks

Gopi

0 votes
Conor
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 19, 2012

If I understand correctly, you are sending this content for JSON:

my $content ={Content=>'this is a test rest API'};

and this content for XML:

Content => 'This is test'

Neither of those appear to be correctly formatted. If you are going to send XML, you need to send a full XML document.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events