Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Output API successful call response to ticket comment?

Eric Smith
Contributor
June 19, 2019

I am attempting to define a successful call response from an API and output it to a Jira comment. However, I keep getting the same error message:

2019-06-19 19:58:25,551 http-nio-8080-exec-5 url:/secure/WorkflowUIDispatcher.jspa username:esmith DEBUG esmith 1198x73906x1 1pjq8zi 10.233.231.48,10.231.220.22 /secure/WorkflowUIDispatcher.jspa [c.o.j.groovy.groovyrunner.spring] BeforeInstantiation [bean=com.onresolve.jira.groovy.GroovyFunctionPlugin, type=com.onresolve.jira.groovy.GroovyFunctionPlugin]
2019-06-19 19:58:25,552 http-nio-8080-exec-5 url:/secure/WorkflowUIDispatcher.jspa username:esmith DEBUG esmith 1198x73906x1 1pjq8zi 10.233.231.48,10.231.220.22 /secure/WorkflowUIDispatcher.jspa [c.o.j.groovy.groovyrunner.spring] AfterInitialisation [bean=com.onresolve.jira.groovy.GroovyFunctionPlugin, type=com.onresolve.jira.groovy.GroovyFunctionPlugin]
2019-06-19 19:58:25,557 http-nio-8080-exec-5 url:/secure/WorkflowUIDispatcher.jspa username:esmith ERROR esmith 1198x73906x1 1pjq8zi 10.233.231.48,10.231.220.22 /secure/WorkflowUIDispatcher.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] *************************************************************************************
2019-06-19 19:58:25,558 http-nio-8080-exec-5 url:/secure/WorkflowUIDispatcher.jspa username:esmith ERROR esmith 1198x73906x1 1pjq8zi 10.233.231.48,10.231.220.22 /secure/WorkflowUIDispatcher.jspa [c.o.s.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: TPPI-495, actionId: 61, file: <inline script>
groovy.lang.MissingPropertyException: No such property: event for class: Script467
at Script467.run(Script467.groovy:23)

 

My code is here, the main interesting bits are near the bottom:

 

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import groovyx.net.http.Method
//import static groovyx.net.http.Method.*
import static groovyx.net.http.Method.PATCH
//import static groovyx.net.http.ContentType.JSON

import com.atlassian.jira.component.ComponentAccessor
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.issue.Issue
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)

Issue sourceIssue = event.getIssueLink().getSourceObject()
Issue issue = event.issue
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def CommentManager = ComponentAccessor.getCommentManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def ComponentManager = ComponentManager.getInstance()

//Call the School URL field from the ticket
def schoolUrl_field = customFieldManager.getCustomFieldObject("customfield_12456")

// Get the value of the School URL field from schoolURL
def schoolUrl_value = issue.getCustomFieldValue(schoolUrl_field).toString()

//define CNAME entry based on School URL field first octet
def siteName = schoolUrl_value.tokenize(".")[0].toString()

//Define domain based on the School URL field second octet
def siteDomain = schoolUrl_value.tokenize(".")[1].toString()

//Get the value of the Shard field
def shard_field = customFieldManager.getCustomFieldObject("customfield_12454")

// Get the value of the Shard field from schoolURL
def shard = issue.getCustomFieldValue(shard_field).toString()

//Bring them together
def content = "owsoo${shard}.${siteDomain}.com.".toString()
def name = "${siteName}.${siteDomain}.com.".toString()
def dig = "dig ${schoolUrl_value}".execute().text
def comment_success = "School added to DNS \n ${dig}"
def comment_failure = "Adding school to DNS failed. See logs for more info"

def http = new HTTPBuilder("http://[IP_ADDRESS][PORT]/api/v1/servers/localhost/zones/[domain].com")
http.request(PATCH,ContentType.JSON) {
headers."X-API-KEY" = "[API_CALL]"
headers.contentType = "application/json"
body = [
"rrsets": [
[
"name": name,
"type": "CNAME",
"ttl": 300,
"changetype":"REPLACE",
"records": [
[
"content": content,
"disabled": false
]
]
]
]
]
response.failure = { resp, reader ->
[response_failure:resp, reader_failure:reader]
}
response.success = { resp, reader ->

[response_success:resp, reader_success:reader]
}

def response_success = http["response_success"]
def response_fail = http["response_fail"]
def reader_success = http["JSON_success"]
def reader_failure = http["JSON_failure"]

if (response_success == true) {
CommentManager.create(issue, currentUser,comment_success, true)
}
else {
CommentManager.create(issue, currentUser,comment_failure, true)
}
}

 

 

What am I doing wrong? I am trying to define a variable in HTTPBuilder, swap it out of HTTPBuilder, and then input it in a comment in the issue that kicked off the post-function

1 answer

1 accepted

0 votes
Answer accepted
Marc Minten _EVS_
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.
June 20, 2019

Where do you use your script ? In a workflow ???

I think the "event" variable is only available in a Listener script...

Eric Smith
Contributor
June 20, 2019

Yes, I am calling it in a post-function

Marc Minten _EVS_
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.
June 20, 2019

...I think in that case you directly should use "issue" in stead of "event.issue"....

Eric Smith
Contributor
June 20, 2019

Are you talking about these lines?

Issue sourceIssue = event.getIssueLink().getSourceObject()
Issue issue = event.issue

 

If I remove event I dont think they are going to work. Whats the normal method to refer to the issue that is executing the transition? I usually use JMWE which uses originalIssue I believe.

Marc Minten _EVS_
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.
June 20, 2019

Yes, you can simply remove both lines. "sourceIssue" is never used in your script, and the method getIssueLink() doesn't even exist on an event, so that line is meaningless anyway.

In workflow scripts you can refer to the issue in transition by "issue"

Eric Smith
Contributor
June 20, 2019

It worked!

I must have left getIssueLink() in from a previous iteration, the Issue one was all on me as I used it before in a listener and thought it carried over. Thanks for the response!

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events