Attempting to use groovy script to assign sub task via post function to specific project role.

Hello,

I am attempting to use groovy script to assign sub task via post function to specific project role. I am new to groovy script and seems simple enough but my programming experience is limited. I am about 3 months in on a new job with an existing JIRA instance. I recently recieved a ticket stating that a specific workflow was not assigning subtasks properly. checking the post function i see this.

Selected Script: Clones an issue and links. : Clones this issue to another issue, optioninally in another project, and optionally a different issue type.

Additional issue actions:

import com.atlassian.jira.project.Project

issue.summary = "Integrate: " + issue.summary

Project myProject = issue.getProjectObject()
def myLead = myProject.getLead()
issue.assignee = myLead

def sprintField = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Sprint'}
issue.setCustomFieldValue(sprintField, null)


### Result: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: issue for class: Script2

This code seems to attempt to assign the subtask to project lead but the code does not seem to be working at all when I run it through the script runner for testing. I am not sure

The idea is to get this to assign to a different unique project role. Any thoughts on if this is even the right way to persue this goal?

Thanks,

~Jon

3 answers

1 accepted

0 votes
Accepted answer

We camup with this last night. works like a charm.

import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager
import java.util.Set
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.roles.*


issue.summary = "Integrate: " + issue.summary

//Assign to the UX Lead
Project myProject = issue.getProjectObject()
ComponentManager componentManager = ComponentManager.getInstance()
ProjectRoleManager roleManager = componentManager.getComponent(ProjectRoleManager.class)
ProjectRole role = roleManager.getProjectRole("UX Lead")
ProjectRoleActors actors = roleManager.getProjectRoleActors(role, myProject)
Set<ApplicationUser> users = actors.getApplicationUsers()
ApplicationUser user = users.iterator().next()

issue.assignee = user.getDirectoryUser()

//Add to the current sprint
def sprintField = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Sprint'}
issue.setCustomFieldValue(sprintField, null)

Hi Jon, we are having the same problem here when we try to assign the issue to a user retrieved from a custom field using Groovy script in a workflo post function. Can you let me know what is the jira/script runner version you are using? thx We are on JIRA 6.4.5/SR 3.0

0 votes

I can see a couple of things here,

  • I think the script might be failing because you haven't defined "issue". If you're running it in the right place, issue should be defined for you (e.g. a post-function), but I don't know where you're running this or feeding it the issue
  • You say "assign to a different unique project role". I'm not sure I've understood this in full, but as you've written it, it will not work. You can't put anything other than users in the assignee field - assign to role won't work, you need a person.

for each project, we have many roles. Examples are, designers, artists, project lead, teachers, QA Lead, UX Lead etc. For this workflow, once a certain point is reached and the issue is ready for the next phase of work, we close the ticket and it spawns a new ticket assigned to the next specific role in the project.

example, once artists are done working on a specific task they pass the ticket to content integration team.

Context always helps a bit, but I'm still stuck on the problems:

1. Where are you running the script?

2. You can't assign to a role. Only a single person.

1. Running the script in a post function but testing it in "script runner".

2. So in other words I would need to hard code a username ?

thank you for your help.

Ah, ok, well, the script-runner box is lacking the context of an issue, so you may want to try inserting a specific issue into it.

Well, you don't need to hard code a single user name directly. You could do something a bit more clever in the script, like get a list of the users who are currently in that role, look for the last one of them to update the issue and assign it to that person, for example. It just needs to be a person you're assigning it to.

Okay, I appreciate the insight. No ideas on how to be clever with this as im struggling with syntax :) thanks for you help.

~Jon

I'd start by picking a volunteer and hard coding them in there! Once you can get the main "assign to user" to work, you can add the "work out who" bit later.

thank you again. Ill give it a try.

My boss and I came up with this last night, mostly my boss. Works like a charm. Hope others can use it as well.

import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager
import java.util.Set
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.security.roles.*


issue.summary = "Integrate: " + issue.summary

//Assign to the specific role
Project myProject = issue.getProjectObject()
ComponentManager componentManager = ComponentManager.getInstance()
ProjectRoleManager roleManager = componentManager.getComponent(ProjectRoleManager.class)
ProjectRole role = roleManager.getProjectRole("UX Lead")
ProjectRoleActors actors = roleManager.getProjectRoleActors(role, myProject)
Set<ApplicationUser> users = actors.getApplicationUsers()
ApplicationUser user = users.iterator().next()

issue.assignee = user.getDirectoryUser()

//Add to the current sprint
def sprintField = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Sprint'}
issue.setCustomFieldValue(sprintField, null)

Yup, that would do it - it'll assign the issue to the first user found in the role of "UX Lead".

That could be a little arbitrary of course (I'm not sure what order it would find multiple users in) and you might want to catch "no one is in the role" explicity, or you might get failed transactions if assignee is mandatory and it can't assign anyone

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

324 views 0 1
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you