From 2d8ff6ba0ac4e6f4cbbb6372feda1e77f6e17818 Mon Sep 17 00:00:00 2001 From: jantari Date: Sat, 2 Oct 2021 00:14:32 +0200 Subject: [PATCH 1/3] add PSScriptAnalyzer linting CI on every pull request --- .github/workflows/psscriptanalyzer.yml | 80 ++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 .github/workflows/psscriptanalyzer.yml diff --git a/.github/workflows/psscriptanalyzer.yml b/.github/workflows/psscriptanalyzer.yml new file mode 100644 index 00000000..948a0b85 --- /dev/null +++ b/.github/workflows/psscriptanalyzer.yml @@ -0,0 +1,80 @@ +--- + +name: Linting + +on: [pull_request] + +env: + PSSA_VERSION: latest + PSSA_EXCLUDE_RULES: + DELETE_OLD_COMMENTS: 1 + +jobs: + PSScriptAnalyzer: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + + - name: Install PSScriptAnalyzer Module + run: | + Set-PSRepository -Name PSGallery -InstallationPolicy Trusted + if ("${{ env.PSSA_VERSION }}" -in @($null, "latest")) { + Install-Module PSScriptAnalyzer -Scope CurrentUser -Repository PSGallery -Force + } else { + Install-Module PSScriptAnalyzer -RequiredVersion "${{ env.PSSA_VERSION }}" -Scope CurrentUser -Repository PSGallery -Force + } + shell: pwsh -Command "$ProgressPreference = 'SilentlyContinue'; & '{0}'" + + - name: Run PSScriptAnalyzer + run: | + Import-Module PSScriptAnalyzer -Verbose + $ExcludeRules = '${{ env.PSSA_EXCLUDE_RULES }}'.Split([string[]]@(" ", ",", "`n"), [System.StringSplitOptions]::RemoveEmptyEntries) + Invoke-ScriptAnalyzer -Path "$ENV:GITHUB_WORKSPACE" -ExcludeRule $ExcludeRules -Recurse -Verbose | Tee-Object -Variable PSSAResults + $SUMMARY = ($PSSAResults | Group-Object -Property Severity -NoElement | Foreach-Object { "- $($_.Count) $($_.Name)" }) -join [Environment]::NewLine + $DETAILS = ($PSSAResults | Format-List -Property @{'Name' = 'Location'; 'Expression' = { "{0} [{1}, {2}]" -f (Resolve-Path -LiteralPath $_.ScriptPath -Relative), $_.Line, $_.Column }}, RuleName, Severity, Message | Out-String -Width 88).Trim() + $STRINGBODY = "PSScriptAnalyzer results as of this commit: + + $SUMMARY + +
See details + + `````` + $DETAILS + `````` + +
+ " | ConvertTo-Json -Compress + + $BODY = '"body":{0}' -f $STRINGBODY + Set-Content -LiteralPath 'COMMENTBODY.json' -Value "{$BODY}" + shell: pwsh -Command "$ProgressPreference = 'SilentlyContinue'; & '{0}'" + + - name: Get PR number + run: | + CURLOUT="$(echo $GITHUB_REF | awk -F '[/|/]' '{print $3}')" + echo "PR_NUMBER=${CURLOUT}" >> $GITHUB_ENV + + - name: Delete old comments + run: | + $comments = Invoke-RestMethod "https://api.github.com/repos/${env:GITHUB_REPOSITORY}/issues/${env:PR_NUMBER}/comments" -Headers @{ + 'Authorization' = "token ${{ secrets.GITHUB_TOKEN }}" + } + $commentIDs = $comments | Where { $_.body -like "*" } | Select-Object -Expand url + echo "Will be deleting these:" + echo $commentIDs + $commentIDs | Foreach-Object { + Invoke-WebRequest "$_" -Method Delete -Headers @{ + 'Authorization' = "token ${{ secrets.GITHUB_TOKEN }}" + } + } + shell: pwsh -Command "$ProgressPreference = 'SilentlyContinue'; & '{0}'" + if: env.DELETE_OLD_COMMENTS == '1' + + - name: Create PR comment + run: | + curl -sL --data @COMMENTBODY.json \ + -H "Content-Type: application/json" \ + -H "Accept: application/vnd.github.groot-preview+json" \ + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + "https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" + From df02addfceeae9e30cbc753e0f8a320340c6c018 Mon Sep 17 00:00:00 2001 From: jantari Date: Sat, 2 Oct 2021 00:17:13 +0200 Subject: [PATCH 2/3] try fixing GitHub Actions yaml syntax error --- .github/workflows/psscriptanalyzer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/psscriptanalyzer.yml b/.github/workflows/psscriptanalyzer.yml index 948a0b85..24a46c57 100644 --- a/.github/workflows/psscriptanalyzer.yml +++ b/.github/workflows/psscriptanalyzer.yml @@ -68,7 +68,7 @@ jobs: } } shell: pwsh -Command "$ProgressPreference = 'SilentlyContinue'; & '{0}'" - if: env.DELETE_OLD_COMMENTS == '1' + if: env.DELETE_OLD_COMMENTS == 1 - name: Create PR comment run: | From 39b0e11ccc7332728dc3bf704b1c00ee97a7f914 Mon Sep 17 00:00:00 2001 From: jantari Date: Sat, 2 Oct 2021 00:18:46 +0200 Subject: [PATCH 3/3] syntax error was because of accidental tabs, wrong indentation --- .github/workflows/psscriptanalyzer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/psscriptanalyzer.yml b/.github/workflows/psscriptanalyzer.yml index 24a46c57..5c55e84e 100644 --- a/.github/workflows/psscriptanalyzer.yml +++ b/.github/workflows/psscriptanalyzer.yml @@ -68,7 +68,7 @@ jobs: } } shell: pwsh -Command "$ProgressPreference = 'SilentlyContinue'; & '{0}'" - if: env.DELETE_OLD_COMMENTS == 1 + if: env.DELETE_OLD_COMMENTS == 1 - name: Create PR comment run: |