-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildChecks.ps1
181 lines (153 loc) · 5.41 KB
/
BuildChecks.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
param([int] $ErrorThreshold, [int] $WarningThreshold, [string] $Extension)
Function Get-Files($MinLines, $MaxLines)
{
Write-Message "Finding $($Extension) files with > $($MinLines) lines and <= $($MaxLines)..."
return Get-ChildItem -Recurse `
| Where-Object { $_.Name.EndsWith($Extension) } `
| Where-Object { $_.FullName.Contains("node_modules") -eq $False } `
| Select-Object @{ label='name'; expression={$_.name} }, @{ label='lines'; expression={(Get-Content $_.FullName | Measure-Object -Line).Lines} } `
| Where-Object { $_.lines -gt $MinLines -and $_.lines -le $MaxLines } `
| Sort-Object -descending -property 'lines' `
| Select-Object @{ label='row'; expression = {"| $($_.name) | $($_.lines) |" } }
}
Function Print-Files($Heading, $Files)
{
$Output=""
$Output += "| File | Lines |`n"
$Output += "| --- | --- |`n"
$Files | ForEach-Object { $Output += "$($_.row)`n" }
[Console]::Error.WriteLine($Heading)
[Console]::Error.WriteLine($Output)
Add-PullRequestComment -Markdown "### $($Heading)`n$($Output)"
}
Function Get-PullRequestComments()
{
If ($env:GITHUB_EVENT_NAME -ne "pull_request")
{
[Console]::Error.WriteLine("Cannot add PR comment; workflow isn't running from a pull-request - $($env:GITHUB_EVENT_NAME)")
$EmptyList = @()
Return ,$EmptyList
}
$Url="https://api.github.com/repos/$($Repo)/issues/$($PullRequestNumber)/comments"
Write-Message "Get pull-request comments"
$Response = Invoke-WebRequest `
-Uri $Url `
-Headers @{
Authorization="Bearer $($Token)";
} `
-Method Get `
if ($Response.StatusCode -ne 200)
{
$Response
Write-Error "Error getting comment"
}
$Json = $Response | ConvertFrom-Json
Return $Json `
| Where-Object { $_.body -like "*$($Extension) file/s approaching*" -Or $_.body.Value -like "*$($Extension) file/s exceeding*" } `
}
Function Remove-ExistingComment($Comment)
{
$CommentId = $Comment.id
$Url = $Comment.url
Write-Message "Deleting comment '$($CommentId)'"
$Response = Invoke-WebRequest `
-Uri $Url `
-Headers @{
Accept="application/vnd.github+json";
Authorization="Bearer $($Token)";
} `
-Method Delete
if ($Response.StatusCode -ne 204)
{
Write-Error "Error deleting comment at url $($Url)"
$Response
}
}
Function Remove-ExistingComments()
{
If ($env:GITHUB_EVENT_NAME -ne "pull_request")
{
[Console]::Error.WriteLine("Cannot remove existing PR comments; workflow isn't running from a pull-request - $($env:GITHUB_EVENT_NAME)")
Return
}
Write-Message "Remove existing comments: $($Comments.Count)"
$Comments | ForEach-Object { Remove-ExistingComment -Comment $_ }
}
Function Write-Message($Message)
{
[Console]::Out.WriteLine($Message)
}
Function Add-PullRequestComment($Markdown)
{
If ($env:GITHUB_EVENT_NAME -ne "pull_request")
{
[Console]::Error.WriteLine("Cannot add PR comment; workflow isn't running from a pull-request - $($env:GITHUB_EVENT_NAME)")
Return
}
$Body = "{""body"": ""$($Markdown.Replace("`n", "\n"))""}"
$Url="https://api.github.com/repos/$($Repo)/issues/$($PullRequestNumber)/comments"
Write-Message "Sending POST request to $($Url) with body $($Body)"
$Response = Invoke-WebRequest `
-Uri $Url `
-Headers @{
Accept="application/vnd.github+json";
Authorization="Bearer $($Token)";
} `
-Method Post `
-Body $Body
if ($Response.StatusCode -ne 201)
{
Write-Error "Error creating comment at url $($Url)"
$Response
}
}
If ($Extension -eq $null -or $Extension -eq "" -or $Extension -eq ".")
{
[Console]::Error.WriteLine("File extension must be supplied")
Exit 1
}
$RefName=$env:GITHUB_REF_NAME # will be in the format <pr_number>/merge
$Token=$env:GITHUB_TOKEN
If ($RefName -ne $null)
{
$PullRequestNumber=$RefName.Replace("/merge", "")
}
else
{
$PullRequestNumber = ""
}
$Repo = $env:GITHUB_REPOSITORY
$Comments = Get-PullRequestComments
$MaxLines = [int]::MaxValue
Remove-ExistingComments
If ($ErrorThreshold -gt 0)
{
$FilesOverThreshold = [array] (Get-Files -MinLines $ErrorThreshold -MaxLines $MaxLines)
If ($FilesOverThreshold.Length -gt 0)
{
Print-Files -Heading "$($FilesOverThreshold.Length) file/s exceeding limit" -Files $FilesOverThreshold
[Console]::Error.WriteLine("There are $($FilesOverThreshold.Length) $($Extension) file/s that have more than $($ErrorThreshold) lines")
}
}
If ($WarningThreshold -gt 0)
{
If ($ErrorThreshold -le 0)
{
$Warning = "over $($WarningThreshold) line warning threshold"
$FilesNearingLimit = [array] (Get-Files -MinLines $WarningThreshold -MaxLines $MaxLines)
}
else
{
$Warning = "approaching $($ErrorThreshold) line limit"
$FilesNearingLimit = Get-Files -MinLines $WarningThreshold -MaxLines $ErrorThreshold
}
If ($FilesNearingLimit.Length -gt 0)
{
Print-Files -Heading "$($FilesNearingLimit.Length) $($Extension) file/s $($Warning)" -Files $FilesNearingLimit
}
}
if ($ErrorThreshold -le 0 -and $WarningThreshold -le 0)
{
[Console]::Error.WriteLine("Neither -ErrorThreshold nor -WarningThreshold are supplied; no constraints to apply to files")
}
Exit $FilesOverThreshold.Length