Forums

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

Updated Components between Projects

Salim Hammar
Contributor
February 19, 2022

Hello everyone

This script will serve when i create , update and delete the component these features will apply in any project (with project key) 

 

Actually when i edit a name for the component it's working without issue but when i change the lead the component (edit) it goes on a loop and after i don't access the page component 

 

My opinion the problem it's : "destComponent = projectComponentManager.findByComponentName(project.id, oldComponent.name as String)" because he updated the name only but i don't know for add the other attributes 

that is to say (name , description, lead and assignee) 

You have idea ??

--------------------------------------------------------------------------------

def dest_prj = ['TS', 'TD', 'TK']
import com.atlassian.jira.bc.project.component.MutableProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.bc.project.component.ProjectComponentCreatedEvent
import com.atlassian.jira.event.bc.project.component.ProjectComponentDeletedEvent
import com.atlassian.jira.event.bc.project.component.ProjectComponentUpdatedEvent
import com.atlassian.jira.project.AssigneeTypes
import com.atlassian.jira.project.Project
import com.atlassian.jira.bc.project.component.ProjectComponent

def projectManager = ComponentAccessor.projectManager
def projectComponentManager = ComponentAccessor.projectComponentManager

ProjectComponent component = event.projectComponent
log.info "Deletected ${event.getClass()} for $component.name ($component.id)"
dest_prj.each { String destProjectKey ->
log.info "Getting project object from dest_prj: $destProjectKey"
def project = projectManager.getProjectObjByKey(destProjectKey)
log.info "Project object found: $project"
def destComponent = projectComponentManager.findByComponentName(project.id, component.name)
log.info "Attempted to identify matching destination component in $project using name=$component.name : $destComponent"
if (event instanceof ProjectComponentCreatedEvent) {
if (!destComponent) {
log.info "Attempting to create $component.name into $project"

projectComponentManager.create(component.name, component.description, component.lead, component.assigneeType, project.id)
} else {
log.error "Can't create a copy of $component.name in $destProjectKey project. A component with that name alreay exist."
}
}
if (event instanceof ProjectComponentUpdatedEvent) {
def oldComponent = (event as ProjectComponentUpdatedEvent).oldProjectComponent as ProjectComponent
if (event instanceof ProjectComponentUpdatedEvent) {
destComponent = projectComponentManager.findByComponentName(project.id, oldComponent.name as String)
log.info "Attempted to identify matching destination component in $project using name=$oldComponent.name : $destComponent"
}

if (destComponent) {
log.info "Attempting to update $destComponent.name ($destComponent.id) from $project project using data from $component.name ($component.id)"
destComponent = MutableProjectComponent.copy(destComponent)
destComponent.with {
name = component.name
description = component.description
lead = component.lead
assigneeType = component.assigneeType //or use component.assigneeType
}
projectComponentManager.update(destComponent)
} else {
log.warn "No component found in $destProjectKey with name $oldComponent.name. Can't perform an update."
}
}

if (event instanceof ProjectComponentDeletedEvent) {
log.info "Detected Component Deletion event for $component.name"
if (destComponent) {
log.info "Attempting to detelete $destComponent.name ($destComponent.id) from $project project"
projectComponentManager.delete(destComponent.id)
} else {
log.warn "No component found in $destProjectKey with name $component.name. Can't delete it."
}
}
}

--------------------------------------------------------

1 answer

1 accepted

1 vote
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
February 19, 2022

Hi @Salim Hammar  I think the problem is that when you update a component it fires another "ProjectComponentUpdatedEvent" and it works for component's name update because you search components by name so your algorithm can't search component by new name.

I would implement a simple check which will cause that component is not updated when all the attributes of the component match -> it will stop the loop. It should be something like (not exact tested code, but I guess you can make it :))

  • if(destComponent.name != component.name ||
    destComponent.description != component.description ||
    destComponent.lead != component.lead ||
    destComponent.assigneeType != component.assigneeType){
         projectComponentManager.update(destComponent)
    }
