Show a list of projects and the users-roles of those project on a JIRA dashboard (groovy script)

Shahriar July 22, 2019

Hi folks, 

Here is what I am trying to accomplish. I am trying to somehow display a list of selected JIRA projects and the user-roles of those project on a JIRA dashboard. I have looked into this and I couldn't find any dashboard gadget that could give me that. I think the only way I can get this information is by writing groovy script which I am not good at :( .  I was wondering if anyone could help me write a groovy script so that I could display the list I am asking for. I don't mind have a scripted field to show that information in a JIRA issue. I could create a story or task for each of those project and list those stories on the dashboard which would show that scripted field. 

I honestly don't care about how it should look but here is an example what I am trying to accomplish. I will also attach a screenshot. 

Project

Developers

Scrum Master

Product Owner

Project A

Tom, Adam, Julia

Robert

Lew

Project B

Cynthia, Melissa, Jared

John

Terri

Project C

 

 

 

 

 

 

 

Thank you so much! 

- Shahriar

Screen Shot 2019-07-22 at 1.29.27 PM.png

Thank you so much! 

- Shahriar

2 answers

1 accepted

1 vote
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 25, 2019

Hi @Shahriar ,

That looked fun so I gave it a try, here is a script to display this table in the issue description using behaviours. So of course you would have to adapt it to display it in a appropriate way/place (not sure about that).

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.ComponentManager
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours


def desc = getFieldById("description")

String table = "||Project||"
def projects = ["A","B", "C"]

def projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager

def projectRoles = projectRoleManager.getProjectRoles()
projectRoles.each {
table += it.getName() + "||"
}

projects.each {
def project = ComponentAccessor.getProjectManager().getProjectObjByKey(it)
table += "\n||" + it + "|"
projectRoles.each {
def actors = projectRoleManager.getProjectRoleActors(it, project).getUsers().collect {
it.getDisplayName()
}

table += (actors.size() == 0?" ":actors.join(",")) + "|"
}
}
desc.setFormValue(table)

Antoine

Shahriar Kabir July 25, 2019

Hi Antoine, 

Man you are freaking genius!

I used your script but for some reason it gives me the data for all projects instead of just those three, for e.g. A, B and C.

  • Is there a way to just show selected projects instead of all?
  • Is there a way to show users from just selected roles instead of all the roles. For e.g. show the users in the "Developers, QA, Scrum Masters" role. 
  • One more request please, is there a way for it to auto update by itself whenever there is a change in the users and roles. For e.g. if I remove User A from "Developers" role, can it automatically update instead of me creating another story or issue to get the new information?

FYI - I believe the following class has been depreciated in the newer version of JIRA. It keeps giving error.

import com.atlassian.jira.ComponentManager

So I had to remove that and also had to make this change: 

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class) as ProjectRoleManager

Thank you so much for your help, Sir! 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 26, 2019

Hi @Shahriar Kabir ,

I wish I were a genius! :)

Regarding your questions : 

  • You should only see projects A B and C (note that you need to use the key of the project). If the key is not valid it will return an error, so not sure how you managed to display all the projects (maybe need to investigate with the logs).
  • Sure, updated script below.
  • To my knowledge there is no "role updated" event. So you would need to either edit your issue to generate the new description. Or you could use a post-function in a transition so you just need to click a button (You would need to adapt the script a bit though).

Good job seeing that deprecation, I am working on 7.6.0, so I will keep an eye on that when I upgrade. :)

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.security.roles.ProjectRoleManager
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours


def desc = getFieldById("description")

String table = "||Project||"
def projects = ["A","B", "C"]
def roles = ["Administrators","Developers"]

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager.class) as ProjectRoleManager

def projectRoles = projectRoleManager.getProjectRoles().findAll{ it.getName() in roles }
projectRoles.each {
table += it.getName() + "||"
}

projects.each {
def project = ComponentAccessor.getProjectManager().getProjectObjByKey(it)
table += "\n||" + it + "|"
projectRoles.each {
def actors = projectRoleManager.getProjectRoleActors(it, project).getUsers().collect {
it.getDisplayName()
}

table += (actors.size() == 0?" ":actors.join(",")) + "|"
}
}
desc.setFormValue(table)

Antoine

Like # people like this
Shahriar Kabir July 26, 2019

Hi @Antoine Berry , 

Thank you again so much. I will try the updated script you sent. Thank you! 

 

- Shahriar

0 votes
Shahriar July 27, 2019

Hi @Antoine Berry , thank you again so much for your help!

- Shahriar

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 29, 2019

You're welcome ! :)

Like sanjay likes this

Suggest an answer

Log in or Sign up to answer