We added users by mistake to the Jira Internal Directory, there are no issues associcated with these userNames. These users already exist in crowd or LDAP, so there is no problem with us deleting these users.
I have a csv file with a list of users(3000)-- csvFile.csv
"user1","user2","user3", "user4", "user5"
I would like to access this csv file read the data from it and delete all the users listed in the file from the jira Internal directory.
I know how to delete these users by adding them to a string in the script but would like help in figuring out how to read the list from the csv file and deleting them using the list of usernames within the file:
How do I get the contents of the contents of the csvFile to the def userNames?
import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import org.supercsv.io.CsvBeanReader
import org.supercsv.io.CsvMapReader
import org.supercsv.prefs.CsvPreference
import org.supercsv.cellprocessor.CellProcessorAdaptor
import org.supercsv.cellprocessor.ift.CellProcessor
import org.supercsv.cellprocessor.Optional
def csvFile="/some/path/csvFile.csv"
def userService = ComponentAccessor.getComponent(UserService)
def asUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
//usernames to remove
def userNames =
userNames?.each { it ->
final UserService.DeleteUserValidationResult result = userService.validateDeleteUser(asUser, it)
if (result.isValid()) {
userService.removeUser(asUser, result)
log.info "User with username $it removed"
}
else {
log.error "Failed to remove user with username $it. " + result.getErrorCollection().errorMessages
}
}
You could upload your csv to import folder in server
I attach my code like i read csv and use in script console of Scriptrunner
//CSV with 3 columns (userName,userEmail,userFullName)
File file = new File("/data/atlassian/application-data/jira/import/Users_.csv")
def csvMapList = []
file.eachLine { line ->
def columns = line.split(";")
def tmpMap = [:]
tmpMap.putAt("userName", columns[0])
tmpMap.putAt("userEmail", columns[1])
tmpMap.putAt("userFullName", columns[2])
csvMapList.add(tmpMap)
}
csvMapList.forEach{line->
// the username of the new user - needs to be lowercase and unique - required
final String userName = line.getAt("userName")
...
...}
I hope this help!
@David A Thanks David, but I only have one row with a list of the users, so I don't need to iterate through rows and columns.
My csv file contains just a list of users in one row.
"user1","user2","user3"....
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.