Skip to content

Commit

Permalink
Update psscriptanalyzer.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
T0pCyber authored Nov 27, 2024
1 parent 3f4815e commit dd9a9c3
Showing 1 changed file with 89 additions and 89 deletions.
178 changes: 89 additions & 89 deletions .github/workflows/psscriptanalyzer.yml
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
name: PSScriptAnalyzer
on:
pull_request:
paths:
- "**.ps1"
- "**.psm1"
- "**.psd1"
push:
branches:
- main
- development
- Development
- "feature/144-implement-dual-validation-psscriptanalyzer-with-standardized-settings"
paths:
- "**.ps1"
- "**.psm1"
- "**.psd1"
# name: PSScriptAnalyzer
# on:
# pull_request:
# paths:
# - "**.ps1"
# - "**.psm1"
# - "**.psd1"
# push:
# branches:
# - main
# - development
# - Development
# - "feature/144-implement-dual-validation-psscriptanalyzer-with-standardized-settings"
# paths:
# - "**.ps1"
# - "**.psm1"
# - "**.psd1"

jobs:
analyze:
name: PSScriptAnalyzer
runs-on: ubuntu-latest
# jobs:
# analyze:
# name: PSScriptAnalyzer
# runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Required for getting changed files
# steps:
# - uses: actions/checkout@v4
# with:
# fetch-depth: 0 # Required for getting changed files

- name: Get changed files
shell: pwsh
run: |
if ($env:GITHUB_EVENT_NAME -eq 'pull_request') {
$baseCommit = git rev-parse $env:GITHUB_EVENT.pull_request.base.sha
$headCommit = git rev-parse HEAD
$changedFiles = git diff --name-only $baseCommit..$headCommit
} else {
$changedFiles = git diff --name-only HEAD^1 HEAD
}
# - name: Get changed files
# shell: pwsh
# run: |
# if ($env:GITHUB_EVENT_NAME -eq 'pull_request') {
# $baseCommit = git rev-parse $env:GITHUB_EVENT.pull_request.base.sha
# $headCommit = git rev-parse HEAD
# $changedFiles = git diff --name-only $baseCommit..$headCommit
# } else {
# $changedFiles = git diff --name-only HEAD^1 HEAD
# }

$powershellFiles = $changedFiles | Where-Object {
$_ -match '\.(ps1|psm1|psd1)$'
}
# $powershellFiles = $changedFiles | Where-Object {
# $_ -match '\.(ps1|psm1|psd1)$'
# }

$powershellFiles | Out-File -FilePath $env:GITHUB_WORKSPACE/changed_files.txt
Write-Host "Changed PowerShell files:"
$powershellFiles | ForEach-Object { Write-Host " $_" }
# $powershellFiles | Out-File -FilePath $env:GITHUB_WORKSPACE/changed_files.txt
# Write-Host "Changed PowerShell files:"
# $powershellFiles | ForEach-Object { Write-Host " $_" }

- name: Install PSScriptAnalyzer
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module PSScriptAnalyzer -Force
# - name: Install PSScriptAnalyzer
# shell: pwsh
# run: |
# Set-PSRepository PSGallery -InstallationPolicy Trusted
# Install-Module PSScriptAnalyzer -Force

- name: Run PSScriptAnalyzer
shell: pwsh
run: |
$settingsPath = Join-Path $env:GITHUB_WORKSPACE 'Hawk' 'internal' 'configurations' 'PSScriptAnalyzerSettings.psd1'
# - name: Run PSScriptAnalyzer
# shell: pwsh
# run: |
# $settingsPath = Join-Path $env:GITHUB_WORKSPACE 'Hawk' 'internal' 'configurations' 'PSScriptAnalyzerSettings.psd1'

Write-Output "Using settings file: $settingsPath"
if (-not (Test-Path $settingsPath)) {
Write-Error "PSScriptAnalyzer settings file not found at: $settingsPath"
exit 1
}
# Write-Output "Using settings file: $settingsPath"
# if (-not (Test-Path $settingsPath)) {
# Write-Error "PSScriptAnalyzer settings file not found at: $settingsPath"
# exit 1
# }

$changedFiles = Get-Content -Path "$env:GITHUB_WORKSPACE/changed_files.txt"
if (-not $changedFiles) {
Write-Output "No PowerShell files were changed"
$null > (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt')
exit 0
}
# $changedFiles = Get-Content -Path "$env:GITHUB_WORKSPACE/changed_files.txt"
# if (-not $changedFiles) {
# Write-Output "No PowerShell files were changed"
# $null > (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt')
# exit 0
# }

$results = @()
foreach ($file in $changedFiles) {
$fullPath = Join-Path $env:GITHUB_WORKSPACE $file
if (Test-Path $fullPath) {
Write-Output "Analyzing $fullPath"
$fileResults = Invoke-ScriptAnalyzer -Path $fullPath -Settings $settingsPath
if ($fileResults) {
$results += $fileResults
}
}
}
# $results = @()
# foreach ($file in $changedFiles) {
# $fullPath = Join-Path $env:GITHUB_WORKSPACE $file
# if (Test-Path $fullPath) {
# Write-Output "Analyzing $fullPath"
# $fileResults = Invoke-ScriptAnalyzer -Path $fullPath -Settings $settingsPath
# if ($fileResults) {
# $results += $fileResults
# }
# }
# }

if ($results) {
Write-Output "Found $($results.Count) issues in changed files:"
$results | Format-Table -AutoSize | Out-String | Write-Output
$results | Format-Table -AutoSize | Out-File (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt')
exit 1
} else {
Write-Output "No PSScriptAnalyzer issues found in changed files"
$null > (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt')
exit 0
}
# if ($results) {
# Write-Output "Found $($results.Count) issues in changed files:"
# $results | Format-Table -AutoSize | Out-String | Write-Output
# $results | Format-Table -AutoSize | Out-File (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt')
# exit 1
# } else {
# Write-Output "No PSScriptAnalyzer issues found in changed files"
# $null > (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt')
# exit 0
# }

- name: Upload Results
if: always()
uses: actions/upload-artifact@v4
with:
name: psscriptanalyzer-results
path: psscriptanalyzer-results.txt
if-no-files-found: warn
# - name: Upload Results
# if: always()
# uses: actions/upload-artifact@v4
# with:
# name: psscriptanalyzer-results
# path: psscriptanalyzer-results.txt
# if-no-files-found: warn

0 comments on commit dd9a9c3

Please sign in to comment.