Script to copy Organisations to a second project

Dani Miles
Contributor
May 16, 2023

Hi there, 

I am splitting a project into two projects but need to maintain the same organisations across both. I have found this one thread but it seems quite old and doesn't work as is. 

https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Copy-Organisation-into-another-project/qaq-p/627649

Anyone know if there is a way to script this to be done? I have thousands and cannot do this as a manual task. 

Thanks, 

Danielle

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 16, 2023

Hi @Dani Miles 

Below is the updated version of the code which has been tested and is working:-

import com.adaptavist.hapi.jira.projects.Projects
import com.adaptavist.hapi.jira.users.Users
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.servicedesk.api.ServiceDesk
import com.atlassian.servicedesk.api.ServiceDeskManager
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.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")
def projectFromKey = 'ST'
def projectToKey = 'SS'

def asUser = Users.getByName('admin')
def fromProject = Projects.getByKey(projectFromKey)
def toProject = Projects.getByKey(projectToKey)

getOrganizationsForProject(asUser, fromProject).each {
addOrganizationToProject(it, toProject)
}

void addOrganizationToProject (CustomerOrganization organization, Project project) {
@PluginModule OrganizationService organizationService
def adminUser = Users.loggedInUser
def sdProjectId = getServiceDeskForProject(project)?.id

if (!organization || !sdProjectId) {
log.warn "Could not find organization ${organization} or service desk project ${project}"
return
}

def organizationServiceDeskUpdateParameters = organizationService
.newOrganizationServiceDeskUpdateParametersBuilder()
.organization(organization)
.serviceDeskId(sdProjectId)
.build()

organizationService.addOrganizationToServiceDesk(adminUser, organizationServiceDeskUpdateParameters)
}

ServiceDesk getServiceDeskForProject(Project sdProject) {
@PluginModule ServiceDeskManager serviceDeskManager
def serviceDeskProject = serviceDeskManager.getServiceDeskForProject(sdProject)

if (!serviceDeskProject) {
log.warn "${serviceDeskProject.id}"
return null
}
serviceDeskProject
}

List <CustomerOrganization> getOrganizationsForProject (ApplicationUser user, Project project) {
@PluginModule ServiceDeskService serviceDeskService
@PluginModule OrganizationService organizationService

def srcServiceDesk = serviceDeskService.getServiceDeskForProject(user, project)

if (!srcServiceDesk) {
log.warn "Could not find service desk id for project ${project?.key}.\n${srcServiceDesk.id}"
}

def srcServiceDeskProjectId = srcServiceDesk.id

def pagedRequest = new SimplePagedRequest(0, 50)
def organizationQuery = organizationService
.newOrganizationsQueryBuilder()
.pagedRequest(pagedRequest)
.serviceDeskId(srcServiceDeskProjectId)
.build()

def pagedResponse = organizationService.getOrganizations(user, organizationQuery)

if (!pagedResponse) {
log.warn "${pagedResponse.find()}"
}
pagedResponse.results
}

Please note that the sample working code above is not 100% exact to your environment. Hence you will need to make the required modifications.

The main changes made are the removal of the object invocation using the ComponentAccessor and replacing them with the Annotations.

Also, I have used ScriptRunner's HAPI feature to simplify the initialization of objects like Users, i.e. logged-in users and Projects.

Please ensure that you upgrade your ScriptRunner plugin to the latest release, i.e. 8.2.1 so you will be able to use the code.

You can directly execute this code on the ScriptRunner console.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Dani Miles
Contributor
May 16, 2023

Thanks Ram
I can't try this as unfortunately we are on an old system where we cannot upgrade to the latest version of Script Runner at the moment. I have found a script from which I can add a list of Organizations which gets me round this for now. 
Danielle

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events