-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck_styling.ps1
More file actions
260 lines (210 loc) · 7.92 KB
/
check_styling.ps1
File metadata and controls
260 lines (210 loc) · 7.92 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateScript({ Test-Path $_ })]
[string]$StylingGuidePath,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ModifiedFiles,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$OpenAIKey
)
function Send-StyleCheckRequest {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$FileContent,
[Parameter(Mandatory)]
[string]$StyleGuide,
[Parameter(Mandatory)]
[string]$ApiKey,
[Parameter(Mandatory)]
[string]$FilePath
)
$headers = @{
'Authorization' = "Bearer $ApiKey"
'Content-Type' = 'application/json'
}
$body = @{
model = 'gpt-4'
messages = @(
@{
role = 'system'
content = 'You are a PowerShell code review assistant. Review code against the provided style guide. For each violation, provide both the issue and a suggested fix using code examples. Return "NO_VIOLATIONS" if no issues found. Always include a count of total violations found at the start of your response.'
}
@{
role = 'user'
content = @"
Style Guide:
$StyleGuide
PowerShell Code to Review:
$FileContent
Review this PowerShell code against the style guide. Structure your response as follows:
1. Start with "VIOLATION_COUNT:[number]" on its own line
2. Then for each violation:
- Use the format "### Violation XX - [Category] - [Brief Description]" where XX is the zero-padded number (01, 02, etc.)
- Under each violation header:
* Show the current code in a powershell code block
* Show the suggested fix in a powershell code block
* Explain why this improves compliance with the style guide
Format each violation consistently following this template:
### Violation XX - [Category] - [Brief Description]
**Current Code:**
\`\`\`powershell
# Problem code here
\`\`\`
**Suggested Fix:**
\`\`\`powershell
# Fixed code here
\`\`\`
**Explanation:** Why this change improves style guide compliance.
If no violations exist, respond with NO_VIOLATIONS.
"@
}
)
temperature = 0.7
} | ConvertTo-Json -Depth 10
try {
$response = Invoke-RestMethod -Uri 'https://api.openai.com/v1/chat/completions' -Method Post -Headers $headers -Body $body
Write-Host "Debug: Received response from OpenAI"
return $response.choices[0].message.content
}
catch {
Write-Error "Failed to get OpenAI response: $_"
throw
}
}
function Add-PullRequestComment {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$Content,
[Parameter()]
[string]$FilePath = $null
)
try {
if (-not $env:SYSTEM_PULLREQUEST_PULLREQUESTID) {
Write-Host "Not running in PR context, skipping comment creation"
return
}
$organization = $env:SYSTEM_COLLECTIONURI.TrimEnd('/')
$project = $env:SYSTEM_TEAMPROJECT
$repositoryId = $env:BUILD_REPOSITORY_ID
$pullRequestId = $env:SYSTEM_PULLREQUEST_PULLREQUESTID
$url = "$organization/$project/_apis/git/repositories/$repositoryId/pullRequests/$pullRequestId/threads?api-version=7.1"
$body = @{
comments = @(
@{
content = $Content
}
)
status = "active"
}
if ($FilePath) {
$body.threadContext = @{
filePath = $FilePath
}
}
$body = $body | ConvertTo-Json -Depth 10
$headers = @{
'Authorization' = "Bearer $env:SYSTEM_ACCESSTOKEN"
'Content-Type' = 'application/json'
}
Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
Write-Host "Successfully created PR comment"
}
catch {
Write-Warning "Unable to create PR comment: $_"
}
}
try {
Write-Host "Debug: Start of script"
Write-Host "Debug: StylingGuidePath = $StylingGuidePath"
Write-Host "Debug: ModifiedFiles received = $ModifiedFiles"
# Read the style guide
$styleGuide = Get-Content -Path $StylingGuidePath -Raw
Write-Host "Debug: Successfully read style guide"
Write-Host "Debug: Attempting to parse ModifiedFiles"
$modifiedFilesList = $ModifiedFiles | ConvertFrom-Json
Write-Host "Debug: Successfully parsed JSON. Found $($modifiedFilesList.Count) files"
$totalFiles = 0
$totalViolations = 0
$filesWithViolations = @()
foreach ($file in $modifiedFilesList) {
Write-Host "Debug: Processing file: $file"
if (Test-Path $file) {
$totalFiles++
$fileContent = Get-Content -Path $file -Raw
Write-Host "Debug: Successfully read file content"
if ([string]::IsNullOrWhiteSpace($fileContent)) {
Write-Host "File is empty, skipping style check"
continue
}
$violations = Send-StyleCheckRequest -FileContent $fileContent -StyleGuide $styleGuide -ApiKey $OpenAIKey -FilePath $file
if ($violations -ne "NO_VIOLATIONS") {
# Extract violation count from first line
if ($violations -match "VIOLATION_COUNT:(\d+)") {
$violationCount = [int]$Matches[1]
$totalViolations += $violationCount
# Remove the count line from the response
$violations = ($violations -split "`n" | Select-Object -Skip 1) -join "`n"
}
Write-Host "Debug: Violations found in $file"
$filesWithViolations += @{
Path = $file
Name = $file.Split('/')[-1]
ViolationCount = $violationCount
Violations = $violations
}
}
}
else {
Write-Warning "File not found: $file"
}
}
# Create the report file path
$reportPath = Join-Path $env:BUILD_ARTIFACTSTAGINGDIRECTORY "StyleViolations.md"
# Create comments if violations were found
if ($totalViolations -gt 0) {
# First post individual file violation comments
foreach ($file in $filesWithViolations) {
$fileComment = "### Style Guide Violations for $($file.Name)
**File Path:** $($file.Path)
$($file.Violations)"
Add-PullRequestComment -Content $fileComment -FilePath $file.Path
}
# Then post the overview comment last
$overviewComment = "⚠️ PowerShell Style Guide Violations Found
# PowerShell Style Check Summary
## Overview
- Total files checked: $totalFiles
- Files with violations: $($filesWithViolations.Count)
- Total violations found: $totalViolations
## Files with Violations"
foreach ($file in $filesWithViolations) {
$overviewComment += "
- **$($file.Name)** - $($file.ViolationCount) violation(s)"
}
$overviewComment += "
Please review the file-specific comments for detailed information about each violation."
# Post overview comment
Add-PullRequestComment -Content $overviewComment
# Create report file for artifact
$overviewComment | Out-File -FilePath $reportPath -Encoding UTF8
Write-Error "Style guide violations found. See report for details."
exit 1
}
else {
Write-Host "✅ No style guide violations found in any files!"
# Create an empty report file to avoid publishing artifact failure
"# PowerShell Style Check Results`n`n✅ No style guide violations found in any files!" | Out-File -FilePath $reportPath -Encoding UTF8
exit 0
}
}
catch {
Write-Host "Debug: Script failed with error: $_"
Write-Host "Debug: Stack trace: $($_.ScriptStackTrace)"
Write-Error "Script failed: $_"
exit 1
}