-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathe2e.sh
More file actions
executable file
·3597 lines (3190 loc) · 138 KB
/
e2e.sh
File metadata and controls
executable file
·3597 lines (3190 loc) · 138 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)
# --use-samples Use declared samples for more deterministic testing
# --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)
# Parallel execution settings
BATCH_SIZE=10
NO_PARALLEL=false
# Lock file for synchronized result tracking across parallel processes
RESULTS_LOCK="/tmp/e2e-results-$$.lock"
# Global tracking of workflows that need to be disabled
# This is used by the trap handler to ensure cleanup on early exit
declare -a GLOBAL_WORKFLOWS_TO_DISABLE=()
GLOBAL_WORKFLOWS_LOCK="/tmp/e2e-workflows-$$.lock"
# 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
USE_SAMPLES=false
# Shared results file for parallel execution
RESULTS_FILE="/tmp/e2e-results-$$.txt"
# --gh-aw-ref: when non-empty, the script resets a parallel ../gh-aw checkout to
# this ref, builds it, and uses the resulting binary for compile+enable+disable+run.
# Compiled workflows will then reference github/gh-aw/actions/setup@<GH_AW_REF>
# instead of the published github/gh-aw-actions/setup@<version>.
GH_AW_REF=""
GH_AW_SRC_DIR="../gh-aw"
GH_AW_BIN="gh aw"
# CI mode: when the standard `$CI` environment variable is set to `true`
# (which GitHub Actions and most other CI providers do automatically), e2e.sh
# does NOT mutate repository secrets. Default to false if unset.
CI="${CI:-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() {
# Prevent double execution
if [[ "${CLEANUP_DONE:-false}" == "true" ]]; then
return 0
fi
CLEANUP_DONE=true
echo
info "Performing cleanup..."
# Load workflows from temp file (for parallel processes)
if [[ -f "/tmp/e2e-workflows-list-$$.txt" ]]; then
while IFS= read -r wf; do
[[ -n "$wf" ]] && GLOBAL_WORKFLOWS_TO_DISABLE+=("$wf")
done < "/tmp/e2e-workflows-list-$$.txt"
fi
# Disable any workflows that were enabled during testing
if [[ ${#GLOBAL_WORKFLOWS_TO_DISABLE[@]} -gt 0 ]]; then
# Remove duplicates
local -A seen
local unique_workflows=()
for workflow in "${GLOBAL_WORKFLOWS_TO_DISABLE[@]}"; do
if [[ -z "${seen[$workflow]:-}" ]]; then
seen[$workflow]=1
unique_workflows+=("$workflow")
fi
done
info "Disabling ${#unique_workflows[@]} workflow(s) in parallel that were enabled during testing..."
for workflow in "${unique_workflows[@]}"; do
(disable_workflow "$workflow" 2>/dev/null || warning "Failed to disable workflow '$workflow', continuing...") &
done
wait
fi
delete_temp_user_pat
# Clean up lock files
rm -f "$RESULTS_LOCK" "$GLOBAL_WORKFLOWS_LOCK" "$RESULTS_FILE" "/tmp/e2e-workflows-list-$$.txt" 2>/dev/null || true
}
# 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
}
# Get title prefix for validation based on workflow name and type
# PR tests use different prefixes to avoid conflicts in parallel execution
get_title_prefix() {
local workflow_name="$1"
local ai_type="$2"
# Determine the appropriate suffix based on workflow type
if [[ "$workflow_name" == *"create-two-pull-requests"* ]]; then
echo "[${ai_type}-test-two-prs] "
elif [[ "$workflow_name" == *"create-pull-request"* ]]; then
echo "[${ai_type}-test-single-pr] "
elif [[ "$workflow_name" == *"gh-steps"* ]]; then
echo "[${ai_type}-test-gh-steps] "
else
# Default for non-PR tests (issues, discussions, etc.)
echo "[${ai_type}-test] "
fi
}
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"
echo "test-copilot-update-discussion"
echo "test-copilot-assign-to-user"
echo "test-copilot-unassign-from-user"
echo "test-copilot-assign-milestone"
echo "test-copilot-link-sub-issue"
echo "test-copilot-hide-comment"
# 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"
echo "test-copilot-add-reviewer"
# 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"
echo "test-copilot-submit-pull-request-review"
# Workflow_dispatch tests with inputs (dispatch-workflow needs a sentinel)
echo "test-copilot-dispatch-workflow"
# Nosandbox tests - limited set for claude/codex, full matrix for copilot
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
}
# Reset a parallel gh-aw checkout to $GH_AW_REF, build it, and set GH_AW_BIN.
# Used when the user passes --gh-aw-ref <ref> to test compiled workflows against
# a specific gh-aw branch, tag, or SHA. The compiled lock.yml files will then
# pin github/gh-aw/actions/setup@<ref> at runtime instead of the published
# github/gh-aw-actions/setup@<version>.
setup_local_gh_aw_binary() {
info "Setting up local gh-aw build for ref '$GH_AW_REF'..."
if [[ ! -d "$GH_AW_SRC_DIR/.git" ]]; then
error "--gh-aw-ref requires a parallel gh-aw checkout at $GH_AW_SRC_DIR (not found)"
exit 1
fi
progress "Fetching latest refs from origin in $GH_AW_SRC_DIR..."
# --force + --prune-tags tolerates moved/deleted upstream tags
if ! git -C "$GH_AW_SRC_DIR" fetch origin --prune --prune-tags --tags --force &>> "$LOG_FILE"; then
error "Failed to fetch from origin in $GH_AW_SRC_DIR"
exit 1
fi
progress "Resetting $GH_AW_SRC_DIR to '$GH_AW_REF'..."
# Try branch (origin/<ref>) first, then fall back to tag/SHA.
if git -C "$GH_AW_SRC_DIR" rev-parse --verify "origin/$GH_AW_REF" &>/dev/null; then
if ! git -C "$GH_AW_SRC_DIR" checkout -B "$GH_AW_REF" "origin/$GH_AW_REF" &>> "$LOG_FILE"; then
error "Failed to checkout branch '$GH_AW_REF' in $GH_AW_SRC_DIR"
exit 1
fi
else
if ! git -C "$GH_AW_SRC_DIR" checkout --detach "$GH_AW_REF" &>> "$LOG_FILE"; then
error "Failed to checkout ref '$GH_AW_REF' in $GH_AW_SRC_DIR (not a branch, tag, or SHA)"
exit 1
fi
fi
progress "Building gh-aw binary at $GH_AW_SRC_DIR (make build)..."
if ! (cd "$GH_AW_SRC_DIR" && make build) &>> "$LOG_FILE"; then
error "Failed to build gh-aw binary in $GH_AW_SRC_DIR. Check $LOG_FILE for details"
exit 1
fi
local bin_path="$GH_AW_SRC_DIR/gh-aw"
if [[ ! -x "$bin_path" ]]; then
error "Built binary not found at $bin_path"
exit 1
fi
GH_AW_BIN="$bin_path"
local built_version
built_version=$($GH_AW_BIN --version 2>/dev/null || echo "unknown")
success "Built gh-aw binary: $bin_path ($built_version)"
# Verify the binary supports --gh-aw-ref.
if ! $GH_AW_BIN compile --help 2>&1 | grep -q -- "--gh-aw-ref"; then
error "The built gh-aw binary does not support --gh-aw-ref. The ref '$GH_AW_REF' is likely older than the flag's introduction."
exit 1
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
# Either install/upgrade the released gh-aw extension OR build the requested
# ref from a parallel ../gh-aw checkout.
if [[ -n "$GH_AW_REF" ]]; then
setup_local_gh_aw_binary
else
info "Checking gh-aw extension..."
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
fi
# Verify the chosen gh-aw binary is available
if ! $GH_AW_BIN --version &>> "$LOG_FILE"; then
error "gh-aw binary ($GH_AW_BIN) is not available"
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
# Compile workflows. When --gh-aw-ref is set, pass it through so compiled
# workflows reference github/gh-aw/actions/setup@<ref> at runtime.
local compile_cmd=($GH_AW_BIN compile)
if [[ -n "$GH_AW_REF" ]]; then
compile_cmd+=(--gh-aw-ref "$GH_AW_REF")
fi
if [[ "$USE_SAMPLES" == true ]]; then
compile_cmd+=(--use-samples)
fi
info "Running: ${compile_cmd[*]}"
if ! "${compile_cmd[@]}" 2>&1 | tee -a "$LOG_FILE"; then
error "'${compile_cmd[*]}' 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.
# NOTE: when running against --gh-aw-ref the lock.yml diff is expected; we still
# push so the test runs see the ref-specific workflows.
local git_status
git_status=$(git status --porcelain)
if [[ -n "$git_status" ]]; then
local commit_msg="chore: update compiled workflows via e2e.sh"
if [[ -n "$GH_AW_REF" ]]; then
commit_msg="chore: e2e.sh recompile against gh-aw ref ${GH_AW_REF}"
fi
info "Detected changes after compile; committing and pushing to main branch"
git add . &>> "$LOG_FILE"
git commit -m "$commit_msg" &>> "$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 compile"
fi
# PAT handling.
#
# Two modes (selected automatically via the standard `$CI` env var):
# * Local mode (CI unset or != "true"): the script uses its own
# `gh auth token` to set the repository's TEMP_USER_PAT secret so
# dispatched workflows can do cross-repo operations. We also delete
# the secret on exit.
# * CI mode (CI=true): the script MUST NOT mutate repo secrets (parallel
# matrix runs would clobber each other and we don't want CI write-scope
# tokens leaking through `gh secret set`). Instead, the PAT is supplied
# as the GH_AW_TEST_PAT environment variable, sourced from the actions
# secret of the same name. The repo's TEMP_USER_PAT secret is expected
# to be pre-configured (typically to the same value).
if [[ "$CI" == true ]]; then
if [[ -z "${GH_AW_TEST_PAT:-}" ]]; then
error "CI mode (CI=true) requires the GH_AW_TEST_PAT environment variable to be set (sourced from the actions secret of the same name). e2e.sh will not set repo secrets in this mode."
exit 1
fi
info "CI mode: skipping TEMP_USER_PAT secret management; using pre-configured repo secret + GH_AW_TEST_PAT env"
else
# 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
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 -a to_disable=()
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
to_disable+=("$workflow_name")
done <<< "$workflows_output"
local disabled_count=0
if [[ ${#to_disable[@]} -gt 0 ]]; then
local parallelism=$BATCH_SIZE
[[ $parallelism -lt 1 ]] && parallelism=1
info "Disabling ${#to_disable[@]} workflow(s) in parallel (up to $parallelism at a time)..."
local results_file
results_file=$(mktemp)
local running=0
for workflow_name in "${to_disable[@]}"; do
(
if gh workflow disable "$workflow_name" &>> "$LOG_FILE"; then
printf 'ok\t%s\n' "$workflow_name" >> "$results_file"
else
printf 'fail\t%s\n' "$workflow_name" >> "$results_file"
fi
) &
running=$((running + 1))
if [[ $running -ge $parallelism ]]; then
wait -n 2>/dev/null || wait
running=$((running - 1))
fi
done
wait
while IFS=$'\t' read -r status name; do
if [[ "$status" == "ok" ]]; then
success " ✓ Disabled '$name'"
disabled_count=$((disabled_count + 1))
else
warning "Failed to disable workflow '$name'"
fi
done < "$results_file"
rm -f "$results_file"
fi
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"
local track_globally="${2:-true}" # Default to tracking globally for cleanup
info "Enabling workflow '$workflow_name'..."
# Redirect gh aw enable output to log file to prevent terminal control codes from clearing previous output
$GH_AW_BIN enable "$workflow_name" &>> "$LOG_FILE"
local rc=$?
if [[ $rc -eq 0 ]]; then
success "Successfully enabled '$workflow_name'"
# Add to global tracking for cleanup on exit (unless disabled immediately after)
if [[ "$track_globally" == "true" ]]; then
(
flock -x 200
GLOBAL_WORKFLOWS_TO_DISABLE+=("$workflow_name")
# Also write to temp file for persistence across subprocesses
echo "$workflow_name" >> "/tmp/e2e-workflows-list-$$.txt"
) 200>"$GLOBAL_WORKFLOWS_LOCK" 2>/dev/null || true
fi
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_BIN disable "$workflow_name" &>> "$LOG_FILE"
local rc=$?
if [[ $rc -eq 0 ]]; then
success "Successfully disabled '$workflow_name'"
# Remove from global tracking (unless we're in cleanup mode)
if [[ "${CLEANUP_DONE:-false}" != "true" ]]; then
(
flock -x 200
# Remove from temp file
if [[ -f "/tmp/e2e-workflows-list-$$.txt" ]]; then
grep -v "^${workflow_name}$" "/tmp/e2e-workflows-list-$$.txt" > "/tmp/e2e-workflows-list-$$.txt.tmp" 2>/dev/null || true
mv "/tmp/e2e-workflows-list-$$.txt.tmp" "/tmp/e2e-workflows-list-$$.txt" 2>/dev/null || true
fi
) 200>"$GLOBAL_WORKFLOWS_LOCK" 2>/dev/null || true
fi
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_BIN 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