Set all existing project Private

Must-share information (formatted with Markdown):

  • which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension):- * Enterprise Edition
  • Version 9.9.3
  • how is SonarQube deployed: zip, Docker, Helm: Zip
  • what are you trying to achieve: Setting All exiting projects to “Private”
  • what have you tried so far to achieve this

Do not share screenshots of logs – share the text itself (bonus points for being well-formatted)!

We have 200 + projects. What I am doing is to apply 2 sepearte access of users on those projects. I have created 2 permsissiin template and use Apply Bulk template option. Is there any way we can set up all those projects to “Private” in one go. Currently I have to go to each project and apply one by one.

Thanks
Sunil

Hi,

you may loop over all projects and use the web api api/projects/update_visibility
see https://<yoursonarhost>/web_api/api/projects/update_visibility

Gilbert

1 Like

Here’s a Groovy snippet i would use for that case.

import groovy.json.*

def sqRest(url,method) {
  println "sqRest => " + url
  jsonSlurper = new JsonSlurper()
  raw ='<yourtoken>:'
  bauth = 'Basic ' + raw.bytes.encodeBase64().toString()
  conn = url.toURL().openConnection()
  conn.setRequestMethod(method)
  conn.setRequestProperty("Authorization", bauth)
  conn.connect()
  httpstatus = conn.responseCode
//  println "sqRest ResponseCode = $httpstatus"
  if(method == 'GET') {
    object = jsonSlurper.parseText(conn.content.text)
  }
}

getproj = sqRest('https://<yoursonarhost>/api/projects/search?ps=500', 'GET')
  println 'project count => ' + getproj.paging.total
  foo = getproj.paging.total.toFloat()/500
  counter = 1
  while(counter <= foo.round()) {
    sqRest("https://<yoursonarhost>/api/projects/search?ps=500&p=$counter", 'GET').components.key.each { it ->
    println "https://<yoursonarhost>/api/projects/api/projects/update_visibility?project=$it&visibility=private" // for testing
    //   sqRest("https://<yoursonarhost>/api/projects/api/projects/update_visibility?project=$it&visibility=private", 'POST')
      }
    counter++  
  }