Creating workflow post functions with Script Runner to assign to member of a group

S September 1, 2015

Hello,

I'm looking for a couple post functions to assign issues automatically to certain group members, specifically:

  • Ability to assign the issue to the last member of a specific group that was previously assigned to it.
  • Ability to assign the issue to a random member of a specific group (excluding self).

I've installed the Script Runner add-on, but while I see it has post functions like the aboves, they're project role based rather than group. Project roles aren't really going to work for me.

Could somebody please provide a custom scripted post function to be able to do the above?

Thanks!

9 answers

2 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 5, 2015

Hi Sebastiono,

In order to set the Assignee to random member of a group you will need to use a custom script post function.

I have posted below an example script which shows how to set the Assignee to a random member of a group.

This code can also be modified to set the assignee to the last member of a group.

 

import com.atlassian.jira.component.ComponentAccessor
import java.util.Random
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;

// Get the current logged in user
user = ComponentAccessor.getJiraAuthenticationContext().getUser().getName()

//Get a pointer to the current Issue
def issueManager = ComponentAccessor.getIssueManager();
Issue issueKey  = issue
def id=issueKey.getId()

// Work out the size of the Group
def groupManager = ComponentAccessor.getGroupManager()
def GroupSize = groupManager.getUsersInGroup("jira-users").size()

// Pick a Random number to determine the member of the Group to assign the issue to
Random Rand = new Random()
int Max = GroupSize
def RandomMember = Rand.nextInt(Max+1)

// Get the member details for the random number selected and assign the issue to them if its not the current user
def Assignee = groupManager.getUsersInGroup("jira-users").get(RandomMember).getName()
if (Assignee != user){
    issue.setAssigneeId(Assignee)
// If the user picked is the current user get the next user
}else{
    def AltRandomMember = RandomMember +1
    def AltAssignee = groupManager.getUsersInGroup("jira-users").get(AltRandomMember).getName()
    issue.setAssigneeId(AltAssignee)
}
issue.store()

 

I hope this helps.

Thanks

 

Kristian Walker

0 votes
S October 21, 2015

Hi Kristian,

I added that code but the issue still happened. Then I added the same code also for AltRandomMember, repeated the test 20 times, and the error never come up again. So looks like that did it. Thanks!

Just one more question. Shouldn't:

(RandomMember >= Max)

Be just > rather than >=? Isn't >= going to prevent the last member of the group from ever being selected?

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 21, 2015

Yes you can have (RandomMember > Max). Kristian

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 20, 2015

Hi Sebastiano,

I have looked at replicating this locally and it looks as if you are hitting the last member in your group and that is why picking a random member is failing.

This can be fixed by adding a simple condition similar to below which picks a different member if the random member picked exceeds the size of the group. I placed this condition between lines 23 and 27 in my script locally and I did not encounter your error.

 

if (RandomMember >= Max){
 // If Random Number tries to pick a member larger than the group size then pick a member from the middle of a group by subtracting a provided number
    RandomMember = Max - 2 // Configure this number depending on how many users you have in your group.
}

 

Also having a group of 2 will never pick a random member as if the assignee is the current user it will always pick the other user in the group so you would need more than users in the group for this code to be effective.

I hope this helps

Thanks

Kristian

0 votes
S October 20, 2015

Hi Kristian,

When the script fails to reassign the issue and leaves it assigned to the current user these entries appear in the logs:

ERROR svassellatti 340x7372x1 ftjc32 /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] *************************************************************************************
ERROR svassellatti 340x7372x1 ftjc32 /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: TST-20234, actionId: 821, file: <inline script>
java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
        at java_util_List$get$4.call(Unknown Source)
        at Script1.run(Script1.groovy:24)

The script is run as an inline script, and the post function is the second in the list.

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 19, 2015

Hi Sebastiano,

The script works for me on my instances.

Can you please post your log file entry from when the script is run as well as how you have the script configured.

Thanks

Kristian

0 votes
S October 18, 2015

Hi Kristian,

After using the script you provided in production, it appears that the bit of code to prevent the issue from being assigned to the current user is not working. I tried to look into that but couldn't figure out what's wrong. Could you please help me on that?

0 votes
S October 7, 2015

Hi Kristian,

Apologies for my delay in responding but it appears this system limits my posting to once every 24 hours.

Unfortunately using project roles isn't an option for us, because we don't use project roles for.. well, roles. We're looking at this ability only for a specific category of projects where the people most likely to reopen the issues do not have the Assign Issues permission, which is why we're looking at having this happen automatically.

0 votes
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 6, 2015

Hi Sebastiano,

 

The best way to do this would be to map your developers group to the developers role of the project and then simply add a  Assign to last role member Post Function onto the ReOpen transition of the workflow.

This would allow you to specify extra parameters as well such as overriding the assignee if a different assignee has been specified on the re open issue screen.

Thanks

 

Kristian

0 votes
S October 6, 2015

Hi Kristian,

Thanks a lot, that is working perfectly.

 

"This code can also be modified to set the assignee to the last member of a group."

Hmm, would you mind providing me with the script code for that too? What I'm trying to do is for the following scenario:

A developer (member of the "Developer" group) resolves an issue, then a tester tests it. If the tester reopens it, I would like the issue be automatically re-assigned to the user member of the Developer group that was last assigned to it previously.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events