Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to automate moving objects in JSM

Dom L October 31, 2023

I'd like to be able to move objects when a certain status is changed. It will only be moving to another object type in the same schema. I don't see this as an option in Automation, so can I use ScriptRunner/Groovy scripting? If so, what command would be used to move an object while retaining history and inbound references? And references from issues in Jira.

 

Is this possible, or should I look for another route?

1 answer

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 31, 2023

Yes, you can use scriptrunner to move an object. But it's not a simple command.

You have to re-map all the attributes (even if they are the same), then because moving is an asynchronous process, you might need to wait for it to finish. But you can't wait forever, so you need a timeout.

Here is some sample generic code to move an object in scriptrunner:

 

import com.adaptavist.hapi.jira.assets.Assets
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectFacade
import com.riadalabs.jira.plugins.insight.channel.external.api.facade.ObjectTypeAttributeFacade
import com.riadalabs.jira.plugins.insight.services.model.ObjectBean
import com.riadalabs.jira.plugins.insight.services.model.move.MoveAttributeMapping
import com.riadalabs.jira.plugins.insight.services.model.move.MoveObjectBean
import com.riadalabs.jira.plugins.insight.services.model.move.MoveObjectMapping

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin('com.riadalabs.jira.plugins.insight') insightPlugin

@PluginModule ObjectTypeAttributeFacade objectTypeAttributeFacade
@PluginModule ObjectFacade objectFacade
ObjectBean object = Assets.getByKey('xxx-123') //you'll need to specify the object to move

def TARGET_SCHEMA_ID = 1 //you need to specify the target schema id
def TARGET_OBJECT_TYPE_ID = 1 //you need to specify the target object type id
def MAX_WAIT_MILLIS = 10000 //10 seconds


MoveObjectBean moveObjectBean = new MoveObjectBean()
moveObjectBean.setObjectSchemaId(TARGET_SCHEMA_ID)
moveObjectBean.setFromObjectTypeId(object.objectTypeId)
moveObjectBean.setToObjectTypeId(TARGET_OBJECT_TYPE_ID)
moveObjectBean.setReferences(MoveObjectBean.References.REMOVE_REFERENCES_TO_OBJECT)
MoveObjectMapping moveMap = new MoveObjectMapping()
objectTypeAttributeFacade.findObjectTypeAttributeBeans(object.objectTypeId).each { att ->
def attMap = MoveAttributeMapping.create(att, att)
log.info "Preparing to move $att (${att.getClass()})"
moveMap.map(att.id, attMap)
}

moveObjectBean.setMapping(moveMap)
moveObjectBean.setIql("Key = $object.objectKey")

def moveSuccess = false

try {
def moveProgress = objectFacade.moveObjects(moveObjectBean)
//wait for move to finish
Integer curWait = 0
log.debug "moveObjectAndWait: Waiting for move to finish: $curWait ($moveProgress.status)"
while (moveProgress.isFinished() == false && curWait < MAX_WAIT_MILLIS) {
Thread.sleep(100)
curWait = curWait + 100
log.debug "moveObjectAndWait: Waiting for move to finish: $curWait ($moveProgress.status)"
}
if (moveProgress.isFinished()) {
log.info "moveObjectAndWait: Move complete"
return true
} else if (moveProgress.isError()) {
log.error "moveObjectAndWait: error during move - "
} else {
log.warn "moveObjectAndWait: Gave up waiting for move to finish."
}
} catch (moveEx) {
log.error "moveObjectAndWait: Could not move ${object?.objectKey} to $TARGET_OBJECT_TYPE_ID: $moveEx.message"
}
return false
Dom L November 6, 2023

I don't know how this works, but thanks! I only needed to take the middle section to use in my script but it does work somehow. Although it doesn't grab any attributes so I needed to update them automatically after moving it.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events