Skip to content

Instantly share code, notes, and snippets.

@jechlin
Last active September 4, 2023 09:11
Show Gist options
  • Save jechlin/9722753 to your computer and use it in GitHub Desktop.
Save jechlin/9722753 to your computer and use it in GitHub Desktop.
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")
}
}
}
@rlader
Copy link

rlader commented Aug 29, 2023

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

@rlader
Copy link

rlader commented Aug 29, 2023

Hello Jamie

I found the reason myself. It was the missing call to mapper.register(childGroup);.

Sorry to bother you, Rolf

@jechlin-adaptavist
Copy link

Wow I don't remember writing this or what it's supposed to do... is there an accompanying community post or something similar?

@rlader
Copy link

rlader commented Sep 4, 2023

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