Skip to content

Commit

Permalink
Merge pull request #8 from miiitch/feature/AddSonarQubeEndpoint
Browse files Browse the repository at this point in the history
SonarQube Endpoint
  • Loading branch information
DarqueWarrior authored Jul 10, 2017
2 parents 4b2c492 + 1f87f76 commit cc5cac8
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 17 deletions.
3 changes: 2 additions & 1 deletion Team.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
RootModule = ''

# Version number of this module.
ModuleVersion = '0.1.22'
ModuleVersion = '0.1.23'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -82,6 +82,7 @@

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = @('Add-AzureRMServiceEndpoint',
'Add-SonarQubeEndpoint',
'Add-Build',
'Add-BuildDefinition',
'Add-Project',
Expand Down
156 changes: 149 additions & 7 deletions en-US/Team-Help.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<helpItems xmlns="http://msh" schema="maml">
<!-- Edited with: SAPIEN PowerShell HelpWriter 2017 v2.1.34-->
<!--Edited with: SAPIEN PowerShell HelpWriter 2017 v2.1.34-->
<command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10">
<!-- Command-->
<command:details>
Expand Down Expand Up @@ -210,7 +210,7 @@ Remove-BuildDefinition</maml:linkText>
<command:details>
<command:name>Add-AzureRMServiceEndpoint</command:name>
<maml:description>
<maml:para />
<maml:para>Adds a new Azure Resource Manager service endpoint.</maml:para>
</maml:description>
<maml:copyright>
<maml:para />
Expand All @@ -220,7 +220,7 @@ Remove-BuildDefinition</maml:linkText>
<dev:version />
</command:details>
<maml:description>
<maml:para />
<maml:para>The cmdlet adds a new connection between TFS/VSTS and Azure using the Azure Resource Manager connection type.</maml:para>
</maml:description>
<command:syntax>
<!-- Parameter Sets-->
Expand Down Expand Up @@ -264,9 +264,10 @@ you do not have to pass the ProjectName with each call.</maml:para>
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="3">
<maml:name>EndpointName</maml:name>
<maml:description>
<maml:para />
<maml:para>The name displayed on the services page. In VSTS this is the Connection Name.</maml:para>
</maml:description>
<command:parameterValue required="false" variableLength="false">String</command:parameterValue>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
</command:syntaxItem>
</command:syntax>
Expand Down Expand Up @@ -311,14 +312,14 @@ you do not have to pass the ProjectName with each call.</maml:para>
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="3">
<maml:name>EndpointName</maml:name>
<maml:description>
<maml:para />
<maml:para>The name displayed on the services page. In VSTS this is the Connection Name.</maml:para>
</maml:description>
<command:parameterValue required="false" variableLength="false">String</command:parameterValue>
<dev:type>
<maml:name>String</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue />
<dev:defaultValue></dev:defaultValue>
</command:parameter>
<command:parameter required="true" variableLength="false" globbing="false" pipelineInput="True (ByPropertyName)" position="0" aliases="">
<!--Dynamic-->
Expand Down Expand Up @@ -365,6 +366,147 @@ you do not have to pass the ProjectName with each call.</maml:para>
</command:returnValue>
</command:returnValues>
</command:command>
<command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10">
<!-- Command-->
<command:details>
<command:name>Add-SonarQubeEndpoint</command:name>
<maml:description>
<maml:para>Adds a new SonarQube service endpoint.</maml:para>
</maml:description>
<maml:copyright>
<maml:para />
</maml:copyright>
<command:verb>Add</command:verb>
<command:noun>SonarQubeEndpoint</command:noun>
<dev:version />
</command:details>
<maml:description>
<maml:para>The cmdlet adds a new connection between TFS/VSTS and a SonarQube server using the SonarQube connection type. This is only used when using the SonarQube tasks. Using SonarQube with the Maven tasks uses a Generic Connection type.</maml:para>
</maml:description>
<command:syntax>
<!-- Parameter Sets-->
<command:syntaxItem>
<maml:name>Add-SonarQubeEndpoint</maml:name>
<command:parameter required="true" variableLength="false" globbing="false" pipelineInput="True (ByPropertyName)" position="0" aliases="">
<!--Dynamic-->
<maml:name>ProjectName</maml:name>
<maml:description>
<maml:para>Specifies the team project for which this function operates.</maml:para>
<maml:para>You can tab complete from a list of available projects.</maml:para>
<maml:para>You can use Set-DefaultProject to set a default project so
you do not have to pass the ProjectName with each call.</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
<command:parameter required="true" variableLength="false" globbing="false" pipelineInput="True (ByPropertyName)" position="2">
<maml:name>Token</maml:name>
<maml:description>
<maml:para>Authentication Token generated by SonarQube.</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
<command:parameter required="true" variableLength="false" globbing="false" pipelineInput="True (ByPropertyName)" position="2">
<maml:name>SonarQubeUrl</maml:name>
<maml:description>
<maml:para>URL of the sonarqube server.</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="3">
<maml:name>EndpointName</maml:name>
<maml:description>
<maml:para>The name displayed on the services page. In VSTS this is the Connection Name.</maml:para>
</maml:description>
<command:parameterValue required="false" variableLength="false">String</command:parameterValue>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
</command:syntaxItem>
</command:syntax>
<command:parameters>
<!-- All Parameters-->
<command:parameter required="true" variableLength="false" globbing="false" pipelineInput="True (ByPropertyName)" position="2">
<maml:name>SonarQubeUrl</maml:name>
<maml:description>
<maml:para>URL of the sonarqube server.</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
<dev:type>
<maml:name>String</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
<command:parameter required="true" variableLength="false" globbing="false" pipelineInput="True (ByPropertyName)" position="2">
<maml:name>Token</maml:name>
<maml:description>
<maml:para>Authentication Token generated by SonarQube.</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
<dev:type>
<maml:name>String</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="false" globbing="false" pipelineInput="false" position="3">
<maml:name>EndpointName</maml:name>
<maml:description>
<maml:para>The name displayed on the services page. In VSTS this is the Connection Name.</maml:para>
</maml:description>
<command:parameterValue required="false" variableLength="false">String</command:parameterValue>
<dev:type>
<maml:name>String</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
<command:parameter required="true" variableLength="false" globbing="false" pipelineInput="True (ByPropertyName)" position="0" aliases="">
<!--Dynamic-->
<maml:name>ProjectName</maml:name>
<maml:description>
<maml:para>Specifies the team project for which this function operates.</maml:para>
<maml:para>You can tab complete from a list of available projects.</maml:para>
<maml:para>You can use Set-DefaultProject to set a default project so
you do not have to pass the ProjectName with each call.</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
<dev:type>
<maml:name>String</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue></dev:defaultValue>
</command:parameter>
</command:parameters>
<command:inputTypes>
<!-- Inputs-->
<command:inputType>
<dev:type>
<maml:name>
System.String
</maml:name>
<maml:uri />
</dev:type>
<maml:description>
<maml:para />
</maml:description>
</command:inputType>
</command:inputTypes>
<command:returnValues>
<!-- Outputs-->
<command:returnValue>
<dev:type>
<maml:name>System.Object</maml:name>
<maml:uri />
</dev:type>
<maml:description>
<maml:para />
</maml:description>
</command:returnValue>
</command:returnValues>
</command:command>
<command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10">
<!-- Command-->
<command:details>
Expand Down Expand Up @@ -5556,5 +5698,5 @@ to pass the ProjectName with each call.</maml:para>
</maml:alert>
</maml:alertSet>
</command:command>
<!-- Edited with: SAPIEN PowerShell HelpWriter 2017 v2.1.34-->
<!--Edited with: SAPIEN PowerShell HelpWriter 2017 v2.1.34-->
</helpItems>
81 changes: 80 additions & 1 deletion src/serviceendpoints.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,85 @@ function Remove-ServiceEndpoint {
}
}

