Skip to content

Commit cc27f7c

Browse files
MoChiliaisra-fel
andauthored
[CI Example Analyzer] Install Az-Cmdlets-latest (#18920)
* install storage account * fix a bug * fix unassigned_parameter; no silentErrorAction; split errors and warnings * fix a bug * fix position parameter * try to fix invalid_cmdlet * fix some isssues * fix a bug * skip -name <type> * regress for -name <Type> * fix issues * Update .azure-pipelines/util/analyze-steps.yml Co-authored-by: Yeming Liu <[email protected]> Co-authored-by: Yeming Liu <[email protected]>
1 parent b049ef1 commit cc27f7c

File tree

6 files changed

+93
-36
lines changed

6 files changed

+93
-36
lines changed

.azure-pipelines/util/analyze-steps.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ steps:
3131

3232
- pwsh: 'Install-Module "platyPS", "PSScriptAnalyzer" -Force -Confirm:$false -Scope CurrentUser'
3333
displayName: 'Install PowerShell Dependencies'
34+
35+
- task: PowerShell@2
36+
displayName: 'Install latest modules'
37+
inputs:
38+
targetType: 'inline'
39+
script: |
40+
New-Item -ItemType Directory -Path "Az-Cmdlets-latest"
41+
Invoke-WebRequest -Uri "https://azpspackage.blob.core.windows.net/release/Az-Cmdlets-latest.tar.gz" -OutFile "Az-Cmdlets-latest/Az-Cmdlets-latest.tar.gz" -MaximumRetryCount 2 -RetryIntervalSec 1
42+
tar -xvzf "Az-Cmdlets-latest/Az-Cmdlets-latest.tar.gz" -C "Az-Cmdlets-latest"
43+
. Az-Cmdlets-latest/InstallModule.ps1
44+
pwsh: true
3445

3546
- task: DotNetCoreCLI@2
3647
displayName: 'Generate Help'

.ci-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"src/{ModuleName}/**/*.md$"
8888
],
8989
"phases": [
90-
"build:related-module",
90+
"build:module",
9191
"help:module"
9292
]
9393
},

tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/CommandName.psm1

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.NOTES
55
File: CommandName.psm1
66
#>
7+
. $PSScriptRoot\..\utils.ps1
78

