Jira Script runner - add user to project role

I am working with JIRA Service Desk plugin and JIRA script runner. I am trying to implement a script that will add new users based off people that are CCed on the original email to the Service Desk and add them to the ticket.

I have a external python script that gets the emails, but I dont know how to add the new users i am creating to a project role?

Below is my current script, The commented section is where I am trying to add new users:

 

String[] emails = output.split(",");

List<ApplicationUser> requestParticipants = [];

for(int i = 0; i < emails.length; i++){
    
    def new_user = UserUtils.getUserByEmail(emails[i])
    if(!new_user){
    	ApplicationUser user = userUtil.getUserByName(new_user.getName())
		requestParticipants.add(user);
    }
	else{
       
		String uuid = UUID.randomUUID().toString().replaceAll("\\W+", "");
        userUtil.createUserNoNotification(emails[i], uuid, emails[i], emails[i])
		ApplicationUser user = userUtil.getUserByName(new_user.getName())
		// how can I assign them to the project role named Service Desk Customers
		requestParticipants.add(user);        
    }
}

1 answer

Hi Daniel,

You can try something like this. 

import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleActor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.util.SimpleErrorCollection

def projectManager = ComponentAccessor.getProjectManager()
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def errorCollection = new SimpleErrorCollection()

def project = projectManager.getProjectObjByKey("PROJKEY")
def projectRole = projectRoleManager.getProjectRole("Service Desk Customers")
def actors = ['actor1Key', 'actor2Key']
projectRoleService.addActorsToProjectRole(actors,
        projectRole,
        project,
        ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
        errorCollection)

The code is not tested, but I think it can give you a hint.

Regards

Thanos

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

825 views 2 9
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