Hi all,
I have found the below 2 url which explained on how to update Jira user's username in bulk.
https://community.atlassian.com/t5/Jira-questions/How-to-bulk-rename-username/qaq-p/216275
However I need additional help on how to set scriptrunner so it will read the a csv file so it can set the "new_username" based on the existing "old_username" which already set in Jira.
The csv file will have content like :
oldUserName1 newUserName1
oldUserName2 newUserName2
...
This is an old question, but in case anyone comes upon it as I did earlier, here is an answer (which relies heavily on can i update user email using script? ).
I needed to update the email value and then the username (we use email address for username).
Not pretty, but it works.
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.user.ApplicationUserBuilderImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.AttachmentUtils
//import com.atlassian.jira.util.PathUtils
import org.apache.log4j.Level
import org.apache.log4j.Logger
Logger.getLogger("com.onresolve.scriptrunner").setLevel(Level.DEBUG)
def myCSV = 'userRenames.csv' // MUST BE A TEXT FILE
def issue = ComponentAccessor.getIssueManager().getIssueObject("SSNCATLAS-1656") // Key for attachment issue
def aM= ComponentAccessor.getAttachmentManager()
def uM = ComponentAccessor.getUserManager()
def attachments = aM.getAttachments(issue)
attachments.each{ a ->
def fileName = a.filename
log.warn fileName + ' is the user source'
// This method to get the file path is deprecated, but it works
def filePath = AttachmentUtils.getAttachmentFile(a)
// Concatenation only works for projects which have never edited their project key
// Anyone know a way to get the original project key?
// def pathManager = ComponentAccessor.getAttachmentPathManager()
// def filePath = PathUtils.joinPaths(pathManager.attachmentPath, issue.projectObject.key, '10000', issue.key, a.id.toString())
if (fileName.toString() == myCSV) {
File fileContents = new File(filePath.toString())
def mailChange = 0
if (fileContents) {
def lines = fileContents.readLines()
for (item in lines) {
def columns = item.split(",")
def oldMail = columns[0]
def newMail = columns[1]
// log.info "Old mail is " +oldMail + " and new mail is " +newMail // items before change
def user = uM?.getUserByName(oldMail) as ApplicationUser
def userCheck = uM?.getUserByName(newMail) as ApplicationUser
// Excel compatible logging: item|action|result
if ((! user) && (! userCheck)) {
log.info oldMail + "|" + "No action|User does not exist" // log when no user exists
}
if ((userCheck) && (! user)) {
log.info oldMail + "|" + "No action|Only new account " + newMail + " exists; nothing to do" // log when only new account exits
}
if ((userCheck) && (user)) {
log.info oldMail + "|" + "Duplicate|Migration required for " + newMail // log when two accounts are present
}
if ((user) && (! userCheck)) {
// def check = uM.canRenameUser(user) // Boolean in case perms or directory type is in question
//log.info "Check was " +check
def nMail = new ApplicationUserBuilderImpl(user).emailAddress(newMail.toLowerCase()).build()
uM.updateUser(nMail)
mailChange = 1
log.info oldMail + "|" + "Action|User was uptated to " + newMail // log when user is updated
}
}
}
if (mailChange == 1) {
// Yes, you have to re-declare the user to perform a second update
def lines = fileContents.readLines()
for (item in lines) {
def columns = item.split(",")
def oldMail = columns[0]
def newMail = columns[1]
def user = uM?.getUserByName(oldMail) as ApplicationUser
def userCheck = uM?.getUserByName(newMail) as ApplicationUser
if ((user) && (! userCheck)) {
def nName = new ApplicationUserBuilderImpl(user).name(newMail.toLowerCase()).build()
uM.updateUser(nName)
log.info oldMail + "|Action|User email was updated to " + newMail
}
}
}
}
}
I use your script to only update User Emails and it works great.
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
update user mail with script groovy:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.ApplicationUserBuilderImpl
import com.atlassian.jira.user.ApplicationUser
def um = ComponentAccessor.getUserManager()
//edit jira_username
def user = um.getUserByName('jira_username') as ApplicationUser
def newData = new
//edit newemailaddress
ApplicationUserBuilderImpl(user).emailAddress('newemailaddress').build()
um.updateUser(newData)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.