-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalRunScript.ps1
More file actions
191 lines (165 loc) · 6.86 KB
/
Copy pathLocalRunScript.ps1
File metadata and controls
191 lines (165 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
[CmdletBinding()]
param (
[Parameter()]
[switch]
$AnalyzeOnSonarCloud = $false,
[Parameter()]
[switch]
$PR = $false,
[Parameter()]
[string]
$prId = [string]::Empty,
[Parameter()]
[string]
$prBaseBranch = [string]::Empty,
[Parameter()]
[switch]
$AnalysisDebugLog = $false
)
class AnalysisParameter {
[string]$PlatformType
[string]$AnalysisType
[string]$Value
AnalysisParameter([string]$pType, [string]$aType, [string]$val) {
$this.PlatformType = $pType
$this.AnalysisType = $aType
$this.Value = $val
}
}
# Powershell Config
$ErrorActionPreference = "Stop"
###
### Constants
###
$SONARQUBE_URL = [Environment]::GetEnvironmentVariable("SONARQUBE_URL")
$SONARQUBE_TOKEN = [Environment]::GetEnvironmentVariable("SONARQUBE_TOKEN")
$SONARCLOUD_URL = "https://sonarcloud.io"
$SONARCLOUD_TOKEN = [Environment]::GetEnvironmentVariable("SONARCLOUD_TOKEN")
$SOLUTION = "./src/MonorepoDotnetProject/MonorepoDotnetProject.sln"
$coverageReportDirectory = ".\TestResults"
$coverageReportPath = "$coverageReportDirectory\dotCover.Output.html"
$testReportPath = ".\**\*.trx"
$mainBranchName = "main"
$branchName = git rev-parse --abbrev-ref HEAD
###
### Process flags and parameters
###
$BR = $false
if ($PR) {
[boolean]$doExit = $false
if ($prId -eq [string]::Empty) {
Write-Warning "Provide pull request ID with the '-prId' parameter"
$doExit = $true
}
if ($prBaseBranch -eq [string]::Empty) {
Write-Warning "Provide pull request target (base) branch with the '-prBaseBranch' parameter"
$doExit = $true
}
if ($doExit) {
Exit 1
}
}
else {
if ($mainBranchName -ne $branchName) {
Write-Host "Running branch analysis"
$BR = $true
}
}
if ($AnalyzeOnSonarCloud) {
$SONAR_URL = $SONARCLOUD_URL
$SONAR_TOKEN = $SONARCLOUD_TOKEN
}
else {
$SONAR_URL = $SONARQUBE_URL
$SONAR_TOKEN = $SONARQUBE_TOKEN
}
if ($AnalysisDebugLog) {
$AnalysisDebugLogString = "true"
}
else {
$AnalysisDebugLogString = "false"
}
# .NET Analysis parameters
$dotnetScannerParameterList = @(
[AnalysisParameter]::new("SC", "--", "/o:lukas-frystak-sonarsource")
[AnalysisParameter]::new("SC", "--", "/key:lukas-frystak-sonarsource_MonorepoExample_DotnetProject")
[AnalysisParameter]::new("SQ", "--", "/key:MonorepoExample_DotnetProject")
[AnalysisParameter]::new("SQ", "--", "/name:""Monorepo Example: .NET Project""")
[AnalysisParameter]::new("--", "--", "/d:sonar.host.url=$SONAR_URL")
[AnalysisParameter]::new("--", "--", "/d:sonar.login=$SONAR_TOKEN")
[AnalysisParameter]::new("--", "--", "/v:1.0.0")
[AnalysisParameter]::new("--", "--", "/d:sonar.cs.dotcover.reportsPaths=$coverageReportPath")
[AnalysisParameter]::new("--", "--", "/d:sonar.cs.vstest.reportsPaths=$testReportPath")
[AnalysisParameter]::new("--", "--", "/d:sonar.verbose=$AnalysisDebugLogString")
[AnalysisParameter]::new("--", "BR", "/d:sonar.branch.name=$branchName")
[AnalysisParameter]::new("--", "PR", "/d:sonar.pullrequest.key=$prId")
[AnalysisParameter]::new("--", "PR", "/d:sonar.pullrequest.base=$prBaseBranch")
[AnalysisParameter]::new("--", "PR", "/d:sonar.pullrequest.branch=$branchName")
)
# CLI Analysis parameters
$cliScannerParameterList = @(
[AnalysisParameter]::new("SQ", "--", "-D sonar.projectKey=MonorepoExample_PythonProject"),
[AnalysisParameter]::new("SQ", "--", "-D sonar.projectName=""Monorepo Example: Python Project"""),
[AnalysisParameter]::new("SC", "--", "-D sonar.organization=lukas-frystak-sonarsource"),
[AnalysisParameter]::new("SC", "--", "-D sonar.projectKey=lukas-frystak-sonarsource_MonorepoExample_PythonProject"),
[AnalysisParameter]::new("--", "--", "-D sonar.projectVersion=1.0.0"),
[AnalysisParameter]::new("--", "--", "-D sonar.host.url=$SONAR_URL"),
[AnalysisParameter]::new("--", "--", "-D sonar.login=$SONAR_TOKEN"),
[AnalysisParameter]::new("--", "--", "-D sonar.sources=./src"),
[AnalysisParameter]::new("--", "--", "-D sonar.exclusions=src/MonorepoDotnetProject/**/*"),
[AnalysisParameter]::new("--", "--", "-D sonar.python.version=3"),
[AnalysisParameter]::new("--", "PR", "-D sonar.pullrequest.key=$prId")
[AnalysisParameter]::new("--", "PR", "-D sonar.pullrequest.branch=$branchName"),
[AnalysisParameter]::new("--", "PR", "-D sonar.pullrequest.base=$prBaseBranch"),
[AnalysisParameter]::new("--", "BR", "-D sonar.branch.name=$branchName")
)
# Filter the analysis parameters
if ($AnalyzeOnSonarCloud) {
# Exclude parameters related to SonarQube
$dotnetScannerParameterList = $dotnetScannerParameterList | Where-Object { $_.PlatformType -ne "SQ" }
$cliScannerParameterList = $cliScannerParameterList | Where-Object { $_.PlatformType -ne "SQ" }
}
else {
# Exclude parameters related to SonarCloud
$dotnetScannerParameterList = $dotnetScannerParameterList | Where-Object { $_.PlatformType -ne "SC" }
$cliScannerParameterList = $cliScannerParameterList | Where-Object { $_.PlatformType -ne "SC" }
}
if ($PR) {
# Exclude only parameters related to Branch analysis
$dotnetScannerParameterList = $dotnetScannerParameterList | Where-Object { $_.AnalysisType -ne "BR" }
$cliScannerParameterList = $cliScannerParameterList | Where-Object { $_.AnalysisType -ne "BR" }
}
else {
if ($BR) {
# Exclude only parameters related to Pull Request analysis
$dotnetScannerParameterList = $dotnetScannerParameterList | Where-Object { $_.AnalysisType -ne "PR" }
$cliScannerParameterList = $cliScannerParameterList | Where-Object { $_.AnalysisType -ne "PR" }
}
else {
# Exclude parameters related to branch analysis and pull request analysis
# I.e., use only the generic parameters
$dotnetScannerParameterList = $dotnetScannerParameterList | Where-Object { $_.AnalysisType -eq "--" }
$cliScannerParameterList = $cliScannerParameterList | Where-Object { $_.AnalysisType -eq "--" }
}
}
###
### Build and analyze the projects
###
# Prepare .NET analysis
$dotnetScannerParameters = [string]::Join(' ', $($dotnetScannerParameterList).Value)
$beginCmd = "dotnet sonarscanner begin $dotnetScannerParameters"
Invoke-Expression $beginCmd
# .NET build and test
dotnet build $SOLUTION --configuration Release
dotnet dotcover test $SOLUTION --no-build --configuration Release --dcReportType=html --dcOutput=$coverageReportPath --logger trx
# Run .NET analysis
dotnet sonarscanner end /d:sonar.login=$SONAR_TOKEN 3>&1 2>&1 > dotnet-analysis.log
# Analyze the Python project - SonarScanner CLI
$cliScannerParameters = [string]::Join(' ', $($cliScannerParameterList).Value)
# Append debug flag in debug mode
if ($AnalysisDebugLog){
$cliScannerParameters = "$cliScannerParameters -X"
}
# Run CLI analysis
$cliScannerCmd = "sonar-scanner $cliScannerParameters"
Invoke-Expression $cliScannerCmd 3>&1 2>&1 > cli-analysis.log