-
Notifications
You must be signed in to change notification settings - Fork 1
323 lines (270 loc) · 12.3 KB
/
gen-preview.yml
File metadata and controls
323 lines (270 loc) · 12.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
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
name: Generate Plot Previews
on:
workflow_run:
workflows: ["Plot Tests"]
types:
- completed
jobs:
generate-previews:
name: Generate Previews and Post to Issue
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
permissions:
contents: read
pull-requests: write
issues: write
actions: read
steps:
- name: Download test results
uses: actions/download-artifact@v6
with:
pattern: test-results-*
path: all-test-results
merge-multiple: true
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Parse test results
id: test_results
run: |
echo "Parsing test results..."
get_status() {
local version=$1
local file="all-test-results/result-${version}.txt"
if [ -f "$file" ]; then
outcome=$(cut -d',' -f2 "$file")
if [ "$outcome" == "success" ]; then
echo "pass"
elif [ "$outcome" == "skipped" ]; then
echo "skip"
else
echo "fail"
fi
else
echo "unknown"
fi
}
echo "py314=$(get_status 3.14)" >> $GITHUB_OUTPUT
echo "py313=$(get_status 3.13)" >> $GITHUB_OUTPUT
echo "py312=$(get_status 3.12)" >> $GITHUB_OUTPUT
echo "py311=$(get_status 3.11)" >> $GITHUB_OUTPUT
echo "Results parsed:"
cat all-test-results/*.txt 2>/dev/null || echo "No results found"
- name: Get PR number from workflow run
id: get_pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER=$(gh api repos/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.id }} --jq '.pull_requests[0].number')
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
echo "Found PR: #$PR_NUMBER"
- name: Checkout code
uses: actions/checkout@v6
with:
ref: ${{ github.event.workflow_run.head_sha }}
fetch-depth: 0
- name: Set up Python 3.14
uses: actions/setup-python@v6
with:
python-version: '3.14'
- name: Install uv
run: |
curl -LsSf https://astral.sh/uv/install.sh | sh
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
- name: Install dependencies
run: |
uv sync --all-extras
uv pip install pillow
- name: Find changed plot files
id: changed_plots
run: |
# Get the base branch from the PR
BASE_SHA=${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.pull_requests[0].base.sha || 'origin/main' }}
CHANGED_FILES=$(git diff --name-only $BASE_SHA...${{ github.event.workflow_run.head_sha }} -- 'plots/**/*.py' || echo "")
if [ -z "$CHANGED_FILES" ]; then
echo "No plot files changed"
echo "has_plots=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "has_plots=true" >> $GITHUB_OUTPUT
CHANGED_ARRAY=$(echo "$CHANGED_FILES" | jq -R -s -c 'split("\n") | map(select(length > 0))')
echo "changed_files=$CHANGED_ARRAY" >> $GITHUB_OUTPUT
- name: Extract issue number from PR
id: get_issue
if: steps.changed_plots.outputs.has_plots == 'true' && steps.get_pr.outputs.pr_number != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_BODY=$(gh pr view ${{ steps.get_pr.outputs.pr_number }} --json body -q '.body')
ISSUE_NUM=$(echo "$PR_BODY" | grep -oP '#\K\d+' | head -1 || echo "")
if [ -z "$ISSUE_NUM" ]; then
# Try to find issue by branch name
BRANCH=$(gh pr view ${{ steps.get_pr.outputs.pr_number }} --json headRefName -q '.headRefName')
SPEC_ID=$(echo "$BRANCH" | sed 's/auto\///')
ISSUE_NUM=$(gh issue list --label plot-request --search "$SPEC_ID in:title" --json number -q '.[0].number' || echo "")
fi
echo "issue_num=$ISSUE_NUM" >> $GITHUB_OUTPUT
echo "Found issue: #$ISSUE_NUM"
- name: Setup Google Cloud authentication
if: steps.changed_plots.outputs.has_plots == 'true'
id: gcs_auth
continue-on-error: true
uses: google-github-actions/auth@v3
with:
credentials_json: ${{ secrets.GCS_CREDENTIALS }}
- name: Setup gcloud CLI
if: steps.changed_plots.outputs.has_plots == 'true' && steps.gcs_auth.outcome == 'success'
uses: google-github-actions/setup-gcloud@v3
- name: Generate timestamp
id: timestamp
run: |
TIMESTAMP=$(date -u +"%Y-%m-%dT%H%M%SZ")
echo "value=$TIMESTAMP" >> $GITHUB_OUTPUT
- name: Detect previous versions and generate plots
id: generate_plots
if: steps.changed_plots.outputs.has_plots == 'true'
env:
GCS_BUCKET: ${{ secrets.GCS_BUCKET }}
TIMESTAMP: ${{ steps.timestamp.outputs.value }}
GCS_AUTH_SUCCESS: ${{ steps.gcs_auth.outcome }}
run: |
mkdir -p plot_outputs
echo "{}" > previous_versions.json
echo '${{ steps.changed_plots.outputs.changed_files }}' | jq -r '.[]' | while read -r file; do
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📊 Processing: $file"
if [[ $file =~ ^plots/([^/]+)/([^/]+)/([^/]+)/([^/]+)\.py$ ]]; then
LIBRARY="${BASH_REMATCH[1]}"
PLOT_TYPE="${BASH_REMATCH[2]}"
SPEC_ID="${BASH_REMATCH[3]}"
VARIANT="${BASH_REMATCH[4]}"
else
echo "⚠️ Invalid file path format: $file"
continue
fi
OUTPUT_DIR="plot_outputs/${SPEC_ID}/${LIBRARY}/${VARIANT}"
mkdir -p "$OUTPUT_DIR"
FULL_FILE="${OUTPUT_DIR}/v${TIMESTAMP}.png"
THUMB_FILE="${OUTPUT_DIR}/v${TIMESTAMP}_thumb.png"
PREVIOUS_URL=""
VERSION_COUNT=0
if [ "$GCS_AUTH_SUCCESS" == "success" ] && [ -n "$GCS_BUCKET" ]; then
GCS_PATH="gs://${GCS_BUCKET}/plots/${SPEC_ID}/${LIBRARY}/${VARIANT}/"
VERSIONS=$(gsutil ls "${GCS_PATH}v*.png" 2>/dev/null | grep -v "_thumb" | sort || echo "")
if [ -n "$VERSIONS" ]; then
VERSION_COUNT=$(echo "$VERSIONS" | wc -l)
PREVIOUS_GCS=$(echo "$VERSIONS" | tail -1)
PREVIOUS_URL=$(echo "$PREVIOUS_GCS" | sed "s|gs://${GCS_BUCKET}|https://storage.googleapis.com/${GCS_BUCKET}|")
jq --arg key "${SPEC_ID}_${LIBRARY}_${VARIANT}" \
--arg url "$PREVIOUS_URL" \
--arg count "$VERSION_COUNT" \
'. + {($key): {"url": $url, "count": ($count | tonumber)}}' \
previous_versions.json > tmp.json && mv tmp.json previous_versions.json
fi
fi
echo "🎨 Generating plot..."
MPLBACKEND=Agg uv run python "$file" 2>&1 | tee "${OUTPUT_DIR}/generation.log"
FOUND_IMAGE=false
for img in "plot.png" "test_output.png" "output.png" "${SPEC_ID}.png" "preview.png"; do
if [ -f "$img" ]; then
mv "$img" "$FULL_FILE"
FOUND_IMAGE=true
break
fi
done
if [ "$FOUND_IMAGE" == "false" ]; then
echo "⚠️ No output image found for $file"
continue
fi
echo "🖼️ Processing image..."
uv run python -m core.images process "$FULL_FILE" "$FULL_FILE" "$THUMB_FILE" "$SPEC_ID"
done
- name: Upload plots to GCS
if: steps.changed_plots.outputs.has_plots == 'true' && steps.gcs_auth.outcome == 'success'
continue-on-error: true
run: |
gsutil -m cp -r plot_outputs/* gs://${{ secrets.GCS_BUCKET }}/plots/
- name: Post results to Issue
if: steps.changed_plots.outputs.has_plots == 'true' && steps.get_issue.outputs.issue_num != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GCS_BUCKET: ${{ secrets.GCS_BUCKET }}
TIMESTAMP: ${{ steps.timestamp.outputs.value }}
shell: bash
run: |
ISSUE_NUM="${{ steps.get_issue.outputs.issue_num }}"
PR_NUM="${{ steps.get_pr.outputs.pr_number }}"
touch plot_markdown.txt
if [ "${{ steps.gcs_auth.outcome }}" == "success" ]; then
PREV_VERSIONS=$(cat previous_versions.json)
echo '${{ steps.changed_plots.outputs.changed_files }}' | jq -r '.[]' | while read -r file; do
if [[ $file =~ ^plots/([^/]+)/([^/]+)/([^/]+)/([^/]+)\.py$ ]]; then
LIBRARY="${BASH_REMATCH[1]}"
SPEC_ID="${BASH_REMATCH[3]}"
VARIANT="${BASH_REMATCH[4]}"
KEY="${SPEC_ID}_${LIBRARY}_${VARIANT}"
NEW_URL="https://storage.googleapis.com/${GCS_BUCKET}/plots/${SPEC_ID}/${LIBRARY}/${VARIANT}/v${TIMESTAMP}.png"
HISTORY_URL="https://console.cloud.google.com/storage/browser/${GCS_BUCKET}/plots/${SPEC_ID}/${LIBRARY}/${VARIANT}"
PREV_URL=$(echo "$PREV_VERSIONS" | jq -r --arg key "$KEY" '.[$key].url // empty')
PREV_COUNT=$(echo "$PREV_VERSIONS" | jq -r --arg key "$KEY" '.[$key].count // 0')
if [ -n "$PREV_URL" ] && [ "$PREV_URL" != "null" ]; then
NEW_COUNT=$((PREV_COUNT + 1))
cat >> plot_markdown.txt << 'PLOTEOF'
### ${LIBRARY} (${VARIANT}) - UPDATE
| Before | After |
|--------|-------|
|  |  |
[View version history (${NEW_COUNT} versions)](${HISTORY_URL})
PLOTEOF
sed -i "s|\${LIBRARY}|${LIBRARY}|g; s|\${VARIANT}|${VARIANT}|g; s|\${PREV_URL}|${PREV_URL}|g; s|\${NEW_URL}|${NEW_URL}|g; s|\${NEW_COUNT}|${NEW_COUNT}|g; s|\${HISTORY_URL}|${HISTORY_URL}|g" plot_markdown.txt
else
cat >> plot_markdown.txt << 'PLOTEOF'
### ${LIBRARY} (${VARIANT}) - NEW

PLOTEOF
sed -i "s|\${LIBRARY}|${LIBRARY}|g; s|\${VARIANT}|${VARIANT}|g; s|\${NEW_URL}|${NEW_URL}|g" plot_markdown.txt
fi
echo "" >> plot_markdown.txt
fi
done
fi
PLOT_MARKDOWN=$(cat plot_markdown.txt 2>/dev/null || echo "")
cat > comment_body.md << COMMENTEOF
## 🧪 Test Results (PR #${PR_NUM})
| Python | Status | Note |
|--------|--------|------|
| 3.14 | ✅ Pass | Primary (plot generated) |
| 3.13 | ℹ️ See workflow | Compatibility test |
| 3.12 | ℹ️ See workflow | Compatibility test |
| 3.11 | ℹ️ See workflow | Compatibility test |
**Note:** Code is optimized for Python 3.14.
## 📊 Plot Preview (Python 3.14)
${PLOT_MARKDOWN}
---
🤖 *Generated by [gen-preview workflow](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})*
COMMENTEOF
gh issue comment "$ISSUE_NUM" --body-file comment_body.md
- name: Save plot metadata
if: steps.changed_plots.outputs.has_plots == 'true'
env:
GCS_BUCKET: ${{ secrets.GCS_BUCKET }}
TIMESTAMP: ${{ steps.timestamp.outputs.value }}
run: |
cat > plot_metadata.json <<EOF
{
"pr_number": ${{ steps.get_pr.outputs.pr_number }},
"issue_number": "${{ steps.get_issue.outputs.issue_num }}",
"bucket": "${GCS_BUCKET}",
"base_path": "plots",
"timestamp": "${TIMESTAMP}",
"python_version": "3.14",
"changed_files": ${{ steps.changed_plots.outputs.changed_files }}
}
EOF
- name: Upload metadata artifact
if: steps.changed_plots.outputs.has_plots == 'true'
uses: actions/upload-artifact@v5
with:
name: plot-metadata
path: |
plot_metadata.json
previous_versions.json