function Add-SonarQubeEndpoint {
[CmdletBinding(DefaultParameterSetName = 'Secure')]
param(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string] $endpointName,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string] $sonarqubeUrl,
[parameter(ParameterSetName = 'Plain', Mandatory = $true, Position = 2, HelpMessage = 'Personal Access Token')]
[string] $personalAccessToken,
[parameter(ParameterSetName = 'Secure', Mandatory = $true, HelpMessage = 'Personal Access Token')]
[securestring] $securePersonalAccessToken
)

DynamicParam {
_buildProjectNameDynamicParam
}

Process {


if ($personalAccessToken) {
$token = $personalAccessToken
}
else {
$credential = New-Object System.Management.Automation.PSCredential "nologin", $securePersonalAccessToken
$token = $credential.GetNetworkCredential().Password
}
# Bind the parameter to a friendly variable
$ProjectName = $PSBoundParameters["ProjectName"]

# Build the url
$url = _buildURL -projectName $projectName

$obj = @{
authorization = @{
parameters = @{
username = $token;
password = ''
};
scheme = 'UsernamePassword'
};
data = @{
};
name = $endpointName;
type = 'sonarqube';
url = $sonarqubeUrl
}

$body = $obj | ConvertTo-Json

try {

# Call the REST API
if (_useWindowsAuthenticationOnPremise) {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Post -Body $body -ContentType "application/json" -Uri $url -UseDefaultCredentials
}
else {
$resp = Invoke-RestMethod -UserAgent (_getUserAgent) -Method Post -Body $body -ContentType "application/json" -Uri $url -Headers @{Authorization = "Basic $env:TEAM_PAT"}
}

}
catch [System.Net.WebException] {
if ($_.Exception.status -eq "ProtocolError") {
$errorDetails = ConvertFrom-Json $_.ErrorDetails
[string] $message = $errorDetails.message
if ($message.StartsWith("Endpoint type couldn't be recognized 'sonarqube'")) {
Write-Error -Message "The Sonarqube extension not installed. Please install from https://marketplace.visualstudio.com/items?itemName=SonarSource.sonarqube"
return
}
}
throw
}
_trackProgress -projectName $projectName -resp $resp -title 'Creating Service Endpoint' -msg "Creating $endpointName"

return Get-ServiceEndpoint -projectName $projectName -id $resp.id
}
}


