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
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()
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
:)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Some good tips in there!
Just FYI, there are already constants for the actor types, so no need to declare your own. ;)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.