• Community
  • Products
  • Jira
  • 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

0 vote
Vasiliy Zverev Community Champion Feb 19, 2016

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.

 

Vasiliy Zverev Community Champion Feb 19, 2016

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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Asked 6 hours ago in Confluence

What are the resources that you use to learn more about Atlassian Products?

I am gathering information about resources available for Atlassian product knowledge transferring for a presentation in our local Atlassian User Group. I want to group them in four categories From ...

46 views 3 3
View question

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