Script to assign issue to user as post function based on Project Role

In our company I have a situation that once the task as been completed by the current user it should be re-assigned to another user for review.

We define the 'Reviewer' in the issue but there is the possibility that the person who finished the task could end up reviewing their own work, which isn't acceptable. For this reason I want to follow this logic...

if(current assignee ! reviewer){
	assign to reviewer
}else if(current assignee == reviewer){
	if(current assignee not in project role 'technical lead'){
		assign to member in project role 'technical lead'
	}else{   
		assign to member in project role 'engineering manager'
	}
}

I have tried to use some of the more simple post functions but these will only do so much, I am at the realisation that I need to create a proper groovy script for this but have no idea where to start.

I have found a number of examples of scripts for assigning issues to users but haven't much about handling the Project Roles etc.

2 answers

1 accepted

here's a script that i use where i did something similar (but not identical). if reviewer is filled, it reassigns on the workflow transition. if reviewer not filled, it fills in with the current assignee. you can tweak it to get what you're after.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Category

log = Category.getInstance("com.custom.Reviewer")
log.setLevel(org.apache.log4j.Level.WARN)
log.debug("---- begin reviewer post-fuction -----")

def assignee = issue.getAssignee()
def reviewerCustomField = ComponentManager.getInstance().getCustomFieldManager().getCustomFieldObjectByName("Reviewer")
def reviewer = issue.getCustomFieldValue(reviewerCustomField)
log.debug("assignee: $assignee || reviewer: $reviewer")

if (reviewer == null) {
	log.debug ("Empty reviewer.  Set to assignee.")
	MutableIssue myIssue = issue
	IssueManager issueManager = ComponentAccessor.getIssueManager()
	UserManager userManager = ComponentAccessor.getUserManager()
	myIssue.setCustomFieldValue(reviewerCustomField, assignee)
	issueManager.updateIssue(userManager.getUser("automation"), myIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
log.debug("---- end reviewer post-fuction -----")

oops. i misspoke. this code sets reviewer where it's empty. this post-function is executed and then another post-function does the reassign as illustrated below.

Thanks, this helped me a lot!

Final code...

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleActors
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.info("Starting GroovyScript AssignToReviewer")

MutableIssue issue = issue
Project project = issue.getProjectObject()
UserManager userManager = ComponentManager.getComponentInstanceOfType(UserManager.class) as UserManager
def currentAssignee = issue.getAssignee()
def assignee = null

log.debug("Working with Issue " + issue.getKey() + " ("+project.getName()+"), currently assigned to " + currentAssignee.getDisplayName())

CustomField reviewerCustomField = ComponentManager.getInstance().getCustomFieldManager().getCustomFieldObjectByName("Reviewer")
def reviewerID = (User)issue.getCustomFieldValue(reviewerCustomField)
def reviewer = null

if(reviewerID != null){
    reviewer = userManager.getUserByName(((User)reviewerID).getDisplayName())
    log.debug("Current defined reviewer is " + reviewer.getDisplayName())
}else{
    log.error("Reviewer is null")
}

ComponentManager componentManager = ComponentManager.getInstance()
ProjectManager projectManager = componentManager.getProjectManager()
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager

// Get Technical Lead Project Role
ProjectRole techLeaderProjectRole = projectRoleManager.getProjectRole("Technical Leader")
ProjectRoleActors techLeaders = projectRoleManager.getProjectRoleActors(techLeaderProjectRole, project)
log.debug "Members of project role 'Technical Leaders' ${techLeaders.getUsers()*.name}";
def techLeader = techLeaders.getUsers().toList().first()
log.debug("Fallback Technical Leader is "+techLeader.getDisplayName())

// Get Engineering Manager Project Role
ProjectRole engManProjectRole = projectRoleManager.getProjectRole("Engineering Manager")
ProjectRoleActors engManagers = projectRoleManager.getProjectRoleActors(engManProjectRole, project)
log.debug "Members of project role 'Engineering Manager' ${engManagers.getUsers()*.name}";
def engManager = engManagers.getUsers().toList().first()
log.debug("Fallback Engineering Manager is "+engManager.getDisplayName())

def projectLead = project.getLeadUser()
log.debug("Fallback Project Lead is "+projectLead.getDisplayName())

if(reviewer == null){
    log.error("Reviewer is null, will be assigned to Technical Leader")
    if(((User)techLeader).getDisplayName().matches(((User)currentAssignee).getDisplayName())){
           log.debug("Technical Leader and Reviewer are the same person; will assign to Engineering Manager")
            if(((User)engManager).getDisplayName().matches(((User)currentAssignee).getDisplayName())){
                log.debug("Engineering Manager and Reviewer are the same person; will assign to Project Lead")
                assignee = projectLead
            }else{
                assignee = engManager
            }
    }else{
       assignee = techLeader
    }
}else{
    log.debug("Will be assigned to Reviewer")
    if(((User)reviewer).getDisplayName().matches(((User)currentAssignee).getDisplayName())){
        log.debug("Assignee and Reviewer are the same person; will assign to Technical Leader")
        if(((User)techLeader).getDisplayName().matches(((User)currentAssignee).getDisplayName())){
            log.debug("Technical Leader and Reviewer are the same person; will assign to Engineering Manager")
            if(((User)engManager).getDisplayName().matches(((User)currentAssignee).getDisplayName())){
                log.debug("Engineering Manager and Reviewer are the same person; will assign to Project Lead")
                assignee = projectLead
            }else{
                assignee = engManager
            }
        }else{
            assignee = techLeader
        }
    } else{
         assignee = reviewer
    }

}
issue.setAssignee(assignee)
issue.store()

I am new to the JIRA API and Groovy, if anyone has any input on my solution to make it more eloquent or exception safe then I would appreciate any input.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

71 views 0 5
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