Script Listener - ONLY Run on Issue Type ("A") AND Linked to Unresolved Issue Type ("B") - CLOUD

Emily Guadalupe December 11, 2018

I want a script that listens for changes to a particular field (in this case "due date") and then sends an email to specific users notifying them that the due date changed on that issue. But I want to restrict it to run only when it meets the following criteria:

 

 1) If issue is a specific issue type (example: "Issue Type A")

AND

2) If issue is linked to issues of a different issue type (example: "Issue Type B")

AND

3) If issue is linked to said issue type, the linked issues are unresolved

 

To be extra clear, I do NOT care about the "link type" (ex: "relates to," "blocks," etc.). I only care about the issue type of the linked issue (and only if it's unresolved).

Also, ScriptRunner's support team mentioned that this is possible by using "an IF/Elsestatement in your script where you check if the matches a certain issue type and to check if it has an issue link to a certain issue type with a certain status if you made a call to get all linked issues before the if statement." But that's all the information they could give me since they do not provide assistance with custom scripts.

I've got some code started but I'm new to Groovy/ScriptRunner and I would appreciate any help! 

 

import groovy.xml.MarkupBuilder

if (!issue.fields.issuetype.id == '11000') { // Only run script if issue is 'Issue Type A' AND Here's where I need something about checking to make sure the linked issues have a specific issue type ('Issue Type B') and only return if they're unresolved
return
}

def fieldIdsOfInterest = 'duedate' // Due Date field

def histories = get(issue.self)
.queryString('expand', 'changelog')
.asObject(Map)
.body
.changelog
.histories

def field = histories.isEmpty() ? null : histories
.first()
.items
.find {fieldIdsOfInterest.contains(it['fieldId']) }

if (!field) {
logger.info("Due date was not changed on issue {}.", issue.key)
return
}
logger.info ("'{}' field changed from {} to {} on issue {}", field.field, field.from, field.to, issue.key)

if (field != null) {
def writer = new StringWriter()
// Note that markup builder will result in static type errors as it is dynamically typed.
// These can be safely ignored
def markupBuilder = new MarkupBuilder(writer)
markupBuilder.div {
p {
a(href: "https://company.atlassian.net/browse/${issue.key}", issue.key)
span("'s ${field.field} was changed from ${field.from} to ${field.to} by ${user.displayName}.")
}
}
def htmlMessage = writer.toString()
def textMessage = new XmlSlurper().parseText(htmlMessage).text()
def resp = post("/rest/api/2/issue/${issue.id}/notify")
.header("Content-Type", "application/json")
.body([
subject: 'Due Date Changed',
textBody: textMessage,
htmlBody: htmlMessage,
to: [
users: [[name: "example.user"]]
]
])
.asString()
assert resp.status == 204
logger.info("Due date change notification email sent to Example User.")
logger.info("{}'s {} field was changed from {} to {} by {}.", issue.key, field.field, field.from, field.to, user.displayName)
} else {
def changedField = histories.isEmpty() ? null : histories
.first()
.items
logger.info("Due date was not changed on ${issue.key}. Instead (changed field was ${changedField.field}, fieldId: ${changedField.fieldId}), no notification was sent.")
}

 

1 answer

0 votes
fjodors
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.
December 12, 2018

Hi

If you need to check issue type in script listener I think you can use getIssueType method.


def issueToCompare = ... //define your issue object, it should be Issue or MutableIssue M
def acceptedIssueTypes = ["TypeA", "TypeB"];
if(issueToCompare.getIssueType().name in (acceptedIssueTypes)){
    //do something
    //run code, send email, etc
}
Emily Guadalupe December 12, 2018

Thanks @fjodors! I'll give that a try!

Emily Guadalupe December 17, 2018

Unfortunately, I wan't able to get it working.

fjodors
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.
December 19, 2018

You need to check your code why it's not working

I suggest you to enable logging and print your values.

Below is a fragment from my custom script listener, it should log a username (user key) of user who performed this action (event).


....
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Category
...

Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.INFO)
ApplicationUser currentUser=event.getUser();


log.info("currentuser key is ...")
log.info(currentUser.getKey());
Emily Guadalupe December 21, 2018

@fjodors Thank you for providing that information. I added that to my script but I received this error. 

2018-12-21 17:48:10.225 ERROR - startup failed:
Script1.groovy: 4: unable to resolve class com.atlassian.jira.user.ApplicationUser
 @ line 4, column 1.
   import com.atlassian.jira.user.ApplicationUser
   ^

1 error

2018-12-21 17:48:10.307 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null 

Suggest an answer

Log in or Sign up to answer