Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

How to update multiple user's username value and properties such as email address using scriptrunner

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

https://community.atlassian.com/t5/Jira-questions/Bulk-update-usernames-in-JIRA-7-DelegatingApplicationUser/qaq-p/460954

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

...

 

2 answers

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!

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)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events