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

Confluence Script to update username in bulk

Ankit Choudhary August 4, 2023

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.")

0 comments

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events