Show the list of all Project Admin Role "users" on project summary page.

Manu Mishra December 21, 2017

I want to show the list of all Project Admin "Users" on project summary page. As i have created REST Endpoints and below is my code.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.user.ApplicationUsers
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import com.atlassian.jira.bc.projectroles.DefaultProjectRoleService
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger

@@BaseScript CustomEndpointDelegate delegat

ProjectAdmins() { MultivaluedMap queryParams ->

// get a reference to the current page...
// def page = getPage(queryParams)

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

long projectRoleId = 10050 // ID of Automatic Watcher project role
def role = projectRoleManager.getProjectRole(projectRoleId)
def actors = projectRoleManager.getProjectRoleActors(role, Issue.getProjectObject())
def usersInRole = actors.getApplicationUsers();
def projectPermissionList = new StringBuffer()
projectPermissionList.append("Test value; ")
//for (g in usersInRole) {
//projectPermissionList.append(g.getName()+"; ")
//groupList.append("<br>")
//}

def dlgHeader = """<div>
<b><u>The Puspose of this Diaglog is print the Project Admins</u></b>
</div>"""
def dialog =
"""<section role="dialog" id="sr-dialog" class="aui-layer aui-dialog2 aui-dialog2-medium" aria-hidden="true" data-aui-remove-on-hide="true">
<header class="aui-dialog2-header">
<h2 class="aui-dialog2-header-main"><b>Project Admin List</b></h2>
<a class="aui-dialog2-header-close">
<span class="aui-icon aui-icon-small aui-iconfont-close-dialog"><b>Close</b></span>
</a>
</header>
<div class="aui-dialog2-content">
""" + dlgHeader + """
""" + projectPermissionList + """
</div>
</section>
"""

Response.ok().type(MediaType.TEXT_HTML).entity(dialog.toString()).build()
}

The Above script is not working though there is no compilation error. But the script above is not returning the desired outputs. As far as my understanding says there is an error in the line 

def actors = projectRoleManager.getProjectRoleActors(role, Issue.getProjectObject())

Please help me debugging the script. If possible can you modify the script.

1 answer

1 accepted

1 vote
Answer accepted
Alexey Matveev
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.
December 21, 2017

Issue.getProjectObject() will not work. You need to get a reference to an issue first. For example

def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("STAR-1")

and then you can use

def actors = projectRoleManager.getProjectRoleActors(role, issue.getProjectObject())

Manu Mishra December 21, 2017

Thanks @Alexey Matveev The script is working untill 

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

long projectRoleId = 10050 // ID of Automatic Watcher project role
def role = projectRoleManager.getProjectRole(projectRoleId)
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DevOps")

What you are referring <("STAR-1")>. Do i have to pass the "issue key"  (e.g. devops-1).
So when i will be passing the issue key, will it return the project role admin users?

Alexey Matveev
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.
December 21, 2017

You should provide "DEVOPS-1". It should work after it.  Of course the issue with the key DEVOPS-1 must exist in your Jira instance

Manu Mishra December 21, 2017

So do i have to pass the key very time?

My requirement is whenever someone walks into any project summary page they should click on the button "Project Admin" (Right Corner) and that will display the list of Project Admin Role "Users" in comma delimited string.

Manu Mishra December 21, 2017

@Alexey MatveevProjectRole.JPG

If you look at the URL the project is "DevOps" and under the Projet Roles Admin there is only one user (Manoj Mishra). So my requirement is whenever someone click one the Project Admin button in the project summary page. He should only get one User which is (Manoj Mishra). But the scripts is returning all the users who have Project Role Admin permission irrespective of any projects. I only want the users to be returned against the projects.

Alexey Matveev
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.
December 21, 2017

I really do not understand the architecture what you are doing there. Your question was about the error in your script. I answered about the error. The problem is that you should initialize the issue. How you do it depends on how the whole thing, you are developing, works. 

If you want your code to work, you should pass to your code either a project key or issue key and get the project. 

Manu Mishra December 26, 2017

Thanks @Alexey Matveev for your kind help.

Suggest an answer

Log in or Sign up to answer