Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,295,762
Community Members
 
Community Events
165
Community Groups

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

Mike Rathwell Community Leader Jan 03, 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

0 votes
Answer accepted

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 Community Leader Jan 07, 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.

Hi @Aleksandr Zuevich 

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

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

308 views 0 9
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you