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

How can I get ACTIVE notification when a project role has user or group added to it?

Mike Rathwell
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.
January 3, 2019

Please don't get me started on the why I need this as that way lay madness. However, I have managed to get part of a requirement answered with a listener that actively notifies when a Jira group is updated and need to do the same thing when a project ROLE is updated.

The piece I have working (and need the same for project role changes) is when a user is added to a group using the "GroupMembershipCreatedEvent" event in a groovy script listener. The code below looks for additions to a SPECIFIC group (I only have been told to care about this particular one and have a separate one for when humans are REMOVED from the group).

import com.atlassian.crowd.event.group.GroupMembershipCreatedEvent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.mail.Email
import com.atlassian.mail.queue.SingleMailQueueItem

def event = event as GroupMembershipCreatedEvent
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (event.groupName == "GroupIcareAbout"){
def email = new Email("whoWantsToKnow@address.com"
email.setSubject("User added to the ${event.groupName} group")
email.setBody("User ${event.entityName} has been added to Jira's ${event.groupName} group by ${currentUser.displayName}" )
email.setMimeType("text/html")

SingleMailQueueItem item = new SingleMailQueueItem(email)
ComponentAccessor.getMailQueue().addItem(item)
}

How do I do the same thing but for project role changes? I tried, with trolling the API to use the seemingly equivalent "RoleMembershipCreatedEvent" event but no soap so far. Should I be using the "ProjectRoleUpdatedEvent" and root it out that way? Anyone with a script fragment or example that might be able to help?

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Aleksandr Zuevich
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.
January 5, 2019

Hi MIke,

Yes, you can use ProjectRoleUpdatedEvent which has project field also with projectRole and originalRoleActors and roleActors which contain UserRoleActors or GroupRoleActors where parameter is String userKey or String groupName respectively.

So the analogue for ProjectRoleUpdatedEvent could be:

import com.atlassian.jira.event.role.ProjectRoleUpdatedEvent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.mail.Email
import com.atlassian.mail.queue.SingleMailQueueItem

def event = event as ProjectRoleUpdatedEvent

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def project = event.project
def role = event.projectRole

if (role.name == "RoleIcareAbout" && project.key == "ProjectIcareAbout"){
def email = new Email("whoWantsToKnow@address.com")

email.setSubject("Role ${role.name} has been updated for ${project.key} project")
def newUserKeys = event.roleActors.roleActors.stream().map{actor -> actor.parameter}.collect()
def oldUserKeys = event.originalRoleActors.roleActors.stream().map{actor -> actor.parameter}.collect()
email.setBody("Jira's ${role.name} role for ${project.key} project has been updated by ${currentUser.displayName}. Original user keys or group names: ${oldUserKeys}, new user keys or group names: ${newUserKeys}" )
email.setMimeType("text/html")

SingleMailQueueItem item = new SingleMailQueueItem(email)
ComponentAccessor.getMailQueue().addItem(item)
}
Mike Rathwell
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.
January 7, 2019

Thanks @Aleksandr Zuevich. THAT gets me to the bit I couldn't get to. In the interim (and to get them at least partly off my back) I had come up with a subset of that which at least said that a role changed and who did it. This gets me the WHAT changed.

Rodolfo So February 8, 2022

Hi @Aleksandr Zuevich 

Do you know how to change userkey to user display name in the output?

Thanks

TAGS
AUG Leaders

Atlassian Community Events