Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Jira Permission Scheme API using Script Runner

Mohit Dhir Community Leader Mar 25, 2019

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

 

3 answers

0 votes
Mohit Dhir Community Leader Mar 25, 2019
  • STATUS 401Returned if the user is not logged in.
  • STATUS 403Returned if the user does not have permissions to edit project's permission schemes. In practice the user needs to be a JIRA administrator.
  • STATUS 404Returned if the project or permission scheme is not found.
Mohit Dhir Community Leader Mar 25, 2019

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. 

Like Cibi P Cherian likes this
Tarun Sapra Community Leader Mar 26, 2019

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!

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.

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.

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)

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you