Saturday, December 27, 2014

JIRA REST Client Implementation in Grails

If you ever wondered whether there is an easy way to create JIRA issue entries using RESTFULL client in Grails application, this might be the right place to read further. After experiencing some difficulties with integrating REST JAVA client for JIRA in my Grails application, I decided to look for a different approach. After digging into various options, the Groovy HTTP Builder seemed to be the right choice.

Using Groovy HTTP Builder insert the following lines in BuildConfig.groovy file.
   
 
dependencies {
     compile("org.codehaus.groovy.modules.http-builder:http-builder:0.5.0-RC1") {
      excludes 'groovy', 'xml-apis'
 }
  
    }

The reason that I have excluded 'groovy' and 'xml-apis' is to avoid class path and dependencies conflict.

Now the next step is to create a service and start with the fun part. In this example I try to create a JIRA issue entry containing the following fields:
  • project key
  • summary
  • assignee
  • issuetype
  • description
  • reporter
  • fixVersions
  • components
  • customfield_<customfield_number> (Replace with the actual customfield number)

The first step is to create an JIRA REST authentication key. In my example, I have the userName and password are in an external and already encrypted. So, I need to Base64 decode both userName and password before encoding the entire authentication key string. Remember the JIRA REST basic authentication should be in the following format:
Base64.encodeBase64String(("<userName>:<password>").getBytes())

I'm using Apache Common Codec in my example. Here is the closure for creating authentication key:
   def authenticationKey = {

       String username = new String(Base64.decodeBase64(grailsApplication.config.jira.client.username)
       String password = new String(Base64.decodeBase64(grailsApplication.config.jira.client.password)
       Base64.encodeBase64String(("${username}:${password}").getBytes())
   }


Now, the next step is to create a data query in JSON format:
def query = [
  fields:[
   project:[key: 'project key'], 
   summary : 'JIRA REST Client Using Groovy HTTP Builder', 
   assignee : [name: 'assignee name'],
   issuetype:[name : 'Change Request'],
   description : 'This is a blog post about how to implement JIRA REST Client using Groovy HTTP builder', 
          reporter: [name: 'report name'],
   fixVersions: [[name: 'Add a fix version if require']],
   components: [[name: 'Add a component if require']]
   ]
       ]


The last step is to instantiate RESTClient and add authentication and query:
// change path "/jira/rest/api/2/issue/" if neccessary
def jiraClient = new RESTClient("https://<domain-name>/jira/rest/api/2/issue/")
jiraClient.headers['Authorization'] = 'Basic '+ authenticationKey()
def resp = jiraClient.post(requestContentType: JSON, body: query)


Remember this is a simple example, just to give you an idea how to call JIRA REST web services. The code does require improvement and error handling. Note that the "resp" variable contains HTTP response code and possible error messages. They are helpful information to let your end user know what went wrong, in case of errors.
Now, I have shared my implementation, if you have any suggestions or better and/or more elegant way, post a comment.

No comments :

Post a Comment