Groovy class code for adding JIRA groups


Hi,
I am working on adding JIRA groups to watchers list. So far i am able to add an individual user to watcher list, i am also able to fetch the priority value based on that i need to add different groups. Below is the code which i have written so far. Can anyone help me with the group code ? thanks in advance






class TaskListener extends AbstractIssueEventListener {
Category log = Category.getInstance(TaskListener.class)



@Override
void workflowEvent(IssueEvent event) {
Issue issue = event.getIssue();



def watchUsers = {usernames ->
usernames.each {
def user = userManager.getUser(it)
watcherManager.startWatching(user, issue.getGenericValue())
}
}
MutableIssue mutableIssue = (MutableIssue) issue;
def priority = mutableIssue.getPriority().getString("name");
log.debug priority


//code for groups based on priority


//User user = ComponentManager.getJiraAuthenticationContext().getUser()
//log.debug user
// ComponentManager.instance.watcherManager.startWatching(user, event.issue.genericValue)
}}



2 answers

1 accepted

0 vote
Henning Tietgens Community Champion Mar 06, 2014

Are you sure you want to nest the start and stop watching loops? For each user you start, all users in the other group are stopped. I optimized the code a little bit and removed some deprecated calls.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger

import static org.apache.log4j.Level.DEBUG

class TaskListener extends AbstractIssueEventListener {
    Logger log = Logger.getLogger(TaskListener.class)
    def groupManager = ComponentAccessor.getGroupManager()
    def watcherManager = ComponentAccessor.getWatcherManager()

    @Override
    void workflowEvent(IssueEvent event) {
        Issue issue = event.getIssue();

        log.setLevel(DEBUG)
        log.debug "Event: ${event.getEventTypeId()} fired for ${issue} and caught by TaskVersionListener"

        def priority = issue.getPriorityObject().getName()
        log.debug priority

        switch (priority) {
            case 'Critical':
                stopGroupWatching('Blocker',issue)
                startGroupWatching('Critical', issue)
                break
            case 'Blocker':
                stopGroupWatching('Critical',issue)
                startGroupWatching('Blocker', issue)
                break
        }
    }

    void startGroupWatching(String groupName, Issue issue) {
        groupManager.getUsersInGroup(groupName).each {
            watcherManager.startWatching(it, issue)
        }
    }

    void stopGroupWatching(String groupName, Issue issue) {
        groupManager.getUsersInGroup(groupName).each {
            watcherManager.stopWatching(it, issue)
        }
    }
}

The start and stopWatching calls are still deprecated because they use User objects, but because the result of getUsersInGroup is a list of Users I think this is ok. If there is a time where you have to change it you have to change it only once on one place.

I switched the order of start -> stop to stop -> start because if there are users in both groups you may remove watchers which shouldn't be removed.

Thanks a lot Henning for your code :) you made my work very easy. i am new to Groovy and was not able to add the group users, when i got the solution just pasted it before optimizing the code,but you did all the work. thanks a ton :)

Hi All,

Was able to write the code for adding and removing groups from JIRA

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.comments.Comment
import org.apache.log4j.Category
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.util.ErrorCollection
import com.onresolve.jira.groovy.canned.CannedScript
import com.onresolve.jira.groovy.canned.utils.ConditionUtils
import org.apache.log4j.Category
import com.atlassian.jira.event.type.EventType;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.Issue




class TaskListener extends AbstractIssueEventListener {
Category log = Category.getInstance(TaskListener.class)



@Override
void workflowEvent(IssueEvent event) {
Issue issue = event.getIssue();


log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by TaskVersionListener"

def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()
def group = userManager.getGroup("Serious")
def watcherManager = ComponentAccessor.getWatcherManager()


def watchUsers = {usernames ->
usernames.each {
def user = userManager.getUser(it)
watcherManager.startWatching(user, issue.getGenericValue())
}
}

MutableIssue mutableIssue = (MutableIssue) issue;
def priority = mutableIssue.getPriority().getString("name");
log.debug priority


if(priority.equals("Critical"))

{

//watchUsers(group);
groupManager.getUsersInGroup("Critical").each {
watcherManager.startWatching(it, issue)

groupManager.getUsersInGroup("Blocker").each {
watcherManager.stopWatching(it, issue)

}
}
}

else if(priority.equals("Blocker"))
{

groupManager.getUsersInGroup("Blocker").each {
watcherManager.startWatching(it, issue)

groupManager.getUsersInGroup("Critical").each {
watcherManager.stopWatching(it, issue)
}
}
}



}

}

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

263 views 0 6
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