Last active
September 4, 2023 09:11
-
-
Save jechlin/9722753 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package examples.usermgr | |
import com.atlassian.jira.bc.JiraServiceContextImpl | |
import com.atlassian.jira.bc.group.GroupRemoveChildMapper | |
import com.atlassian.jira.bc.group.GroupService | |
import com.atlassian.jira.component.ComponentAccessor | |
import com.atlassian.jira.issue.Issue | |
import com.opensymphony.workflow.loader.WorkflowDescriptor | |
/* CONFIGURABLE SECTION */ | |
def parentGroupFieldName = "Parent Group" | |
def childGroupFieldName = "Child Group" | |
def addActionName = "Add Group to group" | |
def removeActionName = "Remove From Group" | |
/* END CONFIGURABLE SECTION */ | |
def wd = transientVars["descriptor"] as WorkflowDescriptor | |
def actionId = transientVars["actionId"] as Integer | |
def isRemove = wd.getAction(actionId).getName().equalsIgnoreCase(removeActionName) | |
def groupService = ComponentAccessor.getComponent(GroupService.class) | |
def user = ComponentAccessor.getJiraAuthenticationContext().getUser() | |
def serviceContext = new JiraServiceContextImpl(user) | |
def customFieldManager = ComponentAccessor.getCustomFieldManager() | |
Issue issue = issue | |
def parentGroupCf = customFieldManager.getCustomFieldObjectByName(parentGroupFieldName) | |
def childGroupCf = customFieldManager.getCustomFieldObjectByName(childGroupFieldName) | |
if (! (childGroupCf && parentGroupCf)) { | |
log.warn ("Custom fields not set up correctly") | |
return | |
} | |
// Both fields contain List<String (name of group)> | |
def parentGroup = issue.getCustomFieldValue(parentGroupCf).value as String | |
def childGroup = issue.getCustomFieldValue(childGroupCf).value as String | |
if (isRemove) { | |
GroupRemoveChildMapper mapper = new GroupRemoveChildMapper([parentGroup]); | |
mapper.register(childGroup); | |
def validateRemoveGroupsFromGroups = groupService.validateRemoveGroupsFromGroups(serviceContext, mapper) | |
if (! validateRemoveGroupsFromGroups) { | |
def errorCollection = serviceContext.errorCollection | |
log.warn ("Could not remove child groups: ${errorCollection}") | |
} | |
else { | |
groupService.removeGroupsFromGroups(serviceContext, mapper) | |
} | |
} | |
else { | |
def groupValidationResult = groupService.validateAddGroupsToGroup(serviceContext, [parentGroup], [childGroup]) | |
if (groupValidationResult.getInvalidChildren()) { | |
log.debug("Already contained child group") | |
} | |
else { | |
def result = groupService.addGroupsToGroups(serviceContext, [parentGroup], [childGroup]) | |
if (! result) { | |
log.warn ("Adding groups failed") | |
} | |
} | |
} | |
Hello Jamie
I found the reason myself. It was the missing call to mapper.register(childGroup);
.
Sorry to bother you, Rolf
Wow I don't remember writing this or what it's supposed to do... is there an accompanying community post or something similar?
Hello Jamie
You can find our Self Service approach at https://ravenlogic.atlassian.net/wiki/spaces/PUBLIC/pages/2695430209/Jira+Self+Service+Portal
See you, Rolf
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello Jamie
I tried to reproduce the
removeGroupsFromGroups
section, but it didn`t work. Do you have any idea why this is?With kind regards, Rolf