Forums

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

How to send HTTP post Request via Scriptrunner Post function to receive JSON response

srikanth kocherlakota
January 21, 2020

Hi Everyone,

 We have done the integration from jira to pager duty.

When the ticket is created in jira, Automatically it should trigger the event in the pagerduty.

While adding the scripted post function during create transition getting below errors,

The variable [body] is undeclared, [variable response is undeclared] and No such property:messages for class: java.lang.object..

the following script as follows:

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

if(issue.issueType.name=="ZIA OPS Escalation"){

def http = new HTTPBuilder("https://events.pagerduty.com/generic/2010-04-15/create_event.json")
def summary = issue.summary
def issueDetails = "ZIA OPS Escalation " + issue.key
def body
def issueJson = http.request(Method.POST, ContentType.JSON) {
body [ "service_key": "77011ebbefad47dd987f045636e04390","event_type": "trigger", "description": "summary", "details": "issueDetails"]
response.success = { resp, json ->
log.debug("PagerDuty Trigger Successful = " + json.messages)
}
response.failure = { resp, json ->
log.debug("PagerDuty Trigger Failed = " + json.messages)
}
}
}

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Anzar
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 Champions.
May 5, 2022

A similar code is mentioned here:

https://community.atlassian.com/t5/Jira-questions/Updating-a-custom-field-from-a-script-listener/qaq-p/759053

 

As @Nic Brough -Adaptavist- mentioned you can tweak the code by getting the logged in user and setting the 'modifed by' custom field. You can get the logged in user by:

def LoggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 

Hope that helps.

Regards,

Anzar Khan

Anzar
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 Champions.
May 5, 2022
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue

def issue = event.issue as Issue
def desiredValue = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() //TODO: Change this to be the actual calculation you want
def customFieldName = "Modified By" //TODO: Change this to match the custom field you want
def customFieldManager = ComponentAccessor.customFieldManager
def field = customFieldManager.getCustomFieldObjects(issue).find{
it.name == customFieldName
}
def currentCustomFieldValue = issue.getCustomFieldValue(field)
if (currentCustomFieldValue == desiredValue) {
return //Quit -- we don't want to update the field if it's already correct.
}
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.addCustomFieldValue(field.id, desiredValue)
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def validationResult = issueService.validateUpdate(loggedInUser,
issue.id,
issueInputParameters
)
if (validationResult.isValid()) {
def updateResult = issueService.update(loggedInUser,
validationResult,
EventDispatchOption.DO_NOT_DISPATCH, //This is important to avoid infinite loops
false)
if (!updateResult.isValid()) {
log.error "Failed to update issue $issue.key"
log.error updateResult.errorCollection.errorMessages
log.warn updateResult.warningCollection.warnings
}
} else {
log.error "Failed to update issue $issue.key"
log.error validationResult.errorCollection.errorMessages
log.warn validationResult.warningCollection.warnings
}
0 votes
Nic Brough -Adaptavist-
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 Champions.
May 4, 2022

I'm sorry, I do not understand the question.

It sounds like you want to read a field from some issues, but JQL is about finding issues, not reading fields, so I'm lost on that.  And what "user name" part of the "modified by" - which field are you actually interested in and which user name?

Use of SRs lastupdated  function is documented at https://docs.adaptavist.com/sr4js/latest/get-started/tutorials/jql-functions-tutorial near the bottom of the page.

Garden16_
May 5, 2022

I have a field by name Modified By . Any  time the test  issue type is updated or edited. The Modified by field should display the persons name who modified it.

Looking for solutions using script runner.

Nic Brough -Adaptavist-
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 Champions.
May 5, 2022

So nothing to do with JQL or searching.

You could write a scripted field to drag it out of the history and comments, or you could write a listener to pick up all the change events (edit, assign, comment, transition, etc) and populate the field with the person who triggered the event.

Like Anzar likes this
Garden16_
May 5, 2022

Please share scripted field code 

Garden16_
May 9, 2022

Modified By.PNG                                      Getting error . I created a text custom field by name  'Modified By' and created a listener  as suggested by you. Configured Modified By to Test and Test set issue type. Getting error in the script

TAGS
AUG Leaders

Atlassian Community Events