89
enum RuleNames {
910
Invalid_Cmdlet
@@ -53,12 +54,15 @@ function Measure-CommandName {
5354
$GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue
5455
if ($null -eq $GetCommand) {
5556
# CommandName is not valid.
56-
$global:CommandParameterPair += @{
57-
CommandName = $CommandName
58-
ParameterName = "<is not valid>"
59-
ModuleCmdletExNum = $ModuleCmdletExNum
57+
# Redo import-module
58+
if(!(Redo-ImportModule $CommandName)){
59+
$global:CommandParameterPair += @{
60+
CommandName = $CommandName
61+
ParameterName = "<is not valid>"
62+
ModuleCmdletExNum = $ModuleCmdletExNum
63+
}
64+
return $true
6065
}
61-
return $true
6266
}
6367
else {
6468
if ($GetCommand.CommandType -eq "Alias") {

tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.NOTES
55
File: ParameterNameAndValue.psm1
66
#>
7+
. $PSScriptRoot\..\utils.ps1
78

89
enum RuleNames {
910
Unknown_Parameter_Set
@@ -381,6 +382,12 @@ function Measure-ParameterNameAndValue {
381382
[System.Management.Automation.Language.CommandElementAst]$CommandElementAst = $Ast
382383
[System.Management.Automation.Language.CommandAst]$CommandAst = $CommandElementAst.Parent
383384

385+
# Skip all the statements with -ParameterName <Type>
386+
if($Ast.Parent.Extent.Text -match "-\w+\s*<.*?>"){
387+
Write-Debug "Skip $($Ast.Parent.Extent.Text)"
388+
return $false
389+
}
390+
384391
if ($global:SkipNextCommandElementAst) {
385392
$global:SkipNextCommandElementAst = $false
386393
return $false
@@ -398,7 +405,10 @@ function Measure-ParameterNameAndValue {
398405

399406
# Skip parameters for invaild cmdlet
400407
if ($null -eq $GetCommand) {
401-
return $false
408+
# Redo import-module
409+
if(!(Redo-ImportModule $CommandName)){
410+
return $false
411+
}
402412
}
403413
# Get command from alias
404414
if ($GetCommand.CommandType -eq "Alias") {

tools/StaticAnalysis/ExampleAnalyzer/utils.ps1

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
AnalysisOutput
1010
Functions: Get-ExamplesDetailsFromMd
1111
Get-NonExceptionRecord
12+
Get-RecordsNotInAllowList
1213
Measure-SectionMissingAndOutputScript
13-
Measure-NotInWhiteList
1414
Get-ScriptAnalyzerResult
15+
Redo-ImportModule
1516
#>
1617

1718
$SYNOPSIS_HEADING = "## SYNOPSIS"
@@ -176,6 +177,32 @@ function Get-NonExceptionRecord{
176177
return $results
177178
}
178179

180+
<#
181+
.SYNOPSIS
182+
Get AnalysisOutput entries not in the allow list.
183+
#>
184+
function Get-RecordsNotInAllowList{
185+
param (
186+
[AnalysisOutput[]]$records
187+
)
188+
return $records | Where-Object {
189+
# Skip the unexpected error caused by using <xxx> to assign parameters
190+
if($_.RuleName -eq "RedirectionNotSupported"){
191+
return $false
192+
}
193+
# Skip the invaild cmdlet "<"
194+
$CommandName = ($_.Description -split " ")[0]
195+
if($CommandName -eq "<"){
196+
return $false
197+
}
198+
# Skip NeedDeleting in Storage
199+
if($_.RuleName -eq "NeedDeleting" -and $_.Module -eq "Storage.Management"){
200+
return $false
201+
}
202+
return $true
203+
}
204+
}
205+
179206
<#
180207
.SYNOPSIS
181208
Tests whether the script is integral, outputs examples in ".md" to "TempScript.ps1"
@@ -370,7 +397,7 @@ function Measure-SectionMissingAndOutputScript {
370397
ProblemID = 5051
371398
Remediation = "Delete the prompt of example."
372399
}
373-
$results += $result
400+
$results += $result
374401
$newCode = $exampleCodes -replace "`n([A-Za-z \t\\:>])*(PS|[A-Za-z]:)(\w|[\\/\[\].\- ])*(>|&gt;)+( PS)*[ \t]*", "`n"
375402
$newCode = $newCode -replace "(?<=[A-Za-z]\w+-[A-Za-z]\w+)\.ps1"
376403
$exampleCodes = $newCode
@@ -392,7 +419,8 @@ function Measure-SectionMissingAndOutputScript {
392419
}
393420
}
394421
}
395-
422+
# Except records in allow list
423+
$results = Get-RecordsNotInAllowList $results
396424
# Except the suppressed records
397425
$results = Get-NonExceptionRecord $results
398426

@@ -404,26 +432,6 @@ function Measure-SectionMissingAndOutputScript {
404432
}
405433
}
406434

407-
<#
408-
.SYNOPSIS
409-
Measure whether the AnalysisOutput entry is in white list.
410-
#>
411-
function Measure-InAllowList{
412-
param (
413-
[AnalysisOutput]$result
414-
)
415-
# Skip the unexpected error caused by using <xxx> to assign parameters
416-
if($result.RuleName -eq "RedirectionNotSupported"){
417-
return $false
418-
}
419-
# Skip the invaild cmdlet "<"
420-
$CommandName = ($result.Description -split " ")[0]
421-
if($CommandName -eq "<"){
422-
return $false
423-
}
424-
return $true
425-
}
426-
427435
<#
428436
.SYNOPSIS
429437
Invoke PSScriptAnalyzer with custom rules, return the error set.
@@ -486,15 +494,34 @@ function Get-ScriptAnalyzerResult {
486494
Extent = $analysisResult.Extent.ToString().Trim() -replace "`"","`'" -replace "`n"," " -replace "`r"," "
487495
ProblemID = 5200
488496
Remediation = "Unexpected Error! Please check your example or contact the Azure Powershell Team. (Appeared in Line $($analysisResult.Line))"
489-
}
490497
}
491-
# Measure whether the result is in the white list
492-
if(Measure-InAllowList $result){
493-
$results += $result
494498
}
499+
$results += $result
495500
}
496-
#Except the suppressed records
501+
# Except records in allow list
502+
$results = Get-RecordsNotInAllowList $results
503+
# Except the suppressed records
497504
$results = Get-NonExceptionRecord $results
498505

499506
return $results
507+
}
508+
509+
<#
510+
.SYNOPSIS
511+
Retry import-module
512+
#>
513+
function Redo-ImportModule {
514+
param (
515+
[string]$CommandName
516+
)
517+
$modulePath = "$PSScriptRoot\..\..\..\..\artifacts\Debug\Az.*\Az.*.psd1"
518+
Get-Item $modulePath | Import-Module -Global
519+
$GetCommand = Get-Command $CommandName -ErrorAction SilentlyContinue
520+
if ($null -eq $GetCommand) {
521+
return $false
522+
}
523+
else{
524+
Write-Debug "Succeed by retrying import-module"
525+
return $true
526+
}
500527
}

tools/StaticAnalysis/IssueChecker/IssueChecker.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private bool IsSingleExceptionFileHasCriticalIssue(string exceptionFilePath, str
108108
}
109109
var errorText = new StringBuilder();
110110
errorText.AppendLine(recordList.First().PrintHeaders());
111+
var warningText = new StringBuilder();
111112
foreach (IReportRecord record in recordList)
112113
{
113114
if (record.Severity < 2)
@@ -117,13 +118,17 @@ private bool IsSingleExceptionFileHasCriticalIssue(string exceptionFilePath, str
117118
}
118119
else if (record.Severity == 2 && outputWarning)
119120
{
120-
errorText.AppendLine(record.FormatRecord());
121+
warningText.AppendLine(record.FormatRecord());
121122
}
122123
}
123124
if (hasError)
124125
{
125126
Console.WriteLine("{0} Errors", exceptionFilePath);
126127
Console.WriteLine(errorText.ToString());
128+
if(outputWarning && !String.IsNullOrEmpty(warningText.ToString())){
129+
Console.WriteLine("Following are warning issues. It is recommended to correct them as well.");
130+
Console.WriteLine(warningText.ToString());
131+
}
127132
}
128133
}
129134
return hasError;

0 commit comments

Comments
 (0)