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

Get Entity Properties for User without REST call

flaimo September 21, 2020

We have an external application which adds entity properties to an user through a REST call. (not to confuse with the user properties that can be edited through the jira user interface)

is there a way to access these properties using a groovy script in behaviours and post functions through the object model? the examples I am finding all make use of the old user properties, but not the new entity properties or use a rest call like this:

def rest_call = "/usr/bin/curl -u user:pw http://yourserver.com/rest/api/2/user/properties/propertyname?user=someuuser"
def rest_call_response = rest_call.execute().text

I was able to make the REST call work in a post function, but not in a behaviour, therefore I am interested in an approach without a rest call.

-----------------------

the actual use case I would like to accomplish looks like this:

the jira username of the manager of a user is stored as an entity property "managerUsername" with the service desk customer user object.

when a service desk customer creates a new service request issue through the service desk interface a custom field "Approver" should be initialized with the user object of the manager. the user should still be able to change the user in the field.

---------------------

As a post function in the create transition it works, but that would mean the customer can't override the value to be set.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 25, 2020

Hi @flaimo check out this manager 

ComponentAccessor.getUserPropertyManager()

Regards 

flaimo September 25, 2020

that's for the old user properties, not for entity properties.

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 26, 2020

@flaimo Well, you were right. The rest endpoint is using JsonEntityProperties. 

To achieve what you want you have to have a behaviour with the service desk mapping for your project. Then add this code to the Initialiser:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.entity.property.JsonEntityPropertyManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserUtils
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String managerUsernameProperty = "managerUsername"

String managerUsername = getUserProperty(currentUser, managerUsernameProperty)
FormField ffManager = getFieldByName("Manager")
ApplicationUser manager = UserUtils.getUser(managerUsername)
if (manager) {
ffManager.setFormValue(managerUsername)
ffManager.setDescription(manager.getDisplayName())
}

static String getUserProperty(ApplicationUser targetUser, String propertyKey) {
JsonEntityPropertyManager jsonEntityPropertyManager = ComponentAccessor.getComponent(JsonEntityPropertyManager)
String value = jsonEntityPropertyManager.get("UserProperty", targetUser.getId()).getAt(propertyKey)?.getValue()
value?.contains("\"") ? value.replaceAll("\"","") : value
}

Then you have to update the current property in the create postfunction of the workflow if its changed by the customer:

import com.atlassian.jira.bc.user.property.UserPropertyHelper
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.entity.property.EntityProperty
import com.atlassian.jira.entity.property.JsonEntityPropertyManager
import com.atlassian.jira.event.entity.EntityPropertySetEvent
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserUtils

import java.util.function.BiFunction

CustomField cfManager = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Manager")?.first()

ApplicationUser newManagerUser = issue.getCustomFieldValue(cfManager) as ApplicationUser
ApplicationUser adminUser = UserUtils.getUser("admin")
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String managerUsernameProperty = "managerUsername"
String currentManager = getUserProperty(UserUtils.getUser("test"), managerUsernameProperty)
String newManager = newManagerUser?.getUsername()

if (currentManager != newManager) {
setUserProperty(adminUser, currentUser, managerUsernameProperty, newManagerUser.getUsername(), false)
}

static void setUserProperty(ApplicationUser adminUser, ApplicationUser targetUser, String propertyKey, String propertyValue, Boolean dispatchEvent) {
JsonEntityPropertyManager jsonEntityPropertyManager = ComponentAccessor.getComponent(JsonEntityPropertyManager)
UserPropertyHelper userPropertyHelper = ComponentAccessor.getComponent(UserPropertyHelper)
BiFunction<ApplicationUser, EntityProperty, ? extends EntityPropertySetEvent> eventBiFunction = userPropertyHelper.createSetPropertyEventBiFunction()
jsonEntityPropertyManager.put(adminUser, "UserProperty", targetUser.getId(), propertyKey, toJsonString(propertyValue), eventBiFunction, dispatchEvent)
}

static String getUserProperty(ApplicationUser targetUser, String propertyKey) {
JsonEntityPropertyManager jsonEntityPropertyManager = ComponentAccessor.getComponent(JsonEntityPropertyManager)
String value = jsonEntityPropertyManager.get("UserProperty", targetUser.getId()).getAt(propertyKey)?.getValue()
value.contains("\"") ? value.replaceAll("\"", "") : value
}

static String toJsonString(String property) {
"\"" + property + "\""
}

Finally, I dont know if its a bug or what, but when I set the formValue in the user picker in my scriptrunner version, it only shows the username, not the Full name formatted with the avatar. If it happens in your side too I suggest you to add a second behaviour to add a description to the field when it changes.

You only have to add the "Manager" field and set this script:

import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.UserUtils
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

FormField ffManager = getFieldById(getFieldChanged())
String managerValue = ffManager.getValue().toString()
if (managerValue) {
ApplicationUser manager = UserUtils.getUser(managerValue)
ffManager.setDescription(manager.getDisplayName())
}

Hope it helps! 

flaimo September 28, 2020

works like a charm. thank you. the manager name is actually displayed correctly in our service desk form (first script).

Alejandro Suárez - TecnoFor
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
September 28, 2020

Glad to hear that :)

Reading the code I made a mistake, but I guess you fixed it. The script for the create postfunction in this line:

String currentManager = getUserProperty(UserUtils.getUser("test"), managerUsernameProperty)

Should be:

String currentManager = getUserProperty(currentUser, managerUsernameProperty)
TAGS
AUG Leaders

Atlassian Community Events