Populate a custom field in jira with the name of group the assignee user belongs to

ashleyg February 24, 2021

We have a use case where as soon as a jira issue is assigned to a particular user, a custom field (could be regular text field or group picker field), is populated by the name of group the assignee belongs to. In this case any of the users who will be assigned to these issues will always only belong to two groups. One would ofcourse be the default jira-software-users group for login and the other group would be some xyz group and we would like that xyz group to be populated for that user under that custom field. So lets say when assignee is user1 and belongs to groups jira-software-users group and group1 then the custom field is automatically populated with group1 name. If its user2 with same jira-software-users group but this time group2, then that custom field is populated with group2 and so on and so forth.

Is anyone aware of how this could be accomplished. I feel script runner plugin could come handy, but i am a novice in that area, hence any help or suggestions will be greatly appreciated as always.

Cheers,

Ashley

1 answer

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 24, 2021

Hi @ashleyg you can use listeners of script runner to achieve this (https://docs.adaptavist.com/sr4js/6.19.0/get-started/tutorials/listeners-tutorial).

Events you want to listen are:

  • Issue Updated (it is here because when you edit Assignee on transition screen, Issue Assigned event is not triggered)
  • Issue Assigned

I'm using following code to find out whether Assignee field was changed during Issue Updated:

def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find { assigneeField.equals(it.field) }
if (change) {
def userKey = change.newvalue
...
}
ashleyg February 24, 2021

Thanks Martin for the prompt assistance.
As far as the code is concerned how do i enable it to populate my custom field with the name of the other group the assignee user belongs to?

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 24, 2021

hi @ashleyg you will just need to filter them out. So you will have to:

  1. define "general" groups which you do not want to take into account
  2. go through user's groups and filter this groups out
  3. select ONE group (if you use sinlge group picker field) or ALL THE OTHER GROUPS (if you use multi group picker field)

It could be something like:

def filteredGroupsNames = ['jira-users', 'jira-software-users']
def
userKey = change.newvalue
def user = ComponentAccessor.getUserManager().getUserByKey(userKey)
def userGroups = ComponentAccessor.getGroupManager().getGroupsForUser(user)
def filteredUserGroups = userGroups.findAll {!filteredGroupsNames.contains(it.name)}
// now set filteredUserGroups to your field
ashleyg February 25, 2021

Thanks again Martin for clarifying and putting together the code snippet.
I will be using single ggroup picker as my custom field to populate for the assignee user.
So if i were to implement the code you provided, what would be the complete code i can use. I'm not much into groovy and just trying to see if script runner can make it easier for me to implement. For now if i combine your code snippets, here is what i get, i am wondering how do i set the single group picker customfield (you can name it anything like xyz) to set the value that is not the filtered ones which means whatever remains other than jira-users and jira-software-users. We have only one other internal custom group name for each such assignee user so if we filter the common jira-users and jira-software-users, whatever is left out will be the actual group name for that assignee user.

def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find { assigneeField.equals(it.field) }
def filteredGroupsNames = ['jira-users', 'jira-software-users']
def userKey = change.newvalue
def user = ComponentAccessor.getUserManager().getUserByKey(userKey)
def userGroups = ComponentAccessor.getGroupManager().getGroupsForUser(user)
def filteredUserGroups = userGroups.findAll {!filteredGroupsNames.contains(it.name)}

if (change) {
def userKey = change.newvalue
...
}

ashleyg February 25, 2021

Ah my bad, i see the catch here :-
def filteredUserGroups = userGroups.findAll {!filteredGroupsNames.contains(it.name)}
so this will only assign the other group that i need, so i guess it should work.

so basically the only final step would be to set the filteredUserGroups to my single group picker custom field. How can i do that in script runner groovy? Doi have to retrieve the custom field id from jira and then use that , just looking for some basic guidance there.
Once i have it all, i will test it in our jira and confirm if that works.

Suggest an answer

Log in or Sign up to answer