Hi,
I have a huge list of projects which I want to assing to a permission scheme. I have explored the api option which works using postman then I created a script but I am facing the issue in authentication. I am not sure what else I need to do.
first time I run the script it gives me 401, second time it gives me 403 and then my account needs a capcha. I am running the script on the script console. Jira Server versions 7.5.3. The user I use is Jira Admin.
The scripts goes like below.
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
// Permission Scheme ID for Archive
def archivePermisssionScheme=10602
// Base64 for username:password
String authString = "XXXXXXXXX" // Hidden for this post
String requestBody = "{\"id\": " + archivePermisssionScheme + " }"
// Get the Project Manager
def projectManager = ComponentAccessor.getProjectManager() ;
def log = Logger.getLogger("com.onresolve.scriptrunner.runner.ScriptRunnerImpl") ;
def projs_key = ['PROJECT1'] ; // This will be the list of all the projects, testing script for 1 project now
def counter = 0
def counter_update=0
// Iterate over each Project Object
def project = projectManager.getProjectObjects()
project.each {
String projectKey = it.key ;
String requestUrl = "https://XXXX.XXXXX.com/rest/api/2/project/${projectKey}/permissionscheme"
if (projs_key.contains(it.key))
{
log.warn(it.name + " - " + projectKey + " - " + it.lead.name) ;
counter_update++ ;
try {
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("Authorization", "Basic " + authString);
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(requestBody);
out.close();
connection.getInputStream();
log.warn("Response code: " + connection.responseCode);
}
catch( e ) {
log.error(e.getMessage())
}
}
counter++ ;
}
log.warn("Total projects " + counter );
log.warn("Total impacted projects " + counter_update );
Another way is to use PermissionSchemeService class:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.permission.PermissionSchemeService def pss = ComponentAccessor.getComponent(PermissionSchemeService) // 10002 is the new permission scheme id, 10000 is the project id
pss.assignPermissionSchemeToProject(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), 10002, 10000)
Hello @Mohit Dhir
If you are using Script runner, then you can use the native Jira Java API to update the permission scheme instead of going via the REST API.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Thanks for sharing this. This helped.
Word of Caution : addSchemeToProject does not remove the old one automatically, If it is executed on a project having a permission scheme, it breaks Jira. I learned it through hard way though.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Mohit Dhir
Indeed, it's import to mention the earlier permission scheme should be removed from the project using method and then the addSchemeToProject can be used-
removeSchemesFromProject
If my answer, helped you please upvote/accept so that other users are also helped. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
but this will remove all schemes from project? so I need to collect all schemes first, then remove all schemes and apply the schemes again, while the one I want to change to be overwritten by the new scheme? sounds like a lot overhead.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Mohit Dhir
We too learnt it in the hard way. We then had to remove the project from db. Since it was a test project, it was fine. Is there a better way to fix this?
It would be a good if jira could handle it in a better way.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Mohit Dhir , @Tarun Sapra thank you for this.
I just broke my staging Jira by only adding the new scheme and not removing the initial one first.
Then action related to projects and issues returned an error : "too many permission schemes are assigned to this project: Project_1234"
I fixed it by then running the script again with the "removeSchemesPromProject" function:
permissionSchemeManager.removeSchemesFromProject(projectManager.getProjectByCurrentKey(currentProjectKey)) //this removed all schemes on the project
permissionSchemeManager.addSchemeToProject(projectManager.getProjectObjByKey(currentProjectKey), newPermissionScheme) //assigned the correct permission scheme
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.