From 1c551ab03e3283e1d17ba41dd9f1eb64ff0734e3 Mon Sep 17 00:00:00 2001 From: "John L. Singleton" Date: Fri, 3 Mar 2023 10:11:40 -0500 Subject: [PATCH 1/5] small enhancement to make rules that don't match not appear --- scripts/PSCodingStandards/Get-RuleForPath.ps1 | 33 +++++++++++++--- .../PSCodingStandards/Test-GetRuleForPath.ps1 | 39 +++++++++++++++++++ 2 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 scripts/PSCodingStandards/Test-GetRuleForPath.ps1 diff --git a/scripts/PSCodingStandards/Get-RuleForPath.ps1 b/scripts/PSCodingStandards/Get-RuleForPath.ps1 index bcb056a1f8..575bb7d222 100644 --- a/scripts/PSCodingStandards/Get-RuleForPath.ps1 +++ b/scripts/PSCodingStandards/Get-RuleForPath.ps1 @@ -28,6 +28,9 @@ # is a substring of the path once the substitution `/src/` -> `/test/` is # applied +$global:ruleCacheC = $null; +$global:ruleCacheCPP = $null; +$global:enableRuleCache = $false function Get-RuleForPath { param([Parameter(Mandatory)] [string] @@ -41,9 +44,30 @@ function Get-RuleForPath { $allQueries = @() $queriesToCheck = @() - # load all the queries - foreach ($s in $AVAILABLE_SUITES) { - $allQueries += Get-RulesInSuite -Suite $s -Language $Language + if($global:enableRuleCache){ + # load all the queries + if($Language -eq 'cpp'){ + $ruleCache = $global:ruleCacheCPP + }else{ + $ruleCache = $global:ruleCacheC + } + } + + if(-not $ruleCache){ + + foreach ($s in $AVAILABLE_SUITES) { + $allQueries += Get-RulesInSuite -Suite $s -Language $Language + } + + if($global:enableRuleCache){ + if($Language -eq 'cpp'){ + $global:ruleCacheCPP = $allQueries + }else{ + $global:ruleCacheC = $allQueries + } + } + }else{ + $allQueries = $ruleCache } $modifiedPathWithReplacement = Join-Path (Resolve-Path . -Relative) $Path @@ -57,12 +81,11 @@ function Get-RuleForPath { # for each query, create the test directory foreach($q in $allQueries){ - # get test directory $testDirs = (Get-ATestDirectory -RuleObject $q -Language $Language) foreach($testDirectory in $testDirs){ # resolve path to be compatible - $testPath = Join-Path (Resolve-Path . -Relative) $testDirectory + $testPath = (Join-Path (Resolve-Path . -Relative) $testDirectory) + [IO.Path]::DirectorySeparatorChar # see if the TEST directory is a substring of the full path if($modifiedPath.StartsWith($testPath)){ diff --git a/scripts/PSCodingStandards/Test-GetRuleForPath.ps1 b/scripts/PSCodingStandards/Test-GetRuleForPath.ps1 new file mode 100644 index 0000000000..b3c78cc3dc --- /dev/null +++ b/scripts/PSCodingStandards/Test-GetRuleForPath.ps1 @@ -0,0 +1,39 @@ +function Test-GetRuleForPath { +param( + [Parameter(Mandatory)] + [string] + $PR +) + +$prData = (gh pr view -R github/codeql-coding-standards $PR --json headRefOid,headRepository,author,isCrossRepository,headRepositoryOwner,headRefName,files) | ConvertFrom-Json + +foreach($f in $prData.files){ + try { + Write-Host "[C] Scanning file for relationship $($f.path)..." + $rulesToTest = Get-RuleForPath -Language c -Path "$($f.path)" + + Write-Host "[C] Got $($rulesToTest.Count) potential C rules..." + + foreach($r in $rulesToTest){ + $ruleNames += $r.__memberof_rule + Write-Host "[C] Found rule $r " + } + }catch{ + Write-Host "No $Language rules found for path: $($f.path)" + } + + + try { + Write-Host "[CPP] Scanning file for relationship $($f.path)..." + $rulesToTest = Get-RuleForPath -Language cpp -Path "$($f.path)" + + Write-Host "[CPP] Got $($rulesToTest.Count) potential CPP rules..." + + foreach($r in $rulesToTest){ + Write-Host "[CPP] Found rule $r " + } + }catch{ + Write-Host "No CPP rules found for path: $($f.path)" + } +} +} \ No newline at end of file From 79d9b3a3227ddb47d33af0ea6714abd6b6e2e2dd Mon Sep 17 00:00:00 2001 From: "John L. Singleton" Date: Fri, 3 Mar 2023 10:39:59 -0500 Subject: [PATCH 2/5] probably better --- scripts/PSCodingStandards/Get-RuleForPath.ps1 | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scripts/PSCodingStandards/Get-RuleForPath.ps1 b/scripts/PSCodingStandards/Get-RuleForPath.ps1 index 575bb7d222..63602c09af 100644 --- a/scripts/PSCodingStandards/Get-RuleForPath.ps1 +++ b/scripts/PSCodingStandards/Get-RuleForPath.ps1 @@ -85,15 +85,14 @@ function Get-RuleForPath { $testDirs = (Get-ATestDirectory -RuleObject $q -Language $Language) foreach($testDirectory in $testDirs){ # resolve path to be compatible - $testPath = (Join-Path (Resolve-Path . -Relative) $testDirectory) + [IO.Path]::DirectorySeparatorChar + $testPath = (Join-Path (Resolve-Path . -Relative) $testDirectory) - # see if the TEST directory is a substring of the full path - if($modifiedPath.StartsWith($testPath)){ + if((Split-Path $modifiedPath -Parent) -eq $testPath){ $matchingRules += $q continue } - if($modifiedPathWithReplacement.StartsWith($testPath)){ + if((Split-Path $modifiedPathWithReplacement -Parent) -eq $testPath){ $matchingRules += $q continue } From 0411421bb112013c40110851008a55da9033139d Mon Sep 17 00:00:00 2001 From: "John L. Singleton" Date: Fri, 3 Mar 2023 10:50:02 -0500 Subject: [PATCH 3/5] remove cache stuff --- scripts/PSCodingStandards/Get-RuleForPath.ps1 | 29 ++----------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/scripts/PSCodingStandards/Get-RuleForPath.ps1 b/scripts/PSCodingStandards/Get-RuleForPath.ps1 index 63602c09af..34e927e854 100644 --- a/scripts/PSCodingStandards/Get-RuleForPath.ps1 +++ b/scripts/PSCodingStandards/Get-RuleForPath.ps1 @@ -28,9 +28,6 @@ # is a substring of the path once the substitution `/src/` -> `/test/` is # applied -$global:ruleCacheC = $null; -$global:ruleCacheCPP = $null; -$global:enableRuleCache = $false function Get-RuleForPath { param([Parameter(Mandatory)] [string] @@ -44,32 +41,12 @@ function Get-RuleForPath { $allQueries = @() $queriesToCheck = @() - if($global:enableRuleCache){ - # load all the queries - if($Language -eq 'cpp'){ - $ruleCache = $global:ruleCacheCPP - }else{ - $ruleCache = $global:ruleCacheC - } - } - if(-not $ruleCache){ - - foreach ($s in $AVAILABLE_SUITES) { - $allQueries += Get-RulesInSuite -Suite $s -Language $Language - } - - if($global:enableRuleCache){ - if($Language -eq 'cpp'){ - $global:ruleCacheCPP = $allQueries - }else{ - $global:ruleCacheC = $allQueries - } - } - }else{ - $allQueries = $ruleCache + foreach ($s in $AVAILABLE_SUITES) { + $allQueries += Get-RulesInSuite -Suite $s -Language $Language } + $modifiedPathWithReplacement = Join-Path (Resolve-Path . -Relative) $Path # replace "src" with "test" to make it match up $sep = [IO.Path]::DirectorySeparatorChar From 029a3821acf66543c35cd432fb47e11213d57271 Mon Sep 17 00:00:00 2001 From: "John L. Singleton" Date: Fri, 3 Mar 2023 11:21:04 -0500 Subject: [PATCH 4/5] update codeql version -- fix check --- scripts/matrix_testing/CompileFixTool.ps1 | 2 +- scripts/matrix_testing/Config.ps1 | 2 +- scripts/matrix_testing/CreateMatrixTestReport.ps1 | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/matrix_testing/CompileFixTool.ps1 b/scripts/matrix_testing/CompileFixTool.ps1 index 1927e981c3..ba4cd2c146 100755 --- a/scripts/matrix_testing/CompileFixTool.ps1 +++ b/scripts/matrix_testing/CompileFixTool.ps1 @@ -37,7 +37,7 @@ Write-Host -ForegroundColor ([ConsoleColor]2) "OK" $CODEQL_VERSION = (codeql version --format json | ConvertFrom-Json).version Write-Host "Checking 'codeql' version = $REQUIRED_CODEQL_VERSION...." -NoNewline -if (-Not $CODEQL_VERSION -eq $REQUIRED_CODEQL_VERSION) { +if (-Not ($CODEQL_VERSION -eq $REQUIRED_CODEQL_VERSION)) { throw "Invalid CodeQL version $CODEQL_VERSION. Please install $REQUIRED_CODEQL_VERSION." } Write-Host -ForegroundColor ([ConsoleColor]2) "OK" diff --git a/scripts/matrix_testing/Config.ps1 b/scripts/matrix_testing/Config.ps1 index 05aad5dd61..27b03b29f6 100644 --- a/scripts/matrix_testing/Config.ps1 +++ b/scripts/matrix_testing/Config.ps1 @@ -27,7 +27,7 @@ $COMPILER_ARGS = @{ } -$REQUIRED_CODEQL_VERSION = "2.6.3" +$REQUIRED_CODEQL_VERSION = "2.9.4" $REPORT_QUERY = @" diff --git a/scripts/matrix_testing/CreateMatrixTestReport.ps1 b/scripts/matrix_testing/CreateMatrixTestReport.ps1 index 6928154d4d..f60889fa7a 100644 --- a/scripts/matrix_testing/CreateMatrixTestReport.ps1 +++ b/scripts/matrix_testing/CreateMatrixTestReport.ps1 @@ -262,7 +262,7 @@ Write-Host -ForegroundColor ([ConsoleColor]2) "OK" $CODEQL_VERSION = (codeql version --format json | ConvertFrom-Json).version Write-Host "Checking 'codeql' version = $REQUIRED_CODEQL_VERSION...." -NoNewline -if (-Not $CODEQL_VERSION -eq $REQUIRED_CODEQL_VERSION) { +if (-Not ($CODEQL_VERSION -eq $REQUIRED_CODEQL_VERSION)) { throw "Invalid CodeQL version $CODEQL_VERSION. Please install $REQUIRED_CODEQL_VERSION." } Write-Host -ForegroundColor ([ConsoleColor]2) "OK" From b35d032e8d95ac1a41ff14ea6500b7bf62210053 Mon Sep 17 00:00:00 2001 From: "John L. Singleton" Date: Tue, 7 Mar 2023 13:58:32 -0500 Subject: [PATCH 5/5] updated to use settings --- scripts/matrix_testing/Config.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/matrix_testing/Config.ps1 b/scripts/matrix_testing/Config.ps1 index 27b03b29f6..9ae4bb9600 100644 --- a/scripts/matrix_testing/Config.ps1 +++ b/scripts/matrix_testing/Config.ps1 @@ -1,3 +1,5 @@ +Import-Module -Name "$PSScriptRoot/../PSCodingStandards/CodingStandards" + $COMPILER_MAPPINGS = @{ "cpp" = @{ "clang" = "clang++"; @@ -27,7 +29,7 @@ $COMPILER_ARGS = @{ } -$REQUIRED_CODEQL_VERSION = "2.9.4" +$REQUIRED_CODEQL_VERSION = (Get-Content (Join-Path (Get-RepositoryRoot) "supported_codeql_configs.json") | ConvertFrom-Json).supported_environment.codeql_cli $REPORT_QUERY = @"