DEV Community

Vivek Soni
Vivek Soni

Posted on

Connecting ServiceNow to Jira

Creating a seamless integration for automatic bug reporting


Introduction:

In this guide, we'll walk through the process of integrating ServiceNow's Automated Test Framework (ATF) with Jira. This integration enables automatic bug reporting whenever a test case fails, streamlining the communication between testing and development teams. While this is a personalized approach, it can be adapted to integrate Jira with other functionalities of ServiceNow.


Understanding the Workflow:

1) Identifying Triggers:

We'll set up a business rule in ServiceNow to trigger whenever a test case fails. This rule will be activated when a new entry is inserted in the sys_atf_test_result table indicating a failure.

Why Use a Business Rule?
A business rule in ServiceNow allows you to execute server-side logic whenever records are inserted, updated, deleted, or queried. It helps automate processes and ensure that specific actions are taken under defined conditions. In this case, the business rule will detect test failures and trigger the creation of a Jira bug.

Image description

  • Navigating to the sys_atf_test_result table
  • Setting up the business rule with the correct conditions

2) Utilizing Jira API:

We'll leverage the Jira API to create a bug issue in Jira automatically. It's essential to understand the structure of the Jira API. Initially, you can test the API using tools like Postman to ensure it works as expected.

Implementation Steps:

3) Setting Up Credentials:

First, securely store your Jira API token and username in ServiceNow. This ensures that your credentials are safely accessed later during the integration process.

Steps:

  • Navigate to the credentials section in ServiceNow.
  • Input your Jira username and API token.

Image description

  • Storing credentials in ServiceNow

4) Writing the Business Rule:

  • Navigate to the sys_atf_test_result table either by searching it in the All section or directly entering its name followed by .list in the search.
  • Create a business rule triggered on update when the status is 'fail'.

Steps:

  • Go to sys_atf_test_result table.
  • Right-click on the table header > Configure > Business Rule.
  • Ensure 'Advanced' is checked since we will write code.
  • Set "When to run" to "After" and check "Update" only.

Image description

Filter Condition:

  • Status is 'fail'.
  • Accessing the sys_atf_test_result table
  • Configuring the business rule settings Image description

5) Integration with Jira:

Using the ServiceNow web service (sn_ws), we will send a REST message to Jira to create a bug issue.

Code Snippet:

   var request = new sn_ws.RESTMessageV2();
   request.setEndpoint("https://yourcompany.atlassian.net/rest/api/3/issue"); // Replace with a generic URL
   request.setHttpMethod("POST");

   var provider = new sn_cc.StandardCredentialsProvider();
   var credential = provider.getCredentialByID('your_credential_id');
   var user = credential.getAttribute("user_name");
   var password = credential.getAttribute("password");

   request.setBasicAuth(user, password);
   request.setRequestHeader('Accept', 'application/json');
   request.setRequestHeader('Content-Type', 'application/json');
Enter fullscreen mode Exit fullscreen mode
  • Writing and testing the REST message code
  • Setting up credentials in the script

6) Constructing the Request Body:

Retrieve the necessary data such as summary and description from the sys_atf_test_result_step table, ensuring to filter for the current test result.

Code Snippet:

   var testSteps = new GlideRecord('sys_atf_test_result_step');
   testSteps.addQuery("status", 'Failure');
   testSteps.addQuery("test_result", current.sys_id);
   testSteps.query();
   testSteps.next();

   var des = testSteps.description;
   var summary = testSteps.summary;

   // Formatting summary and description
   summary = summary.replace(/['"]/g, '');
   if (summary.includes("\n")) {
       summary = summary.replace(/\n/g, ' ');
   }

   var arr = des.split("\n");
   des = "";
   for (var j = 0; j < arr.length; j++) {
       des += "{\"type\": \"paragraph\",\"content\": [{\"type\": \"text\",\"text\": \"" + arr[j] + "\"}]},";
   }
   des = des.substring(0, des.length - 1);

   var body = "{\"fields\": {\"project\":{\"key\": \"PROJECT_KEY\"},\"summary\":\"" + summary + " " + current.status + "\",\"description\": {\"version\": 1,\"type\": \"doc\",\"content\": [" + des + "]},\"issuetype\": {\"name\": \"Bug\"}}}";

   request.setRequestBody(body);
   var response = request.execute();
Enter fullscreen mode Exit fullscreen mode
  • Querying and retrieving test step data
  • Formatting the retrieved data

Conclusion:

By following these steps, you can seamlessly integrate ServiceNow ATF with Jira to automate bug reporting. This approach streamlines the process, ensuring efficient communication between testing and development teams. Feel free to adapt this framework to suit other integration needs within your organization.

Additional Tips:

  • Regularly test the integration to ensure seamless functionality.
  • Keep credentials secure to prevent unauthorized access.
  • Document the integration process for future reference and troubleshooting.

Top comments (1)

Collapse
 
ajay_verma_75c05e3c215743 profile image
Ajay Verma

Good work