-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathentrypoint.ps1
68 lines (58 loc) · 2.63 KB
/
entrypoint.ps1
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
[cmdletbinding()]
param()
$ErrorActionPreference = 'Stop'
$nl = [Environment]::NewLine
$analyzeParams = @{
Recurse = $true
}
# By default, run PSScriptAnalyzer on the whole repository
# but allow overriding this with INPUT_ROOTPATH environment variable
if ($env:INPUT_ROOTPATH) {
$analyzeParams.Path = Join-Path '/github/workspace' $env:INPUT_ROOTPATH
} else {
$analyzeParams.Path = $env:GITHUB_WORKSPACE
}
# Path to custom script analyzer settings
if ($env:INPUT_SETTINGSPATH) {
$analyzeParams.Settings = Join-Path '/github/workspace' $env:INPUT_SETTINGSPATH
}
# Run PSScriptAnalyzer
$issues = Invoke-ScriptAnalyzer @analyzeParams
$errors = $issues.Where({$_.Severity -eq 'Error'})
$warnings = $issues.Where({$_.Severity -eq 'Warning'})
$infos = $issues.Where({$_.Severity -eq 'Information'})
# Create comment string
$comment = '**PSScriptAnalyzer results:**'
$comment += '{0}<details><summary>Errors: [{1}], Warnings: [{2}], Information: [{3}]</summary><p>{4}{5}```' -f $nl, $errors.Count, $warnings.Count, $infos.Count, $nl, $nl
if ($errors.Count -gt 0) {
$comment += $nl + ($errors | Format-List -Property RuleName, Severity, ScriptName, Line, Message | Out-String -Width 80).Trim()
}
if ($warnings.Count -gt 0) {
$comment += $nl+ $nl + ($warnings | Format-List -Property RuleName, Severity, ScriptName, Line, Message | Out-String -Width 80).Trim()
}
if ($infos.Count -gt 0) {
$comment += $nl + $nl + ($infos | Format-List -Property RuleName, Severity, ScriptName, Line, Message | Out-String -Width 80).Trim()
}
$comment += '{0}{1}```{2}</p></details>' -f $nl, $nl, $nl
Write-Output $comment
# Get comment URL
$ghEvent = Get-Content -Path $env:GITHUB_EVENT_PATH | ConvertFrom-Json -Depth 30
$commentsUrl = $ghEvent.pull_request.comments_url
# Send comment back to PR if any issues were found
if ($commentsUrl -and ($env:INPUT_SENDCOMMENT -eq "$true" -or $env:INPUT_SENDCOMMENT -eq 1) -and $env:INPUT_REPOTOKEN -and ($errors.Count -gt 0 -or $warnings.Count -gt 0 -or $infos.Count -gt 0)) {
$params = @{
Uri = $commentsUrl
Method = 'Post'
Headers = @{
Authorization = "token $env:INPUT_REPOTOKEN"
}
ContentType = 'application/json'
Body = @{body = $comment} | ConvertTo-Json
}
Invoke-RestMethod @params > $null
}
$exitCode = 0
if ($env:INPUT_FAILONERRORS -eq 'true' -or $env:INPUT_FAILONERRORS -eq 1) { $exitCode += $errors.Count}
if ($env:INPUT_FAILONWARNINGS -eq 'true' -or $env:INPUT_FAILONWARNINGS -eq 1) { $exitCode += $warnings.Count}
if ($env:INPUT_FAILONINFOS -eq 'true' -or $env:INPUT_FAILONINFOS -eq 1) { $exitCode += $infos.Count}
exit $exitCode