Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script to Add Project Role to Permission in all Permission Schemes

William Tanner June 16, 2023

I am working on a script to add a project role to the Browse Projects permission in all permission schemes.  Currently, I can do it for one project at a time:

 

import com.atlassian.jira.permission.JiraPermissionHolderType
import com.atlassian.jira.permission.ProjectPermissions

def project = Projects.getByKey('RBM')

def permissionscheme = project.permissionScheme
// gives the View Only role Browse Projects permission
permissionscheme.addPermission(ProjectPermissions.BROWSE_PROJECTS, JiraPermissionHolderType.PROJECT_ROLE, '10700')
How can I do it for all projects, or even better, all Permission Schemes?

2 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
June 17, 2023

Hi @William Tanner 

Welcome to the Community.

Great to see you are using ScriptRunner HAPI features. :-)

You are on the right track. The main modification you will need to make is instead of using:-

def project = Projects.getByKey('RBM')

to invoke all projects, you will need to use:-

def projects = Projects.allProjects

Below is an updated example code for your reference:-

import com.atlassian.jira.permission.JiraPermissionHolderType
import com.atlassian.jira.permission.ProjectPermissions
import com.adaptavist.hapi.jira.projects.Projects
 
def projects = Projects.allProjects

projects.each {
def permissionscheme = it.permissionScheme
permissionscheme.addPermission(ProjectPermissions.BROWSE_PROJECTS, JiraPermissionHolderType.PROJECT_ROLE, '10700')
}

So invoking the allProjects method will return a Collection of all the projects. 

You will then need to iterate through the list, i.e. using the each parameter as shown above and using the it variable to invoke the iteration and update all the projects.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

LMI Atlassian
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 20, 2023

Hi Ram,

Thanks for the script.  It works for some projects, but we noticed it is not updating every permission scheme.  Any idea why it would only update some permission schemes and not all? It appears to have update 54 projects (per the "Result" in the script), but not every project/permissions scheme.

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
June 20, 2023

Hi @LMI Atlassian

From what you have mentioned, it may be possible that not all the projects are using the same Permission Scheme.

Try modifying the code to:-

import com.atlassian.jira.permission.JiraPermissionHolderType
import com.atlassian.jira.permission.ProjectPermissions
import com.adaptavist.hapi.jira.projects.Projects

def projects = Projects.allProjects

projects.each {
def permissionscheme = it.permissionScheme
permissionscheme.addPermission(it.permissionScheme, ProjectPermissions.BROWSE_PROJECTS, JiraPermissionHolderType.PROJECT_ROLE, '10700')
}

and see if it makes a difference.

Also @William Tanner, if your question has been answered, please accept the solution.

Thank you and Kind regards,

Ram 

William Tanner June 21, 2023

Hi Ram,

Appreciate the response and, yes, we do have multiple permission schemes were are trying to update. When I tried the updated script you provided, I get an error: 

 

Something went wrong: MissingMethodException

No signature of method: com.atlassian.jira.permission.data.PermissionSchemeImpl.addPermission() is applicable for argument types: (com.atlassian.jira.permission.data.PermissionSchemeImpl, com.atlassian.jira.security.plugin.ProjectPermissionKey...) values: [PermissionSchemeImpl{id=10100, name=LMI Permission Scheme A, description=This is the default Permission Scheme. Any new projects that are created will be assigned this scheme., permissions=[PermissionGrantImpl{id=10500, holder=(PROJECT_ROLE, some(10101)), permission=ADD_COMMENTS}, PermissionGrantImpl{id=10504, holder=(PROJECT_ROLE, some(10101)), permission=BROWSE_PROJECTS},

 

And, then it has the following as a possible solution:

Possible solutions: addPermission(com.atlassian.jira.security.plugin.ProjectPermissionKey, com.atlassian.jira.permission.PermissionHolderType, java.lang.String), addPermission(com.atlassian.jira.security.plugin.ProjectPermissionKey, com.atlassian.jira.permission.PermissionHolderType), getPermissions(), getPermissions(), hasPermission(com.atlassian.jira.security.plugin.ProjectPermissionKey, com.atlassian.jira.permission.PermissionHolderType, java.lang.String)

 

Any suggestions?  I am still learning Groovy and ScriptRunner.

 

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
July 8, 2023

Hi @William Tanner

Sorry for the delay in responding. Was pretty busy these couple of weeks.

Could you please specify which code sample did you use?

Was it the first example or the second example I provided?

Thank you and Kind regards,

Ram

0 votes
Mohamed Benziane
Community Champion
June 17, 2023

Hi,

you need to get access to this interface https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/project/ProjectManager.html

and call the following method, you will get a list off all projects in your instance

getProjectObjects()
TAGS
AUG Leaders

Atlassian Community Events