Jump to content


Automate swarm review approval

restfulapi swarm approve

  • Please log in to reply
4 replies to this topic

#1 sreenikarthik

sreenikarthik

    Member

  • Members
  • PipPip
  • 12 posts

Posted 08 January 2016 - 08:54 AM

Hi,

How to approve a swarm review request using REST api.
Could you please give the curl command?

Thanks,
Sreeni.

#2 P4Shimada

P4Shimada

    Advanced Member

  • Members
  • PipPipPip
  • 831 posts

Posted 22 January 2016 - 01:38 AM

Hi Sreeni,

Are you using the Jenkins plugin for Jira? If so, the following article under 'Troubleshooting' mentions some details regarding Swarm refreshing JIRA connection details:

- Swarm JIRA plugin usage
http://answers.perfo...rticles/KB/3894

If you are making a URL link, we have a reference here 'Swarm URL Syntax':

http://answers.perfo...ticles/KB/13217

Our RESTful API has an endpoint for reviews:

  https://swarm.perfor...dpoints.Reviews

Here is an example of an automated using one of Swarm's undocumented endpoints:


Quote

curl -d 'state=archived' -u user:password \
  http://[swarm-host]/reviews/[review-id]/transition


#3 P4Shimada

P4Shimada

    Advanced Member

  • Members
  • PipPipPip
  • 831 posts

Posted 28 January 2016 - 01:07 AM

This functionality (a REST API for Swarm) should be coming in the Swarm 2016.1 release.  I have added you to this enhancement request.

In the meantime, you can use this script from one of our Swarm developers, that does it from a php request using curl. See below:

<?php
$swarmHost = 'SWARM-HOST'
$username = 'P4USER';
$password = 'P4PASSWD';
$cookieFile = tempnam(null, 'SWARM');
# define a function to make POST requests simple(r)
function request($url, $post) {
global $cookieFile;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, count($post));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookieFile);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
# login and get the SWARM cookie plus CSRF token
$result = request(
"http://$swarmHost/login",
array(
'user' => $username,
'password' => $password
)
);
$json = json_decode($result, true);
$csrf = $json['csrf'];
//print_r($json);
if(isset($argv[1]) && isset($argv[2])){
# now transition the review
$result = request(
"http://$swarmHost/reviews/".$argv[1]."/transition",
array(
'state' => $argv[2],
'_csrf' => $csrf,
)
	 /*
	 States you can use are:
	 1. needRevision
	 2. approved
	 3. approved:commit
	 4. rejected
	 5. archived
	 */
);
print_r($result);
} else {
echo "Need to pass review number and status like this:: php file.php 53
archived";
}


#4 sreenikarthik

sreenikarthik

    Member

  • Members
  • PipPip
  • 12 posts

Posted 01 February 2016 - 08:42 AM

Hi

Thanks for your response.But I'm still not able to crack it.

The Curl command gives the following response

""isValid":false,"messages":{"state":{"callbackValue":"You cannot transition this review to \u0027approved\u0027."}},"

#5 sreenikarthik

sreenikarthik

    Member

  • Members
  • PipPip
  • 12 posts

Posted 01 February 2016 - 08:46 AM

Hi,

Thanks a lot.It worked !!
The issue was with the ID. Which didn't have permission to approve the review request.



Also tagged with one or more of these keywords: restfulapi, swarm, approve

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users