Hi guys,
Quick question, I have a requirement to do a API GET call from a jira workflow and return the data to a field for verification.
I am trying to do the following:
def http = new HTTPBuilder('https://server'){
http.get( path : '/api/ADUsers?search=', query : [param1:'fullNameValue',param2:'container=DC=contoso,DC=com'] )
response.success = { resp, JSON ->
return JSON
}
response.failure = { resp ->
return "Request failed with status ${resp.status} and body ${body}"
}
}
fullNameValue is a parameter I am obtaining earlier on, the search query if I use a browser is as follows:
https://server/api/ADUsers?search=John Done&container=DC=contose,DC=com
However trying to run the query from Adaptivist Script runner I receive the following error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script259.groovy: 52: unexpected token: http @ line 52, column 1. http.get( path : '/api/ADUsers?search=', query : [param1:'fullNameValue',param2:'container=DC=supergrp,DC=net'] ) ^ 1 error
Is there something I am missing? I have looked at a few articles and did google searches which took me to a site (can't remember the exact one) which led me to the original code block above.
Is there a better way to it? The results returned would be as follows:
{
"id": "123456789",
"name": "John Doe",
"displayName": "John Doe",
"userPrincipalName": "john.doe@contoso.com",
"samAccountName": "john.doe",
"distinguishedName": "CN=John Doe,OU=Users,OU=Local,DC=contoso,DC=com",
"email": "john.doe@contoso.com"
}
Results returned could be for each entry returned I then need to extract the following information and place it in tabular form (or at least into the comments section):
name, displayName and samAccountName
I am unsure how to do this as I am new to Groovy.
Hi Andrew,
An example of using the httpBuilder is the following
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
def httpBuilder = new HTTPBuilder("https://server")
httpBuilder.get(
path: "/api/ADUsers",
requestContentType: ContentType.JSON,
query: [search: "John Done", container: "DC"]
) {
resp, reader ->
doSomething(resp, reader)
}
def doSomething(def resp, def reader) {
log.debug "Response is $resp"
log.debug "Reader is $reader"
def id = reader.id
def name = reader.name
// do something with the data
}
Hope that helps,
Thanos
Hi Thanos,
Looks like it's returning what I need. Should be able to work with it from here. Thank you for the assist. Will mark the answer as accepted and will post a follow up if needed.
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Thanos, so I have done the above and am trying to now get it working in a workflow process. My completed code looks like this:
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 groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
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 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.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
// Define Required Component Access
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
// Get Issue ID that contains the Data
Issue issue = issueManager.getIssueObject(issue.GetKey())
// Get field values
def fullName = customFieldManager.getCustomFieldObjectByName("Full Name");
def fullNameValue = issue.getCustomFieldValue(fullName);
def searchPath = "DC=supergrp,DC=net"
// Get required configuration for comment
//ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
//String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
//String currentUser = event.getUser().getName();
//commmgr = (CommentManager) ComponentManager.getComponentInstanceOfType(CommentManager.class)
// Define HTTP URL and call API
def httpBuilder = new HTTPBuilder("https://server")
httpBuilder.setHeaders([Authorization: "REDACTED"])
httpBuilder.get(
path: "/api/ADUsers",
requestContentType: ContentType.JSON,
query: [search: "${fullNameValue}", container: "DC=contoso,DC=com"]
){
resp, reader ->
userList(resp, reader)
}
def userList(def resp, def reader){
log.debug "Response is $resp"
log.debug "Reader is $reader"
// Return Results
def id = reader.id
def name = reader.name
def displayName = reader.displayName
def samAccountName = reader.samAccountName
//commentMgr.create(issue, currentUser, $reader, true)
}
What I am now trying to do is as follows:
1) Get the current issue key I am working on
2) Update the comment field with the data returned by $reader
3) I also would like to update a custom field with the data returned
I get the following error when attempting go through the workflow:
Time (on server): Wed Jul 18 2018 14:19:26 GMT+0200 (South Africa Standard Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2018-07-18 14:19:26,371 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-07-18 14:19:26,372 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SSD-18222, actionId: 31, file: <inline script> groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.GetKey() is applicable for argument types: () values: [] Possible solutions: getKey(), setKey(java.lang.String), getId(), getAt(java.lang.String), notify(), every() at Script327.run(Script327.groovy:33)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Were you able to call the rest API sucessfully from a workflow post function?
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.