Thursday 21 December 2017

Connect Jira from Java using Jira REST API


Jira is one of the popular bug tracking tool developed by Atlassian. It also provides project management features inline with Agile methodology. Jira is basically written in Java & can integrate with source control programs like Perforce, Git, Clearcase, Subversion, TFS etc.
It also provides robust set of APIs which enables its integration with any system.

An interesting fact about its name 'JIRA': It is not an acronym, but a truncation of 'Gojira', the japanese name for 'Godzilla'.  :)

Example to Create Issue & Search Issue:

This example shows how to consume Jira REST APIs from Java using REST Assured libraries.

Step 1: Create a Project with dependent REST assured libraries.

NOTE: For the set up / usage of REST assured libraries can be found in another post.
https://advancedtestautomation.blogspot.in/2016/08/test-rest-web-services-using-rest.html

Step 2: Create a class file by name 'JiraConnect'.


Step 3: Define a main method with the below variables.


RestAssured.baseURI = "https://testserver.atlassian.net/rest/api/2";
String createIssueUrl = "/issue";
String searchurl = "/search?jql=assignee='testUser'&startAt=1&maxResults=1&fields=id,key,summary,description";

HashMap<String,String> headerMap = new HashMap<String,String>();
headerMap.put("Content-Type", "application/json");
headerMap.put("Authorization", "Basic bmC5jb20sdsdyYWtpbmczMjE=");

String createPaylod = "{\"fields\":{\"project\":{\"key\":\"CRE\"},\"summary\":\"Test REST API for creating an Issue\",\"description\":\"Creating an issue\",\"issuetype\":{\"name\":\"Bug\"},\"labels\":[\"Automation\"],\"assignee\":{\"name\":\"\"},\"environment\":\"TestEnv\",\"priority\":{\"name\":\"High\"}}}";

Step 4: Create a method 'createIssue' to create an issue / bug:


public static void createIssue(String url, HashMap<String,String> headerMap, String body){
Response response = null;
try{
response = RestAssured.given().log().all().headers(headerMap).body(body).post(url);
response.prettyPrint();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}

Step 5: Create a method 'searchIssue' to search issues based on filter parameters like assignee, Results Limit, fields to be displayed in results.. etc.


public static void searchIssue(String url, HashMap<String,String> headerMap){
Response response = null;
try{
response =  RestAssured.given().log().all().headers(headerMap).get(url);
response.prettyPrint();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}

Step 6: Call the methods 'createIssue' and 'searchIssue' in main method accordingly. 


Similar to the above, many such methods are available to perform actions on Jira thorough Jira REST APIs.

NOTE:

  • Create Issue payload mentioned above can be modified according to needs.
    Below shown in json format:
    {
      "fields": {
        "project": {
          "key": "CRE"
        },
        "summary": "Issue REST Summary",
        "description": "Creating an issue",
        "issuetype": {
          "name": "Bug"
        },
        "labels": [
          "Automation"
        ],
        "assignee": {
          "name": ""
        },
        "environment": "TestEnv",
        "priority": {
          "name": "Major"
        }
      }
    }
  • Sample response for Create issue:
    {
    "id": "10002",
    "key": "CRE-3",
    "self": "https://testserver.atlassian.net/rest/api/2/issue/10002"
    }

References:

https://developer.atlassian.com/jiradev/jira-apis



10 comments:

  1. Superb.... Great. Very nice Information.

    ReplyDelete
  2. Hi, I am trying to connect with JIRA Rest Services with Postman first before I can automate them with rest assured. What Header should I use for Authentication key? How you have generated that key here?

    ReplyDelete
    Replies
    1. Hi, it is basically base64 encoded string using username & password used for accessting Jira.

      Delete
  3. Hi , I am trying to get status of the issue but not able to get it via Rest Assured.But I was able to get the Jira Service desk issue status by using Jira Rest Client. Is there a way to get the status of the issue using Rest Assured ?

    Kr,
    Yogesh

    ReplyDelete
    Replies
    1. Hi Yogesh, Ideally you should have got the status with REST api also.
      Try using the below endpoint, if that works:
      https:///rest/api/2/issue/ISSUE-2?fields=status

      Delete
    2. Make sure to use APIToken instead of Password , as password support for API call is deprecated from jira side. I tried login to my jira cloud instance using postman (considering username & token & choosing Basic authorization authentication). It worked fine

      Delete
  4. This is useful information for all of us building any software or website in Jira integration, but also check out free trial of workshop management software

    ReplyDelete
  5. Your blog post on Jira is both informative and concise. I appreciate the detailed steps on creating issues and searching for them using REST APIs. It's a valuable resource for those looking to integrate Jira into their workflow. Keep up the great work!

    ReplyDelete