Hi Community,
for a one-off task I would be looking for a way to copy all watchers of all issues in one specific project to an already populated custom field of type user picker (multiple).
For example in project TEST watchers alice, bob should be appended to executing (custom field, user picker (multiple)) where already charlie is in - so the content of the field is afterwards alice, bob, charlie.
Using "Copy Field Values" I was not able to preserve "charlie" in the example above.
Further a crucial requirement would be to not touch the updated timestamp, also "Copy Field Values" covers that but as values of the custom field are overwritten but not appended I cannot use it.
As this will be a one-off I thought about using "Script Console" but I am not sure.
Is there an easy way to achieve the requirement?
The environment is: Script Runner (latest version possible to enroll!) on a Jira Server 8.20.
Thanks in advance and best regards,
Birgit
I think doing this in the console is the correct approach.
Here is a script I quickly put together (not quite fully tested), so verify in a staging environment:
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.web.bean.PagerFilter
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def watcherManager = ComponentAccessor.watcherManager
def searchService = ComponentAccessor.getComponent(SearchService)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def projectKey = "XXX"
def userCfName = "name"
def usersCf = customFieldManager.getCustomFieldObjectsByName(userCfName)[0]
def query = searchService.parseQuery(currentUser, "project = $projectKey").query
searchService.search(currentUser, query, PagerFilter.unlimitedFilter).results.each{
def issue = issueManager.getIssueObject(it.id)
def currentUserList = issue.getCustomFieldValue(usersCf) as List
def watchers = watcherManager.getWatchersUnsorted(issue)
def newUserList = currentUserList + watchers
usersCf.updateValue(null, issue,new ModifiedValue(currentUserList,newUserList.unique() ), new DefaultIssueChangeHolder())
//update the JIRA index
def wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
The trick to avoiding the Issue Updated date to be refreshed is to use the lower-level api customField.updateValue() method instead of issueManager.updateIssue or issueService.updateIssue.
As a primer here is the difference between the apis:
The recommendation is to try to use those three in the reverse order. Favor issueService over issueManager and use customField.updateValue as a last resort.
But since that's the only one that skips the refresh of the Updated date, that seems like the right choice for you.
Let me know if adding issue history item is important, that can be added.
@PD Sheehan that worked out perfectly. That is so amazing!
Also a big thank you for the thorough explanation.
Of course, I will accept this splendid answer and wish you a great weekend ahead!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Peter,
Hope you are doing good!
I am receiving the below error when running the above script to copy all watchers to a custom field of type user picker(multiple).
Can you please help me in resolving the issue.
2023-03-20 10:25:19,288 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2023-03-20 10:25:19,288 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueWatcherAddedEvent, file: null
java.lang.NullPointerException: Cannot execute null+
at Script3$_run_closure1.doCall(Script3.groovy:25)
at Script3.run(Script3.groovy:21)
Payload :
{ "projects": " (java.util.ArrayList)", "@class": "com.onresolve.scriptrunner.canned.jira.workflow.listeners.model.CustomListenerCommand (java.lang.String)", "issue": " (com.atlassian.jira.issue.IssueImpl)", "log": "org.apache.log4j.Logger@60ae7f07", "friendlyEventNames": "IssueWatcherAddedEvent, IssueWatcherDeletedEvent (java.lang.String)", "version": "10 (java.lang.Integer)", "relatedProjects": "[[key:, name:]] (java.util.ArrayList)", "name": "Custom listener (java.lang.String)", "canned-script": "com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener (java.lang.String)", "id": "faaf1d02-18a9-41cd-9479-ecab72c652c2 (java.lang.String)", "event": "com.atlassian.jira.event.issue.IssueWatcherAddedEvent@eee6048c", "\u00a3beanContext": "com.onresolve.scriptrunner.beans.BeanFactoryBackedBeanContext@32ec6597", "events": "[com.atlassian.jira.event.issue.IssueWatcherAddedEvent, com.atlassian.jira.event.issue.IssueWatcherDeletedEvent] (java.util.ArrayList)" }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The important lines to me are:
Cannot execute null+[JSothoron(jsothoron), LTaylor1(ltaylor1)
at Script3$_run_closure1.doCall(Script3.groovy:25)
Script3.groovy:25 means that the error is on line 25.
Which matches what we see in the error as "null+[JSothoron(jsothoron), LTaylor1(ltaylor1)"
Because line 25 was:
def newUserList = currentUserList + watchers
What this tells us is that "currentUserList" was returned as null... meaning there were not values in the userCf.
So we can fix it by changing line 23 to
def currentUserList = issue.getCustomFieldValue(usersCf) as List ?: []
What this will do is when the field is empty, the currentUserList will be initialized as an empty array.
Then, when we try to add the watchers to the currentUserList to make the newUserList, we will be adding content of an array to another array (possibly empty).
This lead to possibly needing an extra bit of logic... what if there are no watchers?
We should probably skip issues without watchers.
We can do that by adding:
if(!watchers) return
Here is a fully updated script:
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.web.bean.PagerFilter
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def watcherManager = ComponentAccessor.watcherManager
def searchService = ComponentAccessor.getComponent(SearchService)
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def projectKey = "XXX"
def userCfName = "name"
def usersCf = customFieldManager.getCustomFieldObjectsByName(userCfName)[0]
def query = searchService.parseQuery(currentUser, "project = $projectKey").query
searchService.search(currentUser, query, PagerFilter.unlimitedFilter).results.each {
def issue = issueManager.getIssueObject(it.id)
def currentUserList = issue.getCustomFieldValue(usersCf) as List ?: []
def watchers = watcherManager.getWatchersUnsorted(issue)
if (!watchers) return //no watchers nothing to do
def newUserList = currentUserList + watchers
usersCf.updateValue(null, issue, new ModifiedValue(currentUserList, newUserList.unique()), new DefaultIssueChangeHolder())
//update the JIRA index
def wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@PD Sheehan Thank you so much, we were able to append users dynamically to the custom user picker when watches are added to an issue. But when watcher is deleted from an issue, the custom picker is not getting updated with the latest user list.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.