Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Post-Function to Assign Issue/Subtask to Project Role based on Component

Kim Rivera October 1, 2015

Hi all,

I am trying to develop a post-function groovy script that would basically run after the issue is created.

It would do the following:

  • Check the Component of the Issue created.
  • Based on that component, assign the issue to a Project Role.

So far, what I have is this but i'm not sure where exactly I am wrong since this one doesn't work. It's not assigning to a default member in the Project Role.

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserManager

def assignee = null
MutableIssue issue = issue
Project project = issue.getProjectObject()
ProjectRole role = null

if(issue.getComponentObjects().getName().equals("ProductA"))
{
    role = projectRoleManager.getProjectRole("ProdADevelopers")
}
else if (issue.getComponentObjects().getName().equals("ProductB"))
{
    role = projectRoleManager.getProjectRole("ProdBDevelopers")
}
else
{
    role = projectRoleManager.getProjectRole("GeneralPool")
}

ProjectRoleActors roleMembers = projectRoleManager.getProjectRoleActors(role, project)
assignee = roleMembers.getUsers().toList().first()
    
issue.setAssignee(assignee)
issue.store()

 

Any help would be greatly appreciated! Thanks so much!

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
JamieA
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.
October 1, 2015

I think you just want:

if ("ProductA" in issue.componentObjects*.name)  { ...

Using "def" is fine.

No need for issue.store if your function is before the system one that stores it.

toList() is this: http://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Iterable.html#toList()

Jeremy Gaudet
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.
October 1, 2015

Cleaner than my approach. Re: "toList()", the Set returned by the DefaultRoleActors doc linked to the standard java doc; I checked the parents for "toList()", but didn't think to check for a non-standard parent. That makes tracking those down difficult, perhaps Groovy should provide a doc for all standard implementors of their custom interfaces so Atlassian can link to a Groovy doc with the correct methods, instead of the standard doc. Either way, thanks for that reference; now that I'm aware of it, I can at least check for it explicitly.

1 vote
Jeremy Gaudet
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.
October 1, 2015

High level, I think "getComponentObjects()" is going to return a list or collection, you'll want to iterate through it and decide what precedence each component has (say, if two are selected, which one do you want to assign based on?).  Also, I think "assignee" is going to need to be a "User" object (com.atlassian.crowd.embedded.api.User).  That may work as is, but I think you may as well instantiate it as such, instead of "def".

Finally, I don't see that "Set" has a "toList()" method, it appears to be "toArray()"... at least, following the doc link from https://docs.atlassian.com/jira/6.4.3/com/atlassian/jira/security/roles/DefaultRoleActors.html .

If you don't have access to JIRA logs (those will show an exception for any failures), what I do is add a hard-coded side effect such as assigning to myself, and add that line first, comment out the rest, and then uncomment them one line at a time moving the self-assignment to the bottom.  If it fails to self-assign, then you know the uncommented line is failing, otherwise it's good and you can move on to the next one.  That's much easier for a validator than a post operation, but the concept still applies.

0 votes
ChrisR October 19, 2015

I think my issue is associated but I am looking to auto assign in a transition a user based on the component & role. Is there an add-on or am I missing something. I thought I would be able to add a post function to my transition by using the update issue field setting but you can not use a Role for assignee. Another option I thought of was adding a new Post Function Transition to the list... It would be nice to have a pick option for Assign to Lead QA... where is the love for QA? You can assign a lead developer but not a lead QA?

TAGS
AUG Leaders

Atlassian Community Events