• Community
  • Products
  • Jira Core
  • Questions
  • I worked on filter , which has huge number of SCRs. I tried to add watchers. But wrongly deleted existing watchers. Please let me know if there is any shortcut to undo this last operation.

I worked on filter , which has huge number of SCRs. I tried to add watchers. But wrongly deleted existing watchers. Please let me know if there is any shortcut to undo this last operation.

 

1 answer

1 accepted

The onle advice is to restore backup on test instance and get watchers list.

If you delete watcher for several users you can do it manually. If not - I could help you to write respective script.

I had around 300 scrs. For each SCR, watcher list was different. I wrongly updated each watcher list with some fixed names. I want to revert back to old one. It will be very helpful if some methods or scripts exist.

 

The idea is following: 

  • we have correct watchers lists on test instance restored from back up
  • we need to copy it from test instance to prod one

Here is code to get all watchers for specified issue list:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.util.UserManager

/**
 * Created by VZverev on 20.02.2016.
 */

//create a list of issues to get watchers
List<Issue> issueList = new ArrayList<>();
issueList.add(ComponentAccessor.getIssueManager().getIssueObject("ZVER-3"))
issueList.add(ComponentAccessor.getIssueManager().getIssueObject("ZVER-4"))

UserManager userManager = ComponentAccessor.getUserManager();

//now lets find all watchers
WatcherManager watcherManager = ComponentAccessor.getWatcherManager();
StringBuilder result = new StringBuilder();
for(Issue issue: issueList) {
    result.append("list.add(new AddWatchers(" + issue.getId() + ", [")
    for (String userKey : watcherManager.getWatcherUserKeys(issue)) {
        result.append( "\"" + userManager.getUserByKey(userKey).getKey() + "\", ")
    }
    result.append("])); ")
}

return result.toString().replace(", ]", "]")

You should run it via Script Console provided ScriptRunner plugin.

As result you will get strings to user in script for add watchers. Here it is:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.util.UserManager

/**
 * Created by VZverev on 20.02.2016.
 */

List<AddWatchers> list = new ArrayList<>()
//Plave here all strings from previous script
list.add(new AddWatchers(24910, ["vzverev@phosagro.ru"]));

//Loop to add watchers
for(AddWatchers addWatchers: list){
    addWatchers.doSetWatchers();
}

class AddWatchers{
    Issue issue;
    List<String> watchersUserKeys;
    private static WatcherManager watcherManager = ComponentAccessor.getWatcherManager();
    private static IssueManager issueManager = ComponentAccessor.getIssueManager();
    private static UserManager userManager = ComponentAccessor.getUserManager();

    public AddWatchers(long _key, List<String> _users  ){
        issue = issueManager.getIssueObject(_key);
        watchersUserKeys = _users;
    }

    public doSetWatchers(){
        for(String user: watchersUserKeys)
            watcherManager.startWatching(userManager.getUserByKey(user), issue)
    }
}

I strongly reccomend you to test it ar first on test instance.

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

592 views 2 15
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot