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 );