Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

...

 

1 answer

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
}
}
}
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

New cloud apps roundup - June 2021

Since our last roundup in April, Atlassian's Marketplace Partners have added over 100 new cloud apps to the Atlassian Marketplace to help your teams work more efficiently. Let’s take a quick look a...

487 views 6 12
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you