Wednesday, April 4, 2012

Using JIRA REST API in Groovy

Recently, I want to update my company's site using program to automatic update the issue status, adding comments, etc.
I could use JIRA Web service SOAP/XML-RPC  to access, like Jenkins JIRA plugin, but according to the JIRA document about remote API reference,  the REST API is recommended and supported.
so I decide to use JIRA REST API. I did some research, there are some possible options:
- Use JIRA Java Client library;
- Use Groovy HttpBuilder

For JIRA java client library, I get stuck with the maven, it always gives me error, and I was scared by its depencency on so many libraries.And For Groovy HttpBuilder, I have the same feeling, it depends on many 3rd party libraries.
So I decide to choose the 3rd solution. Just use Java HttpConnection object inside the Groovy script, and finally I got it works, it didn't use too many lines of codes, and Groovy 1.8 built-in support JSON parser, which is very handy.

Here I posted two groovy samples, you can easily integrate them into your own JIRA site by adding the url and account information.

Request an JIRA issue
JIRA add comment 

Note:1. JIRA URL should be like this:

2. According to JIRA reference: The prefered authentication methods are HTTP Basic (when using SSL) and OAuth, in Groovy code

     def authString = "user_id:password".getBytes().encodeBase64().toString()
     conn.setRequestProperty( "Authorization", "Basic ${authString}" )

Useful resource
 JIRA REST API reference
 JIRA REST API Version 2 Tutorial


  1. Fantastic :) You are a genius!

  2. Great!

    Exactly what I am loolking for.

    Thanks a lot!

  3. Very cool. I am actually trying to CREATE an issue. Any sample code on doing this?


  4. Sorry I don't have it right now, but I don't think it is too hard to use JIRA REST API

    1. Yea, Ive been trying to figure out how to do it but have not been getting too far. If you happen to try it, please let me know.


    2. I've done that :) Just read the documentation. It's all there.

  5. How can we call post method