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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,561
Community Members
 
Community Events
176
Community Groups

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

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

0 votes
Answer accepted

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.

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

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events