Call external REST API from workflow transition

Andrew Downs May 10, 2018

We have a custom web api that I have configured as a web hook in Jira, what I need to do is call this API and pass specific parameters from the Service Desk call to it in a specific workflow step.

 

For example on the Jira call we have the following custom fields:

Full Name: John Doe

First Name: John

Surname: Doe

ID Number: 1234567890

Office Number etc

 

What I would like to do is call the web hook passing the following information:

 

https://<apiserver>/api/Employee/{"fullName": "John Doe", "firstName": "John", "lastName": "Doe", "identificationNumber": "1234567890"}

 

Is this possible to do from within Jira Groovy script or workflow process?

 

 

4 answers

1 accepted

1 vote
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 10, 2018

It is possible to call external REST API from a groovy script. You can find an example here:

https://community.atlassian.com/t5/Answers-Developer-Questions/Making-a-REST-call-using-script-runner/qaq-p/576712

Or you can use the Jira web hook functionality to get the same result. You can not change the default json sent by the Jira web hook. But you can change the Rest Method in the external application. You can read more about Jira webhooks here:

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

Andrew Downs May 11, 2018

Hi Alexey,

 

Ok, so I have managed to piece together something, but am battling when trying to format the JSON.

 

This is what I have so far:

import groovy.json.JsonSlurper;
import groovy.json.StreamingJsonBuilder;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import org.apache.commons.codec.binary.Base64;
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import groovy.json.JsonOutput

// Define Required Component Access

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

// Get Issue ID that contains the Data
Issue issue = issueManager.getIssueObject( "SSD-18222" );

// Get field values
def fullName = customFieldManager.getCustomFieldObjectByName("Full Name");
def fullNameValue = issue.getCustomFieldValue(fullName);
//def firstName = customFieldManager.getCustomFieldObjectByName("Firstname");
//def firstNameValue = issue.getCustomFieldValue(firstName);
def firstNameValue = "Jane"
def lastName = customFieldManager.getCustomFieldObjectByName("Surname");
def lastNameValue = issue.getCustomFieldValue(lastName);
//def officeNumber = customFieldManager.getCustomFieldObjectByName("Office Number");
//def officeNumberValue = issue.getCustomFieldValue(officeNumber);
def officeNumberValue = "+27115234020"
def mobileNumber = customFieldManager.getCustomFieldObjectByName("Cell Phone Number");
def mobileNumberValue = issue.getCustomFieldValue(mobileNumber);
//def faxNumber = customFieldManager.getCustomFieldObjectByName("Office Number");
//def faxNumberValue = issue.getCustomFieldValue(faxNumber);
def title = customFieldManager.getCustomFieldObjectByName("Job Title");
def titleValue = issue.getCustomFieldValue(title);
def department = customFieldManager.getCustomFieldObjectByName("Department");
def departmentValue = issue.getCustomFieldValue(department);
def siteName = customFieldManager.getCustomFieldObjectByName("Site Code");
def siteNameValue = issue.getCustomFieldValue(siteName);
def idNumber = customFieldManager.getCustomFieldObjectByName("ID Number");
def idNumberValue = issue.getCustomFieldValue(idNumber);
//def passportNumber = customFieldManager.getCustomFieldObjectByName("Passport Number");
//def passportNumberValue = issue.getCustomFieldValue(passportNumber);
//def birthdate = customFieldManager.getCustomFieldObjectByName("Birthday");
//def birthdateValue = issue.getCustomFieldValue(birthdate);

// Define JSON

def ContainerPath = "LDAP://OU=BU3,OU=BU2,OU=BU1,OU=Employees,OU=contoso,DC=contoso,DC=com"

def jsonBody = """{
    "parentContainerPath": {ContainerPath},
    "firstName": firstNameValue,
    "lastName": lastNameValue,
    "telehpone": officeNumberValue,
    "mobile": mobileNumberValue,
    "title": titleValue,
    "department": departmentValue,
    "siteName": siteNameValue,
    "identificationNumber": idNumberValue,
    "passportNumber": idNumberValue,
    "birthDate": "birthday"
}"""

return jsonBody


// Define Web API to post to
def baseURL = "http://<server>:8090/api/Employees";

// Establish Connection and post data

URL url = new URL(baseURL);
//URLConnection connection = url.openConnection();

HttpURLConnection connection = url.openConnection() as HttpURLConnection;
connection.requestMethod = "POST"
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.outputStream
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, jsonBody) }
connection.connect();
log.info ("URL="+url+"Status="+connection.getResponseCode() as String)


