auto assign labels based on the user group

Hello,

I have groovy post-function script which auto assign predefined labels to the issue. The assignment is based on the infomation that user belongs to the particular user group.

The script works fine in JIRA4.3 but in JIRA5.0 throws following error

2012-05-21 13:57:33,472 http-80-ERROR admin 837x7696x1 18v94zm 10.161.201.37 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.crowd.embedded.ofbiz.OfBizUser.getGroups() is applicable for argument types: () values: []
Possible solutions: getClass()6

The code looks like

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.fields.LabelsSystemField
import com.atlassian.jira.issue.label.LabelParser


//Add here mapping from groups to labels -- labels from all groups will be joined
def groupToLabels = ["Group 1":["label11", "label12"],
		 "Group 2":["label21", "label22"]
]

def componentManager = ComponentManager.getInstance()
def authContext = componentManager.getJiraAuthenticationContext()
def issueManager = componentManager.getIssueManager()
def user = authContext.getUser()
def addLabels = new HashSet()

for (group in user.getGroups()) {
    if (groupToLabels[group])
	addLabels.addAll(LabelParser.buildFromString(groupToLabels[group].join(LabelsSystemField.SEPARATOR_CHAR)))
}
if (addLabels.size() > 0) {
    if(issue.getLabels())
	addLabels.addAll(issue.getLabels())
    issue.setLabels(addLabels)
    issueManager.updateIssue(authContext.getUser(), issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

Could you please what is wrong in the script?

Thank you in advance.

Regards, Georgiy

3 answers

1 accepted

This widget could not be displayed.

All user related api went under lots of change for JIRA 5

check the java api and see the methods you are trying to use is still there and not removed

here is the developer notes

http://confluence.atlassian.com/display/JIRA/Plugin+Developer+Notes+for+JIRA+4.3

http://confluence.atlassian.com/display/JIRA/Plugin+Developer+Notes+for+JIRA+5.0

in the following thread I found that getGroups() is deprecated and GroupManager should be used instead

https://answers.atlassian.com/questions/37977/jiraauthenticationcontext-changes-to-the-jira-sdk-3-7-3

hence i tried the following code

import com.atlassian.jira.security.groups.GroupManager

GroupManager usersGroup = (GroupManager) ComponentManager.getComponentInstanceOfType(GroupManager.class);

String currentUser= authenticationContext.getLoggedInUser().toString();
String currentUserGroups = usersGroup.getGroupNamesForUser(currentUser).toString();

which should just get current user and list of groups where the user are, but it returns the error

2012-05-21 17:10:17,189 http-80-2 ERROR admin 1030x8516x1 18v94zm 10.161.201.37 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: authenticationContext for class: Script24
2012-05-21 17:10:17,189 http-80-2 ERROR admin 1030x8516x1 18v94zm 10.161.201.37 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: authenticationContext for class: Script24

do I miss some import?

You can use: ComponentManager.getInstance().getJiraAuthenticationContext() instead of authcontext, or assign it. You didn't miss any import, but you don't get an authenticationContext for free.

Thank you for advise!

I used

import com.atlassian.jira.component.ComponentAccessor

authenticationContext = ComponentAccessor.getJiraAuthenticationContext()

and everything works!

This widget could not be displayed.

can u post the cmplete script? could be usefull thanks

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.fields.LabelsSystemField
import com.atlassian.jira.issue.label.LabelParser

import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.component.ComponentAccessor

//Add here mapping from groups to labels -- labels from all groups will be joined
def groupToLabels = ["Group1":["Label1", "Label2"],
		 "Group2":["Label1", "Label3"],
		 "Group3":["Label4"]
]

GroupManager usersGroup = (GroupManager) ComponentManager.getComponentInstanceOfType(GroupManager.class);

authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
currentUser= authenticationContext.getLoggedInUser()
currentUserGroups = usersGroup.getGroupNamesForUser(currentUser)

def issueManager = componentManager.getIssueManager()
def addLabels = new HashSet()

for (group in currentUserGroups) {
    if (groupToLabels[group])
	addLabels.addAll(LabelParser.buildFromString(groupToLabels[group].join(LabelsSystemField.SEPARATOR_CHAR)))
}

if (addLabels.size() > 0) {
    if(issue.getLabels())
	addLabels.addAll(issue.getLabels())
    issue.setLabels(addLabels)
	issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

This widget could not be displayed.

This is exactly what I was looking for. However, it does not seem to work....I do get an error in the code which could cause this?

 

2015-11-19 09_37_17-Script Listeners - Jira - TASS International.png

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

460 views 3 17
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