Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need help to migrate from JMWE 4.0.6 to 5.7.0

Bertrand_DELAITRE December 13, 2018

Here is a script that works with JMWE 4.0.6 but not with 5.7.0

import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;

log.warn("JMWE post-fct -- Submitted-->Analyse-->Analysed -- Set Originator kind in accordance to Originator groups (cutomer, internal, private)");

// Retrieve the Originator customField
final CustomField originatorCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Originator");
// Retrieve the issue's Originator
final User originator = (User) issueObject.getCustomFieldValue(originatorCF).getDirectoryUser();
// Retrieve the 3 possible CHARM groups
final Group gcustomer = ComponentAccessor.getGroupManager().getGroupObject("customer");
final Group ginternal = ComponentAccessor.getGroupManager().getGroupObject("internal");
final Group gprivate = ComponentAccessor.getGroupManager().getGroupObject("private");


Group groupToSet = gcustomer;
// Set the target group depending on the Originator's groups
if (ComponentAccessor.getGroupManager().isUserInGroup(
originator
, gcustomer)) {
   groupToSet = gcustomer;
}
if (ComponentAccessor.getGroupManager().isUserInGroup(originator, ginternal)) {
    groupToSet = ginternal;
}
if (ComponentAccessor.getGroupManager().isUserInGroup(originator, gprivate)) {
   groupToSet = gprivate;
}

// Return the value
if (groupToSet != null) {
   log.warn("Setting Originator kind to " + groupToSet.getName());
   return groupToSet.getName();
}

 

In JMWE, I've got an exception:

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.security.groups.RequestCachingGroupManager.isUserInGroup() is applicable for argument types: (com.atlassian.jira.user.BridgedDirectoryUser, com.atlassian.crowd.embedded.impl.ImmutableGroup) values: [U2:1, com.atlassian.crowd.embedded.impl.ImmutableGroup@21ffc6bd]

It seems that the following line doesn't return a "valid" user for the isUserInGroup function

(User) issueObject.getCustomFieldValue(originatorCF).getDirectoryUser()

 

Can anyone help ?

2 answers

1 accepted

1 vote
Answer accepted
David Fischer
Community Champion
December 13, 2018

The reason why this script is not working anymore is not because of the JMWE version change, but because you are moving from Jira 6 to Jira 7. In Jira 7, the User interface was replaced with the ApplicationUser interface, and thus any method that returned or expected a User object now returns or expects an ApplicationUser object.

Another reason why the script broke is because you are not taking advantage of JMWE's specific APIs.

You could rewrite your script as follows:

def originator = issue.get("Originator")
if (ComponentAccessor.getGroupManager().isUserInGroup(originator,"private"))
return "private"
if (ComponentAccessor.getGroupManager().isUserInGroup(originator,"internal"))
return "internal"
if (ComponentAccessor.getGroupManager().isUserInGroup(originator,"customer"))
return "customer"
return null

 However, that script returns a group name, not the group itself. Is that what you wanted?

Bertrand_DELAITRE December 13, 2018

I will try in few days. Thanks for answer

Bertrand_DELAITRE December 17, 2018

Works like a charm. Thanks

0 votes
Christos Moysiadis
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.
December 13, 2018

i really can not understand this originator thing.... could you be more specific? (thats why i left it as it was)

Although i made some changes that make the code for at least a bit more readable.. sorry for the changes

 

import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserUtil;


def GroupManager groupManager;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
groupManager = ComponentAccessor.getComponentOfType(GroupManager.class)

def Issue issue
// Retrieve the Originator customField
def originatorCF = customFieldManager.getCustomFieldObjectByName("Originator");

// Retrieve the issue's Originator
//final User originator = (User) issueObject.getCustomFieldValue(originatorCF).getDirectoryUser();

// Retrieve the 3 possible CHARM groups
def gcustomer = groupManager.getGroup("customer");
def ginternal = groupManager.getGroup("internal");
def gprivate = groupManager.getGroup("private");

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Group groupToSet = gcustomer;
// Set the target group depending on the Originator's groups
if (groupManager.isUserInGroup(currentUser, gcustomer)) {
groupToSet = gcustomer;
}

// Return the value
if (groupToSet != null) {
log.warn("Setting Originator kind to " + groupToSet.getName());
return groupToSet.getName();
}else if (groupManager.isUserInGroup(originator, ginternal)) {
groupToSet = ginternal;
}else if (groupManager.isUserInGroup(originator, gprivate)) {
groupToSet = gprivate;
Bertrand_DELAITRE December 13, 2018

Thanks for answer.

 

"originator" is a custom field that contains an user.

I've to set the field "Originator Kind" according the first group in "customer", "internal", "private" the user belongs to.

So i'm trying to retrieve the "originator" user in the variable named "originator", then call the "groupManager.isUserInGroup" to check if the user belongs to a group.

But, the "originator" variable seems to have a bad type for the isUserInGroup function. That's my problem

Christos Moysiadis
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.
December 13, 2018
// Retrieve the issue's Originator
//final User originator = (User) issueObject.getCustomFieldValue(originatorCF).getDirectoryUser();
def originatorCF = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject(11625l)).toString() // 11625l is the customfield by id
log.error("originatorCF: " + originatorCF)

in my case im retriving a number... make the appropriate changes for User 

Also.... //11625l is the customfield by id in my case... , your custom field has other value

and use the log. more often in the values that you retrieve to have an overview all the time.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events