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

Is there a way to update User Properties via API or script?

gtfcom January 26, 2015

We're using JIRA 6.3.7

Is there any possibility to update custom User Properties (via API or via Script)?

I want to add new properties and update the values of existing ones.

I'm referring to these properties:

Clipboard_1.png

I want to avoid updating it via SQL script executed on the JIRA database, but I cannot find any documention regarding the supported APIs or scripts. It seems that database update is the only possible way.


Thank you in advance for your help!

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Cesare Jacopo Corzani
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.
January 30, 2015

You can use script-runner and doing something like that

import com.atlassian.jira.component.ComponentAccessor

def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()

def admin = userManager.getUserByName("admin")

userPropertyManager.getPropertySet(admin).setString("jira.meta.myproperty","123")

The prefix "jira.meta." is imporant, if you don't use this convention new properties will not be shown on that section (the form on your screenshot).

Aron Felberbaum June 1, 2016

Cesare,

If I wand to add a property to all users, where do I put this script.

Michael Schultz September 26, 2017

@cesare I was hoping you could provide a solution similar to this one above. I need to update all user's properties that are part of a specific group.

 

import com.atlassian.jira.component.ComponentAccessor
def userPropertyManager = ComponentAccessor.getUserPropertyManager()
def userManager = ComponentAccessor.getUserManager()
def groupManager = ComponentAccessor.getGroupManager()
def users = groupManager.getUsersInGroup("Testing")
for (user in users) {
userPropertyManager.getPropertySets(user).setString("jira.meta.myproperty","123")
}

 This is what I have now. 

1 vote
Andreyev Dias de Melo February 7, 2017

Hi all!

I adapted @Cesare Jacopo Corzani to a small ScriptRunner REST Endpoint to update legacy user properties thru URL query params (_https://yourjira/rest/scriptrunner/latest/custom/updateUserProperty?value=12345678&username=some-user&property=abc_): https://gist.github.com/andreyev/94bd402cdcc41ef4166f5abf97bd43da
Hope it helps!

Andreyev

Tim Eddelbüttel
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 13, 2017

In combination with Postman you can update the user properties in bulk with a csv file smile

https://www.getpostman.com/docs/multiple_instances

Before JIRA 7 i used the JIRA CLI but this isn't free anymore. With this snippet i can replace this and with the csv it's better than before.

Thanks,

1 vote
gtfcom February 2, 2015

Cesare,

I have a code that gets the values, but I cannot find a way to update them in system when I want to insert new values.

So this piece gets the data:

 

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.UserPropertyManager
 
 
String propertyKey = "xxx"
String propertyValue = null


UserPropertyManager userPropertyManager = ComponentAccessor.getUserPropertyManager()
User u = issue.reporter
if (u) {
    propertyValue = userPropertyManager.getPropertySet(u)?.getString('jira.meta.'+propertyKey)
}

return propertyValue

 

and this piece updates the values directly in the database (but this is something I consider as a hack, not a official supported method). 

Another thing is that the values are updated, but they are not refreshed instantly, it's something related to the JIRA cache.

import groovy.sql.Sql
import java.sql.Driver
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.UserPropertyManager
 
def driver = Class.forName('org.postgresql.Driver').newInstance() as Driver
 
def props = new Properties()
props.setProperty("user", "xxx")
props.setProperty("password", "yyy")
 
def conn = driver.connect("jdbc:postgresql://localhost:5432/jira", props)
def sql = new Sql(conn)

...
some code
...
 
 
try {
    sql.executeUpdate("update propertystring set propertyvalue = $newValue WHERE id = (SELECT id FROM propertyentry WHERE property_key = $propertyKey AND entity_id = (SELECT id FROM app_user WHERE user_key = $userLogin))")
    }
finally {
    sql.close()
    conn.close()
}

Thank you for your answer.

0 votes
Cesare Jacopo Corzani
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 2, 2015

Is the method setString I suggested before not working?
If you use that on the PropertySet, Jira should save the data for you without using the DB hack.

with :

userPropertyManager.getPropertySet(admin).setString("jira.meta.myproperty","123")

You should have an implicit insert/update. I've tried it before posting the solution and after executing that line I had entries on the section you were referring to. Isn't it?

gtfcom February 4, 2015

I must have misread your oiginal post. Works perfectly, thank you Cesare!

Cesare Jacopo Corzani
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 4, 2015

No problem, you are welcome.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events