Skip to main content
Skip table of contents

Bulk Change Owners of Structures and Generators


GROOVY
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.almworks.jira.structure.api.StructureComponents
import com.almworks.jira.structure.api.auth.StructureAuth
import com.almworks.jira.structure.api.structure.Structure
import com.almworks.jira.structure.api.sync.SyncInstance

@Grab(group = 'com.almworks.jira.structure', module = 'structure-api', version = '*')
@WithPlugin('com.almworks.jira.structure')

def oldOwnerName = 'admin'
def newOwnerName = 'alice'
def resync = true // do full resync after update

def components = ScriptRunnerImpl.getPluginComponent(StructureComponents)
def structureManager = components.structureManager
def syncManager = components.syncManager

def oldOwner = Users.getByName(oldOwnerName)
def newOwner = Users.getByName(newOwnerName)
if (newOwner == null) {
  def message = "Cannot find user by username: $newOwnerName"
  log.error(message)
  return message
}

// The actual work is done here

List<Structure> changedStructures = []
List<SyncInstance> changedSynchronizers = []
def success = false
def exception = null

try {
  StructureAuth.sudo {
    structureManager.getAllStructures(null, true).each { st ->
      // Change owner
      if (st.owner == oldOwner) {
        st.owner = newOwner
        st.saveChanges()
        changedStructures << st
      }
      // Change owner of synchronizers installed for this structure
      syncManager.getInstalledSynchronizersForStructure(st.id).each { sync ->
        if (sync.userKey == oldOwner?.key) {
          def enabled = syncManager.isAutosyncEnabled(sync.id)
          if (enabled) {
            syncManager.setAutosyncEnabled(sync.id, false)
            syncManager.updateSynchronizer(sync.id, sync.parameters, newOwner)
            if (resync) {
              syncManager.resync(sync.id, true, null)
            } else {
              syncManager.setAutosyncEnabled(sync.id, true)
            }
          } else {
            syncManager.updateSynchronizer(sync.id, sync.parameters, newOwner)
          }
          changedSynchronizers << sync
        }
      }
    }
  }
  success = true
} catch (Exception e) {
  log.warn("Failed to change owner from '$oldOwnerName' to '$newOwnerName'", e)
  exception = e
}

// Output message about changed structures and synchronizers to the log and to the console output
def msg = "Script to change owner from '$oldOwnerName' for '$newOwnerName' " +
  (success ? "finished successfully" : "failed (${exception && exception.message})") + "\n" +
  "Changed structures:\n" + changedStructures.collect({ "#${it.id} ${it.name}" }).join("\n") + "\n" +
  "Changed synchronizers:\n" + changedSynchronizers.collect({ "#${it.id} (for structure #${it.structureId})" }).join("\n")

log.warn(msg)
msg.replaceAll("\n", "<br>")

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.