Hey everyone,
I have a script to update username in bulk from CSV file but it's not working. I'm using renameUser as method
Can you please review the script and recommend changes-
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.user.UserAccessor
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
@ShortTextInput(label = 'File path', description = 'The file path to your CSV file')
String csvFilePath
assert csvFilePath
def userAccessor = ComponentLocator.getComponent(UserAccessor)
def file = new File(csvFilePath)
def csvMapList = []
file.eachLine { line ->
def columns = line.split(",")
def tmpMap = [:]
tmpMap.putAt("username", columns[0])
tmpMap.putAt("new_username", columns[1]) // New usernames from CSV
csvMapList.add(tmpMap)
}
def usernamesUpdated = 0
csvMapList.each { map ->
def oldUsername = map["username"] as String
def newUsername = map["new_username"] as String
if (userAccessor.exists(oldUsername)) {
userAccessor.renameUser(oldUsername, newUsername)
if (userAccessor.exists(newUsername)) {
log.debug("Successfully updated username from $oldUsername to $newUsername")
usernamesUpdated++
} else {
log.debug("Failed to update username from $oldUsername to $newUsername")
}
} else {
log.debug("User $oldUsername was not found.")
}
}
log.debug("Username update completed. Successfully updated $usernamesUpdated usernames.")