-
Notifications
You must be signed in to change notification settings - Fork 0
750 lines (614 loc) · 28.9 KB
/
bot-auto-merge.yml
File metadata and controls
750 lines (614 loc) · 28.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
name: "Bot: Auto-Merge"
run-name: "Auto-Merge: ${{ github.event.inputs.pr_number || github.event.pull_request.head.ref || 'push' }}"
on:
pull_request:
types: [labeled, closed]
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to auto-merge'
required: true
type: number
push:
branches: [main]
paths:
- 'plots/**'
jobs:
# ============================================================================
# Job 1: Enable auto-merge when ai-approved label is added or dispatched
# ============================================================================
auto-merge:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
issues: write
steps:
- name: Check conditions
id: check
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Handle workflow_dispatch trigger
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
PR_NUM="${{ github.event.inputs.pr_number }}"
PR_DATA=$(gh pr view "$PR_NUM" --repo ${{ github.repository }} --json headRefName,labels)
BRANCH=$(echo "$PR_DATA" | jq -r '.headRefName')
HAS_APPROVED=$(echo "$PR_DATA" | jq -r '.labels[].name' | grep -c "ai-approved" || echo "0")
if [[ ! "$BRANCH" =~ ^auto/ ]]; then
echo "::notice::Skipping: Branch '$BRANCH' is not auto/*"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
if [[ "$HAS_APPROVED" == "0" ]]; then
echo "::notice::Skipping: PR does not have ai-approved label"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "pr_number=$PR_NUM" >> $GITHUB_OUTPUT
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
echo "should_run=true" >> $GITHUB_OUTPUT
exit 0
fi
# Handle pull_request labeled trigger
ACTION="${{ github.event.action }}"
LABEL="${{ github.event.label.name }}"
BRANCH="${{ github.event.pull_request.head.ref }}"
if [[ "$ACTION" != "labeled" ]]; then
echo "::notice::Skipping: Action is '$ACTION', not 'labeled'"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
if [[ "$LABEL" != "ai-approved" ]]; then
echo "::notice::Skipping: Label is '$LABEL', not 'ai-approved'"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
if [[ ! "$BRANCH" =~ ^auto/ ]]; then
echo "::notice::Skipping: Branch '$BRANCH' is not auto/*"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "pr_number=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
echo "should_run=true" >> $GITHUB_OUTPUT
- name: Checkout repository
if: steps.check.outputs.should_run == 'true'
uses: actions/checkout@v6
- name: Extract info from branch
if: steps.check.outputs.should_run == 'true'
id: extract
run: |
BRANCH="${{ steps.check.outputs.branch }}"
# Format: auto/{spec-id}/{library}
SPEC_ID=$(echo "$BRANCH" | cut -d'/' -f2)
LIBRARY=$(echo "$BRANCH" | cut -d'/' -f3)
echo "spec_id=$SPEC_ID" >> $GITHUB_OUTPUT
echo "library=$LIBRARY" >> $GITHUB_OUTPUT
- name: Get sub-issue from PR body
if: steps.check.outputs.should_run == 'true'
id: sub_issue
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUM="${{ steps.check.outputs.pr_number }}"
PR_BODY=$(gh pr view "$PR_NUM" --json body -q '.body' 2>/dev/null || echo "")
SUB_ISSUE=$(echo "$PR_BODY" | grep -oP '\*\*Sub-Issue:\*\* #\K\d+' | head -1 || echo "")
echo "number=$SUB_ISSUE" >> $GITHUB_OUTPUT
- name: React with rocket emoji
if: steps.check.outputs.should_run == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh api repos/${{ github.repository }}/issues/${{ steps.check.outputs.pr_number }}/reactions \
-f content=rocket
- name: Update sub-issue label
if: steps.check.outputs.should_run == 'true' && steps.sub_issue.outputs.number != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh issue edit ${{ steps.sub_issue.outputs.number }} \
--remove-label "reviewing" \
--remove-label "ai-rejected" \
--add-label "ai-approved"
- name: Merge PR directly
if: steps.check.outputs.should_run == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr merge ${{ steps.check.outputs.pr_number }} \
--repo ${{ github.repository }} \
--squash \
--delete-branch
- name: Close sub-issue after merge
if: steps.check.outputs.should_run == 'true' && steps.sub_issue.outputs.number != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SUB_ISSUE="${{ steps.sub_issue.outputs.number }}"
LIBRARY="${{ steps.extract.outputs.library }}"
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
PR_NUM="${{ steps.check.outputs.pr_number }}"
echo "Closing sub-issue #$SUB_ISSUE after merge"
# Update labels
gh issue edit $SUB_ISSUE \
--remove-label "ai-approved" \
--remove-label "reviewing" \
--remove-label "generating" \
--remove-label "testing" \
--add-label "merged" 2>/dev/null || true
# Post completion comment (only if not already posted)
EXISTING_MERGED=$(gh issue view $SUB_ISSUE --json comments -q '[.comments[].body | select(startswith("## Merged"))] | length' 2>/dev/null || echo "0")
if [ "$EXISTING_MERGED" = "0" ]; then
gh issue comment $SUB_ISSUE --body "## Merged
**$LIBRARY** implementation for \`$SPEC_ID\` has been merged!
- **PR:** #$PR_NUM
---
:rocket: *Auto-merged by pyplots CI*"
fi
# Close sub-issue (ignore if already closed)
gh issue close $SUB_ISSUE --reason completed 2>/dev/null || true
echo "Sub-issue #$SUB_ISSUE closed"
- name: Get main issue number
if: steps.check.outputs.should_run == 'true'
id: main_issue
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUM="${{ steps.check.outputs.pr_number }}"
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
PR_BODY=$(gh pr view "$PR_NUM" --json body -q '.body' 2>/dev/null || echo "")
# Extract main issue from PR body
MAIN_ISSUE=$(echo "$PR_BODY" | grep -oP '\*\*Parent Issue:\*\* #\K\d+' | head -1 || echo "")
# Fallback: search for main issue
if [ -z "$MAIN_ISSUE" ]; then
MAIN_ISSUE=$(gh issue list --label plot-request --search "$SPEC_ID in:title" --json number -q '.[0].number' 2>/dev/null || echo "")
fi
echo "number=$MAIN_ISSUE" >> $GITHUB_OUTPUT
echo "Main issue: #$MAIN_ISSUE"
- name: Check if all libraries are done
if: steps.check.outputs.should_run == 'true' && steps.main_issue.outputs.number != ''
id: check_complete
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
MAIN_ISSUE="${{ steps.main_issue.outputs.number }}"
# Get all sub-issues for this main issue
SUB_ISSUES=$(gh issue list --label "plot-request:impl" --search "Parent Issue.*#$MAIN_ISSUE" --state all --json number,labels -q '.' 2>/dev/null || echo "[]")
# Count statuses (use 'any' to avoid double-counting issues with multiple labels)
TOTAL=$(echo "$SUB_ISSUES" | jq 'length')
MERGED=$(echo "$SUB_ISSUES" | jq '[.[] | select(any(.labels[]; .name == "merged"))] | length')
NOT_FEASIBLE=$(echo "$SUB_ISSUES" | jq '[.[] | select(any(.labels[]; .name == "not-feasible"))] | length')
DONE=$((MERGED + NOT_FEASIBLE))
echo "total=$TOTAL" >> $GITHUB_OUTPUT
echo "merged=$MERGED" >> $GITHUB_OUTPUT
echo "not_feasible=$NOT_FEASIBLE" >> $GITHUB_OUTPUT
if [ "$DONE" -eq "$TOTAL" ] && [ "$TOTAL" -gt 0 ]; then
echo "all_done=true" >> $GITHUB_OUTPUT
else
echo "all_done=false" >> $GITHUB_OUTPUT
fi
echo "Progress: $DONE/$TOTAL complete ($MERGED merged, $NOT_FEASIBLE not feasible)"
- name: Create and merge feature-to-main PR
if: steps.check.outputs.should_run == 'true' && steps.check_complete.outputs.all_done == 'true' && steps.main_issue.outputs.number != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
MAIN_ISSUE="${{ steps.main_issue.outputs.number }}"
MERGED="${{ steps.check_complete.outputs.merged }}"
NOT_FEASIBLE="${{ steps.check_complete.outputs.not_feasible }}"
# Check if main issue already completed
LABELS=$(gh issue view "$MAIN_ISSUE" --json labels -q '.labels[].name' 2>/dev/null || echo "")
if echo "$LABELS" | grep -q "completed"; then
echo "Main issue #$MAIN_ISSUE already completed"
exit 0
fi
# Check if feature branch exists
if ! gh api repos/${{ github.repository }}/branches/plot/$SPEC_ID &>/dev/null; then
echo "Feature branch plot/$SPEC_ID does not exist, skipping"
exit 0
fi
# Check if PR already exists
EXISTING_PR=$(gh pr list --base main --head "plot/$SPEC_ID" --json number -q '.[0].number' 2>/dev/null || echo "")
if [ -n "$EXISTING_PR" ]; then
echo "PR #$EXISTING_PR already exists for plot/$SPEC_ID -> main"
gh pr merge "$EXISTING_PR" --squash --delete-branch --auto 2>/dev/null || true
FEATURE_PR="$EXISTING_PR"
else
# Create PR from feature branch to main
FEATURE_PR=$(gh pr create \
--base main \
--head "plot/$SPEC_ID" \
--title "feat: add $SPEC_ID implementation ($MERGED libraries)" \
--body "## Summary
Adds \`$SPEC_ID\` plot implementation.
### Libraries
- **Merged:** $MERGED
- **Not Feasible:** $NOT_FEASIBLE
### Links
- **Spec:** \`specs/$SPEC_ID.md\`
- **Parent Issue:** #$MAIN_ISSUE (closes on merge)
---
:robot: *Auto-generated by pyplots CI*" | sed 's|.*/||')
echo "Created PR #$FEATURE_PR"
# Enable auto-merge
gh pr merge "$FEATURE_PR" --squash --delete-branch --auto 2>/dev/null || true
fi
# Post completion to main issue
if [ "$NOT_FEASIBLE" -gt 0 ]; then
STATUS_MSG="$MERGED libraries ready, $NOT_FEASIBLE not feasible"
else
STATUS_MSG="All $MERGED libraries ready"
fi
gh issue comment "$MAIN_ISSUE" --body "## :tada: All Libraries Complete!
**Status:** $STATUS_MSG
**Feature PR:** #$FEATURE_PR (auto-merge enabled)
---
:rocket: *pyplots CI*"
# Add completed label
gh issue edit "$MAIN_ISSUE" --add-label "completed" 2>/dev/null || true
# ============================================================================
# Job 2: Post-merge summary (per library) - only for PR close events
# ============================================================================
post-merge-summary:
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
pull-requests: read
steps:
- name: Check conditions
id: check
run: |
ACTION="${{ github.event.action }}"
MERGED="${{ github.event.pull_request.merged }}"
BRANCH="${{ github.event.pull_request.head.ref }}"
if [[ "$ACTION" != "closed" ]]; then
echo "::notice::Skipping: Action is '$ACTION', not 'closed'"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
if [[ "$MERGED" != "true" ]]; then
echo "::notice::Skipping: PR was closed but not merged"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
if [[ ! "$BRANCH" =~ ^auto/ ]]; then
echo "::notice::Skipping: Branch '$BRANCH' is not auto/*"
echo "should_run=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "should_run=true" >> $GITHUB_OUTPUT
- name: Checkout code
if: steps.check.outputs.should_run == 'true'
uses: actions/checkout@v6
- name: Extract spec ID, library, and issues
if: steps.check.outputs.should_run == 'true'
id: extract
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="${{ github.event.pull_request.head.ref }}"
PR_NUM="${{ github.event.pull_request.number }}"
PR_BODY=$(gh pr view "$PR_NUM" --json body -q '.body' 2>/dev/null || echo "")
# Format: auto/{spec-id}/{library}
SPEC_ID=$(echo "$BRANCH" | cut -d'/' -f2)
LIBRARY=$(echo "$BRANCH" | cut -d'/' -f3)
# Extract issues from PR body
SUB_ISSUE=$(echo "$PR_BODY" | grep -oP '\*\*Sub-Issue:\*\* #\K\d+' | head -1 || echo "")
MAIN_ISSUE=$(echo "$PR_BODY" | grep -oP '\*\*Parent Issue:\*\* #\K\d+' | head -1 || echo "")
# Fallback: search for main issue
if [ -z "$MAIN_ISSUE" ]; then
MAIN_ISSUE=$(gh issue list --label plot-request --search "$SPEC_ID in:title" --json number -q '.[0].number' || echo "")
fi
echo "spec_id=$SPEC_ID" >> $GITHUB_OUTPUT
echo "library=$LIBRARY" >> $GITHUB_OUTPUT
echo "sub_issue=$SUB_ISSUE" >> $GITHUB_OUTPUT
echo "main_issue=$MAIN_ISSUE" >> $GITHUB_OUTPUT
echo "Spec: $SPEC_ID, Library: $LIBRARY, Sub: #$SUB_ISSUE, Main: #$MAIN_ISSUE"
- name: Find implementation file
if: steps.check.outputs.should_run == 'true'
id: files
run: |
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
LIBRARY="${{ steps.extract.outputs.library }}"
# Find implementation file for this library
IMPL_FILE=$(find plots/$LIBRARY -name "default.py" -path "*/$SPEC_ID/*" 2>/dev/null | head -1 || echo "")
echo "impl_file=$IMPL_FILE" >> $GITHUB_OUTPUT
- name: Update sub-issue to merged
if: steps.check.outputs.should_run == 'true' && steps.extract.outputs.sub_issue != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SUB_ISSUE="${{ steps.extract.outputs.sub_issue }}"
LIBRARY="${{ steps.extract.outputs.library }}"
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
PR_NUM="${{ github.event.pull_request.number }}"
# Check if already merged (skip if so)
LABELS=$(gh issue view "$SUB_ISSUE" --json labels -q '.labels[].name' 2>/dev/null || echo "")
if echo "$LABELS" | grep -q "merged"; then
echo "Sub-issue #$SUB_ISSUE already merged, skipping"
exit 0
fi
# Update labels
gh issue edit $SUB_ISSUE \
--remove-label "ai-approved" \
--remove-label "reviewing" \
--remove-label "generating" \
--remove-label "testing" \
--add-label "merged" 2>/dev/null || true
# Post completion comment (only if not already posted)
EXISTING_MERGED=$(gh issue view $SUB_ISSUE --json comments -q '[.comments[].body | select(startswith("## Merged"))] | length' 2>/dev/null || echo "0")
if [ "$EXISTING_MERGED" = "0" ]; then
gh issue comment $SUB_ISSUE --body "## Merged
**$LIBRARY** implementation for \`$SPEC_ID\` has been merged!
- **PR:** #$PR_NUM
---
:rocket: *Auto-merged by pyplots CI*"
fi
# Close sub-issue
gh issue close $SUB_ISSUE --reason completed 2>/dev/null || true
- name: Check if all libraries are done
if: steps.check.outputs.should_run == 'true'
id: check_complete
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
MAIN_ISSUE="${{ steps.extract.outputs.main_issue }}"
if [ -z "$MAIN_ISSUE" ]; then
echo "all_done=false" >> $GITHUB_OUTPUT
exit 0
fi
# Get all sub-issues for this main issue
SUB_ISSUES=$(gh issue list --search "**Parent Issue:** #$MAIN_ISSUE in:body" --json number,labels -q '.')
# Count statuses (use 'any' to avoid double-counting issues with multiple labels)
TOTAL=$(echo "$SUB_ISSUES" | jq 'length')
MERGED=$(echo "$SUB_ISSUES" | jq '[.[] | select(any(.labels[]; .name == "merged"))] | length')
NOT_FEASIBLE=$(echo "$SUB_ISSUES" | jq '[.[] | select(any(.labels[]; .name == "not-feasible"))] | length')
DONE=$((MERGED + NOT_FEASIBLE))
echo "total=$TOTAL" >> $GITHUB_OUTPUT
echo "merged=$MERGED" >> $GITHUB_OUTPUT
echo "not_feasible=$NOT_FEASIBLE" >> $GITHUB_OUTPUT
if [ "$DONE" -eq "$TOTAL" ] && [ "$TOTAL" -gt 0 ]; then
echo "all_done=true" >> $GITHUB_OUTPUT
else
echo "all_done=false" >> $GITHUB_OUTPUT
fi
echo "Progress: $DONE/$TOTAL complete ($MERGED merged, $NOT_FEASIBLE not feasible)"
- name: Post progress to main issue
if: steps.check.outputs.should_run == 'true' && steps.extract.outputs.main_issue != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
MAIN_ISSUE="${{ steps.extract.outputs.main_issue }}"
LIBRARY="${{ steps.extract.outputs.library }}"
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
PR_NUM="${{ github.event.pull_request.number }}"
MERGED="${{ steps.check_complete.outputs.merged }}"
NOT_FEASIBLE="${{ steps.check_complete.outputs.not_feasible }}"
TOTAL="${{ steps.check_complete.outputs.total }}"
gh issue comment $MAIN_ISSUE --body "## :white_check_mark: $LIBRARY Merged
**$LIBRARY** implementation for \`$SPEC_ID\` has been merged!
**Progress:** $MERGED merged, $NOT_FEASIBLE not feasible, $((TOTAL - MERGED - NOT_FEASIBLE)) pending
---
:robot: *PR #$PR_NUM*"
- name: Create and merge feature-to-main PR
if: steps.check.outputs.should_run == 'true' && steps.check_complete.outputs.all_done == 'true' && steps.extract.outputs.main_issue != ''
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
MAIN_ISSUE="${{ steps.extract.outputs.main_issue }}"
MERGED="${{ steps.check_complete.outputs.merged }}"
NOT_FEASIBLE="${{ steps.check_complete.outputs.not_feasible }}"
# Check if feature branch exists
if ! gh api repos/${{ github.repository }}/branches/plot/$SPEC_ID &>/dev/null; then
echo "Feature branch plot/$SPEC_ID does not exist, skipping"
exit 0
fi
# Check if PR already exists
EXISTING_PR=$(gh pr list --base main --head "plot/$SPEC_ID" --json number -q '.[0].number' 2>/dev/null || echo "")
if [ -n "$EXISTING_PR" ]; then
echo "PR #$EXISTING_PR already exists for plot/$SPEC_ID -> main"
if gh pr merge "$EXISTING_PR" --squash --delete-branch; then
FEATURE_PR="$EXISTING_PR"
else
echo "::error::Failed to merge existing PR #$EXISTING_PR"
exit 1
fi
else
# Create and merge feature PR using reusable script
.github/scripts/create-feature-pr.sh "$SPEC_ID" "$MAIN_ISSUE" "$MERGED" "$NOT_FEASIBLE"
fi
# Post completion to main issue
gh issue comment "$MAIN_ISSUE" --body "## :tada: All Complete!
All library implementations for \`$SPEC_ID\` have been merged to main!
### Final Status
- **Merged:** $MERGED libraries
- **Not Feasible:** $NOT_FEASIBLE libraries
- **Feature PR:** #$FEATURE_PR
---
:rocket: *pyplots CI*"
# Add completed label and close
gh issue edit "$MAIN_ISSUE" --add-label "completed"
gh issue close "$MAIN_ISSUE" --reason completed
# ============================================================================
# Job 3: Handle push-triggered post-merge summary
# ============================================================================
push-post-merge:
if: github.event_name == 'push'
runs-on: ubuntu-latest
permissions:
contents: read
issues: write
pull-requests: read
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 2
- name: Extract info from commit
id: extract
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# Get the commit message
COMMIT_MSG=$(git log -1 --format='%s')
COMMIT_SHA="${{ github.sha }}"
SHORT_SHA=$(echo "$COMMIT_SHA" | cut -c1-7)
echo "commit_msg=$COMMIT_MSG" >> $GITHUB_OUTPUT
echo "commit_sha=$COMMIT_SHA" >> $GITHUB_OUTPUT
echo "short_sha=$SHORT_SHA" >> $GITHUB_OUTPUT
# Try to extract library and spec from commit message
# Format: feat(library): implement spec-id (#PR)
if [[ "$COMMIT_MSG" =~ feat\(([a-z]+)\):.*implement[[:space:]]+([a-z0-9-]+) ]]; then
LIBRARY="${BASH_REMATCH[1]}"
SPEC_ID="${BASH_REMATCH[2]}"
echo "library=$LIBRARY" >> $GITHUB_OUTPUT
echo "spec_id=$SPEC_ID" >> $GITHUB_OUTPUT
echo "found=true" >> $GITHUB_OUTPUT
echo "Found: Library=$LIBRARY, Spec=$SPEC_ID"
else
echo "found=false" >> $GITHUB_OUTPUT
echo "Could not extract library/spec from commit: $COMMIT_MSG"
fi
- name: Find and update sub-issue
if: steps.extract.outputs.found == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
LIBRARY="${{ steps.extract.outputs.library }}"
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
COMMIT_SHA="${{ steps.extract.outputs.commit_sha }}"
SHORT_SHA="${{ steps.extract.outputs.short_sha }}"
# Find sub-issue for this library/spec
SUB_ISSUE=$(gh issue list --label "library:$LIBRARY" --search "[$SPEC_ID] in:title" --json number,labels -q '.[0].number' 2>/dev/null || echo "")
if [ -z "$SUB_ISSUE" ]; then
echo "No sub-issue found for $LIBRARY/$SPEC_ID"
exit 0
fi
# Check if already merged
LABELS=$(gh issue view "$SUB_ISSUE" --json labels -q '.labels[].name' 2>/dev/null || echo "")
if echo "$LABELS" | grep -q "merged"; then
echo "Sub-issue #$SUB_ISSUE already has merged label"
exit 0
fi
# Find implementation file
IMPL_FILE=$(find plots/$LIBRARY -name "default.py" -path "*/${SPEC_ID}/*" 2>/dev/null | head -1 || echo "")
# Update labels
gh issue edit "$SUB_ISSUE" \
--remove-label "ai-approved" \
--remove-label "reviewing" \
--remove-label "generating" \
--remove-label "testing" \
--add-label "merged" 2>/dev/null || true
# Post completion comment
gh issue comment "$SUB_ISSUE" --body "## Merged
**$LIBRARY** implementation for \`$SPEC_ID\` has been merged to main!
### Details
- **Commit:** [$SHORT_SHA](https://github.com/${{ github.repository }}/commit/$COMMIT_SHA)
- **File:** \`$IMPL_FILE\`
---
:rocket: *Auto-merged by pyplots CI (push trigger)*"
# Close sub-issue
gh issue close "$SUB_ISSUE" --reason completed
echo "Updated sub-issue #$SUB_ISSUE"
- name: Check if all libraries are done
if: steps.extract.outputs.found == 'true'
id: check_complete
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
# Find main issue
MAIN_ISSUE=$(gh issue list --label plot-request --search "$SPEC_ID in:title" --json number -q '.[0].number' 2>/dev/null || echo "")
if [ -z "$MAIN_ISSUE" ]; then
echo "all_done=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "main_issue=$MAIN_ISSUE" >> $GITHUB_OUTPUT
# Get all sub-issues for this main issue
SUB_ISSUES=$(gh issue list --search "**Parent Issue:** #$MAIN_ISSUE in:body" --json number,labels -q '.' 2>/dev/null || echo "[]")
# Count statuses (use 'any' to avoid double-counting issues with multiple labels)
TOTAL=$(echo "$SUB_ISSUES" | jq 'length')
MERGED=$(echo "$SUB_ISSUES" | jq '[.[] | select(any(.labels[]; .name == "merged"))] | length')
NOT_FEASIBLE=$(echo "$SUB_ISSUES" | jq '[.[] | select(any(.labels[]; .name == "not-feasible"))] | length')
DONE=$((MERGED + NOT_FEASIBLE))
echo "total=$TOTAL" >> $GITHUB_OUTPUT
echo "merged=$MERGED" >> $GITHUB_OUTPUT
echo "not_feasible=$NOT_FEASIBLE" >> $GITHUB_OUTPUT
if [ "$DONE" -eq "$TOTAL" ] && [ "$TOTAL" -gt 0 ]; then
echo "all_done=true" >> $GITHUB_OUTPUT
else
echo "all_done=false" >> $GITHUB_OUTPUT
fi
echo "Progress: $DONE/$TOTAL complete ($MERGED merged, $NOT_FEASIBLE not feasible)"
- name: Create and merge feature-to-main PR
if: steps.extract.outputs.found == 'true' && steps.check_complete.outputs.all_done == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
MAIN_ISSUE="${{ steps.check_complete.outputs.main_issue }}"
SPEC_ID="${{ steps.extract.outputs.spec_id }}"
MERGED="${{ steps.check_complete.outputs.merged }}"
NOT_FEASIBLE="${{ steps.check_complete.outputs.not_feasible }}"
# Check if already completed
LABELS=$(gh issue view "$MAIN_ISSUE" --json labels -q '.labels[].name' 2>/dev/null || echo "")
if echo "$LABELS" | grep -q "completed"; then
echo "Main issue #$MAIN_ISSUE already completed"
exit 0
fi
# Check if feature branch exists
if ! gh api repos/${{ github.repository }}/branches/plot/$SPEC_ID &>/dev/null; then
echo "Feature branch plot/$SPEC_ID does not exist, skipping"
exit 0
fi
# Check if PR already exists
EXISTING_PR=$(gh pr list --base main --head "plot/$SPEC_ID" --json number -q '.[0].number' 2>/dev/null || echo "")
if [ -n "$EXISTING_PR" ]; then
echo "PR #$EXISTING_PR already exists for plot/$SPEC_ID -> main"
if gh pr merge "$EXISTING_PR" --squash --delete-branch; then
FEATURE_PR="$EXISTING_PR"
else
echo "::error::Failed to merge existing PR #$EXISTING_PR"
exit 1
fi
else
# Create PR from feature branch to main
PR_URL=$(gh pr create \
--base main \
--head "plot/$SPEC_ID" \
--title "feat: add $SPEC_ID implementation ($MERGED libraries)" \
--body "## Summary
Adds \`$SPEC_ID\` plot implementation.
### Libraries
- **Merged:** $MERGED
- **Not Feasible:** $NOT_FEASIBLE
### Links
- **Spec:** \`specs/$SPEC_ID.md\`
- **Parent Issue:** #$MAIN_ISSUE
---
:robot: *Auto-generated by pyplots CI*")
FEATURE_PR=$(echo "$PR_URL" | sed 's|.*/||')
echo "Created PR #$FEATURE_PR"
# Merge the PR immediately
if ! gh pr merge "$FEATURE_PR" --squash --delete-branch; then
echo "::error::Failed to merge PR #$FEATURE_PR"
gh issue comment "$MAIN_ISSUE" --body "## :warning: Merge Failed
Failed to automatically merge PR #$FEATURE_PR. Please merge manually."
exit 1
fi
fi
# Post completion to main issue
gh issue comment "$MAIN_ISSUE" --body "## :tada: All Complete!
All library implementations for \`$SPEC_ID\` have been merged to main!
### Final Status
- **Merged:** $MERGED libraries
- **Not Feasible:** $NOT_FEASIBLE libraries
- **Feature PR:** #$FEATURE_PR
---
:rocket: *pyplots CI (push trigger)*"
# Add completed label and close
gh issue edit "$MAIN_ISSUE" --add-label "completed"
gh issue close "$MAIN_ISSUE" --reason completed