Use users set in environments to approve a Booking Request
Use Case
Imagine that we have the following situation
Our user names of our approvers are defined in an environment attribute called “Approvers” (separated by a comma when several approvers)
We use an “Approvers” multi-user select custom field in our Booking Requests to approve the transition
We would like to achieve the following:
When the requester selects an Environment in the Environment Custom Field “Environment(s) to Book”, we would like to automatically populate the “Approvers” custom field with all user names found in “Approvers” attribute of the environments to book
You can perform this by adding a Script Runner Listener or a Script Runner Post Function with following script. Example of a Listener:

This script will only work with of Golive v9.1.+
Main Script GOLIVE 9.1.+
Adapt this script to use the names of attributes and custom fields that are setup in your context.
import com.atlassian.jira.event.type.EventDispatchOption
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.holydev.env.plugin.jira-holydev-env-plugin")
import com.apwide.golive.accessor.EnvironmentServicesAccessor
import com.apwide.env.environment.Environment
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.apwide.golive.example")
log.setLevel(Level.DEBUG)
def environmentCustomFieldName = "Environment(s) to book"
def environmentAttributeName = "Approver(s)"
def userNameDelimiter = ","
def multiUsersPickerCustomFieldName = "Approvers"
def environmentServices = EnvironmentServicesAccessor.get()
def issueManager = ComponentAccessor.getIssueManager()
// to test in console
// Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ECP-549")
Object getCustomFieldValue(Issue issue, String customFieldName){
CustomField customField= ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName(customFieldName)?.find({ cf -> cf.isRelevantForIssueContext(issue) })
return customField ? issue.getCustomFieldValue(customField) : null
}
def setCustomFieldValue(MutableIssue issue, String customFieldName, Object value){
CustomField customField= ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName(customFieldName)?.find({ cf -> cf.isRelevantForIssueContext(issue) })
if (customField) {
issue.setCustomFieldValue(customField, value)
}
}
List<ApplicationUser> getApplicationUsers(usernames){
return usernames?.collect {
try{
return ComponentAccessor.getUserManager().getUserByName(it)
}
catch (Throwable e){
return null
}
}.findAll()
}
List<Environment> environments = getCustomFieldValue(issue, environmentCustomFieldName)?.collect { environmentServices.getEnvSearcher().getById(it.id)?.entity}
log.debug "environments: ${environments}"
List userNames = environments
.collect {
String attributeValue = it.attributes.find{ (it.attribute.name == environmentAttributeName) }?.value
log.debug "attribute value: ${attributeValue}"
def userNames = attributeValue?.split(userNameDelimiter)
log.debug ("user names: ${userNames}")
return userNames
}
.flatten()
.unique()
log.debug "userNames: ${userNames}"
List<ApplicationUser> users = getApplicationUsers(userNames)
log.debug "application users: ${users}"
setCustomFieldValue(issue, multiUsersPickerCustomFieldName, users)
issue = issueManager.updateIssue(ComponentAccessor.jiraAuthenticationContext.loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.debug "updated issue: ${issue}"
log.debug "updated field ${multiUsersPickerCustomFieldName}: ${getCustomFieldValue(issue, multiUsersPickerCustomFieldName)}"
Dependencies
This script depends on :
com.apwide.golive.accessor.EnvironmentServicesAccessor
groovy classes (you can copy code below). Files must be structured that way in your script root:
EnvironmentServicesAccessor
package com.apwide.golive.accessor
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.holydev.env.plugin.jira-holydev-env-plugin")
import com.atlassian.jira.component.ComponentAccessor
import com.apwide.env.service.GoliveEnvironmentServices
class EnvironmentServicesAccessor {
static GoliveEnvironmentServices get(){
final Class serviceClass = ComponentAccessor.getPluginAccessor().getClassLoader().findClass("com.apwide.env.service.GoliveEnvironmentServices")
return ComponentAccessor.getOSGiComponentInstanceOfType(serviceClass)
}
}