auto assign labels based on the user group

Georgiy Senenkov May 20, 2012

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

2 votes
Answer accepted
parthiban subramaniam
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.
May 20, 2012

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

Georgiy Senenkov May 20, 2012

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?

JamieA
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.
May 21, 2012

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.

Georgiy Senenkov May 21, 2012

Thank you for advise!

I used

import com.atlassian.jira.component.ComponentAccessor

authenticationContext = ComponentAccessor.getJiraAuthenticationContext()

and everything works!

0 votes
Frits van der Holst November 18, 2015

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

0 votes
Fabrizio Galletti
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.
September 22, 2013

can u post the cmplete script? could be usefull thanks

Georgiy Senenkov September 29, 2013
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)
}

Nikhil May 9, 2019

Thank you for the script, I am using this script but facing this error can you please help me out.

ZENDESKPFscripterror.PNG

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events