Groovy class code for adding JIRA groups

jaideep srivastava March 5, 2014


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 votes
Answer accepted
Henning Tietgens
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 6, 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.

jaideep srivastava March 6, 2014

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 :)

0 votes
jaideep srivastava March 6, 2014

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
TAGS
AUG Leaders

Atlassian Community Events