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
Highlighted

Get user roles from all projects in CSV format

Hi @All here !! 

Recently, I was asked to create a new user with the same permission as the current user. It looks trivial, but if you have a lot of projects, no plug-ins and no database access, it’s not easy. I am talking about thousands of projects. In the standard Jira page /secure/admin/user/ViewUserProjectRoles!default.jspa?name=yandex&returnUrl=ViewUser.jspa we can get the necessary information. We just need to parse it in a more useful form. I thought that CSV would not be bad. Because I'm lazy, I used GEB to get the page and convert it to CSV. I planned that I would also need to create a script for changing roles for the user, but fortunately, the current user is a little different from the standard one, and I did it manually.

My groovy code:

@Grapes([
@Grab(group='org.gebish', module='geb-core', version='2.3.1'),//always use latest version of geb and selenium drivers
@Grab("org.seleniumhq.selenium:selenium-firefox-driver:3.141.59"),
@Grab("org.seleniumhq.selenium:selenium-support:3.141.59")
])
import geb.Browser
import geb.Page
import groovy.json.JsonSlurper
import groovyjarjarasm.asm.Attribute

// For decrease load ms
int WAIT_BETWEEN_REQUEST = 300

// http://jira.example.com (without last /)
String JIRA_SERVER_URL = 'http://localhost:8087'

//username from who login
String JIRA_USERNAME = 'AndrewDvizhok'

/**
* Bean for project
*/
class Project {
private String key
private int id
private String name
private int issueTypeSchemeId
}

/**
* For do GEB method
*/
class WebProvider {
private static url
private static Browser browser
private static password
private static username
private static File csv
public static int DELAY_BETWEEN_OPERATION = 500

/**
* Set password for auth on Jira if not set in code than ask in console
* @param password
* @return
*/
public static setPassword(String password){
if (password == null){
this.password = System.console().readPassword("[%s]", 'Password:')
}else{
this.password = password
}
}

public static setUsername(String username){
if (username == null){
this.username = System.console().readLine("[%s]", 'Username:')
}else{
this.username = username
}
}

/**
* make login to Jira
* @return - true if success logged
*/
public static boolean doLogin(){
//if already logged return true
if (checkAuth()) return true
browser.go url+'/secure/Dashboard.jspa'
//need wait when page loaded
sleep(DELAY_BETWEEN_OPERATION)
if (browser.$("form", id: "loginform").displayed){
browser.$("form", id: "loginform").with{
os_username = this.username
os_password = this.password
login().click()
}
return checkAuth()
}else{
return false
}
}

/**
* Page have user info?
* @return - if has link to user profile that mean we logged
*/
public static boolean checkAuth(){
if (browser.$("a", id: "header-details-user-fullname").displayed){
return true
}else{
return false
}
}

/**
* Jira show us webSudo page?
* @return - true if show
*/
public static boolean checkWebSudo(){
if (browser.$("form", action: "/secure/admin/WebSudoAuthenticate.jspa").displayed){
return true
}else{
return false
}
}

/**
* Put pass to webSudo
* @return - true if we see sebSudo
*/
public static boolean doWebSudo(){
if (checkWebSudo()){
browser.$("form", action: "/secure/admin/WebSudoAuthenticate.jspa").with{
webSudoPassword = password
$("input", id: "login-form-submit").click()
}
return true
}else{
return false
}
}

/**
* Get map user roles
* @param user - which we want check
* @return - map of user roles: Map<String, List<String>> where Map<ProjectKey, List<Roles>>
*/
public static Map getActiveRoleUser(String user){
this.getActiveRoleUser(user, null)
}

/**
* Get map user roles and save it to file
* @param user - which we want check
* @param csvFile - file to save
* @return - map of user roles: Map<String, List<String>> where Map<ProjectKey, List<Roles>>
*/
public static Map getActiveRoleUser(String user, String csvFile){
if (csvFile!=null){
csv = new File(csvFile)
}
browser.go url+"/secure/admin/user/ViewUserProjectRoles!default.jspa?name=${user}&returnUrl=ViewUser.jspa"
sleep(DELAY_BETWEEN_OPERATION)
doWebSudo()
sleep(DELAY_BETWEEN_OPERATION)
List roles=[]
String Sroles=''
Map projects=[:]
if (browser.$("h2").text() == 'View Project Roles for User'){
//Get all roles columns
browser.$("table", class: "aui aui-table-rowhover role-access")[0].$("th", class: "cell-type-centered").each{
roles.add(it.text())
Sroles += it.text()+';'
}
projects['Roles']=roles.clone()
roles=[]

def prName = 'Default'
def notFirst=false
if (csv!=null){
csv.append('Roles;'+Sroles+'\n')
Sroles=''
}
// Get all roles for each category
browser.$("tbody").each{it.$("tr").each{
notFirst=false
prName = 'Default'
roles=[]
Sroles=''
it.$("td").each{
if (notFirst){
if(it.attr('class') == 'role-member cell-type-centered'){
roles.add(it.$("span").text())
Sroles += it.$("span").text()+';'
}else{
roles.add('0')
Sroles += '0;'
}
}
if (it.attr('class') == 'cell-type-key'){
prName = it.text()
}
notFirst=true
}
projects[prName]=roles.clone()
if (csv!=null){
csv.append(prName+';'+Sroles+'\n')
Sroles=''
}
}}

}
return projects
}

/**
* Get all projects in Jira
* @return - set of projects Set<Project>
*/
public static Set<Project> getProjects(){
browser.go url+"/rest/api/2/project"
sleep(DELAY_BETWEEN_OPERATION)
browser.$("a", id: "rawdata-tab").click()
sleep(DELAY_BETWEEN_OPERATION)
def json = new JsonSlurper().parseText(browser.$("pre", class: "data").text())
Set projects = []
json.each{
//proj = new Project()
//proj.setId(it.id)
//proj.setKey(it.key)
projects.add(new Project(it.id, it.key))
}
return projects
}
}

