functions/Add-SonarqubeConnector.ps1

<#
.SYNOPSIS
  
Create a single SonarQube connector
  
.DESCRIPTION
Adds a single SonarQube connector using the tool connector config API endpoints
  
.EXAMPLE
Add-SonarQubeConnector "My Code Dx Project" "SonarQube Connector 1" "My SonarQube Project" https://mySonarQubeServer.local/ username@email.com mypw1234 "15:00" $true
 
Output (entry ID)
 
  
#>


Function Add-SonarQubeConnector
{
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true)]
        [string]$ProjectName,
        [Parameter(Mandatory=$true)]
        [string]$NewConnectorName,
        [Parameter(Mandatory=$true)]
        [string]$SQProjectKey,
        [Parameter(Mandatory=$true)]
        [string]$HostURL,
        [Parameter(Mandatory=$true)]
        [string]$Branch,
        [Parameter(Mandatory=$true)]
        [string]$IncludeBugs,
        [Parameter(Mandatory=$true)]
        [string]$IncludeCodeSmells,
        [Parameter(Mandatory=$true)]
        [string]$IncludeVulnerabilities,
        [Parameter(Mandatory=$true)]
        [string]$UserToken,
        [Parameter(Mandatory=$false)]
        [string]$RefreshTime,
        [Parameter(Mandatory=$false)]
        [boolean]$IncludeWithAnalysis
    )
    
    # Get the Code Dx project ID from the name
    $projects = Get-Projects
    $ProjectHash = @{}
    $projects.projects | ForEach-Object {
        $ProjectHash[$_.name] = $_.id
    }
    $ProjectID = $ProjectHash.$ProjectName
    
    # Create a blank tool connector
    $ConnectorInfo = Add-BlankConnector $ProjectID SonarQube $NewConnectorName
    $ConnectorID = $ConnectorInfo.id

    # Update the blank tool connector with the proper configuration info
    $uri = $CDXSERVER + "/x/tool-connector-config/values/" + $ConnectorID

    If($RefreshTime -ne "") {
        $RefreshInterval = @{ daily = $RefreshTime }
    }else {
        $RefreshInterval = $false
    }
    
    @($IncludeBugs,$IncludeCodeSmells,$IncludeVulnerabilities,$IncludeWithAnalysis) | ForEach-Object{
        If($_ -eq "true"){
            $_ = $true
        } else {
            $_ = $false
        }
    }
    
    $body = Convertto-Json @{
        'branch' = $Branch
        'bug' = $IncludeBugs
        'code_smell' = $IncludeCodeSmells
        'vulnerability' = $IncludeVulnerabilities
        'auto-refresh-interval' = $RefreshInterval
        'server_url' = $HostURL
        'project' = $SQProjectKey
        'token' = $UserToken
        'available-during-analysis' = $IncludeWithAnalysis
    }
    $CreateSQ = Invoke-RestMethod -Uri $uri -Method Put -Body $body -Headers $headers -ContentType "application/json" 
    Write-Verbose ( $CreateSQ | Format-Table | Out-String )

    Return $ConnectorID

}