Forums

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

scriptrunner job - update user property

Arthur S
Contributor
April 22, 2022

Hi,

 

I am trying to create a job function to read a csv file and update user properties based on that. I don't want to do it via the REST API because the properties set by the API are not usable in post-function.

I want add the manager of all my users. Those data are present in a csv with 2 columns : user | manager

 

On the job, the line "

userPropertyManager.getPropertySet(row.user)?.setString('jira.meta.'+propertyKey, row.manager);" is not working, any guess why ?
Here is the full script :
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.user.UserPropertyManager

import com.mindprod.csv.CSVReader

def filePath = '/home/jira/atlassian/application-data/jira/scriptrunner/user_test.csv'

def reader = new CSVReader(new FileReader(new File(filePath)))

//Gets the user property value based on the key.

def propertyKey = 'manager'; //Property name

UserPropertyManager userPropertyManager = ComponentAccessor.getUserPropertyManager();

def data= reader.collect { it }.with { rows ->

    def header = rows.head()

    def dataRows = rows.tail()

    dataRows.collect { row ->

        [header, row].transpose().collectEntries()

    }

}

data.each{row->

    userPropertyManager.getPropertySet(row.user)?.setString('jira.meta.'+propertyKey, row.manager);

    // access any data from the row by the name of the header

    // for example, row['colHeader'] or row.colHeader

}



Thanks in advance

1 answer

0 votes
PD Sheehan
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.
April 23, 2022

Without knowing actually what happens in your environment (errors in the log or anything), this is a bit of a shot in the dark... but try like this:

data.each{row->
def user = ComponentAccessor.userManager.getUserByName(row.user)
if(user){
def props = userPropertyManager.getPropertyeSetForUserKey(user.key)
props.setString("jira.meta.$propertyKey", row.manager)
} else {
log.warn "No Jira user found matching $row.user"
}
}
Arthur S
Contributor
April 25, 2022

Hi,

 

Thank you for the feedback. I think the issue is coming from the data not being retrieve from my csv file because it look likes the getUserByName don't retrieve anything :

 

user null.PNG

No Logs, and the result is an empty array.

result.PNG

 

Thanks

PD Sheehan
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.
April 25, 2022

Try to add a bit of logging... 

Just above 'def user = ' add:

log.info "Reading row: $row to extract $row.user"

This will hopefully give you some clues.

Like Arthur S likes this

Suggest an answer

Log in or Sign up to answer