Hi guys,
I have looked at various google pages and searched the forums but don't seem to be comming right. If this question has been asked and answered before then I apologize. and I will rap myself over my knuckles 
The process is run via a script on a workflow post function.
The code I am using is as follows:
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
import com.atlassian.jira.util.json.JSONObject
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
// Enable Logging
def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
// Define Required Component Access
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
// Get Issue ID that contains the Data
def issueKey = issue.getKey()
MutableIssue issue = issueManager.getIssueObject(issueKey)
//Issue issue = issueManager.getIssueObject( "TT-20636" );
// Get field values
def computerName = customFieldManager.getCustomFieldObjectByName("Computer Name");
def computerNameValue = issue.getCustomFieldValue(computerName);
// Define HTTP URL and call API
def httpBuilder = new HTTPBuilder("https://api.contoso.com")
httpBuilder.setHeaders([Authorization: "Basic <redacted>"])
httpBuilder.get(
path: "/api/AdComputers",
requestContentType: ContentType.JSON,
query: [search: "${computerNameValue}", container: "DC=contoso,DC=com"]
){
resp, reader ->
computerList(resp, reader)
}
def computerList(def resp, def reader){
log.debug "Response is $resp"
log.debug"Reader is $reader"
def name = reader.name
def location = reader.description
if (!(name)){
commentBody = "No existing computers have been found."
updateValue = "No existing computers have been found."
}else{
commentBody = "The following computer account(s) have been found: ${name} with location ${location}"
updateValue = "A computer account with ${name} and Location ${location} has been found"
}
// Define Required Component Access
def issueManager = ComponentAccessor.getIssueManager()
// Get Issue ID that contains the Data
def issueKey = issue.getKey()
MutableIssue issue = issueManager.getIssueObject(issueKey)
// Define Comment Manager
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//def commentBody = "Account has been validated"
commentManager.create(issue, user, commentBody, false)
}
I get the results in reader as I can access them and the comment is updated, however the result is as follows:
The following computer account(s) have been found: [NB-ANDREWD, NB-ANDREWDA] with location [OU1, OU1]
What I would like to do is add a comment for each result.
Ideally I would like it do the following:
The following computer account(s) have been found: NB-ANDREWD with location OU1
The following computer account(s) have been found: NB-ANDREWDA with location OU1
But I cannot seem to figure out the looping to get this working, I have tried as follows:
reader.name.each{
.....
}
and
foreach (name in reader){
....
}
All to no avail, I am sure it is something very simply that I am missing here. And I am happy to change the code if required, I also don't know if the code I am using is optimized, as I am new to Groovy and script runner as I have only been working with it for the last month or two.
Any assistance is highly appreciated.