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 " $@ "
0 commit comments