forked from datahub-project/datahub
-
Notifications
You must be signed in to change notification settings - Fork 0
279 lines (238 loc) · 11.3 KB
/
check-python-deps.yml
File metadata and controls
279 lines (238 loc) · 11.3 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
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@v4
- name: Set up Python 3.11
uses: actions/setup-python@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
- name: Checkout master branch
run: |
git fetch origin master
git checkout origin/master
- name: Analyze dependencies on master branch
run: |
echo "Analyzing Python dependencies on master branch..."
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 master 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
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" \
> "/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
echo " ✅ No violations"
fi
fi
done < /tmp/projects_to_check.txt
echo "has_violations=$has_violations" >> $GITHUB_OUTPUT
echo "total_violations=$total_violations" >> $GITHUB_OUTPUT
- name: Generate GitHub Actions Summary
if: always()
run: |
echo "## Python Dependency Pinning Check Results" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Check if we have violations
has_violations="${{ steps.compare.outputs.has_violations }}"
total_violations="${{ steps.compare.outputs.total_violations }}"
if [ "$has_violations" = "true" ]; then
echo "### Status: ❌ FAILED ($total_violations violations)" >> $GITHUB_STEP_SUMMARY
else
echo "### Status: ✅ PASSED" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
echo "---" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Process results for each project
projects_with_violations=()
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")
if [ "$has_viol" = "true" ]; then
projects_with_violations+=("$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" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
for project in "${projects_with_violations[@]}"; do
echo "#### $project" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Type | Dependency | Old Pinning | New Pinning | Source |" >> $GITHUB_STEP_SUMMARY
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 "---" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
fi
# Show projects without issues
if [ ${#projects_without_violations[@]} -gt 0 ]; then
echo "### ✅ Projects with No Issues (${#projects_without_violations[@]})" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
for project in "${projects_without_violations[@]}"; do
echo "- $project" >> $GITHUB_STEP_SUMMARY
done
echo "" >> $GITHUB_STEP_SUMMARY
echo "---" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
fi
# Add policy explanation
echo "### Policy Violation Details" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "This workflow enforces strict dependency pinning to prevent supply chain vulnerabilities:" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Blocked Pin Types:**" >> $GITHUB_STEP_SUMMARY
echo "- **UNPINNED**: No version constraint (e.g., \`requests\`)" >> $GITHUB_STEP_SUMMARY
echo "- **LOWER_BOUND**: Only minimum version (e.g., \`requests>=2.0\`)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**Allowed Pin Types:**" >> $GITHUB_STEP_SUMMARY
echo "- **EXACT**: Fully pinned (e.g., \`requests==2.28.1\`) ✅ Recommended" >> $GITHUB_STEP_SUMMARY
echo "- **RANGE**: Lower and upper bounds (e.g., \`requests>=2.0,<3.0\`)" >> $GITHUB_STEP_SUMMARY
echo "- **COMPATIBLE**: Compatible release (e.g., \`requests~=2.1\`)" >> $GITHUB_STEP_SUMMARY
echo "- **WILDCARD**: Exact with wildcard (e.g., \`requests==2.*\`)" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "**How to Fix:**" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "1. For new dependencies, use exact pinning:" >> $GITHUB_STEP_SUMMARY
echo " \`\`\`python" >> $GITHUB_STEP_SUMMARY
echo " # Bad" >> $GITHUB_STEP_SUMMARY
echo " install_requires=[\"requests>=2.0\"]" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo " # Good" >> $GITHUB_STEP_SUMMARY
echo " install_requires=[\"requests==2.28.1\"]" >> $GITHUB_STEP_SUMMARY
echo " \`\`\`" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "2. For existing dependencies being modified, maintain or improve pinning level" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "3. If you must use loose pinning, ensure it exists in master first (grandfathered)" >> $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@v4
if: always()
with:
name: Event File
path: ${{ github.event_path }}