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:
Note:1. JIRA URL should be like this:
    http://hostname/rest/api/2/issue/MKY-1

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



8 comments:

  1. Fantastic :) You are a genius!

    ReplyDelete
  2. Great!

    Exactly what I am loolking for.

    Thanks a lot!

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

    Thanks

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

    ReplyDelete
    Replies
    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.

      Thanks!

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

      Delete
  5. How can we call post method

    ReplyDelete
    Replies
    1. conn.setRequestMethod("POST");

      Delete