You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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.
@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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.