logo

NJP

Jira Integration with ServiceNow using Webhook

Import · Jul 27, 2020 · article

Jira Cloud Software Integration with ServiceNow using Webhook

Prerequisite:

  1. Jira Cloud Software administrator Access
  2. ServiceNow instance administrator Access

Use Case: Whenever a Jira issue created/updated in Jira cloud software it should create a record in ServiceNow.

image

Follow below steps:

  1. Create custom application/table in ServiceNow with below fields

Table Name: Jira Staging Table | u_jira_stage

Fileds:

Project Key | u_project_key

Project Name | u_project_name

Issue Key | u_issue_key

Issue Id | u_issue_id

Summary | u_summary

Description | u_description

Refer: https://docs.servicenow.com/bundle/orlando-platform-administration/page/administer/table-administrat...

image

2.Create Scripted REST API

Create a scripted REST API resource to define the HTTP method, the processing script

Before you begin

Role required: web_service_admin

About this task

By default, any new Scripted REST API resource you create contains an ACL that prohibits users with the snc_external role from making requests to the API.

Procedure

  1. Navigate to System Web Services> Scripted REST APIs.
  2. Select a scripted REST API record.
  3. In the Resources related list, click New.
  4. Enter a Name.

The resource name affects the URI for sending requests to the API.

image

  1. Select the HTTP method this resource implements, In our case its POST.

image

6. In the Script field, define how the operation parses and responds to requests.

Copy below code and paste in Script:

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    try {       
    //gs.log("Jiralog "+JSON.stringify(request.body.data));
    var requestBody = request.body.data;

    // read the request boy and map with staging table fileds and creat/update a issue record
    gs.log('jiralog '+ requestBody.issue.fields.project.name);

    var jr= new GlideRecord('u_jira_stage');
    jr.addQuery('u_issue_id',requestBody.issue.id);
    jr.addQuery('u_project_key',requestBody.issue.fields.project.key);
    jr.query();
    if(!jr.next())
    {       

    jr.initialize();
    jr.u_project_name = requestBody.issue.fields.project.name;
    jr.u_project_key = requestBody.issue.fields.project.key;
    jr.u_issue_id = requestBody.issue.id;
    jr.u_issue_key = requestBody.issue.key;
    jr.u_summary= requestBody.issue.fields.summary;
    jr.u_description=requestBody.issue.fields.description;
    jr.insert();
    gs.log('Jira Issue created: '+jr.u_issue_id);
    }

    else{

    jr.u_summary= requestBody.issue.fields.summary;
    jr.u_description=requestBody.issue.fields.description;
    jr.update();
    gs.log('Jira Issue updated: '+jr.u_issue_id);

    }

 }
catch(ex) {
 var message = ex.message;
}


})(request, response);

7.Click Submit.

3. Create an API token

Create an API token from your Atlassian account:

  1. Log in to https://id.atlassian.com/manage/api-tokens.
  2. Click Create API token.
  3. From the dialog that appears, enter a memorable and concise Labelfor your token and click Create.
  4. Click Copy to clipboard, then paste the token to your script, or elsewhere to save:

image

Note:

  • For security reasons it isn't possible to view the token after closing the creation dialog; if necessary, create a new token.
  • You should store the token securely, just as for any password.

Refer: https://confluence.atlassian.com/cloud/api-tokens-938839638.html

4.Create a Jira User in ServiceNow

Username*: Jira login email Id*

Password*: API Token*

image

Refer: https://docs.servicenow.com/bundle/orlando-platform-administration/page/administer/users-and-groups/...

5. Registering a webhook

What is webhook?

A webhook is a user-defined callback over HTTP. You can use Jira webhooks to notify your app or web application when certain events occur in Jira. For example, you might want to alert your remote application when an issue is updated or when sprint is started. Using a webhook to do this means that your remote application doesn't have to periodically poll Jira (via the REST APIs) to determine whether changes have occurred.

To register a webhook, you can use any of the following methods:

  • Jira administration console.
  • Jira REST API (note that the user must have the Jira Administrators global permission).

Registering a webhook via the Jira administration console

  1. Go to Jira administration console > System> Webhooks (in the Advanced section).
    You can also use the quick search (keyboard shortcut is .), then type 'webhooks'.
  2. Click Create a webhook.
  3. In the form that is shown, enter the details for your new webhook. For more information on this, see Configuring a webhook later on this page.
  4. To register your webhook, click Create.

image

In our scenario:

Name: ServiceNow Listener

URL: https://dev102311.service-now.com/api/122636/jiratosnow

Events: Issue Updated, Issue Created

image

Refer: https://developer.atlassian.com/server/jira/platform/webhooks

That’s all, now you can login to Jira and test the configuration.

Let’s create an issue:

image

Now login to ServiceNow and check if the issue got created in Jira staging table

image

In case of queries feel free drop your comment on the article.

If you like this article please mark it helpful !!

Regards,

Aj

+91-9769949577

https://www.linkedin.com/in/ajay-chavan-profile

View original source

https://www.servicenow.com/community/developer-articles/jira-integration-with-servicenow-using-webhook/ta-p/2296930