Salim Hammar
Contributor
February 19, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__  

Thanks you for your answer but in my script where i add the script sended that you have send me  ?

Thanks in advance 

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
February 20, 2022

You should replace 

projectComponentManager.update(destComponent)
Salim Hammar
Contributor
February 20, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ 

The script that have send me not working 

update.png

 

Thanks in advance

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
February 20, 2022

Hi @Salim Hammar what exactly does not work? I don't have an environment to test it so you must provide all the information.

Salim Hammar
Contributor
February 20, 2022

Hi when i update the component he update in the project but no and another project 

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
February 21, 2022

Hi @Salim Hammar , sorry but as I told you, I do not have environment to test the usecase. You should use logging some information to your server log to try to figure out what's happening in your script. We can analyze it further if you have more detailed information.

https://scriptrunner.adaptavist.com/5.3.9/jira/recipes/misc/log-to-SR-file.html

Salim Hammar
Contributor
February 22, 2022

Hi @Martin Bayer _MoroSystems_ s_r_o__ 

 

I share my script, and in this when i would like change the lead or description or assignnee he doesn't work 

 

Script : 

def dest_prj = ['TS', 'TD', 'TK']
import com.atlassian.jira.bc.project.component.MutableProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.bc.project.component.ProjectComponentCreatedEvent
import com.atlassian.jira.event.bc.project.component.ProjectComponentDeletedEvent
import com.atlassian.jira.event.bc.project.component.ProjectComponentUpdatedEvent
import com.atlassian.jira.project.AssigneeTypes
import com.atlassian.jira.project.Project
import com.atlassian.jira.bc.project.component.ProjectComponent

def projectManager = ComponentAccessor.projectManager
def projectComponentManager = ComponentAccessor.projectComponentManager

ProjectComponent component = event.projectComponent
log.info "Deletected ${event.getClass()} for $component.name ($component.id)"
dest_prj.each { String destProjectKey ->
log.info "Getting project object from dest_prj: $destProjectKey"
def project = projectManager.getProjectObjByKey(destProjectKey)
log.info "Project object found: $project"
def destComponent = projectComponentManager.findByComponentName(project.id, component.name)
log.info "Attempted to identify matching destination component in $project using name=$component.name : $destComponent"
if (event instanceof ProjectComponentCreatedEvent) {
if (!destComponent) {
log.info "Attempting to create $component.name into $project"

projectComponentManager.create(component.name, component.description, component.lead, component.assigneeType, project.id)
} else {
log.error "Can't create a copy of $component.name in $destProjectKey project. A component with that name alreay exist."
}
}
if (event instanceof ProjectComponentUpdatedEvent) {
def oldComponent = (event as ProjectComponentUpdatedEvent).oldProjectComponent as ProjectComponent
if (event instanceof ProjectComponentUpdatedEvent) {
destComponent = projectComponentManager.findByComponentName(project.id, oldComponent.name as String)
log.info "Attempted to identify matching destination component in $project using name=$oldComponent.name : $destComponent"
}
if (destComponent) {
log.info "Attempting to update $destComponent.name ($destComponent.id) from $project project using data from $component.name ($component.id)"
destComponent = MutableProjectComponent.copy(destComponent)
destComponent.with {
name = component.name
description = component.description
lead = component.lead
assigneeType = component.assigneeType //or use component.assigneeType
}
projectComponentManager.update(destComponent)
} else {
log.warn "No component found in $destProjectKey with name $oldComponent.name. Can't perform an update."
}
}
if (event instanceof ProjectComponentDeletedEvent) {
log.info "Detected Component Deletion event for $component.name"
if (destComponent) {
log.info "Attempting to detelete $destComponent.name ($destComponent.id) from $project project"
projectComponentManager.delete(destComponent.id)
} else {
log.warn "No component found in $destProjectKey with name $component.name. Can't delete it."
}
}
}

Suggest an answer

Log in or Sign up to answer