Skip to content

Commit 46bb965

Browse files
Copilotpelikhan
andcommitted
Refactor cleanup to separate clean.sh script and add clean job to e2e.yml
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent 0d7f49b commit 46bb965

3 files changed

Lines changed: 147 additions & 65 deletions

File tree

.github/workflows/e2e.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,33 @@ permissions:
1515
actions: write
1616

1717
jobs:
18+
clean:
19+
runs-on: ubuntu-latest
20+
name: "Clean test resources"
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
- name: GitHub CLI - show auth status
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
run: |
28+
gh auth status
29+
- name: Run cleanup script
30+
env:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
32+
run: |
33+
chmod +x ./clean.sh
34+
./clean.sh
35+
- name: Upload cleanup log artifact
36+
if: always()
37+
uses: actions/upload-artifact@v4
38+
with:
39+
name: cleanup-log
40+
path: cleanup-*.log
41+
1842
e2e:
1943
runs-on: ubuntu-latest
44+
needs: clean
2045
strategy:
2146
matrix:
2247
test-suite:

clean.sh

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/bin/bash
2+
3+
# Cleanup script for GitHub Agentic Workflows test resources
4+
# This script cleans up test resources (issues, PRs, branches) created during e2e testing
5+
#
6+
# Usage: ./clean.sh
7+
#
8+
# This script will:
9+
# 1. Close all open issues with cleanup comment
10+
# 2. Close all open pull requests with cleanup comment
11+
# 3. Delete test branches matching specific patterns
12+
# 4. Provide detailed logging of all cleanup operations
13+
14+
set -uo pipefail
15+
16+
# Colors for output
17+
RED='\033[0;31m'
18+
GREEN='\033[0;32m'
19+
YELLOW='\033[1;33m'
20+
BLUE='\033[0;34m'
21+
PURPLE='\033[0;35m'
22+
CYAN='\033[0;36m'
23+
NC='\033[0m' # No Color
24+
25+
# Configuration
26+
LOG_FILE="cleanup-$(date +%Y%m%d-%H%M%S).log"
27+
28+
# Utility functions
29+
log() {
30+
echo "$(date '+%Y-%m-%d %H:%M:%S') $*" | tee -a "$LOG_FILE"
31+
}
32+
33+
info() {
34+
echo -e "${BLUE}ℹ️ $*${NC}" | tee -a "$LOG_FILE"
35+
}
36+
37+
success() {
38+
echo -e "${GREEN}$*${NC}" | tee -a "$LOG_FILE"
39+
}
40+
41+
warning() {
42+
echo -e "${YELLOW}⚠️ $*${NC}" | tee -a "$LOG_FILE"
43+
}
44+
45+
error() {
46+
echo -e "${RED}$*${NC}" | tee -a "$LOG_FILE"
47+
}
48+
49+
cleanup_test_resources() {
50+
info "Cleaning up test resources..."
51+
local issues_closed=0
52+
local prs_closed=0
53+
local branches_deleted=0
54+
55+
# Close all issues
56+
info "Checking for open issues to close..."
57+
while read -r issue_num; do
58+
if [[ -n "$issue_num" ]]; then
59+
if gh issue close "$issue_num" --comment "Closed by e2e test cleanup" &>/dev/null; then
60+
info "Closed issue #$issue_num"
61+
((issues_closed++))
62+
else
63+
warning "Failed to close issue #$issue_num"
64+
fi
65+
fi
66+
done < <(gh issue list --limit 20 --json number --jq '.[].number' 2>/dev/null || true)
67+
68+
# Close all PRs
69+
info "Checking for open pull requests to close..."
70+
while read -r pr_num; do
71+
if [[ -n "$pr_num" ]]; then
72+
if gh pr close "$pr_num" --comment "Closed by e2e test cleanup" &>/dev/null; then
73+
info "Closed pull request #$pr_num"
74+
((prs_closed++))
75+
else
76+
warning "Failed to close pull request #$pr_num"
77+
fi
78+
fi
79+
done < <(gh pr list --limit 20 --json number --jq '.[].number' 2>/dev/null || true)
80+
81+
# Delete test branches
82+
info "Checking for test branches to delete..."
83+
while read -r branch; do
84+
if [[ -n "$branch" ]]; then
85+
if git push origin --delete "$branch" &>/dev/null; then
86+
info "Deleted branch: $branch"
87+
((branches_deleted++))
88+
else
89+
warning "Failed to delete branch: $branch"
90+
fi
91+
fi
92+
done < <(git branch -r 2>/dev/null | grep 'origin/test-pr-\|origin/claude-test-branch\|origin/codex-test-branch' | sed 's/origin\///' || true)
93+
94+
success "Cleanup completed: $issues_closed issues closed, $prs_closed PRs closed, $branches_deleted branches deleted"
95+
}
96+
97+
main() {
98+
echo -e "${CYAN}🧹 GitHub Agentic Workflows Test Resource Cleanup${NC}"
99+
echo -e "${CYAN}==================================================${NC}"
100+
echo
101+
102+
log "Starting cleanup at $(date)"
103+
104+
# Check if gh CLI is available and authenticated
105+
if ! command -v gh &> /dev/null; then
106+
error "GitHub CLI (gh) is not installed"
107+
exit 1
108+
fi
109+
110+
if ! gh auth status &> /dev/null; then
111+
error "GitHub CLI is not authenticated. Run 'gh auth login'"
112+
exit 1
113+
fi
114+
115+
cleanup_test_resources
116+
117+
echo
118+
echo -e "${CYAN}📄 Log file: $LOG_FILE${NC}"
119+
}
120+
121+
# Run main function
122+
main "$@"

