-
Notifications
You must be signed in to change notification settings - Fork 1
344 lines (288 loc) Β· 11.9 KB
/
pre-commit.yml
File metadata and controls
344 lines (288 loc) Β· 11.9 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
name: Pre-commit Checks
on:
pull_request:
types: [opened, synchronize, reopened]
push:
branches: [main, develop]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions: {}
env:
PYTHON_VERSION: "3.11"
UV_VERSION: "0.5.7"
jobs:
pre-commit:
name: Pre-commit Hooks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install UV
uses: astral-sh/setup-uv@v5
with:
version: ${{ env.UV_VERSION }}
- name: Set up Python
run: uv python install ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: |
uv sync --all-extras --dev
- name: Cache pre-commit hooks
uses: actions/cache@v4
with:
path: ~/.cache/pre-commit
key: pre-commit-${{ env.PYTHON_VERSION }}-${{ hashFiles('.pre-commit-config.yaml') }}
restore-keys: |
pre-commit-${{ env.PYTHON_VERSION }}-
- name: Install pre-commit
run: |
uv run pip install pre-commit
- name: Install pre-commit hooks
run: |
if [ ! -f ".pre-commit-config.yaml" ]; then
echo "ERROR: .pre-commit-config.yaml not found in repo root."
echo "This file is required and must be committed to the repository."
echo "The actual config is already present β this should never happen on main."
exit 1
fi
pre-commit install
echo "β
Pre-commit hooks installed from .pre-commit-config.yaml"
- name: Run pre-commit on all files
run: |
echo "π Running pre-commit hooks on all files..."
pre-commit run --all-files --show-diff-on-failure
commit-message-check:
name: Commit Message Validation
runs-on: ubuntu-latest
continue-on-error: true
if: github.event_name == 'pull_request'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Validate commit messages
run: |
echo "π Validating commit messages..."
# Get commits in this PR
commits=$(git log --format="%H %s" origin/main..HEAD)
if [ -z "$commits" ]; then
echo "No commits to validate"
exit 0
fi
# Conventional commit pattern
# Type(scope): description
# where type can be: feat, fix, docs, style, refactor, test, chore, ci, build, perf
pattern="^(feat|fix|docs|style|refactor|test|chore|ci|build|perf)(\(.+\))?: .{1,72}$"
invalid_commits=()
echo "$commits" | while IFS= read -r line; do
commit_hash=$(echo "$line" | cut -d' ' -f1)
commit_msg=$(echo "$line" | cut -d' ' -f2-)
if ! echo "$commit_msg" | grep -qE "$pattern"; then
echo "β Invalid commit message: $commit_msg"
echo " Commit: $commit_hash"
echo " Expected format: type(scope): description"
echo " Valid types: feat, fix, docs, style, refactor, test, chore, ci, build, perf"
invalid_commits+=("$commit_hash")
else
echo "β
Valid commit message: $commit_msg"
fi
done
if [ ${#invalid_commits[@]} -gt 0 ]; then
echo ""
echo "β Found ${#invalid_commits[@]} invalid commit message(s)"
echo ""
echo "Please fix the commit messages to follow the conventional commit format:"
echo " type(scope): description"
echo ""
echo "Examples:"
echo " feat: add new authentication module"
echo " fix(auth): resolve token validation issue"
echo " docs: update API documentation"
echo " test: add unit tests for user service"
echo ""
exit 1
else
echo ""
echo "β
All commit messages are valid!"
fi
file-changes-check:
name: File Changes Analysis
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
permissions:
contents: read
issues: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Analyze file changes
run: |
echo "π Analyzing file changes in this PR..."
# Get changed files
changed_files=$(git diff --name-only origin/main..HEAD)
if [ -z "$changed_files" ]; then
echo "No files changed"
exit 0
fi
echo "Changed files:"
echo "$changed_files"
echo ""
# Categorize changes
source_files=$(echo "$changed_files" | grep -E '\.(py|js|ts|jsx|tsx)$' || true)
test_files=$(echo "$changed_files" | grep -E 'test.*\.(py|js|ts)$|.*test\.(py|js|ts)$' || true)
config_files=$(echo "$changed_files" | grep -E '\.(yml|yaml|json|toml|cfg|ini|conf)$' || true)
doc_files=$(echo "$changed_files" | grep -E '\.(md|rst|txt)$' || true)
echo "## Change Analysis" >> change-analysis.md
echo "" >> change-analysis.md
if [ -n "$source_files" ]; then
echo "### Source Code Changes" >> change-analysis.md
echo "$source_files" | while read -r file; do
echo "- $file" >> change-analysis.md
done
echo "" >> change-analysis.md
fi
if [ -n "$test_files" ]; then
echo "### Test Changes" >> change-analysis.md
echo "$test_files" | while read -r file; do
echo "- $file" >> change-analysis.md
done
echo "" >> change-analysis.md
fi
if [ -n "$config_files" ]; then
echo "### Configuration Changes" >> change-analysis.md
echo "$config_files" | while read -r file; do
echo "- $file" >> change-analysis.md
done
echo "" >> change-analysis.md
fi
if [ -n "$doc_files" ]; then
echo "### Documentation Changes" >> change-analysis.md
echo "$doc_files" | while read -r file; do
echo "- $file" >> change-analysis.md
done
echo "" >> change-analysis.md
fi
# Check for important patterns
echo "### Analysis" >> change-analysis.md
echo "" >> change-analysis.md
if echo "$changed_files" | grep -q "pyproject.toml"; then
echo "β οΈ **Dependencies changed** - Review dependency updates carefully" >> change-analysis.md
fi
if echo "$changed_files" | grep -q "\.github/workflows/"; then
echo "π§ **Workflow changes detected** - Test workflows thoroughly" >> change-analysis.md
fi
if [ -n "$source_files" ] && [ -z "$test_files" ]; then
echo "β οΈ **Source code changed without test updates** - Consider adding tests" >> change-analysis.md
fi
if echo "$changed_files" | grep -q "requirements\.txt"; then
echo "π¦ **Requirements changed** - Ensure compatibility" >> change-analysis.md
fi
- name: Comment PR with analysis
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
if (fs.existsSync('change-analysis.md')) {
const analysis = fs.readFileSync('change-analysis.md', 'utf8');
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `## π File Changes Analysis\n\n${analysis}`
});
}
size-check:
name: PR Size Check
runs-on: ubuntu-latest
continue-on-error: true
if: github.event_name == 'pull_request'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check PR size
run: |
echo "π Checking PR size..."
# Get PR stats
additions=$(git diff --numstat origin/main..HEAD | awk '{sum += $1} END {print sum}')
deletions=$(git diff --numstat origin/main..HEAD | awk '{sum += $2} END {print sum}')
changed_files=$(git diff --name-only origin/main..HEAD | wc -l)
echo "PR Statistics:"
echo "- Files changed: $changed_files"
echo "- Lines added: $additions"
echo "- Lines deleted: $deletions"
echo "- Total changes: $((additions + deletions))"
# Set size labels
total_changes=$((additions + deletions))
if [ $total_changes -lt 10 ]; then
size_label="size/XS"
elif [ $total_changes -lt 100 ]; then
size_label="size/S"
elif [ $total_changes -lt 500 ]; then
size_label="size/M"
elif [ $total_changes -lt 1000 ]; then
size_label="size/L"
else
size_label="size/XL"
fi
echo "PR Size: $size_label"
echo "size_label=$size_label" >> $GITHUB_ENV
# Warning for large PRs
if [ $total_changes -gt 1000 ]; then
echo ""
echo "β οΈ **Large PR Warning**"
echo "This PR is quite large ($total_changes changes). Consider:"
echo "- Breaking it into smaller, focused PRs"
echo "- Ensuring comprehensive testing"
echo "- Requesting thorough review"
fi
quality-gates:
name: Quality Gates
runs-on: ubuntu-latest
needs: [pre-commit, commit-message-check, file-changes-check, size-check]
if: always()
steps:
- name: Check quality gates
run: |
echo "πͺ Checking quality gates..."
# Check if all required checks passed
if [[ "${{ needs.pre-commit.result }}" != "success" ]]; then
echo "β Pre-commit checks failed"
exit 1
fi
# Commit message check is informational only (agents may not follow conventional commits)
if [[ "${{ github.event_name }}" == "pull_request" ]] && [[ "${{ needs.commit-message-check.result }}" != "success" ]]; then
echo "β οΈ Commit message validation did not pass (non-blocking)"
fi
echo "β
All quality gates passed!"
- name: Quality summary
run: |
echo "# π Pre-commit Quality Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
if [[ "${{ needs.pre-commit.result }}" == "success" ]]; then
echo "β
**Pre-commit Hooks**: Passed" >> $GITHUB_STEP_SUMMARY
else
echo "β **Pre-commit Hooks**: Failed" >> $GITHUB_STEP_SUMMARY
fi
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
if [[ "${{ needs.commit-message-check.result }}" == "success" ]]; then
echo "β
**Commit Messages**: Valid" >> $GITHUB_STEP_SUMMARY
else
echo "β **Commit Messages**: Invalid" >> $GITHUB_STEP_SUMMARY
fi
if [[ "${{ needs.file-changes-check.result }}" == "success" ]]; then
echo "β
**File Changes**: Analyzed" >> $GITHUB_STEP_SUMMARY
else
echo "β **File Changes**: Analysis failed" >> $GITHUB_STEP_SUMMARY
fi
if [[ "${{ needs.size-check.result }}" == "success" ]]; then
echo "β
**PR Size**: Checked" >> $GITHUB_STEP_SUMMARY
else
echo "β **PR Size**: Check failed" >> $GITHUB_STEP_SUMMARY
fi
fi