New to Scriptrunner. Developing script to update custom field value from last issue comment using RegEx. The target string format in the comment is "Ticket#123456". Getting error result when testing in Scriptrunner console
class com.riadalabs.jira.plugins.insight.common.exception.GroovyInsightException
GroovyInsightException: No such property: event for class: Script1'
Would appreciate any information on the nature of the error.
Thanks!
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.opensymphony.workflow.InvalidInputException
import java.util.regex.Matcher
import java.util.regex.Pattern
// Event listener - listening for the Issue Commented event
def event = event as IssueEvent
def issue = event.issue as Issue
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "TicketNum"}
def changeHolder = new DefaultIssueChangeHolder()
// Run RegEx against last comment to find ticket number
if (comment) {
// String to be scanned
String lastcomment = comment.body
// Regex patterns
String regex1 = "Ticket#[0-9]+"
String regex2 = "[0-9]+"
// Create pattern1 object
Pattern r1 = Pattern.compile(regex1)
// Create matcher1 object
Matcher matcher1 = r1.matcher(lastcomment)
if (matcher1.find( )) {
String result1 = matcher1
// Create pattern2 object
Pattern r2 = Pattern.compile(regex2)
// Create matcher2 object
Matcher matcher2 = r2.matcher(result1)
if (matcher2.find( )) {
String result2 = matcher2
}
// Assign result2 value to custom field "TicketNum"
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), "result2"),changeHolder)
}
}
Figured it out with some help. Added logging (now removed) and better string manipulation and ran the script as a Listener rather than from the Scriptrunner console. Interestingly, even though the listener works, it still errors in the console. Script shown below with resources used to build it.
From an event listener
https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/jira-service-desk.html#_from_an_event_listenerUpdate Custom Field Value using a “Scriptrunner for Jira” Custom Listener
https://community.atlassian.com/t5/Answers-Developer-Questions/Update-Custom-Field-Value-using-a-Scriptrunner-for-Jira-Custom/qaq-p/503643Apache Groovy Documentation
http://docs.groovy-lang.org
http://docs.groovy-lang.org/docs/groovy-1.7.2/html/groovy-jdk/index-all.html
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/regex/Matcher.html
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.MutableIssue
import java.util.regex.Matcher
import java.util.regex.Pattern
def event = event as IssueEvent
def issue = event.issue as Issue
def comment = event.getComment()
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Ticket Number"}
def changeHolder = new DefaultIssueChangeHolder()
// Event listener - listening for the Issue Commented event
// Run RegEx against last comment to find ticket number
// Target string format in comment body is Ticket#123456
if (comment) {
// String to be scanned
String lastcomment = comment.body
String regex1 = "Ticket#[0-9]+"
String regex2 = "[0-9]+"
// Create pattern1 object
Pattern r1 = Pattern.compile(regex1)
// Create matcher1 object
Matcher matcher1 = r1.matcher(lastcomment)
if (matcher1.find( )) {
String result1 = matcher1.getAt([0])
// Create pattern2 object
Pattern r2 = Pattern.compile(regex2)
// Create matcher2 object
Matcher matcher2 = r2.matcher(result1)
if (matcher2.find( )) {
String result2 = matcher2.getAt([0])
result2 = result2.minus("[").minus("]")
// Assign result2 value to custom field "SSI Ticket"
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), result2),changeHolder)
}
}
}
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.