The problem is with the jsonBody, if I run it I get the following returned:

 

{ "parentContainerPath": {ContainerPath}, "firstName": firstNameValue, "lastName": lastNameValue, "telehpone": officeNumberValue, "mobile": mobileNumberValue, "title": titleValue, "department": departmentValue, "siteName": siteNameValue, "identificationNumber": idNumberValue, "passportNumber": idNumberValue, "birthDate": "birthday" }

 

It does not seem to be pulling through the values defined. Is there something that I am missing?

 

if I format the jsonBody object as follows:

 

def jsonBody = [
    "parentContainerPath": {ContainerPath},
    "firstName": firstNameValue,
    "lastName": lastNameValue,
    "telehpone": officeNumberValue,
    "mobile": mobileNumberValue,
    "title": titleValue,
    "department": departmentValue,
    "siteName": siteNameValue,
    "identificationNumber": idNumberValue,
    "passportNumber": idNumberValue,
    "birthDate": "birthday"
]

 

The JSON appears to be malformed and I get an error when posting the web API.

 

Regards

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 11, 2018

That is right you should concatenate your variable, like this

def jsonBody = "{
    \"parentContainerPath\":" +  {ContainerPath} + "," +
    "\"firstName\":" + firstNameValue

......

Andrew Downs May 11, 2018

Ok, formatted as follows:

 

def jsonBody = "{
    \"parentContainerPath\":" +  ContainerPath + "," +
    \"firstName\":" + firstNameValue + "," +
    \"lastName\":" + lastNameValue + "," +
    \"telehpone\":" + officeNumberValue + "," +
    \"mobile\":" + mobileNumberValue + "," +
    \"title\":" + titleValue + "," +
    \"department\":" + departmentValue + "," +
    \"siteName\":" + siteNameValue + "," +
    \"identificationNumber\":" + idNumberValue + "," +
    \"passportNumber\":" + idNumberValue + "," +
    \"birthDate\":" + birthday"
}"

 

But now getting the below error:

 

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script44.groovy: 59: expecting anything but ''\n''; got it anyway @ line 59, column 18. def jsonBody = "{ ^ 1 error

Andrew Downs May 13, 2018

Ok, formatted as follows:

 

def jsonBody = "{
    \"parentContainerPath\":" +  ContainerPath + "," +
    \"firstName\":" + firstNameValue + "," +
    \"lastName\":" + lastNameValue + "," +
    \"telehpone\":" + officeNumberValue + "," +
    \"mobile\":" + mobileNumberValue + "," +
    \"title\":" + titleValue + "," +
    \"department\":" + departmentValue + "," +
    \"siteName\":" + siteNameValue + "," +
    \"identificationNumber\":" + idNumberValue + "," +
    \"passportNumber\":" + idNumberValue + "," +
    \"birthDate\":" + birthday"
}"

 

But now getting the below error:

 

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script44.groovy: 59: expecting anything but ''\n''; got it anyway @ line 59, column 18. def jsonBody = "{ ^ 1 error

0 votes
Andrew Downs May 31, 2018

Managed to get it working as required.

Swarna Radha September 3, 2019

Hi Andrew,

 

I have got an error '\n''; got it anyway.. How did you solve the issue?

 

Thanks,

Swarna

Rodolfo So April 7, 2021

Hi @Andrew Downs 

 

How do you resolved the issue?

0 votes
Andrew Downs May 28, 2018

String is formatted as follows:

def jsonBody = "{
    \"parentContainerPath\":" +  ContainerPath + "," +
    \"firstName\":" + firstNameValue + "," +
    \"lastName\":" + lastNameValue + "," +
    \"telehpone\":" + officeNumberValue + "," +
    \"mobile\":" + mobileNumberValue + "," +
    \"title\":" + titleValue + "," +
    \"department\":" + departmentValue + "," +
    \"siteName\":" + siteNameValue + "," +
    \"identificationNumber\":" + idNumberValue + "," +
    \"passportNumber\":" + idNumberValue + "," +
    \"birthDate\":" + birthday"
}"

 

But now getting the below error:

 

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script44.groovy: 59: expecting anything but ''\n''; got it anyway @ line 59, column 18. def jsonBody = "{ ^ 1 error

0 votes
Andrew Downs May 10, 2018

Thank you Alexey, I will have a look and see what I can do with.

Suggest an answer

Log in or Sign up to answer