e2e.sh

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
#
2323
# Options:
2424
# --dry-run Show what would be tested without running
25-
# --clean Enable cleanup of test resources (default: false)
2625
# --help, -h Show help message
2726
#
2827
# Examples:
2928
# ./e2e.sh # Run all tests
3029
# ./e2e.sh --dry-run # See what would be tested
31-
# ./e2e.sh --clean # Run tests and clean up resources
3230
#
3331
# Prerequisites:
3432
# - GitHub CLI (gh) installed and authenticated
@@ -915,54 +913,6 @@ wait_for_pr_reviews() {
915913
return 1
916914
}
917915

918-
cleanup_test_resources() {
919-
info "Cleaning up test resources..."
920-
local issues_closed=0
921-
local prs_closed=0
922-
local branches_deleted=0
923-
924-
# Close all issues
925-
info "Checking for open issues to close..."
926-
while read -r issue_num; do
927-
if [[ -n "$issue_num" ]]; then
928-
if gh issue close "$issue_num" --comment "Closed by e2e test cleanup" &>/dev/null; then
929-
info "Closed issue #$issue_num"
930-
((issues_closed++))
931-
else
932-
warning "Failed to close issue #$issue_num"
933-
fi
934-
fi
935-
done < <(gh issue list --limit 20 --json number --jq '.[].number' 2>/dev/null || true)
936-
937-
# Close all PRs
938-
info "Checking for open pull requests to close..."
939-
while read -r pr_num; do
940-
if [[ -n "$pr_num" ]]; then
941-
if gh pr close "$pr_num" --comment "Closed by e2e test cleanup" &>/dev/null; then
942-
info "Closed pull request #$pr_num"
943-
((prs_closed++))
944-
else
945-
warning "Failed to close pull request #$pr_num"
946-
fi
947-
fi
948-
done < <(gh pr list --limit 20 --json number --jq '.[].number' 2>/dev/null || true)
949-
950-
# Delete test branches
951-
info "Checking for test branches to delete..."
952-
while read -r branch; do
953-
if [[ -n "$branch" ]]; then
954-
if git push origin --delete "$branch" &>/dev/null; then
955-
info "Deleted branch: $branch"
956-
((branches_deleted++))
957-
else
958-
warning "Failed to delete branch: $branch"
959-
fi
960-
fi
961-
done < <(git branch -r 2>/dev/null | grep 'origin/test-pr-\|origin/claude-test-branch\|origin/codex-test-branch' | sed 's/origin\///' || true)
962-
963-
success "Cleanup completed: $issues_closed issues closed, $prs_closed PRs closed, $branches_deleted branches deleted"
964-
}
965-
966916
run_workflow_dispatch_tests() {
967917
local patterns=("$@")
968918
info "🚀 Running workflow_dispatch tests..."
@@ -1283,7 +1233,6 @@ main() {
12831233
local run_issue_triggered=true
12841234
local run_command_triggered=true
12851235
local dry_run=false
1286-
local clean_resources=false
12871236
local specific_tests=()
12881237

12891238
while [[ $# -gt 0 ]]; do
@@ -1292,10 +1241,6 @@ main() {
12921241
dry_run=true
12931242
shift
12941243
;;
1295-
--clean)
1296-
clean_resources=true
1297-
shift
1298-
;;
12991244
--workflow-dispatch-only)
13001245
run_workflow_dispatch=true
13011246
run_issue_triggered=false
@@ -1319,7 +1264,6 @@ main() {
13191264
echo ""
13201265
echo "Options:"
13211266
echo " --dry-run, -n Show what would be tested without running"
1322-
echo " --clean Enable cleanup of test resources (default: false)"
13231267
echo " --workflow-dispatch-only Run only workflow dispatch tests"
13241268
echo " --issue-triggered-only Run only issue-triggered tests"
13251269
echo " --command-triggered-only Run only command-triggered tests"
@@ -1404,15 +1348,6 @@ main() {
14041348
log "Starting e2e tests at $(date)"
14051349

14061350
check_prerequisites
1407-
1408-
if [[ "$dry_run" == true ]]; then
1409-
info "Dry run mode - skipping cleanup of test resources"
1410-
elif [[ "$clean_resources" == true ]]; then
1411-
info "Cleanup enabled - running cleanup of test resources"
1412-
cleanup_test_resources
1413-
else
1414-
info "Cleanup disabled - skipping cleanup of test resources (use --clean to enable)"
1415-
fi
14161351

14171352
# If specific tests are provided, determine which test suites need to run
14181353
if [[ ${#specific_tests[@]} -gt 0 ]]; then

0 commit comments

Comments
 (0)