Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I bulk Delete userNames in Jira from a csv file using scriptrunner

Karina Green February 16, 2023

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

    }

}

 

 

1 answer

1 vote
David A
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 16, 2023

Hi @Karina Green 

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!

Karina Green February 16, 2023

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

Like David A likes this

Suggest an answer

Log in or Sign up to answer