Skip to content

Instantly share code, notes, and snippets.

@jechlin-adaptavist
Created November 11, 2022 14:55
Show Gist options
  • Save jechlin-adaptavist/cf7c1d3aa67ec35fa6f985e5983d18ff to your computer and use it in GitHub Desktop.
Save jechlin-adaptavist/cf7c1d3aa67ec35fa6f985e5983d18ff to your computer and use it in GitHub Desktop.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.servicedesk.api.ServiceDeskService
import com.atlassian.servicedesk.api.organization.CustomerOrganization
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.util.paging.SimplePagedRequest
import com.atlassian.upm.api.license.entity.Organization
import com.onresolve.scriptrunner.parameters.annotation.ProjectPicker
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import java.util.stream.StreamSupport
@WithPlugin("com.atlassian.servicedesk")
@PluginModule
OrganizationService organizationService
@PluginModule
ServiceDeskService serviceDeskService
@ProjectPicker(label = 'Project', description = 'Choose project to delete organizations from')
Project project
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def serviceDesk = serviceDeskService.getServiceDeskForProject(user, project)
def organizationsIterator = new Iterator<List<Organization>>() {
private hasMoreResults = true
private int offset = 0
@Override
boolean hasNext() {
hasMoreResults
}
@Override
List<Organization> next() {
def query = organizationService
.newOrganizationsQueryBuilder()
.serviceDeskId(serviceDesk.id)
.pagedRequest(new SimplePagedRequest(offset, 50))
.build()
def pagedResponse = organizationService.getOrganizations(user, query)
def orgs = pagedResponse.results as List<Organization>
offset += orgs.size()
hasMoreResults = pagedResponse.hasNextPage()
orgs
}
}
def iterator = StreamSupport.stream(Spliterators.spliteratorUnknownSize(organizationsIterator, Spliterator.ORDERED), false)
.flatMap(List::stream)
.iterator() as List<CustomerOrganization>
iterator.each { CustomerOrganization org ->
def updateParameters = organizationService
.newOrganizationServiceDeskUpdateParametersBuilder()
.serviceDeskId(serviceDesk.id)
.organization(org)
.build()
organizationService.removeOrganizationFromServiceDesk(user, updateParameters)
}
'Done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment