Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get new user id added to the role from ProjectRoleUpdatedEvent?

ss
Contributor
January 25, 2021

Hi,

I am using ProjectRoleUPdatedEvent to catch whenever a user is added to a project. Below is the code I am using.

import com.atlassian.jira.event.role.ProjectRoleUpdatedEvent
def newUser = event.roleActors.roleActors.stream().map{actor -> actor.parameter}.collect()

It runs successfully and also returns the new user created. But it returns the user name instead of userid as below

[cadence.garfield, jira-users]

alongwith its other group membership.
can someone please help me to get only the userid. for example in this case the user id cadgr.

Help please.

Regards

2 answers

1 accepted

1 vote
Answer accepted
Hana Kučerová
Community Champion
January 25, 2021

Hi @ss ,

please, be aware this is the first time I work with this type of event - it seems to me it returns all the records from the project role after the change.

What I did is just get user records (exclude groups) and then find user based on the provided key.

I hope it helps...

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.role.ProjectRoleUpdatedEvent
import com.atlassian.jira.security.roles.RoleActor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

UserManager userManager = ComponentAccessor.getUserManager()
ProjectRoleUpdatedEvent event = event
Set<RoleActor> roleActors = event.getRoleActors().getRoleActors().findAll{RoleActor roleActor -> roleActor.getType() == "atlassian-user-role-actor"}
roleActors.each {RoleActor roleActor ->
ApplicationUser user = userManager.getUserByKey(roleActor.getParameter())
//work with user.getId()
}
ss
Contributor
January 25, 2021

Hi Hana,

First of all thank you so much for addressing my query. so much appreciated.

I am away from my PC now. But cannot wait to try your suggestion first thing tomorrow morning. 

I will update you. 

Thanks

Regards

Like Hana Kučerová likes this
ss
Contributor
January 25, 2021

Hi Hana, Good day.

So far so good. :) 

Now atleast I am atleast getting a user id in the input. After I ran your code, it gives me an output like this:

cadga(cadence.garfield)

I want to get only "cadga" 

So I tried to use the command which you have mentioned in the comment 

def newUserID=user.getID(user)

but it does not work. Sorry I am new to this Jira scripting so I am sure I am doing something wrong here.

But I created a method to truncate the userid. So now my code looks as below:

*****

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.role.ProjectRoleUpdatedEvent
import com.atlassian.jira.security.roles.RoleActor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager

UserManager userManager = ComponentAccessor.getUserManager()
ProjectRoleUpdatedEvent event = event
Set<RoleActor> roleActors = event.getRoleActors().getRoleActors().findAll{RoleActor roleActor -> roleActor.getType() == "atlassian-user-role-actor"}
roleActors.each {RoleActor roleActor ->
ApplicationUser user = userManager.getUserByKey(roleActor.getParameter())
//work with user.getId()
}

//method to truncate user variable to get only user id

static String extractUserId(String s) {
int index;
for (index = 0; index < s.length(); index++) {
if (s.charAt(index) == "(") {
break;
}
}
return s.substring(0, index);
}

 

//call truncate truncate method

newUserID = extractUserID(user) 

*****

but it gives error as below:

[Static type checking] - Cannot find matching method

Script123#extractUserID(com.atlassian.jira.user.ApplicationUser). Please check if the declared type is correct and if the method exists.

I don't know why it is not able to find the method which I created.

Hana Kučerová
Community Champion
January 25, 2021

Hi @ss ,

which attribute of user is "cadga"? Previously you were refering to it as id, but id is number. So do you want to get name of the user?

Maybe it would help if you tell me what you want to do with this user data next.

extractUserId doesn't work because it expects string, but user is object.

 

def newUserID=user.getID(user) 

It is not possible to call it like this:

  • getID vs. getId (getID is invalid, you need to change it to getId)
  • there's no parameter - you call getId on user object 
user.getId()

Only this format will work.

Also please be aware there's a loop. If there's more then one user in your project role, you will get more then one user.

ss
Contributor
January 26, 2021

Hi Hana,

Ok. so the thing is that I want to catch when an individual user which is added to a role and remove it immediately.

cadga is the login id. I mean user logs in with this. And, I guess I need this to remove it from the role. But what I get is cadga(cadence.garfield) . I don't know what is cadence.garflied. a user name or full name. Because full name should be Cadence Garflied.

with loop, i guess it would be OK because we anyway do not want any individual users to be added. So if all such users are returned, i will remove all of them.

Regards

Hana Kučerová
Community Champion
January 26, 2021

Hi @ss ,

thank you. I will try to prepare the whole script (hopefully today) and let you know.

Like ss likes this
ss
Contributor
January 26, 2021

Hi @Hana Kučerová 

Really !!! ..wowww...

Head bowed down...

:)

Regards

ss
Contributor
January 26, 2021

you were right...i need to use user.getId()....

so that gave me an idea to do some hit and trials.. and one of them worked.

user.getUsername()... 

that returns cadga 

:)

Hana Kučerová
Community Champion
January 26, 2021

Hi @ss ,

please try this:

import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.role.ProjectRoleUpdatedEvent
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.project.Project
import com.atlassian.jira.util.SimpleErrorCollection

final String USER_ROLE_ACTOR = "atlassian-user-role-actor"

ProjectRoleUpdatedEvent event = event
ProjectRoleService projectRoleService = ComponentAccessor.getComponentOfType(ProjectRoleService.class)
Project project = event.getProject()
ProjectRole projectRole = event.getProjectRole()
List<String> userKeys = event.getRoleActors().getRoleActors()
.findAll{it.getType() == USER_ROLE_ACTOR}
.collect{it.getParameter()}
SimpleErrorCollection errorCollection = new SimpleErrorCollection()
projectRoleService.removeActorsFromProjectRole(userKeys, projectRole, project, USER_ROLE_ACTOR, errorCollection)
for (String errorMessage : errorCollection.getErrorMessages()) {
log.error(errorMessage)
}

It seems to me it is working, but I didn't test it much. Added single users should be removed immediately with all the other single users with the same project role in the project.

ss
Contributor
January 26, 2021

Hi @Hana Kučerová 

I am right away on it.

will provide you the update soon.

:)

Regards

ss
Contributor
January 26, 2021

Hi @Hana Kučerová ,

Its working like a charm. The moment I add any user to any role to a project, it just removes it immeidately. Infact it even removes if I add multiple users at a time.

Cant thank you enough...

you saved my day...

Thanks a ton.....

thanks.jpg

Like # people like this
Hana Kučerová
Community Champion
January 26, 2021

Hi @ss ,

thank you very much, this is so nice of you! It definitely made my day!

ss
Contributor
January 29, 2021

Hi @Hana Kučerová ,

Hope you are doing fine.

With your help I have manged create listener script which removes both added user or any group. 

can you help me a little more I am struggling with. 

I just want to know how to detect in role update event, if it is a User add or User remove event.

I am not able to find any class or method for that.

 

Regards

ss

Hana Kučerová
Community Champion
January 29, 2021

Hi @ss ,

it seems to me the only possible way is to compare size of original and current role actors like this:

import com.atlassian.jira.event.role.ProjectRoleUpdatedEvent
import com.atlassian.jira.security.roles.RoleActor

ProjectRoleUpdatedEvent event = event
Set<RoleActor> originalRoleActors = event.getOriginalRoleActors().getRoleActors()
Set<RoleActor> roleActors = event.getRoleActors().getRoleActors()
if (roleActors.size() > originalRoleActors.size()) {
// we are adding
} else {
// we are removing
}
ss
Contributor
January 29, 2021

@Hana Kučerová 

damn.... again worked in one shot..

did you make Atlassian API...or did you drink it all...  

:)

thanks again...

enjoy your weekend...

highest regards.

ss

Like Hana Kučerová likes this
WW
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.
September 30, 2021

Some good tips in there!

Just FYI, there are already constants for the actor types, so no need to declare your own. ;)

https://docs.atlassian.com/software/jira/docs/api/8.19.1/constant-values.html#com.atlassian.jira.security.roles.ProjectRoleActor.GROUP_ROLE_ACTOR_TYPE

0 votes
ss
Contributor
January 25, 2021

Anyone, who can help me.., please..

Suggest an answer

Log in or Sign up to answer