Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Appending all watchers to a multiple-user picker (Script Console) for all issues in a given project

bschmi January 27, 2022

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

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 27, 2022

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:

  • customField.updateValue
    • Stores the value of a single customfield to the database
    • No issue history created
    • Issue index is not updated
  • issueManager.updateIssue
    • Stores all the values of the issue object to the database
    • Creates the appropriate issue history entries
    • updated date is refreshed
    • issue updated event dispatched (email optional)
    • Issue index is not updates
  • issueService.update
    • Validates user has permission to update
    • Stored the values of the issueUpdateParameter to the database
    • updated date is refreshed
    • Creates the appropriate issue history entries
    • issue updated event dispatched (email optional)
    • Issue Index is updated

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.

bschmi January 27, 2022

@Peter-Dave 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!

Dinesh Loyapalli March 20, 2023

@Peter-Dave Sheehan 

 

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)" }

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 21, 2023

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)
}
Dinesh Loyapalli April 12, 2023

@Peter-Dave 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.

Capture.PNG

TAGS
AUG Leaders

Atlassian Community Events