/**
* Make work
*/
/**
* Get argumets from CLI
*/
def TASK_TO_DO=''
if (args.length>0){
TASK_TO_DO=args[0]
}else{
println """Wrong parameters! Use:\n
> groovy JiraGebAuto.groovy getRole userName C:/some.csv \n
- for get csv file of all roles for userName;


"""
return 1
}


println 'Need username/password...'


def browser = new Browser()

WebProvider.url = JIRA_SERVER_URL
//if want hide pass change to WebProvider.setPassword(null)
WebProvider.setPassword('test')
WebProvider.setUsername(JIRA_USERNAME)
WebProvider.browser = browser

browser.go JIRA_SERVER_URL

sleep(WebProvider.DELAY_BETWEEN_OPERATION)
println 'We are logged? ' + WebProvider.checkAuth()
sleep(WebProvider.DELAY_BETWEEN_OPERATION)
println 'Loginng success? ' + WebProvider.doLogin()
sleep(WebProvider.DELAY_BETWEEN_OPERATION)
println 'We are logged? ' + WebProvider.checkAuth()



switch(TASK_TO_DO) {

case 'getRole':
if(args.length>2)
if (args[1]!=null && args[2]!=null){
println(WebProvider.getActiveRoleUser(args[1],args[2]))
}
break



case 'test':
//WebProvider.getProjects()
break
}

println 'Done'

sleep(WebProvider.DELAY_BETWEEN_OPERATION)
browser.quit()

About code. "WAIT_BETWEEN_REQUEST = 300" is used to set the delay between operations, because the server does not work as fast as geb. Use the setPassword (null) method if you want to hide the password and put it in the console. You can see the useless getPojectct () method.   I plan to use it in the next task.

For work You need the firefox browser and geckodriver:

https://github.com/mozilla/geckodriver/releases

I hope this post was helpful.

 

Best regards,

Andrey

0 comments

Comment

Log in or Sign up to comment
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