Skip to content

Commit

Permalink
Merge pull request #1806 from Microsoft/users/bgavril/fixes
Browse files Browse the repository at this point in the history
Users/bgavril/fixes
  • Loading branch information
bgavrilMS committed May 27, 2016
2 parents 1cd1f20 + 41d7a68 commit f33801e
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 69 deletions.
24 changes: 10 additions & 14 deletions Tasks/Common/SonarQubeHelpers/SonarQubeHelper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,26 @@ function RetryUntilTrue
return $true;
}

#
# Invoke a SonarQube GET Rest API and fetch the response. This method uses the configured SonarQube server host url, as well as basic auth
# based on the user provided username / password
#
function InvokeGetRestMethod
{
param (
[Parameter(Mandatory=$true)][string]$query,
[bool]$useAuth=$false)

param ([Parameter(Mandatory=$true)][string]$query)

$sonarQubeHostUrl = GetTaskContextVariable "MSBuild.SonarQube.HostUrl"
$sonarQubeHostUrl = $sonarQubeHostUrl.TrimEnd("/");

Assert (![System.String]::IsNullOrWhiteSpace($sonarQubeHostUrl)) "Could not retrieve the SonarQube host url"

$request = $sonarQubeHostUrl + $query;

if ($useAuth)
{
$authHeader = CreateBasicAuthHeaderFromEndpoint
$authHeader = CreateBasicAuthHeaderFromEndpoint

if (![String]::IsNullOrWhiteSpace($authHeader))
{
$allheaders = @{Authorization = $authHeader}
}

}
if (![String]::IsNullOrWhiteSpace($authHeader))
{
$allheaders = @{Authorization = $authHeader}
}

# Fix for HTTPS websites that support only TLS 1.2, as described by https://jira.sonarsource.com/browse/SONARMSBRU-169
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls
Expand Down
12 changes: 9 additions & 3 deletions Tasks/Maven/Strings/resources.resjson/en-US/resources.resjson
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"loc.group.displayName.junitTestResults": "JUnit Test Results",
"loc.group.displayName.codeCoverage": "Code Coverage",
"loc.group.displayName.advanced": "Advanced",
"loc.group.displayName.SQAnalysis": "SonarQube Analysis",
"loc.group.displayName.CodeAnalysis": "Code Analysis",
"loc.input.label.mavenPOMFile": "Maven POM file",
"loc.input.help.mavenPOMFile": "Relative path from the repository root to the Maven POM file.",
"loc.input.label.options": "Options",
Expand Down Expand Up @@ -42,13 +42,19 @@
"loc.input.label.sqAnalysisEnabled": "Run SonarQube Analysis",
"loc.input.help.sqAnalysisEnabled": "Run a [SonarQube analysis](http://go.microsoft.com/fwlink/?LinkID=708598) after executing the current goals. 'install' or 'package' goals should be executed first.",
"loc.input.label.sqConnectedServiceName": "SonarQube Endpoint",
"loc.input.help.sqConnectedServiceName": "The endpoint that specifies the SonarQube server to use",
"loc.input.help.sqConnectedServiceName": "The SonarQube server generic endpoint",
"loc.input.label.sqDbDetailsRequired": "The SonarQube server version is lower than 5.2",
"loc.input.help.sqDbDetailsRequired": "If using a SonarQube server 5.1 or lower, you must specify the database connection details.",
"loc.input.label.sqDbUrl": "Db Connection String",
"loc.input.help.sqDbUrl": "SonarQube server 5.1 and lower only. Enter the database connection setting (i.e. sonar.jdbc.url). For example: jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor",
"loc.input.label.sqDbUsername": "Db Username",
"loc.input.help.sqDbUsername": "SonarQube server 5.1 and lower only. Enter the username for the database user (i.e. sonar.jdbc.username).",
"loc.input.label.sqDbPassword": "Db User Password",
"loc.input.help.sqDbPassword": "SonarQube server 5.1 and lower only. Enter the password for the database user i.e. sonar.jdbc.password"
"loc.input.help.sqDbPassword": "SonarQube server 5.1 and lower only. Enter the password for the database user i.e. sonar.jdbc.password",
"loc.input.label.pmdAnalysisEnabled": "Run PMD Analysis",
"loc.input.help.pmdAnalysisEnabled": "Use the PMD Java static analysis tool to look for bugs in the code.",
"loc.messages.codeAnalysisBuildSummaryLine_SomeViolationsSomeFiles": "%s found %d violations in %d files.",
"loc.messages.codeAnalysisBuildSummaryLine_SomeViolationsOneFile": "%s found %d violations in 1 file.",
"loc.messages.codeAnalysisBuildSummaryLine_OneViolationOneFile": "%s found 1 violation in 1 file.",
"loc.messages.codeAnalysisBuildSummaryLine_NoViolations": "%s found no violations."
}
2 changes: 1 addition & 1 deletion Tasks/Maven/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@
"type": "connectedService:Generic",
"label": "SonarQube Endpoint",
"required": true,
"helpMarkDown": "The endpoint that specifies the SonarQube server to use",
"helpMarkDown": "The SonarQube server generic endpoint",
"groupName": "CodeAnalysis",
"visibleRule": "sqAnalysisEnabled = true"
},
Expand Down
8 changes: 4 additions & 4 deletions Tasks/SonarQubePostTest/SonarQubeMetrics.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function FetchQualityGateDetails
$analysisId = GetAnalysisIdFromCache
Assert (![String]::IsNullOrEmpty($analysisId)) "WaitForAnalysisToFinish should be called first."

