Script for the Group field is populated, then only one of those members may close the ticket

Pavan
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 18, 2019

Hi Team,

i'm New to script runner please help me write the code for the script validator, if the group(single user picker) field is populated, then only one of those members may close the ticket otherwise anyone the ticket assignee or reporter can close the ticket, Thank you 

2 answers

1 accepted

1 vote
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.
February 18, 2019

Hi @Pavan I wrote this script. Is untested but should work:

import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
GroupManager groupManager = ComponentAccessor.getGroupManager()

CustomField cfGroup = customFieldManager.getCustomFieldObject(11111L) // Change the ID with your Single User Picker ID
ArrayList<Group> group = issue.getCustomFieldValue(cfGroup) as ArrayList<Group>
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (group){
return groupManager.isUserInGroup(currentUser, group.first())
} else {
return currentUser in [issue.getAssignee(), issue.getReporter()]
}

Edit: Dont forget to replace the ID of the customField :)

Regards 

Pavan
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 19, 2019

@Alejandro Suárez - TecnoFor Thank you so much for help,its working like charm, i have more question, i would like add one more CustomFiled(single user picker), is that okay i add one more line as below.

CustomField cfGroup = customFieldManager.getCustomFieldObject(XXXXX) Or
CustomField cfGroup = customFieldManager.getCustomFieldObject(XXXX,XXXX)


0 votes
Pavan
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 22, 2019

@Alejandro Suárez - TecnoFor I would like to add two more conditions i.e. Whenever issue is created assignee, repoter, Group 2,Group 3 can close the ticket only when GROUP A, GROUP 1 value are not presented, if value or user present from the GROUP A or GROUP 1 only either GroupA or Group 1 users can close, Please guide me, Thanks  

import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
GroupManager groupManager = ComponentAccessor.getGroupManager()

CustomField cfGroup A= customFieldManager.getCustomFieldObject(11111)
CustomField cfGroup1 = customFieldManager.getCustomFieldObject(xxxxx)
CustomField cfGroup2 = customFieldManager.getCustomFieldObject(xxxxx)
CustomField cfGroup3 = customFieldManager.getCustomFieldObject(xxxxx)
// Change the ID with your Single User Picker ID
ArrayList<Group> groupA = issue.getCustomFieldValue(cfGroup) as ArrayList<Group>
ArrayList<Group> group1 = issue.getCustomFieldValue(cfGroup1) as ArrayList<Group>
ArrayList<Group> group2 = issue.getCustomFieldValue(cfGroup2) as ArrayList<Group>
ArrayList<Group> group3 = issue.getCustomFieldValue(cfGroup3) as ArrayList<Group>
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
if (groupA)
{
return groupManager.isUserInGroup(currentUser, groupA.first())
}
else if (group1)
{
return groupManager.isUserInGroup(currentUser, group1.first())
}
else{
return currentUser in [issue.getAssignee(), issue.getReporter(),group2.first(),group3.first()]
}

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

Hi @Pavan , 

Could you please explain better the conditions? That groups you mention are groups in Jira or groups inside of a custom field in the ticket?

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

Ok, I think I understood it. Here you go (Check the comments in the code):

import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
groupManager = ComponentAccessor.getGroupManager()

CustomField cfGroupA = customFieldManager.getCustomFieldObject(11100L) // Change the ID with your Single GroupA Picker ID
CustomField cfGroup1 = customFieldManager.getCustomFieldObject(11101L) // Change the ID with your Single Group1 Picker ID
ArrayList<Group> groupA = issue.getCustomFieldValue(cfGroupA) as ArrayList<Group>
ArrayList<Group> group1 = issue.getCustomFieldValue(cfGroup1) as ArrayList<Group>
Group group2 = groupManager.getGroup("jira-users") //Replace "jira-users" with the name of your group2
Group group3 = groupManager.getGroup("jira-users2") //Replace "jira-users2" with the name of your group3
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Boolean isUserInGroups(ApplicationUser user, Collection<Group> groups) {
Boolean isUserInGroup = Boolean.FALSE
groups.find { if (groupManager.isUserInGroup(user, it)) isUserInGroup = Boolean.TRUE }
return isUserInGroup
}

if (groupA && group1) {
return this.isUserInGroups(currentUser, groupA + group1)
} else {
return currentUser in [issue.getAssignee(), issue.getReporter()] ||
this.isUserInGroups(currentUser, [group2] + [group3])
}

And sorry, I've went fancy with the code adding the method because I dont like to repeat lines.

Pavan
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 25, 2019

@Alejandro Suárez - TecnoFor Thank you so much for your help but there is a small change, Group2, Group3 are also custom fields those are not groups.

Pavan
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 26, 2019

@Alejandro Suárez - TecnoFor This what i changed but the group 2 and group 3 which are custom fields(single user pcikers).

import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
GroupManager groupManager = ComponentAccessor.getGroupManager()

CustomField cfGroup = customFieldManager.getCustomFieldObject(11111)
CustomField cfGroup1 = customFieldManager.getCustomFieldObject(1121)
CustomField cfGroup2 = customFieldManager.getCustomFieldObject(12345)
CustomField cfGroup3 = customFieldManager.getCustomFieldObject(111122)
// Change the ID with your Single User Picker ID
ArrayList group = issue.getCustomFieldValue(cfGroup) as ArrayList
ArrayList group1 = issue.getCustomFieldValue(cfGroup1) as ArrayList
ArrayList group2 = issue.getCustomFieldValue(cfGroup2) as ArrayList
ArrayList group3 = issue.getCustomFieldValue(cfGroup3) as ArrayList
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

Boolean isUserInGroups(ApplicationUser user, Collection<Group> groups) {
Boolean isUserInGroup = Boolean.FALSE
groups.find { if (ComponentAccessor.getGroupManager().isUserInGroup(user, it)) isUserInGroup = Boolean.TRUE }
return isUserInGroup
}
if (group && group1) {
return this.isUserInGroups(currentUser, group + group1)
} else{
return currentUser in [issue.getAssignee(), issue.getReporter()]|| this.isUserInGroups(currentUser, group2 + group3)// this conditions is not working.
}

Pavan
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 28, 2019

@Alejandro Suárez - TecnoFor  Can you please review above code, thank you

Suggest an answer

Log in or Sign up to answer