Forums

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

Scriptrunner listener watcher script

Jacob Francois March 17, 2022

I hope this is an easy one - I am testing out my script ( I know I will have to add an event issue when I finalize my script I am just testing with one issue for now) 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.AbstractEvent
import com.atlassian.jira.security.roles.ProjectRoleManager

IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("MH-5548")

def watcherManager = ComponentAccessor.getWatcherManager()
def userUtil = ComponentAccessor.userUtil
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_10625")
def system = cField.getValue(issue)

if(system == "Looker (BI)") {
userUtil.getAllUsersInGroupNames(['BI']).each { user ->
watcherManager.startWatching(user, issue)
}

} else {
return "hello"
}

 

 

For some reason when I test the value I get null. When I return system I get [Looker (BI)] which I'm assuming it is returning a value within a list. 

I'm not sure what I am doing wrong here. 

2 answers

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 Leaders.
March 17, 2022

I think it's going wrong in this block:

def system = cField.getValue(issue)

if(system == "Looker (BI)") {
userUtil.getAllUsersInGroupNames(['BI']).each { user ->
watcherManager.startWatching(user, issue)
}

The first line there is fine, but what ends up in "system" is probably not what you think iti is.  The type of field determines what object you get back from that line, and because Groovy is weakly typed, it's not complaining when you get something you are not expecting.

For example, ff the field is a multi-select, then you're going to get a list of options back, if it's a single-select, then either an option or a singleton list with one option in it.  You'll only get "Looker (BI)" from it as a string if the field is text, and even then, it's not a good idea to compare strings with ==, you should use string1.equals(string2)

So, back to the start.  What type of field is the custom field you are reading?

Jacob Francois March 17, 2022

This is a single select field 

Mike Schultz
Contributor
March 17, 2022

This works in my instance with a Select List (Single Choice) field.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.AbstractEvent
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.groups.GroupManager

IssueManager im = ComponentAccessor.getIssueManager()
GroupManager gm = ComponentAccessor.getGroupManager()
MutableIssue issue = im.getIssueObject("TEST-1234")

def watcherManager = ComponentAccessor.getWatcherManager()
def userUtil = ComponentAccessor.userUtil
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_13126")
def system = cField.getValue(issue)

log.warn(system)

if(system.toString() == "High") {
gm.getUsersInGroup('jira-administrators').each { user ->
watcherManager.startWatching(user, issue)
}

} else {
return "hello"
}
Jacob Francois March 17, 2022

@Mike Schultz - I just realize my field is a multi select field. Any thoughts ? 

Mike Schultz
Contributor
March 17, 2022

This should work then. Use log.info(system) to get the value before the block to confirm. In my case, it's [Entity One]

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.AbstractEvent
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.groups.GroupManager

IssueManager im = ComponentAccessor.getIssueManager()
GroupManager gm = ComponentAccessor.getGroupManager()
MutableIssue issue = im.getIssueObject("TEST-1234")

def watcherManager = ComponentAccessor.getWatcherManager()
def userUtil = ComponentAccessor.userUtil
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_55522")
def system = cField.getValue(issue)

log.warn(system)


if(system.toString().equals("[Entity One]")) {
gm.getUsersInGroup('jira-administrators').each { user ->
watcherManager.startWatching(user, issue)
}

} else {
return "hello"
}
Jacob Francois March 17, 2022

@Mike Schultz - This doesn't work. when I add more values it gives me a null. 

Mike Schultz
Contributor
March 18, 2022
if(system.toString().contains("Entity One")) {
0 votes
Mike Schultz
Contributor
March 17, 2022

Try this and let me know.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.AbstractEvent
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.groups.GroupManager

IssueManager im = ComponentAccessor.getIssueManager()
GroupManager gm = ComponentAccessor.getGroupManager()
MutableIssue issue = im.getIssueObject("MH-5548")

def watcherManager = ComponentAccessor.getWatcherManager()
def userUtil = ComponentAccessor.userUtil
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_10625")
def system = cField.getValue(issue)

if(system.toString() == "[Looker (BI)]") {
gm.getUsersInGroup('BI').each { user ->
watcherManager.startWatching(user, issue)
}

} else {
return "hello"
}
Jacob Francois March 17, 2022

Still doesn't work. I've tried using toString() in different places and it never worked. @Mike Schultz 

Jacob Francois March 17, 2022
Mike Schultz
Contributor
March 17, 2022

@Jacob Francois If you add a logging statement within the if block, does it trigger?

if so, you may need to apply the changes to the issue. With something like: 

import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.event.type.EventDispatchOption

def AuthContext=ComponentAccessor.getJiraAuthenticationContext()

updateIssueRequest = UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.DO_NOT_DISPATCH).sendMail(false).build()

issueManager.updateIssue(AuthContext.getLoggedInUser(),
mutableIssue,
updateIssueRequest)
Jacob Francois March 17, 2022

@Mike Schultz when I run the code I get a null return(or hello). I don't think the issue is with what is inside the if or else statement. the problem is that system == "looker (bi) goes to else when I know it is true. 

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 Leaders.
March 17, 2022

Ok, as I mentioned, a single-select list won't hold a string, they hold an option.

To compare an option with a string, you probably want the name of the option, not the whole option object.

Jacob Francois March 17, 2022

Any thoughts on how to do that @Nic Brough -Adaptavist- - also the field is a multi select. 

Suggest an answer

Log in or Sign up to answer