-
Notifications
You must be signed in to change notification settings - Fork 3.4k
365 lines (317 loc) · 13.9 KB
/
check-python-deps.yml
File metadata and controls
365 lines (317 loc) · 13.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
name: check python dependencies
on:
pull_request:
branches:
- "**"
paths:
- "**/requirements*.txt"
- "**/setup.py"
- "**/pyproject.toml"
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
env:
EXCLUSION_PATTERNS: |
examples
jobs:
check-python-deps:
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: acryldata/sane-checkout-action@186e92cc5948a9c3e1cc7a96eaff9f776f3fc8e3 # v7
- name: Set up Python 3.11
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6
with:
python-version: "3.11"
cache: "pip"
- name: Install dependencies for dep-analyzer
run: |
python -m pip install --upgrade pip
python -m pip install packaging tomli
- name: Discover Python projects with exclusions
id: discover
run: |
# Find all directories with Python dependency files
find . -type f \
\( -name "setup.py" -o -name "pyproject.toml" -o -name "requirements.txt" \) \
! -path "*/venv/*" ! -path "*/.venv/*" ! -path "*/node_modules/*" \
! -path "*/.gradle/*" ! -path "*/.tox/*" ! -path "*/__pycache__/*" \
-exec dirname {} \; | sort -u > /tmp/all_projects.txt
# Apply exclusion patterns
cp /tmp/all_projects.txt /tmp/projects_to_check.txt
while IFS= read -r pattern; do
if [ -n "$pattern" ]; then
grep -v "$pattern" /tmp/projects_to_check.txt > /tmp/temp.txt || true
mv /tmp/temp.txt /tmp/projects_to_check.txt
fi
done <<< "$EXCLUSION_PATTERNS"
# Display discovered projects
echo "Projects to check:"
cat /tmp/projects_to_check.txt
# Count projects
project_count=$(wc -l < /tmp/projects_to_check.txt)
echo "project_count=$project_count" >> "$GITHUB_OUTPUT"
- name: Analyze dependencies on PR branch
run: |
echo "Analyzing Python dependencies on PR branch..."
mkdir -p /tmp/pr-results
while IFS= read -r project; do
if [ -n "$project" ]; then
# Sanitize project name for filename
safe_name=$(echo "$project" | sed 's/[\/.]/-/g; s/^-//; s/-$//')
echo " Analyzing: $project -> /tmp/pr-${safe_name}.json"
python .github/scripts/dep-analyzer.py --exec --json "$project" > "/tmp/pr-${safe_name}.json" 2>&1 || {
echo "Warning: Failed to analyze $project on PR branch"
echo '{"by_level": {}, "errors": ["Analysis failed"]}' > "/tmp/pr-${safe_name}.json"
}
fi
done < /tmp/projects_to_check.txt
# Use github.base_ref to automatically detect the PR's target branch.
# Fork repositories no longer need to override the base branch name.
- name: Checkout base branch
run: |
git fetch origin ${{ github.base_ref }}
git checkout origin/${{ github.base_ref }}
- name: Analyze dependencies on base branch
run: |
echo "Analyzing Python dependencies on base branch (${{ github.base_ref }})..."
mkdir -p /tmp/master-results
while IFS= read -r project; do
if [ -n "$project" ]; then
# Sanitize project name for filename
safe_name=$(echo "$project" | sed 's/[\/.]/-/g; s/^-//; s/-$//')
echo " Analyzing: $project -> /tmp/master-${safe_name}.json"
python .github/scripts/dep-analyzer.py --exec --json "$project" > "/tmp/master-${safe_name}.json" 2>&1 || {
echo "Warning: Failed to analyze $project on base branch"
echo '{"by_level": {}, "errors": ["Analysis failed"]}' > "/tmp/master-${safe_name}.json"
}
fi
done < /tmp/projects_to_check.txt
- name: Checkout PR branch
run: |
git checkout -
- name: Compare dependencies and collect results
id: compare
run: |
echo "Comparing dependencies between master and PR..."
mkdir -p /tmp/comparison-results
has_violations=false
total_violations=0
total_excluded=0
while IFS= read -r project; do
if [ -n "$project" ]; then
safe_name=$(echo "$project" | sed 's/[\/.]/-/g; s/^-//; s/-$//')
echo " Comparing: $project"
# Disable exit-on-error to capture exit code without failing
set +e
python .github/scripts/check_python_deps.py \
--baseline-file "/tmp/master-${safe_name}.json" \
--pr-file "/tmp/pr-${safe_name}.json" \
--project-name "$project" \
--exclusions-file ".github/python-deps-exclusions.txt" \
> "/tmp/comparison-results/${safe_name}.json"
exit_code=$?
set -e
if [ $exit_code -eq 1 ]; then
has_violations=true
violations=$(jq -r '.violations | length' "/tmp/comparison-results/${safe_name}.json")
total_violations=$((total_violations + violations))
echo " ❌ Found $violations violation(s)"
elif [ $exit_code -eq 2 ]; then
echo " ⚠️ Error analyzing project"
else
excluded=$(jq -r '.excluded_violations | length' "/tmp/comparison-results/${safe_name}.json")
if [ "$excluded" -gt 0 ]; then
total_excluded=$((total_excluded + excluded))
echo " ⚠️ No violations (but $excluded excluded)"
else
echo " ✅ No violations"
fi
fi
fi
done < /tmp/projects_to_check.txt
{
echo "has_violations=$has_violations"
echo "total_violations=$total_violations"
echo "total_excluded=$total_excluded"
} >> "$GITHUB_OUTPUT"
- name: Generate GitHub Actions Summary
if: always()
run: |
{
echo "## Python Dependency Pinning Check Results"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
# Check if we have violations
has_violations="${{ steps.compare.outputs.has_violations }}"
total_violations="${{ steps.compare.outputs.total_violations }}"
total_excluded="${{ steps.compare.outputs.total_excluded }}"
if [ "$has_violations" = "true" ]; then
echo "### Status: ❌ FAILED ($total_violations violations)" >> "$GITHUB_STEP_SUMMARY"
else
echo "### Status: ✅ PASSED" >> "$GITHUB_STEP_SUMMARY"
fi
if [ "$total_excluded" -gt 0 ]; then
{
echo ""
echo "⚠️ **Note:** $total_excluded unpinned dependencies are allowed via exclusion list"
} >> "$GITHUB_STEP_SUMMARY"
fi
{
echo ""
echo "---"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
# Process results for each project
projects_with_violations=()
projects_with_exclusions=()
projects_without_violations=()
while IFS= read -r project; do
if [ -n "$project" ]; then
safe_name=$(echo "$project" | sed 's/[\/.]/-/g; s/^-//; s/-$//')
result_file="/tmp/comparison-results/${safe_name}.json"
if [ -f "$result_file" ]; then
has_viol=$(jq -r '.has_violations' "$result_file")
excluded_count=$(jq -r '.excluded_violations | length' "$result_file")
if [ "$has_viol" = "true" ]; then
projects_with_violations+=("$project")
elif [ "$excluded_count" -gt 0 ]; then
projects_with_exclusions+=("$project")
else
projects_without_violations+=("$project")
fi
fi
fi
done < /tmp/projects_to_check.txt
# Show violations
if [ ${#projects_with_violations[@]} -gt 0 ]; then
{
echo "### ❌ Violations Detected"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
for project in "${projects_with_violations[@]}"; do
{
echo "#### $project"
echo ""
echo "| Type | Dependency | Old Pinning | New Pinning | Source |"
echo "|------|------------|-------------|-------------|--------|"
} >> "$GITHUB_STEP_SUMMARY"
safe_name=$(echo "$project" | sed 's/[\/.]/-/g; s/^-//; s/-$//')
result_file="/tmp/comparison-results/${safe_name}.json"
jq -r '.violations[] |
[
(if .type == "new_unpinned" then "🆕 New Unpinned"
elif .type == "new_lower_bound" then "🆕 New Lower Bound"
else "📉 Downgraded" end),
.name,
(if .old_spec then .old_spec + " (" + .old_level + ")" else "N/A" end),
(if .new_spec == "" then "(none)" else .new_spec + " (" + .new_level + ")" end),
.source
] | @tsv' "$result_file" | \
while IFS=$'\t' read -r type name old new source; do
echo "| $type | $name | $old | $new | $source |" >> "$GITHUB_STEP_SUMMARY"
done
echo "" >> "$GITHUB_STEP_SUMMARY"
done
{
echo "---"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
fi
# Show excluded violations
if [ ${#projects_with_exclusions[@]} -gt 0 ]; then
{
echo "### ⚠️ Unpinned But Allowed (Exclusions)"
echo ""
echo "These dependencies are unpinned but allowed via \`.github/python-deps-exclusions.txt\`:"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
for project in "${projects_with_exclusions[@]}"; do
{
echo "#### $project"
echo ""
echo "| Type | Dependency | Old Pinning | New Pinning | Source |"
echo "|------|------------|-------------|-------------|--------|"
} >> "$GITHUB_STEP_SUMMARY"
safe_name=$(echo "$project" | sed 's/[\/.]/-/g; s/^-//; s/-$//')
result_file="/tmp/comparison-results/${safe_name}.json"
jq -r '.excluded_violations[] |
[
(if .type == "new_unpinned" then "✓ New Unpinned"
elif .type == "new_lower_bound" then "✓ New Lower Bound"
else "✓ Downgraded" end),
.name,
(if .old_spec then .old_spec + " (" + .old_level + ")" else "N/A" end),
(if .new_spec == "" then "(none)" else .new_spec + " (" + .new_level + ")" end),
.source
] | @tsv' "$result_file" | \
while IFS=$'\t' read -r type name old new source; do
echo "| $type | $name | $old | $new | $source |" >> "$GITHUB_STEP_SUMMARY"
done
echo "" >> "$GITHUB_STEP_SUMMARY"
done
{
echo "---"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
fi
# Show projects without issues
if [ ${#projects_without_violations[@]} -gt 0 ]; then
{
echo "### ✅ Projects with No Issues (${#projects_without_violations[@]})"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
for project in "${projects_without_violations[@]}"; do
echo "- $project" >> "$GITHUB_STEP_SUMMARY"
done
{
echo ""
echo "---"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
fi
# Add policy explanation
{
echo "### Policy Violation Details"
echo ""
echo "This workflow enforces strict dependency pinning to prevent supply chain vulnerabilities:"
echo ""
echo "**Blocked Pin Types:**"
echo "- **UNPINNED**: No version constraint (e.g., \`requests\`)"
echo "- **LOWER_BOUND**: Only minimum version (e.g., \`requests>=2.0\`)"
echo ""
echo "**Allowed Pin Types:**"
echo "- **EXACT**: Fully pinned (e.g., \`requests==2.28.1\`) ✅ Recommended"
echo "- **RANGE**: Lower and upper bounds (e.g., \`requests>=2.0,<3.0\`)"
echo "- **COMPATIBLE**: Compatible release (e.g., \`requests~=2.1\`)"
echo "- **WILDCARD**: Exact with wildcard (e.g., \`requests==2.*\`)"
echo ""
echo "**How to Fix:**"
echo ""
echo "1. For new dependencies, use exact pinning:"
echo " \`\`\`python"
echo " # Bad"
echo " install_requires=[\"requests>=2.0\"]"
echo ""
echo " # Good"
echo " install_requires=[\"requests==2.28.1\"]"
echo " \`\`\`"
echo ""
echo "2. For existing dependencies being modified, maintain or improve pinning level"
echo ""
echo "3. If you must use loose pinning, ensure it exists in master first (grandfathered)"
echo ""
echo "4. For special cases, add to \`.github/python-deps-exclusions.txt\`"
} >> "$GITHUB_STEP_SUMMARY"
- name: Fail if violations found
if: steps.compare.outputs.has_violations == 'true'
run: |
echo "❌ Python dependency pinning violations detected!"
echo "See the job summary for details."
exit 1
- name: Upload event file
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
if: always()
with:
name: Event File
path: ${{ github.event_path }}