Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
Level
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Call external REST API from workflow transition

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

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/

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

That is right you should concatenate your variable, like this

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

......

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

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

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

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

Managed to get it working as required.

Hi Andrew,

 

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

 

Thanks,

Swarna

Hi @Andrew Downs 

 

How do you resolved the issue?

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you