groovy script issue

this days i tested about jira upgrade from ver 4.4.5(standalon, redhat) to 5.0.5(war, open suse, vm enviroment)

there is a groovy script issue.

this script is work well at jira version 4.4.5. but make a issue at jira version 5.0.5

//===========================================================

import com.atlassian.jira.user.util.UserUtilImpl

//import com.atlassian.core.user.GroupUtils

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

import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.workflow.WorkflowContext

//log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
//log.debug ("test " + "test")
//log.error "error"

def allowGroupStr = "crowd-project"
def addUsers = "User to grant"
def addGroups = "Group to add"
String commentValue = ""

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

CustomField customFieldaddUsers = customFieldManager.getCustomFieldObjectByName( addUsers )
println "***" + customFieldaddUsers +"***"
addUsersFieldVal = issue.getCustomFieldValue( customFieldaddUsers )
println "***" + addUsersFieldVal + "***"
CustomField customFieldaddGroups = customFieldManager.getCustomFieldObjectByName( addGroups )
addGroupsFieldVal = issue.getCustomFieldValue( customFieldaddGroups )

//IssueFactory issueFactory = componentManager.getIssueFactory()
//MutableIssue issue = issueFactory.getIssue()
//User currentUser = componentManager.getJiraAuthenticationContext().getUser()
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();

println "*** test code ****"
User userToAdd1 = UserUtils.getUser( "test11" )

println "***test" + userToAdd1 +" **test**"

===========================================================//

from this script

fail to getCustomFieldValue and getUser.

the error log is

//============================================================

***User to grant***
***[com.atlassian.crowd.embedded.ofbiz.OfBizUser@ab3680ae]***
*** test code ****
***testcom.atlassian.crowd.embedded.ofbiz.OfBizUser@ab3680ae **test**

=============================================================//

I don't know why this error message is printed.

do you know the solution?

plz help me :)

2 answers

There are some API changes in 5.0 onwards . Have a look at this .

You will have to update your script accordingly to the updated API

Hi Jiyoung,

I have made some changes in your code to make it run on Jira 5

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.crowd.embedded.api.User;


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

import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.workflow.WorkflowContext


import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.issue.fields.CustomField;

import com.atlassian.jira.issue.Issue;



 

//log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
//log.debug ("test " + "test")
//log.error "error"


def allowGroupStr = "crowd-project"
def addUsers = "User to grant"
def addGroups = "Group to add"
String commentValue = ""

// get all managers if possible using ComponentAccessor
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

// please use this in Jira 5
GroupManager groupManager = ComponentAccessor.getGroupManager()

CustomField customFieldaddUsers = customFieldManager.getCustomFieldObjectByName( addUsers )
println "***" + customFieldaddUsers +"***"
List addUsersFieldVal = issue.getCustomFieldValue( customFieldaddUsers )
addUsersFieldVal.each() {println "*** " + it.getName()}

println "***" + addUsersFieldVal + "***"
CustomField customFieldaddGroups = customFieldManager.getCustomFieldObjectByName( addGroups )
addGroupsFieldVal = issue.getCustomFieldValue( customFieldaddGroups )

//IssueFactory issueFactory = componentManager.getIssueFactory()
//MutableIssue issue = issueFactory.getIssue()
//User currentUser = componentManager.getJiraAuthenticationContext().getUser()

User currentUserObject = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();

println "*** test code ****"
User userToAdd1 = ComponentAccessor.getUserUtil().getUserObject("test11" )

// use getName(). toString() is not implemented anymore for crowd user objects
println "***test" + userToAdd1.getName() +" **test**"
println "***current user" + currentUserObject.getName() +" **current user**"

thanks so much. I solve that issue ^^

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,723 views 17 21
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