-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathe2e.sh
More file actions
executable file
·2534 lines (2208 loc) · 93.5 KB
/
e2e.sh
File metadata and controls
executable file
·2534 lines (2208 loc) · 93.5 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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# End-to-end testing script for GitHub Agentic Workflows
# This script triggers all test workflows and validates their outcomes
#
# Usage: ./e2e.sh [OPTIONS]
#
# This script will:
# 1. Check prerequisites (gh CLI, authentication, gh-aw binary)
# 2. Enable workflows before testing them
# 3. Trigger workflows using "gh aw run"
# 4. Wait for completion and validate outcomes
# 5. Disable workflows after testing
# 6. Generate comprehensive test report
# 7. Optionally clean up test resources
#
# Test Types:
# - workflow_dispatch: Direct trigger tests (create issues, PRs, code scanning alerts, etc.)
# - issue-triggered: Tests triggered by creating issues with specific titles
# - command-triggered: Tests triggered by posting commands in issue comments
# - PR-triggered: Tests triggered by creating pull requests
#
# Options:
# --dry-run Show what would be tested without running
# --workflow-dispatch-only Only run tests that use workflow_dispatch trigger
# (skip issue/comment/PR-triggered tests)
# --help, -h Show help message
#
# Examples:
# ./e2e.sh # Run all tests
# ./e2e.sh --dry-run # See what would be tested
# ./e2e.sh test-copilot-* --workflow-dispatch-only # Only workflow_dispatch tests
#
# Prerequisites:
# - GitHub CLI (gh) installed and authenticated
# - gh-aw binary built (run 'make build')
# - Proper repository permissions for creating issues/PRs
# - Internet access for GitHub API calls
set -uo pipefail # Removed -e to allow test failures without stopping the script
# Error Handling Strategy:
# - Individual test failures are tracked but don't stop the overall test suite
# - Polling timeouts are handled gracefully and recorded as test failures
# - Critical prerequisite failures (like missing gh CLI) still exit immediately
# - Cleanup operations continue even if some steps fail
# Colors and emojis for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Test results tracking
declare -a PASSED_TESTS=()
declare -a FAILED_TESTS=()
declare -a SKIPPED_TESTS=()
declare -A TEST_RUN_URLS=() # maps test name -> actions run URL (when available)
# Record a test pass: update arrays and remove from fails.txt
record_test_pass() {
local test_name="$1"
PASSED_TESTS+=("$test_name")
# Remove the test from fails.txt if present
if [[ -f "fails.txt" ]]; then
local _tmp
_tmp=$(grep -v "^${test_name} \|^${test_name}$" "fails.txt" 2>/dev/null || true)
if [[ -n "$_tmp" ]]; then
echo "$_tmp" > "fails.txt"
else
rm -f "fails.txt"
fi
fi
}
# Record a test failure: update arrays and add/append to fails.txt
record_test_fail() {
local test_name="$1"
FAILED_TESTS+=("$test_name")
# Look up the run ID
local _url="${TEST_RUN_URLS[$test_name]:-}"
local _run_id=""
if [[ -n "$_url" ]]; then
_run_id="${_url##*/}"
fi
if [[ -z "$_run_id" ]]; then
local _wf="${test_name}.lock.yml"
_run_id=$(gh run list \
--repo "$REPO_OWNER/$REPO_NAME" \
--workflow="$_wf" \
--limit=1 \
--json databaseId \
--jq '.[0].databaseId' 2>/dev/null || echo "")
fi
# Update fails.txt: append run ID to existing line or add new entry
if [[ -f "fails.txt" ]] && grep -q "^${test_name} \|^${test_name}$" "fails.txt" 2>/dev/null; then
if [[ -n "$_run_id" ]]; then
local _existing_line
_existing_line=$(grep "^${test_name} \|^${test_name}$" "fails.txt")
if [[ "$_existing_line" != *"$_run_id"* ]]; then
sed -i "s|^${test_name}\( .*\)\?$|${test_name}\1 ${_run_id}|" "fails.txt"
fi
fi
else
if [[ -n "$_run_id" ]]; then
echo "$test_name $_run_id" >> "fails.txt"
else
echo "$test_name" >> "fails.txt"
fi
fi
}
# Helper function to safely execute commands that might fail
# Usage: safe_run "operation description" command arg1 arg2...
safe_run() {
local description="$1"
shift
if "$@"; then
return 0
else
local exit_code=$?
warning "Failed to $description (exit code: $exit_code)"
return $exit_code
fi
}
# Configuration
REPO_OWNER="githubnext"
REPO_NAME="gh-aw-test"
TIMEOUT_MINUTES=10
POLL_INTERVAL=5
LOG_FILE="e2e-test-$(date +%Y%m%d-%H%M%S).log"
TEMP_USER_PAT_SET=false
WORKFLOW_DISPATCH_ONLY=false
# Utility functions
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') $*" | tee -a "$LOG_FILE"
}
info() {
echo -e "${BLUE}ℹ️ $*${NC}" | tee -a "$LOG_FILE"
}
success() {
echo -e "${GREEN}✅ $*${NC}" | tee -a "$LOG_FILE"
}
warning() {
echo -e "${YELLOW}⚠️ $*${NC}" | tee -a "$LOG_FILE"
}
error() {
echo -e "${RED}❌ $*${NC}" | tee -a "$LOG_FILE"
}
progress() {
echo -e "${PURPLE}🔨 $*${NC}" | tee -a "$LOG_FILE"
}
# Secret management functions
set_temp_user_pat() {
info "Setting TEMP_USER_PAT secret for cross-repo testing..."
# Get the current user's PAT
local user_pat=$(gh auth token 2>/dev/null)
if [[ -z "$user_pat" ]]; then
error "Failed to get GitHub auth token. Run 'gh auth login'"
return 1
fi
# Set the secret in the repository (gh secret set is idempotent - overwrites if already present)
local secret_err
local max_attempts=3
local attempt=1
while [[ $attempt -le $max_attempts ]]; do
secret_err=$(echo "$user_pat" | gh secret set TEMP_USER_PAT --repo "$REPO_OWNER/$REPO_NAME" 2>&1)
local rc=$?
echo "$secret_err" >> "$LOG_FILE"
if [[ $rc -eq 0 ]]; then
TEMP_USER_PAT_SET=true
success "TEMP_USER_PAT secret set successfully"
return 0
fi
if [[ $attempt -lt $max_attempts ]]; then
warning "Failed to set TEMP_USER_PAT secret (attempt $attempt/$max_attempts): $secret_err"
sleep 5
fi
attempt=$((attempt + 1))
done
error "Failed to set TEMP_USER_PAT secret after $max_attempts attempts: $secret_err"
return 1
}
delete_temp_user_pat() {
if [[ "$TEMP_USER_PAT_SET" == true ]]; then
info "Cleaning up TEMP_USER_PAT secret..."
if gh secret delete TEMP_USER_PAT --repo "$REPO_OWNER/$REPO_NAME" &>> "$LOG_FILE"; then
TEMP_USER_PAT_SET=false
success "TEMP_USER_PAT secret deleted successfully"
return 0
else
warning "Failed to delete TEMP_USER_PAT secret (it may not exist)"
return 1
fi
fi
}
cleanup_on_exit() {
echo
info "Performing cleanup..."
delete_temp_user_pat
}
# Test pattern matching functions
matches_pattern() {
local test_name="$1"
local pattern="$2"
# Convert glob pattern to regex
local regex_pattern=$(echo "$pattern" | sed 's/\*/[^[:space:]]*/g')
if [[ "$test_name" =~ ^${regex_pattern}$ ]]; then
return 0
else
return 1
fi
}
# Get target repo from workflow name (defaults to current repo)
get_target_repo() {
local workflow_name="$1"
if [[ "$workflow_name" == *"siderepo"* ]]; then
echo "githubnext/gh-aw-side-repo"
else
echo ""
fi
}
# Extract AI type from workflow name
extract_ai_type() {
local workflow_name="$1"
# Check for nosandbox variants first (more specific)
if [[ "$workflow_name" == *"claude-nosandbox"* ]]; then
echo "claude-nosandbox"
elif [[ "$workflow_name" == *"codex-nosandbox"* ]]; then
echo "codex-nosandbox"
elif [[ "$workflow_name" == *"copilot-nosandbox"* ]]; then
echo "copilot-nosandbox"
# Then check for regular variants
elif [[ "$workflow_name" == *"claude"* ]]; then
echo "claude"
elif [[ "$workflow_name" == *"codex"* ]]; then
echo "codex"
elif [[ "$workflow_name" == *"copilot"* ]]; then
echo "copilot"
else
echo ""
fi
}
# Get display name for AI type
get_ai_display_name() {
local ai_type="$1"
case "$ai_type" in
claude-nosandbox)
echo "Claude (No Sandbox)"
;;
codex-nosandbox)
echo "Codex (No Sandbox)"
;;
copilot-nosandbox)
echo "Copilot (No Sandbox)"
;;
claude)
echo "Claude"
;;
codex)
echo "Codex"
;;
copilot)
echo "Copilot"
;;
*)
echo "${ai_type^}"
;;
esac
}
# Get expected labels for AI type
# Nosandbox variants use separate labels: base-type, nosandbox, automation
# Regular variants use: base-type, automation
get_expected_labels() {
local ai_type="$1"
case "$ai_type" in
claude-nosandbox)
echo "claude,nosandbox,automation"
;;
codex-nosandbox)
echo "codex,nosandbox,automation"
;;
copilot-nosandbox)
echo "copilot,nosandbox,automation"
;;
*)
echo "${ai_type},automation"
;;
esac
}
should_run_test() {
local test_name="$1"
local patterns=("${@:2}")
# If no patterns specified, run all tests
if [[ ${#patterns[@]} -eq 0 ]]; then
return 0
fi
# Check if test matches any pattern
for pattern in "${patterns[@]}"; do
if matches_pattern "$test_name" "$pattern"; then
return 0
fi
done
return 1
}
get_all_tests() {
# Workflow dispatch tests
echo "test-claude-create-issue"
echo "test-codex-create-issue"
echo "test-copilot-create-issue"
echo "test-claude-create-discussion"
echo "test-codex-create-discussion"
echo "test-copilot-create-discussion"
echo "test-claude-create-pull-request"
echo "test-codex-create-pull-request"
echo "test-copilot-create-pull-request"
echo "test-claude-create-two-pull-requests"
echo "test-codex-create-two-pull-requests"
echo "test-copilot-create-two-pull-requests"
echo "test-claude-create-code-scanning-alert"
echo "test-codex-create-repository-code-scanning-alert"
echo "test-copilot-create-repository-code-scanning-alert"
echo "test-claude-mcp"
echo "test-codex-mcp"
echo "test-copilot-mcp"
echo "test-claude-custom-safe-outputs"
echo "test-codex-custom-safe-outputs"
echo "test-copilot-custom-safe-outputs"
echo "test-copilot-gh-steps"
# Issue-triggered tests
echo "test-claude-add-comment"
echo "test-claude-add-labels"
echo "test-claude-add-discussion-comment"
echo "test-codex-add-comment"
echo "test-codex-add-labels"
echo "test-codex-add-discussion-comment"
echo "test-copilot-add-comment"
echo "test-copilot-add-labels"
echo "test-copilot-add-discussion-comment"
echo "test-claude-update-issue"
echo "test-codex-update-issue"
echo "test-copilot-update-issue"
echo "test-copilot-close-issue"
echo "test-copilot-remove-labels"
echo "test-copilot-close-discussion"
# PR-triggered tests
echo "test-claude-update-pull-request"
echo "test-codex-update-pull-request"
echo "test-copilot-update-pull-request"
echo "test-copilot-close-pull-request"
# Command-triggered tests
echo "test-claude-command"
echo "test-codex-command"
echo "test-copilot-command"
echo "test-claude-push-to-pull-request-branch"
echo "test-codex-push-to-pull-request-branch"
echo "test-copilot-push-to-pull-request-branch"
echo "test-claude-create-pull-request-review-comment"
echo "test-codex-create-pull-request-review-comment"
echo "test-copilot-create-pull-request-review-comment"
# Nosandbox tests - limited set for claude/codex, full matrix for copilot
echo "test-claude-nosandbox-create-issue"
echo "test-codex-nosandbox-create-issue"
echo "test-copilot-nosandbox-create-issue"
echo "test-copilot-nosandbox-create-discussion"
echo "test-copilot-nosandbox-create-pull-request"
echo "test-copilot-nosandbox-create-two-pull-requests"
echo "test-copilot-nosandbox-create-repository-code-scanning-alert"
echo "test-copilot-nosandbox-mcp"
echo "test-copilot-nosandbox-custom-safe-outputs"
echo "test-copilot-nosandbox-add-comment"
echo "test-copilot-nosandbox-add-labels"
echo "test-copilot-nosandbox-add-discussion-comment"
echo "test-copilot-nosandbox-update-issue"
echo "test-copilot-nosandbox-command"
echo "test-copilot-nosandbox-push-to-pull-request-branch"
echo "test-copilot-nosandbox-create-pull-request-review-comment"
# Siderepo tests - cross-repo private repository tests
echo "test-copilot-siderepo-create-issue"
echo "test-copilot-siderepo-create-discussion"
echo "test-copilot-siderepo-create-pull-request"
echo "test-copilot-siderepo-create-two-pull-requests"
# echo "test-copilot-siderepo-create-repository-code-scanning-alert" # Disabled: doesn't support target-repo
echo "test-copilot-siderepo-mcp"
# echo "test-copilot-siderepo-custom-safe-outputs" # Disabled: doesn't support target-repo
echo "test-copilot-siderepo-add-comment"
echo "test-copilot-siderepo-add-labels"
echo "test-copilot-siderepo-add-discussion-comment"
echo "test-copilot-siderepo-update-issue"
# echo "test-copilot-siderepo-push-to-pull-request-branch" # Disabled: doesn't support target-repo
echo "test-copilot-siderepo-create-pull-request-review-comment"
}
filter_tests() {
local patterns=("$@")
local all_tests
all_tests=($(get_all_tests))
local filtered_tests=()
for test in "${all_tests[@]}"; do
if should_run_test "$test" "${patterns[@]}"; then
filtered_tests+=("$test")
fi
done
# Only print if there are filtered tests
if [[ ${#filtered_tests[@]} -gt 0 ]]; then
printf '%s\n' "${filtered_tests[@]}"
fi
}
check_prerequisites() {
info "Checking prerequisites..."
# Check gh CLI is installed and authenticated
if ! command -v gh &> /dev/null; then
error "GitHub CLI (gh) is not installed"
exit 1
fi
# Check authentication
if ! gh auth status &> /dev/null; then
error "GitHub CLI is not authenticated. Run 'gh auth login'"
exit 1
fi
# Install or upgrade the gh-aw extension
info "Checking gh-aw extension..."
# Check if the extension is already installed
if gh extension list | grep -q "github/gh-aw"; then
info "gh-aw extension already installed, upgrading to latest version..."
if gh extension upgrade github/gh-aw &>> "$LOG_FILE"; then
success "gh-aw extension upgraded successfully"
else
warning "Failed to upgrade gh-aw extension, continuing with existing version"
fi
else
info "Installing gh-aw extension..."
if gh extension install github/gh-aw &>> "$LOG_FILE"; then
success "gh-aw extension installed successfully"
else
error "Failed to install gh-aw extension. Check $LOG_FILE for details"
exit 1
fi
fi
# Verify the extension is available
if ! gh aw --version &>> "$LOG_FILE"; then
error "gh-aw extension is not available after installation"
exit 1
fi
# Check we're in the right repo
local current_repo=$(gh repo view --json nameWithOwner -q .nameWithOwner 2>/dev/null || echo "")
if [[ "$current_repo" != "$REPO_OWNER/$REPO_NAME" ]]; then
error "Not in the correct repository. Expected $REPO_OWNER/$REPO_NAME, got $current_repo"
exit 1
fi
# Run "gh aw compile"
if ! gh aw compile 2>&1 | tee -a "$LOG_FILE"; then
error "'gh aw compile' failed. Check $LOG_FILE for details"
exit 1
fi
# If there are any updates from the compile, commit them and push them to main to make
# sure the workflows are up to date for testing
local git_status
git_status=$(git status --porcelain)
if [[ -n "$git_status" ]]; then
info "Detected changes after 'gh aw compile'; committing and pushing to main branch"
git add . &>> "$LOG_FILE"
git commit -m "chore: update compiled workflows via e2e.sh" &>> "$LOG_FILE"
if git push origin main &>> "$LOG_FILE"; then
success "Changes pushed to main branch"
else
error "Failed to push changes to main branch. Check $LOG_FILE for details"
exit 1
fi
else
info "No changes detected after 'gh-aw compile'"
fi
# Set TEMP_USER_PAT secret for cross-repo testing
if ! set_temp_user_pat; then
error "Failed to set TEMP_USER_PAT secret. Cross-repo tests will fail."
exit 1
fi
success "Prerequisites check passed"
}
disable_all_workflows_before_testing() {
info "Disabling all workflows that aren't already disabled..."
# Get list of all workflows with their state
# Format: workflow_id state
progress "Running: gh workflow list --all --json name,state"
local workflows_output
workflows_output=$(gh workflow list --all --json name,state --jq '.[] | "\(.name)\t\(.state)"' 2>/dev/null)
if [[ -z "$workflows_output" ]]; then
warning "No workflows found or failed to list workflows"
return 0
fi
local disabled_count=0
local already_disabled_count=0
while IFS=$'\t' read -r workflow_name workflow_state; do
# Skip if already disabled
if [[ "$workflow_state" == "disabled_manually" ]] || [[ "$workflow_state" == "disabled_inactivity" ]]; then
info " ⏭️ Skipping '$workflow_name' (already $workflow_state)"
already_disabled_count=$((already_disabled_count + 1))
continue
fi
# Disable the workflow
progress " Disabling '$workflow_name' (currently $workflow_state)..."
if gh workflow disable "$workflow_name" &>> "$LOG_FILE"; then
success " ✓ Disabled '$workflow_name'"
disabled_count=$((disabled_count + 1))
else
warning "Failed to disable workflow '$workflow_name'"
fi
done <<< "$workflows_output"
echo
if [[ $disabled_count -gt 0 ]]; then
success "Disabled $disabled_count workflow(s) ($already_disabled_count were already disabled)"
else
info "All workflows were already disabled ($already_disabled_count total)"
fi
}
wait_for_workflow() {
local workflow_name="$1"
local run_id="$2"
local timeout_seconds=$((TIMEOUT_MINUTES * 60))
local start_time=$(date +%s)
local max_consecutive_failures=10
local consecutive_failures=0
progress "Waiting for workflow '$workflow_name' (run #$run_id) to complete..."
progress "View run details: https://github.com/$REPO_OWNER/$REPO_NAME/actions/runs/$run_id"
while true; do
local current_time=$(date +%s)
local elapsed=$((current_time - start_time))
if [[ $elapsed -gt $timeout_seconds ]]; then
error "Timeout waiting for workflow '$workflow_name' after $TIMEOUT_MINUTES minutes"
error "View run details: https://github.com/$REPO_OWNER/$REPO_NAME/actions/runs/$run_id"
return 1
fi
local status conclusion
if status=$(gh run view "$run_id" --json status,conclusion -q '.status + "," + (.conclusion // "")' 2>/dev/null); then
consecutive_failures=0
IFS=',' read -r run_status run_conclusion <<< "$status"
case "$run_status" in
"completed")
case "$run_conclusion" in
"success")
success "Workflow '$workflow_name' completed successfully"
return 0
;;
"failure"|"cancelled"|"timed_out")
error "Workflow '$workflow_name' failed with conclusion: $run_conclusion"
error "View run details: https://github.com/$REPO_OWNER/$REPO_NAME/actions/runs/$run_id"
return 1
;;
*)
error "Workflow '$workflow_name' completed with unexpected conclusion: $run_conclusion"
error "View run details: https://github.com/$REPO_OWNER/$REPO_NAME/actions/runs/$run_id"
return 1
;;
esac
;;
"in_progress"|"queued"|"requested"|"waiting"|"pending")
echo -n "."
sleep $POLL_INTERVAL
;;
*)
error "Workflow '$workflow_name' has unexpected status: $run_status"
error "View run details: https://github.com/$REPO_OWNER/$REPO_NAME/actions/runs/$run_id"
return 1
;;
esac
else
consecutive_failures=$((consecutive_failures + 1))
if [[ $consecutive_failures -ge $max_consecutive_failures ]]; then
error "Failed to get status for workflow run $run_id after $max_consecutive_failures consecutive attempts"
error "View run details: https://github.com/$REPO_OWNER/$REPO_NAME/actions/runs/$run_id"
return 1
fi
warning "Failed to get status for workflow run $run_id (attempt $consecutive_failures/$max_consecutive_failures, retrying...)"
sleep $POLL_INTERVAL
fi
done
}
get_latest_run_id() {
local workflow_file="$1"
gh run list --workflow="$workflow_file" --limit=1 --json databaseId -q '.[0].databaseId' 2>/dev/null || echo ""
}
enable_workflow() {
local workflow_name="$1"
info "Enabling workflow '$workflow_name'..."
# Redirect gh aw enable output to log file to prevent terminal control codes from clearing previous output
gh aw enable "$workflow_name" &>> "$LOG_FILE"
local rc=$?
if [[ $rc -eq 0 ]]; then
success "Successfully enabled '$workflow_name'"
return 0
else
error "Failed to enable '$workflow_name' (exit code: $rc)"
return 1
fi
}
disable_workflow() {
local workflow_name="$1"
info "Disabling workflow '$workflow_name'..."
gh aw disable "$workflow_name" &>> "$LOG_FILE"
local rc=$?
if [[ $rc -eq 0 ]]; then
success "Successfully disabled '$workflow_name'"
return 0
else
warning "Failed to disable '$workflow_name' (exit code: $rc; may already be disabled)"
return 0 # Don't fail the test if disable fails
fi
}
trigger_workflow_dispatch_and_await_completion() {
local workflow_name="$1"
local workflow_file="${workflow_name}.lock.yml"
info "Triggering workflow_dispatch for '$workflow_name'..."
# Enable the workflow first
# NOTE: This must return early only when enabling fails. A prior bug
# inverted this condition causing immediate failure even when enable succeeded.
if ! enable_workflow "$workflow_name"; then
return 1
fi
# Get the run ID before triggering
local before_run_id=$(get_latest_run_id "$workflow_file")
# Trigger the workflow using gh aw run
if gh aw run "$workflow_name" &>> "$LOG_FILE"; then
success "Successfully triggered '$workflow_name'"
# Wait a bit for the new run to appear
sleep 5
# Get the new run ID
local after_run_id=$(get_latest_run_id "$workflow_file")
if [[ "$after_run_id" != "$before_run_id" && -n "$after_run_id" ]]; then
local result=0
TEST_RUN_URLS["$workflow_name"]="https://github.com/$REPO_OWNER/$REPO_NAME/actions/runs/$after_run_id"
wait_for_workflow "$workflow_name" "$after_run_id" || result=1
# Disable the workflow after running
disable_workflow "$workflow_name"
return $result
else
error "Could not find new workflow run for '$workflow_name'"
disable_workflow "$workflow_name"
return 1
fi
else
error "Failed to trigger '$workflow_name'"
disable_workflow "$workflow_name"
return 1
fi
}
trigger_workflow_with_inputs() {
local workflow_name="$1"
shift
local inputs=("$@")
local workflow_file="${workflow_name}.lock.yml"
info "Triggering workflow_dispatch for '$workflow_name' with inputs..."
# Enable the workflow first
if ! enable_workflow "$workflow_name"; then
return 1
fi
# Get the run ID before triggering
local before_run_id=$(get_latest_run_id "$workflow_file")
# Build the gh workflow run command with inputs
local cmd="gh workflow run \"$workflow_file\""
for input in "${inputs[@]}"; do
cmd+=" -f $input"
done
cmd+=" &>> \"$LOG_FILE\""
# Trigger the workflow using gh workflow run with inputs
if eval "$cmd"; then
success "Successfully triggered '$workflow_name' with inputs"
# Wait a bit for the new run to appear
sleep 5
# Get the new run ID
local after_run_id=$(get_latest_run_id "$workflow_file")
if [[ "$after_run_id" != "$before_run_id" && -n "$after_run_id" ]]; then
local result=0
TEST_RUN_URLS["$workflow_name"]="https://github.com/$REPO_OWNER/$REPO_NAME/actions/runs/$after_run_id"
wait_for_workflow "$workflow_name" "$after_run_id" || result=1
# Disable the workflow after running
disable_workflow "$workflow_name"
return $result
else
error "Could not find new workflow run for '$workflow_name'"
disable_workflow "$workflow_name"
return 1
fi
else
error "Failed to trigger '$workflow_name'"
disable_workflow "$workflow_name"
return 1
fi
}
create_test_issue() {
local title="$1"
local body="$2"
local labels="${3:-}"
local repo="${4:-}"
local repo_flag=""
if [[ -n "$repo" ]]; then
repo_flag="--repo $repo"
fi
local issue_url
if [[ -n "$labels" ]]; then
issue_url=$(gh issue create $repo_flag --title "$title" --body "$body" --label "$labels" 2>/dev/null)
else
issue_url=$(gh issue create $repo_flag --title "$title" --body "$body" 2>/dev/null)
fi
if [[ -n "$issue_url" ]]; then
local issue_number=$(echo "$issue_url" | grep -o '[0-9]\+$')
echo "$issue_number"
else
echo ""
fi
}
create_test_discussion() {
local title="$1"
local body="$2"
local category="${3:-General}"
local repo="${4:-}"
local owner="$REPO_OWNER"
local name="$REPO_NAME"
if [[ -n "$repo" ]]; then
owner=$(echo "$repo" | cut -d/ -f1)
name=$(echo "$repo" | cut -d/ -f2)
fi
# Get repository ID using GraphQL
local repo_query="{
repository(owner: \"$owner\", name: \"$name\") {
id
}
}"
local repo_id=$(gh api graphql -f query="$repo_query" --jq '.data.repository.id' 2>/dev/null)
# Get category ID using GraphQL
local category_query="{
repository(owner: \"$owner\", name: \"$name\") {
discussionCategories(first: 10) {
nodes {
id
name
}
}
}
}"
local category_id=$(gh api graphql -f query="$category_query" --jq ".data.repository.discussionCategories.nodes[] | select(.name==\"$category\") | .id" 2>/dev/null)
if [[ -z "$repo_id" || -z "$category_id" ]]; then
echo ""
return
fi
# Create discussion using GraphQL mutation
local mutation="mutation {
createDiscussion(input: {
repositoryId: \"$repo_id\"
categoryId: \"$category_id\"
title: \"$title\"
body: \"$body\"
}) {
discussion {
number
}
}
}"
local discussion_data=$(gh api graphql -f query="$mutation" --jq '.data.createDiscussion.discussion.number // empty' 2>/dev/null)
if [[ -n "$discussion_data" ]]; then
echo "$discussion_data"
else
echo ""
fi
}
create_test_pr() {
local title="$1"
local body="$2"
local repo="${3:-}"
local branch="test-pr-$(date +%s)"
local repo_flag=""
local api_repo=":owner/:repo"
if [[ -n "$repo" ]]; then
repo_flag="--repo $repo"
api_repo="$repo"
fi
# Create a remote branch from main without changing local git state
if [[ -n "$repo" ]]; then
git push "https://github.com/$repo.git" "main:$branch" &>/dev/null
else
git push origin "main:$branch" &>/dev/null
fi
# Create a commit on the remote branch using GitHub API to make it different from main
local commit_message="Test commit for PR"
local file_content="# Test PR Content\n\nThis is a test file created for PR testing at $(date)"
local file_path="test-file-$(date +%s).md"
# Get the current SHA of the branch
local remote_url="origin"
if [[ -n "$repo" ]]; then
remote_url="https://github.com/$repo.git"
fi
local current_sha=$(git ls-remote --heads "$remote_url" "$branch" 2>/dev/null | cut -f1)
if [[ -n "$current_sha" ]]; then
# Create a new file on the branch using GitHub API
gh api repos/"$api_repo"/contents/"$file_path" \
--method PUT \
--field message="$commit_message" \
--field content="$(echo -e "$file_content" | base64 -w 0)" \
--field branch="$branch" &>/dev/null
# Create a PR using the GitHub CLI
local pr_url=$(gh pr create $repo_flag --title "$title" --body "$body" --head "$branch" --base main 2>/dev/null)
if [[ -n "$pr_url" ]]; then
local pr_number=$(echo "$pr_url" | grep -o '[0-9]\+$')
echo "$pr_number"
else
echo ""
fi
else
echo ""
fi
}
create_test_pr_with_branch() {
local title="$1"
local body="$2"
local repo="${3:-}"
local branch="test-pr-$(date +%s)"
local repo_flag=""
local api_repo=":owner/:repo"
local remote_url="origin"
if [[ -n "$repo" ]]; then
repo_flag="--repo $repo"
api_repo="$repo"
remote_url="https://github.com/$repo.git"
fi
# Create a remote branch from main without changing local git state
git push "$remote_url" "main:$branch" &>/dev/null
# Create a commit on the remote branch using GitHub API to make it different from main
local commit_message="Test commit for PR"
local file_content="# Test PR Content\n\nThis is a test file created for PR testing at $(date)"
local file_path="test-file-$(date +%s).md"
# Get the initial SHA of the branch (before our test commit)
local initial_sha=$(git ls-remote --heads "$remote_url" "$branch" 2>/dev/null | cut -f1)
if [[ -n "$initial_sha" ]]; then
# Create a new file on the branch using GitHub API
gh api repos/"$api_repo"/contents/"$file_path" \
--method PUT \
--field message="$commit_message" \
--field content="$(echo -e "$file_content" | base64 -w 0)" \
--field branch="$branch" &>/dev/null
# Get the SHA after creating the test commit
local after_commit_sha=$(git ls-remote --heads "$remote_url" "$branch" 2>/dev/null | cut -f1)
# Create a PR using the GitHub CLI
local pr_url=$(gh pr create $repo_flag --title "$title" --body "$body" --head "$branch" --base main 2>/dev/null)
if [[ -n "$pr_url" ]]; then
local pr_number=$(echo "$pr_url" | grep -o '[0-9]\+$')
echo "$pr_number,$branch,$after_commit_sha,$repo"
else
echo ""
fi
else
echo ""
fi
}
post_issue_command() {
local issue_number="$1"
local command="$2"
local repo="${3:-}"
local repo_flag=""
if [[ -n "$repo" ]]; then
repo_flag="--repo $repo"
fi
gh issue comment $repo_flag "$issue_number" --body "$command" &>/dev/null
}
post_pr_command() {
local pr_number="$1"
local command="$2"
local repo="${3:-}"
local repo_flag=""
if [[ -n "$repo" ]]; then
repo_flag="--repo $repo"
fi
gh pr comment $repo_flag "$pr_number" --body "$command" &>/dev/null
}
validate_issue_created() {
local title_prefix="$1"
local expected_labels="$2"
local repo="${3:-}"