function Add-AzureRMServiceEndpoint {
[CmdletBinding()]
param(
Expand Down Expand Up @@ -254,4 +333,4 @@ function Get-ServiceEndpoint {
}
}

Export-ModuleMember -Alias * -Function Get-ServiceEndpoint, Add-AzureRMServiceEndpoint, Remove-ServiceEndpoint
Export-ModuleMember -Alias * -Function Get-ServiceEndpoint, Add-AzureRMServiceEndpoint, Remove-ServiceEndpoint, Add-SonarQubeEndpoint
13 changes: 5 additions & 8 deletions src/team.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function Add-TeamAccount {
[parameter(ParameterSetName='Plain', Mandatory=$true, Position=2, HelpMessage='Personal Access Token')]
[string] $PersonalAccessToken,
[parameter(ParameterSetName='Secure', Mandatory=$true, HelpMessage='Personal Access Token')]
[securestring] $PAT
[securestring] $SecurePersonalAccessToken
)

DynamicParam {
Expand Down Expand Up @@ -133,34 +133,31 @@ function Add-TeamAccount {
}

$UsingWindowsAuth = $PSBoundParameters[$ParameterName2]
if (!($PAT) -and !($PersonalAccessToken) -and !($UsingWindowsAuth)) {
if (!($SecurePersonalAccessToken) -and !($PersonalAccessToken) -and !($UsingWindowsAuth)) {
Write-Error "Personal Access Token must be provided if you are not using Windows Authentication; please see the help."
}

} else {
$Level = "Process"
}


if ($PAT) {
if ($SecurePersonalAccessToken) {
# Convert the securestring to a normal string
# this was the one technique that worked on Mac, Linux and Windows
$credential = New-Object System.Management.Automation.PSCredential $account,$PAT
$credential = New-Object System.Management.Automation.PSCredential $account,$SecurePersonalAccessToken
$_pat = $credential.GetNetworkCredential().Password
} else {
$_pat = $PersonalAccessToken
}



# If they only gave an account name add visualstudio.com
if($Account.ToLower().Contains('http') -eq $false) {
$Account = "https://$($Account).visualstudio.com"
}

$encodedPat = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$_pat"))

# If no PAT is entered, and on windows, are we using default credentials for REST calls
# If no SecurePersonalAccessToken is entered, and on windows, are we using default credentials for REST calls
if ((!$_pat) -and (_isOnWindows) -and ($UsingWindowsAuth)) {
Write-Verbose "Using Default Windows Credentials for authentication; no Personal Access Token required"
$encodedPat = ""
Expand Down
Loading

0 comments on commit cc5cac8

Please sign in to comment.