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?
It is possible to call external REST API from a groovy script. You can find an example here:
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That is right you should concatenate your variable, like this
def jsonBody = "{
\"parentContainerPath\":" + {ContainerPath} + "," +
"\"firstName\":" + firstNameValue
......
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Andrew,
I have got an error '\n''; got it anyway.. How did you solve the issue?
Thanks,
Swarna
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you Alexey, I will have a look and see what I can do with.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.