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
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
@Alejandro Suárez García 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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Alejandro Suárez García 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()]
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Alejandro Suárez García Thank you so much for your help but there is a small change, Group2, Group3 are also custom fields those are not groups.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Alejandro Suárez García 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.
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Alejandro Suárez García Can you please review above code, thank you
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.