0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

sonarcube API powershell

Last updated at Posted at 2022-02-10
1 / 2

Generate a token for a user with instance level "Adminster" permission and supply it to the $user variable

$user = 'TOKEN'
$pass = ''

$pair = "$($user):$($pass)"

$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))

$basicAuthValue = "Basic $encodedCreds"

$Headers = @{
Authorization = $basicAuthValue
}

Base URL of your SonarQube instance

$sonarQubeInstance = "http://localhost:9000"

Existing identity provider, in this case "sonarqube" is used as the existing identity provider is LDAP

$currentExternalIdentityProvider = "sonarqube"

Target identity provider, in this case "saml" is used as the existing identity provider is SAML

$targetExternalIdentityProvider = "saml"

Get the first page of all users

$usersResponse = Invoke-RestMethod -Uri "$sonarQubeInstance/api/users/search?ps=500" -Headers $headers -Method GET
$users = $usersResponse.users

foreach($user in $users){

$login = $user.login
$isLocal = $user.local
$email = $user.email
$externalIdentity = $user.externalIdentity
$externalProvider = $user.externalProvider

# Assumes the external identity in SAML is the user's e-mail address
$newExternalIdentity = $email

# Prevents local users from being locked out of the SonarQube instance
if($isLocal -eq $false){
    # Only migrate users of the exsiting identity provider
    if($externalProvider = $currentExternalIdentityProvider){
        Invoke-RestMethod -Uri "$sonarQubeInstance/api/users/update_identity_provider?login=$login&newExternalProvider=$targetExternalIdentityProvider&newExternalIdentity=$newExternalIdentity" -Headers $headers -Method POST
        Write-Output "Migrated user with login $login and external identity $externalIdentity from $currentExternalIdentityProvider to $targetExternalIdentityProvider with new external identity $newExternalIdentity"
    }
}

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?