How to copy watchers to custom field (user picker - multiple) with Script Runnerc

Tomasz Brudziński June 2, 2016

Hi - I need to set a custom field for our clients users, who are not jira-software-users. We're using third party helpdesk plugin to work with them, and I need a custom scripted field to filter watchers. Client's users have "@" character in their usernames, so it's fairly easy. The problem is with User Picker templates - I can't get it to show any results.

 

I tried both single and multiple user template. Single always shows "anonymous" in script preview, and multiple basically shows nothing. Please advise. This is my script:

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.watchers.IssueWatcherAccessor
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
IssueWatcherAccessor iwa = ComponentAccessor.getComponent(IssueWatcherAccessor.class)
def list =  iwa.getWatcherKeys(issue)
List filtered = new ArrayList();
for (user in list){
    // IF/ELSE does the same for test purpose !!!
	if(user==~ /.*@.*/){
    	filtered.add(ApplicationUsers.toDirectoryUser(ApplicationUsers.byKey(user)))
    }else{
        filtered.add(ApplicationUsers.toDirectoryUser(ApplicationUsers.byKey(user)))
    }
}
List<ApplicationUser> clientWatchers = ApplicationUsers.from(filtered)
log.error clientWatchers
return clientWatchers

 

I'm loggin "clientWatchers" to see what it returns:

log.png

 

 

user picker (single user) shows Anonymous in script preview. 

 

Zrzut ekranu 2016-06-02 o 18.23.29.png

 

BUT it does exactly the same whatever I return, example: 

 

Zrzut ekranu 2016-06-02 o 18.27.52.png

 

and multiple user picker shows nothing:

 

Zrzut ekranu 2016-06-02 o 18.24.27.png

 

"test" custom field remains empty in issue view:

Zrzut ekranu 2016-06-02 o 18.21.48.png

 

Either I'm doing something wrong, or user picker templates are broken. Please advise.

1 answer

1 accepted

1 vote
Answer accepted
JamieA
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 2, 2016

Does the "log" line show the correct things?

I see this problem when passing the wrong type of User, but you seem to be using ApplicationUser, which is correct - depending on the version of JIRA. What version are you using?

I am not sure why you are converting from appuser to directory user and back again, why don't you just let "filtered" be a list of AppUsers?

In fact why don't you just filter on com.atlassian.jira.issue.watchers.IssueWatcherAccessor#getWatchers, rather than their keys?

Actually - I suspect the problem is you have the free text searcher on. Make sure you are using the Multi User Picker Searcher, otherwise it coerces the return value to be suitable for the indexer.

 

image2016-6-2 17:46:18.png

JamieA
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 2, 2016

Note - in the screenshot it shows free text searcher, but actually I changed it to multi user search in another window, then it worked properly.

Tomasz Brudziński June 2, 2016

That was it - searcher smile Thank you so much @Jamie Echlin [Adaptavist] for your prompt response and help!

JamieA
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 2, 2016

No problem. And thanks for your review Tomasz, greatly appreciated wink

Suggest an answer

Log in or Sign up to answer