$response = InvokeGetRestMethod "/api/qualitygates/project_status?analysisId=$analysisId" $true
$response = InvokeGetRestMethod "/api/qualitygates/project_status?analysisId=$analysisId"
return $response
}

Expand All @@ -55,7 +55,7 @@ function FetchQualityGateDetails
#
function FetchMetricNames
{
$response = InvokeGetRestMethod "/api/metrics/search?ps=500&f=name" $true
$response = InvokeGetRestMethod "/api/metrics/search?ps=500&f=name"
Assert (HasElements $response) "No metrics were found"

return $response.metrics
Expand Down Expand Up @@ -138,7 +138,7 @@ function IsAnalysisFinished
param ([string]$taskId)

# response is in json and ps deserialize it automatically
$response = InvokeGetRestMethod "/api/ce/task?id=$taskId" $true
$response = InvokeGetRestMethod "/api/ce/task?id=$taskId"
$status = $response.task.status

Write-Verbose "The task status is $status"
Expand All @@ -158,7 +158,7 @@ function QueryAnalysisId
{
param ([string]$taskId)

$response = InvokeGetRestMethod "/api/ce/task?id=$taskId" $true
$response = InvokeGetRestMethod "/api/ce/task?id=$taskId"
return $response.task.analysisId
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"loc.friendlyName": "SonarQube for MSBuild - End Analysis",
"loc.helpMarkDown": "[More Information](http://go.microsoft.com/fwlink/?LinkId=620063)",
"loc.helpMarkDown": "[More Information](https://go.microsoft.com/fwlink/?LinkId=620063)",
"loc.description": "Finish the analysis and upload the results to SonarQube",
"loc.instanceNameFormat": "Finish the analysis and upload the results to SonarQube"
}
4 changes: 2 additions & 2 deletions Tasks/SonarQubePostTest/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "SonarQubePostTest",
"friendlyName": "SonarQube for MSBuild - End Analysis",
"description": "Finish the analysis and upload the results to SonarQube",
"helpMarkDown": "[More Information](http://go.microsoft.com/fwlink/?LinkId=620063)",
"helpMarkDown": "[More Information](https://go.microsoft.com/fwlink/?LinkId=620063)",
"category": "Build",
"visibility": [
"Build"
Expand All @@ -12,7 +12,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 48
"Patch": 49
},
"minimumAgentVersion": "1.99.0",
"demands": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/SonarQubePostTest/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 48
"Patch": 49
},
"minimumAgentVersion": "1.99.0",
"demands": [
Expand Down
2 changes: 1 addition & 1 deletion Tasks/SonarQubePreBuild/SonarQubePreBuildImpl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function InvokePreBuildTask
Write-Verbose "Server Url: $($serviceEndpoint.Url)"

$currentDir = (Get-Item -Path ".\" -Verbose).FullName
$bootstrapperDir = [System.IO.Path]::Combine($currentDir, "MSBuild.SonarQube.Runner-1.1") # the MSBuild.SonarQube.Runner is version specific
$bootstrapperDir = [System.IO.Path]::Combine($currentDir, "SonarQube.Bootstrapper")
$bootstrapperPath = [System.IO.Path]::Combine($bootstrapperDir, "MSBuild.SonarQube.Runner.exe")
$dashboardUrl = GetDashboardUrl $serviceEndpoint.Url $projectKey
Write-Verbose "Dashboard Url: $dashboardUrl"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"loc.friendlyName": "SonarQube for MSBuild - Begin Analysis",
"loc.helpMarkDown": "[More Information](http://go.microsoft.com/fwlink/?LinkId=620063)",
"loc.helpMarkDown": "[More Information](https://go.microsoft.com/fwlink/?LinkId=620063)",
"loc.description": "Fetch the Quality Profile from SonarQube to configure the analysis",
"loc.instanceNameFormat": "Fetch the Quality Profile from SonarQube",
"loc.group.displayName.serverSettings": "SonarQube Server",
Expand All @@ -22,10 +22,10 @@
"loc.input.label.dbPassword": "Db User Password",
"loc.input.help.dbPassword": "SonarQube server 5.1 and lower only. The password for the database user, i.e. sonar.jdbc.password",
"loc.input.label.cmdLineArgs": "Additional Settings",
"loc.input.help.cmdLineArgs": "Space separated settings using the format: /d:propertyName=propertyValue. Normal command line escaping rules apply. For more details on the settings please view the [SonarQube docs](http://go.microsoft.com/fwlink/?LinkID=624390).",
"loc.input.help.cmdLineArgs": "Space separated settings using the format: /d:propertyName=propertyValue. Normal command line escaping rules apply. For more details on the settings please view the [SonarQube docs](https://go.microsoft.com/fwlink/?LinkID=624390).",
"loc.input.label.configFile": "Settings File",
"loc.input.help.configFile": "You can also specify settings via a configuration file. A template is available [here](http://go.microsoft.com/fwlink/?LinkID=620062)",
"loc.input.label.includeFullReport": "Include full analysis report in the build summary",
"loc.input.help.configFile": "You can also specify settings via a configuration file. A template is available [here](https://go.microsoft.com/fwlink/?LinkID=620062)",
"loc.input.label.includeFullReport": "Include full analysis report in the build summary (SQ 5.3+)",
"loc.input.help.includeFullReport": "This option will delay the build until the SonarQube analysis is completed.",
"loc.input.label.breakBuild": "Fail the build on quality gate failure (SQ 5.3+)",
"loc.input.help.breakBuild": "This option is only available when using a SonarQube server 5.3 or above. It will introduce delays as the build must wait for SonarQube to complete the analysis. [More information](http://go.microsoft.com/fwlink/?LinkId=722407)"
Expand Down
10 changes: 5 additions & 5 deletions Tasks/SonarQubePreBuild/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "SonarQubePreBuild",
"friendlyName": "SonarQube for MSBuild - Begin Analysis",
"description": "Fetch the Quality Profile from SonarQube to configure the analysis",
"helpMarkDown": "[More Information](http://go.microsoft.com/fwlink/?LinkId=620063)",
"helpMarkDown": "[More Information](https://go.microsoft.com/fwlink/?LinkId=620063)",
"category": "Build",
"visibility": [
"Build"
Expand All @@ -12,7 +12,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 43
"Patch": 44
},
"minimumAgentVersion": "1.99.0",
"demands": [
Expand Down Expand Up @@ -104,7 +104,7 @@
"type": "string",
"label": "Additional Settings",
"required": false,
"helpMarkDown": "Space separated settings using the format: /d:propertyName=propertyValue. Normal command line escaping rules apply. For more details on the settings please view the [SonarQube docs](http://go.microsoft.com/fwlink/?LinkID=624390).",
"helpMarkDown": "Space separated settings using the format: /d:propertyName=propertyValue. Normal command line escaping rules apply. For more details on the settings please view the [SonarQube docs](https://go.microsoft.com/fwlink/?LinkID=624390).",
"groupName": "advanced"
},
{
Expand All @@ -113,14 +113,14 @@
"defaultValue": "",
"label": "Settings File",
"required": false,
"helpMarkDown": "You can also specify settings via a configuration file. A template is available [here](http://go.microsoft.com/fwlink/?LinkID=620062)",
"helpMarkDown": "You can also specify settings via a configuration file. A template is available [here](https://go.microsoft.com/fwlink/?LinkID=620062)",
"groupName": "advanced"
},
{
"name": "includeFullReport",
"type": "boolean",
"defaultValue": "true",
"label": "Include full analysis report in the build summary",
"label": "Include full analysis report in the build summary (SQ 5.3+)",
"required": false,
"helpMarkDown": "This option will delay the build until the SonarQube analysis is completed.",
"groupName": "advanced"
Expand Down
2 changes: 1 addition & 1 deletion Tasks/SonarQubePreBuild/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 43
"Patch": 44
},
"minimumAgentVersion": "1.99.0",
"demands": [
Expand Down
33 changes: 3 additions & 30 deletions Tests/L0/Common-SonarQubeHelpers/InvokeRestApi.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Register-Mock Invoke-RestMethod # Hijack the PowerShell function used to make RE
# Test case 1: unauthenticated users can make calls to the SQ server

# Act - no credentials
InvokeGetRestMethod -query "/api/test" -useAuth $true
InvokeGetRestMethod -query "/api/test"

# Assert
Assert-WasCalled Invoke-RestMethod -ParametersEvaluator {
Expand All @@ -31,7 +31,7 @@ Register-Mock Get-TaskVariable { 'pa$$word' } -- -Context $distributedTaskContex
Register-Mock Invoke-RestMethod

# Act
InvokeGetRestMethod -query "/api/test" -useAuth $true
InvokeGetRestMethod -query "/api/test"

# Assert
Assert-WasCalled Invoke-RestMethod -ParametersEvaluator {
Expand All @@ -41,31 +41,4 @@ Assert-WasCalled Invoke-RestMethod -ParametersEvaluator {
$Headers['Authorization'] -eq 'Basic dXNlcjpwYSQkd29yZA==' #this is base64 slang for 'user:pa$$word'
}

Unregister-Mock Invoke-RestMethod


# Test case 3: authenticated users can make unauthenicated calls to the SQ server

Register-Mock Get-TaskVariable { 'user' } -- -Context $distributedTaskContext -Name "MSBuild.SonarQube.ServerUsername"
Register-Mock Get-TaskVariable { 'pa$$word' } -- -Context $distributedTaskContext -Name "MSBuild.SonarQube.ServerPassword"
Register-Mock Invoke-RestMethod

# Act
InvokeGetRestMethod -query "/api/test" -useAuth $false

# Assert
Assert-WasCalled Invoke-RestMethod -ParametersEvaluator {
$Uri -eq 'http://testsqserver:9999/api/test' -and
$Method -eq 'Get' -and
$Headers -eq $null # No (auth) headers
}










Unregister-Mock Invoke-RestMethod
4 changes: 2 additions & 2 deletions Tests/L0/SonarQubePostTest/SonarQubeMetrics.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ Register-Mock SetTaskContextVariable
$taskStatusResponse = ConvertFrom-Json '{"task":{"id":"AVQFkrko8fxuJtveJbjv","type":"REPORT","componentId":"AVQFkrko8fxuJtveJbjv","componentKey":"blm5","componentName":"Backlogmaps","componentQualifier":"TRK","analysisId":"10337","status":"SUCCESS","submittedAt":"2016-04-12T10:29:33+0100","startedAt":"2016-04-12T10:29:35+0100","executedAt":"2016-04-12T10:29:39+0100","executionTimeMs":3968,"logs":true}}'

# the task id below is the one in the report-task.txt file
Register-Mock InvokeGetRestMethod {$taskStatusResponse} -- "/api/ce/task?id=AVQFkrko8fxuJtveJbjv" $true
Register-Mock InvokeGetRestMethod {$taskStatusResponse} -- "/api/ce/task?id=AVQFkrko8fxuJtveJbjv"

# the analysis id below comes from the task status reponse
$qualityGateResponse = ConvertFrom-Json '{"projectStatus":{"status":"OK","conditions":[],"periods":[]}}'
Register-Mock InvokeGetRestMethod {$qualityGateResponse} -- "/api/qualitygates/project_status?analysisId=10337" $true
Register-Mock InvokeGetRestMethod {$qualityGateResponse} -- "/api/qualitygates/project_status?analysisId=10337"

# Act 1 - calling GetOrFetchQualityGateStatus without first calling WaitForAnalysisToFinish fails
Assert-Throws {GetOrFetchQualityGateStatus} "*WaitForAnalysisToFinish*"
Expand Down

0 comments on commit f33801e

Please sign in to comment.