-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathaction.yml
More file actions
456 lines (388 loc) · 16.4 KB
/
action.yml
File metadata and controls
456 lines (388 loc) · 16.4 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
name: 'GoSQLX Lint Action'
description: 'Ultra-fast SQL validation, linting, and formatting for CI — 100x faster than SQLFluff, multi-dialect.'
author: 'GoSQLX Team'
branding:
icon: 'check-circle'
color: 'blue'
inputs:
files:
description: 'Glob pattern for SQL files to process (e.g., "**/*.sql", "queries/*.sql")'
required: true
default: '**/*.sql'
rules:
description: 'Comma-separated list of lint rule codes to enable (e.g., "L007,L006"). See docs/LINTING_RULES.md for the full list.'
required: false
default: ''
severity:
description: 'Severity threshold for failure: error, warning, or info'
required: false
default: 'warning'
validate:
description: 'Enable SQL validation (syntax checking)'
required: false
default: 'true'
lint:
description: 'Enable SQL linting (best practices checking) - Advanced feature'
required: false
default: 'false'
format-check:
description: 'Check if SQL files are properly formatted (CI mode)'
required: false
default: 'false'
fail-on-error:
description: 'Fail the build when validation errors are found'
required: false
default: 'true'
config:
description: 'Path to GoSQLX config file (.gosqlx.yml or .gosqlx.yaml)'
required: false
default: ''
dialect:
description: 'SQL dialect: postgresql, mysql, sqlserver, oracle, sqlite (default: auto-detect)'
required: false
default: ''
strict:
description: 'Enable strict validation mode (more rigorous checks)'
required: false
default: 'false'
show-stats:
description: 'Display performance statistics after validation'
required: false
default: 'false'
sarif-output:
description: 'Generate SARIF output for GitHub Code Scanning (requires security-events: write permission)'
required: false
default: 'false'
pr-comment:
description: 'Post validation results as a PR comment (only works on pull_request events)'
required: false
default: 'false'
pr-comment-compact:
description: 'Use compact format for PR comments (limits to 5 errors max)'
required: false
default: 'false'
gosqlx-version:
description: 'GoSQLX version to use (default: latest)'
required: false
default: 'latest'
timeout:
description: 'Timeout in seconds for each file validation (default: 600)'
required: false
default: '600'
working-directory:
description: 'Working directory for SQL file operations'
required: false
default: '.'
outputs:
validated-files:
description: 'Number of files validated'
value: ${{ steps.validate.outputs.validated-files }}
invalid-files:
description: 'Number of files with validation errors'
value: ${{ steps.validate.outputs.invalid-files }}
formatted-files:
description: 'Number of files needing formatting (if format-check enabled)'
value: ${{ steps.format-check.outputs.formatted-files }}
validation-time:
description: 'Total validation time in milliseconds'
value: ${{ steps.validate.outputs.validation-time }}
runs:
using: 'composite'
steps:
- name: Setup Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version: '1.26'
# Disable cache when testing in repository to always build from latest source
# - name: Cache GoSQLX binary
# id: cache-gosqlx
# uses: actions/cache@v4
# with:
# path: ~/go/bin/gosqlx
# key: gosqlx-${{ inputs.gosqlx-version }}-${{ runner.os }}-${{ runner.arch }}-${{ github.sha }}
- name: Install GoSQLX
# if: steps.cache-gosqlx.outputs.cache-hit != 'true'
shell: bash
run: |
# Check if we're in the GoSQLX repository (for testing)
# When using "uses: ./", GITHUB_WORKSPACE contains the repo
if [ -f "$GITHUB_WORKSPACE/go.mod" ] && grep -q "module github.com/ajitpratap0/GoSQLX" "$GITHUB_WORKSPACE/go.mod" 2>/dev/null; then
echo "Building GoSQLX from source (local repository)..."
cd "$GITHUB_WORKSPACE"
mkdir -p "$HOME/go/bin"
go build -o "$HOME/go/bin/gosqlx" ./cmd/gosqlx
echo "GoSQLX built from source successfully"
else
# Validate gosqlx-version input (semver or 'latest')
VERSION="${{ inputs.gosqlx-version }}"
if [ "$VERSION" != "latest" ]; then
if ! echo "$VERSION" | grep -qE '^v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?$'; then
echo "::error::Invalid gosqlx-version format. Must be 'latest' or semver (e.g., v1.4.0)"
exit 1
fi
fi
if [ "$VERSION" = "latest" ]; then
echo "Installing latest GoSQLX..."
go install github.com/ajitpratap0/GoSQLX/cmd/gosqlx@latest
else
echo "Installing GoSQLX $VERSION..."
go install github.com/ajitpratap0/GoSQLX/cmd/gosqlx@"$VERSION"
fi
echo "GoSQLX installed successfully"
fi
"$HOME/go/bin/gosqlx" --version
- name: Verify GoSQLX installation
shell: bash
run: |
if ! command -v "$HOME/go/bin/gosqlx" &> /dev/null; then
echo "ERROR: GoSQLX installation failed"
exit 1
fi
echo "GoSQLX version:"
"$HOME/go/bin/gosqlx" --version
- name: Find SQL files
id: find-files
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
INPUT_FILES: ${{ inputs.files }}
INPUT_WORKING_DIRECTORY: ${{ inputs.working-directory }}
run: bash "${{ github.action_path }}/action/scripts/find-files.sh"
- name: Validate SQL files
id: validate
if: inputs.validate == 'true' && steps.find-files.outputs.file-count != '0'
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
INPUT_CONFIG: ${{ inputs.config }}
INPUT_DIALECT: ${{ inputs.dialect }}
INPUT_STRICT: ${{ inputs.strict }}
INPUT_SHOW_STATS: ${{ inputs.show-stats }}
INPUT_FAIL_ON_ERROR: ${{ inputs.fail-on-error }}
INPUT_TIMEOUT: ${{ inputs.timeout }}
run: bash "${{ github.action_path }}/action/scripts/validate.sh"
- name: Generate SARIF output
id: sarif
if: inputs.sarif-output == 'true' && steps.find-files.outputs.file-count != '0'
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
echo "::group::Generate SARIF Report"
# Build validation command with SARIF output
CMD="$HOME/go/bin/gosqlx validate --output-format sarif --output-file gosqlx-results.sarif"
# Add config if provided
if [ -n "${{ inputs.config }}" ]; then
if [ -f "${{ inputs.config }}" ]; then
export GOSQLX_CONFIG="${{ inputs.config }}"
fi
fi
# Add dialect if provided
DIALECT="${{ inputs.dialect }}"
if [ -n "$DIALECT" ] && [[ "$DIALECT" =~ ^(postgresql|mysql|sqlserver|oracle|sqlite)$ ]]; then
CMD="$CMD --dialect $DIALECT"
fi
# Add strict mode if enabled
if [ "${{ inputs.strict }}" = "true" ]; then
CMD="$CMD --strict"
fi
# Read files and run validation to generate SARIF
cat "$RUNNER_TEMP/gosqlx-files.txt" | tr '\n' ' ' | xargs $CMD || true
# Check if SARIF file was created
if [ -f "gosqlx-results.sarif" ]; then
echo "✓ SARIF report generated: gosqlx-results.sarif"
echo "sarif-file=gosqlx-results.sarif" >> $GITHUB_OUTPUT
else
echo "::warning::SARIF report generation failed"
fi
echo "::endgroup::"
- name: Upload SARIF to GitHub Code Scanning
if: inputs.sarif-output == 'true' && steps.sarif.outputs.sarif-file != ''
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ${{ inputs.working-directory }}/gosqlx-results.sarif
category: gosqlx-sql-validation
- name: Post PR Comment
id: pr-comment
if: inputs.pr-comment == 'true' && github.event_name == 'pull_request' && steps.validate.conclusion != 'skipped'
shell: bash
working-directory: ${{ inputs.working-directory }}
env:
GH_TOKEN: ${{ github.token }}
run: |
echo "::group::Generate PR Comment"
# Create a temporary Go program to format the validation results as a PR comment
cat > "$RUNNER_TEMP/format_comment.go" << 'SCRIPT_EOF'
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
)
type FileValidationResult struct {
Path string
Valid bool
Size int64
Error *string
}
type ValidationResult struct {
TotalFiles int
ValidFiles int
InvalidFiles int
TotalBytes int64
Duration string
Files []FileValidationResult
}
func formatPRComment(result *ValidationResult, compact bool) string {
var sb strings.Builder
duration, _ := time.ParseDuration(result.Duration)
if compact {
if result.InvalidFiles == 0 {
sb.WriteString("## ✅ GoSQLX: All SQL files valid\n\n")
sb.WriteString(fmt.Sprintf("Validated **%d** file(s) in **%v**\n", result.ValidFiles, duration))
} else {
sb.WriteString(fmt.Sprintf("## ❌ GoSQLX: Found issues in %d/%d files\n\n", result.InvalidFiles, result.TotalFiles))
errorCount := 0
maxErrors := 5
for _, file := range result.Files {
if file.Error != nil && errorCount < maxErrors {
sb.WriteString(fmt.Sprintf("- ❌ `%s`: %s\n", file.Path, *file.Error))
errorCount++
}
}
if result.InvalidFiles > maxErrors {
sb.WriteString(fmt.Sprintf("\n*...and %d more error(s). Run locally for full details.*\n", result.InvalidFiles-maxErrors))
}
}
sb.WriteString("\n---\n")
sb.WriteString(fmt.Sprintf("⏱️ %v", duration))
if result.TotalFiles > 0 && duration.Seconds() > 0 {
throughput := float64(result.TotalFiles) / duration.Seconds()
sb.WriteString(fmt.Sprintf(" | 🚀 %.1f files/sec", throughput))
}
} else {
sb.WriteString("## 🔍 GoSQLX SQL Validation Results\n\n")
if result.InvalidFiles == 0 {
sb.WriteString("### ✅ All SQL files are valid!\n\n")
sb.WriteString(fmt.Sprintf("**%d** file(s) validated successfully in **%v**\n\n", result.ValidFiles, duration))
} else {
sb.WriteString(fmt.Sprintf("### ❌ Found issues in %d file(s)\n\n", result.InvalidFiles))
}
sb.WriteString("| Metric | Value |\n|--------|-------|\n")
sb.WriteString(fmt.Sprintf("| Total Files | %d |\n", result.TotalFiles))
sb.WriteString(fmt.Sprintf("| ✅ Valid | %d |\n", result.ValidFiles))
sb.WriteString(fmt.Sprintf("| ❌ Invalid | %d |\n", result.InvalidFiles))
sb.WriteString(fmt.Sprintf("| ⏱️ Duration | %v |\n", duration))
if result.TotalFiles > 0 && duration.Seconds() > 0 {
throughput := float64(result.TotalFiles) / duration.Seconds()
sb.WriteString(fmt.Sprintf("| 🚀 Throughput | %.1f files/sec |\n", throughput))
}
sb.WriteString("\n")
if result.InvalidFiles > 0 {
sb.WriteString("### 📋 Validation Errors\n\n")
for _, file := range result.Files {
if file.Error != nil {
sb.WriteString(fmt.Sprintf("#### ❌ `%s`\n\n```\n%s\n```\n\n", file.Path, *file.Error))
}
}
}
sb.WriteString("---\n*Powered by [GoSQLX](https://github.com/ajitpratap0/GoSQLX) - Ultra-fast SQL validation (100x faster than SQLFluff)*\n")
}
return sb.String()
}
func main() {
var result ValidationResult
if err := json.NewDecoder(os.Stdin).Decode(&result); err != nil {
fmt.Fprintf(os.Stderr, "Error decoding JSON: %v\n", err)
os.Exit(1)
}
compact := len(os.Args) > 1 && os.Args[1] == "compact"
fmt.Print(formatPRComment(&result, compact))
}
SCRIPT_EOF
# Create JSON from validation results
cat > "$RUNNER_TEMP/validation_results.json" << JSON_EOF
{
"TotalFiles": ${{ steps.validate.outputs.validated-files || 0 }} + ${{ steps.validate.outputs.invalid-files || 0 }},
"ValidFiles": ${{ steps.validate.outputs.validated-files || 0 }},
"InvalidFiles": ${{ steps.validate.outputs.invalid-files || 0 }},
"Duration": "${{ steps.validate.outputs.validation-time || 0 }}ms",
"Files": []
}
JSON_EOF
# Format the compact argument
COMPACT_ARG=""
if [ "${{ inputs.pr-comment-compact }}" = "true" ]; then
COMPACT_ARG="compact"
fi
# Generate comment using Go script
COMMENT_BODY=$(go run "$RUNNER_TEMP/format_comment.go" $COMPACT_ARG < "$RUNNER_TEMP/validation_results.json")
# Post comment to PR using gh CLI
echo "$COMMENT_BODY" | gh pr comment ${{ github.event.pull_request.number }} --body-file -
echo "✓ Posted validation results to PR #${{ github.event.pull_request.number }}"
echo "::endgroup::"
- name: Check SQL formatting
id: format-check
if: inputs.format-check == 'true' && steps.find-files.outputs.file-count != '0'
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
echo "::group::SQL Format Check"
NEEDS_FORMATTING=0
while IFS= read -r file; do
echo "Checking format: $file"
if ! "$HOME/go/bin/gosqlx" format --check "$file" 2>&1; then
echo "✗ Needs formatting: $file"
echo "::warning file=$file::File needs formatting"
NEEDS_FORMATTING=$((NEEDS_FORMATTING + 1))
else
echo "✓ Properly formatted: $file"
fi
done < "$RUNNER_TEMP/gosqlx-files.txt"
echo "::endgroup::"
# Set output
echo "formatted-files=$NEEDS_FORMATTING" >> $GITHUB_OUTPUT
# Add to summary
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Format Check" >> $GITHUB_STEP_SUMMARY
echo "Files needing formatting: $NEEDS_FORMATTING" >> $GITHUB_STEP_SUMMARY
# Fail if files need formatting and fail-on-error is true
if [ $NEEDS_FORMATTING -gt 0 ] && [ "${{ inputs.fail-on-error }}" = "true" ]; then
echo "::error::$NEEDS_FORMATTING file(s) need formatting"
exit 1
fi
- name: Run SQL linting
id: lint
if: inputs.lint == 'true' && steps.find-files.outputs.file-count != '0'
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
echo "::group::SQL Linting"
echo "::notice::Advanced linting features are in development (Phase 4)"
echo "::notice::Currently performing basic analysis..."
LINT_ISSUES=0
while IFS= read -r file; do
echo "Analyzing: $file"
# Use analyze command for basic linting
if "$HOME/go/bin/gosqlx" analyze --all "$file" 2>&1; then
echo "✓ No issues: $file"
else
echo "⚠ Issues found: $file"
LINT_ISSUES=$((LINT_ISSUES + 1))
fi
done < "$RUNNER_TEMP/gosqlx-files.txt"
echo "::endgroup::"
# Add to summary
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Linting Results" >> $GITHUB_STEP_SUMMARY
echo "Files with lint issues: $LINT_ISSUES" >> $GITHUB_STEP_SUMMARY
# Note: Linting is advisory, doesn't fail build
if [ $LINT_ISSUES -gt 0 ]; then
echo "::warning::$LINT_ISSUES file(s) have linting suggestions"
fi
- name: Cleanup
if: always()
shell: bash
run: |
rm -f "$RUNNER_TEMP/gosqlx-files.txt" "$RUNNER_TEMP/format_comment.go" "$RUNNER_TEMP/validation_results.json"