-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgh-wizard.sh
More file actions
391 lines (327 loc) · 10.9 KB
/
gh-wizard.sh
File metadata and controls
391 lines (327 loc) · 10.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
#!/bin/bash
# GitHub Issue Manager Wizard
# Interactive command-line interface for managing GitHub issues, releases, and project workflows
set -euo pipefail
# Script directory for relative path resolution
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# === DEPENDENCY VALIDATION ===
# Check for required dependencies
check_dependencies() {
local missing_deps=()
# Check for GitHub CLI
if ! command -v gh >/dev/null 2>&1; then
missing_deps+=("GitHub CLI (gh)")
fi
# Check for jq (used for JSON processing)
if ! command -v jq >/dev/null 2>&1; then
missing_deps+=("jq")
fi
# Check for git
if ! command -v git >/dev/null 2>&1; then
missing_deps+=("git")
fi
# Report missing dependencies
if [ ${#missing_deps[@]} -gt 0 ]; then
echo "Error: Missing required dependencies:" >&2
printf ' - %s\n' "${missing_deps[@]}" >&2
echo >&2
echo "Installation instructions:" >&2
echo " - GitHub CLI: https://cli.github.com/" >&2
echo " - jq: https://stedolan.github.io/jq/download/" >&2
echo " - git: https://git-scm.com/downloads" >&2
return 1
fi
return 0
}
# Validate module files exist
validate_modules() {
local missing_modules=()
local required_modules=(
"lib/wizard-config.sh"
"lib/wizard-display.sh"
"lib/wizard-core.sh"
"lib/wizard-github.sh"
)
for module in "${required_modules[@]}"; do
if [ ! -f "${SCRIPT_DIR}/${module}" ]; then
missing_modules+=("$module")
fi
done
if [ ${#missing_modules[@]} -gt 0 ]; then
echo "Error: Missing required module files:" >&2
printf ' - %s\n' "${missing_modules[@]}" >&2
echo >&2
echo "Please ensure all wizard modules are present in the lib/ directory." >&2
return 1
fi
return 0
}
# === MODULE LOADING ===
# Load modules with error handling
load_modules() {
local modules=(
"lib/wizard-config.sh"
"lib/wizard-display.sh"
"lib/wizard-core.sh"
"lib/wizard-github.sh"
)
for module in "${modules[@]}"; do
local module_path="${SCRIPT_DIR}/${module}"
if [ -f "$module_path" ]; then
# shellcheck source=/dev/null
if ! source "$module_path"; then
echo "Error: Failed to load module: $module" >&2
return 1
fi
else
echo "Error: Module not found: $module" >&2
return 1
fi
done
return 0
}
# === INITIALIZATION ===
# Initialize wizard environment
initialize_wizard() {
# Load configuration first
if ! load_config; then
echo "Error: Failed to load configuration" >&2
return 1
fi
# Setup logging system
if ! setup_logging; then
echo "Warning: Failed to setup logging system" >&2
# Continue without logging
fi
# Validate environment
if ! validate_environment; then
echo "Error: Environment validation failed" >&2
echo "Please resolve the above issues and try again." >&2
return 1
fi
# Validate wizard-specific configuration
validate_wizard_config
# Initialize cross-module state management
init_cross_module_state
# Initialize core module logging if available
if command -v init_core_logging >/dev/null 2>&1; then
init_core_logging
fi
return 0
}
# Initialize cross-module state management
init_cross_module_state() {
# Export shared variables for cross-module communication
export WIZARD_SESSION_ID="wizard_$(date +%s)"
export WIZARD_START_TIME="$(date '+%Y-%m-%d %H:%M:%S')"
# Initialize shared state variables
export CURRENT_WORKFLOW=""
export WORKFLOW_CONTEXT=""
export LAST_OPERATION=""
export OPERATION_RESULT=""
if [ "$ENABLE_LOGGING" = "true" ]; then
log_info "init_cross_module_state" "Cross-module state initialized (Session: $WIZARD_SESSION_ID)"
fi
}
# === COMMAND LINE ARGUMENT PARSING ===
# Show usage information
show_usage() {
echo "GitHub Issue Manager Wizard"
echo "Interactive command-line interface for managing GitHub issues, releases, and project workflows"
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -d, --debug Enable debug mode (verbose logging and output)"
echo " -v, --verbose Enable verbose output"
echo " -p, --performance Enable performance monitoring"
echo " --log-level LEVEL Set log level (DEBUG, INFO, WARN, ERROR)"
echo " --log-file PATH Set log file path"
echo " -h, --help Show this help message"
echo ""
echo "Environment Variables:"
echo " ENABLE_LOGGING Enable logging (true/false)"
echo " LOG_LEVEL Log level (DEBUG/INFO/WARN/ERROR)"
echo " LOG_FILE Log file path"
echo " DEBUG_MODE Enable debug mode (true/false)"
echo " VERBOSE_MODE Enable verbose mode (true/false)"
echo " PERFORMANCE_MONITORING Enable performance monitoring (true/false)"
echo " PROJECT_URL GitHub project URL for auto-assignment"
echo ""
echo "Examples:"
echo " $0 # Run wizard with default settings"
echo " $0 --debug # Run with debug mode enabled"
echo " $0 --verbose --performance # Run with verbose output and performance monitoring"
echo " $0 --log-level DEBUG --log-file ./debug.log # Custom logging configuration"
}
# Parse command line arguments
parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
-d|--debug)
enable_debug_mode
shift
;;
-v|--verbose)
enable_verbose_mode
shift
;;
-p|--performance)
enable_performance_monitoring
shift
;;
--log-level)
if [ -n "$2" ]; then
export LOG_LEVEL="$2"
shift 2
else
echo "Error: --log-level requires a value (DEBUG, INFO, WARN, ERROR)" >&2
exit 1
fi
;;
--log-file)
if [ -n "$2" ]; then
export LOG_FILE="$2"
shift 2
else
echo "Error: --log-file requires a path" >&2
exit 1
fi
;;
*)
echo "Error: Unknown option '$1'" >&2
echo "Use --help for usage information" >&2
exit 1
;;
esac
done
}
# === MAIN FUNCTION ===
# Main function
main() {
# Handle help option first (before dependency checks)
if [[ "${1:-}" == "--help" ]] || [[ "${1:-}" == "-h" ]]; then
show_usage
exit 0
fi
# Check dependencies first
if ! check_dependencies; then
exit 1
fi
# Validate and load modules
if ! validate_modules; then
exit 1
fi
if ! load_modules; then
exit 1
fi
# Parse command line arguments after loading modules (so debug functions are available)
parse_arguments "$@"
# Initialize wizard environment
if ! initialize_wizard; then
exit 1
fi
# Display debug configuration if debug mode is enabled
if [ "$DEBUG_MODE" = "true" ] || [ "$VERBOSE_MODE" = "true" ]; then
dump_debug_config
echo
fi
# Display welcome header
clear_screen
print_header "GitHub Issue Manager Wizard"
# Check GitHub CLI authentication
if ! check_gh_auth; then
print_error "GitHub CLI authentication required"
print_info "Please run 'gh auth login' to authenticate with GitHub"
echo
print_info "After authentication, run this wizard again"
exit 1
fi
# Get repository context for better user experience
if get_repo_context >/dev/null 2>&1; then
print_success "Repository detected: ${REPO_OWNER}/${REPO_NAME}"
# Log successful repository detection
if [ "$ENABLE_LOGGING" = "true" ]; then
log_info "main" "Repository context established: ${REPO_OWNER}/${REPO_NAME}"
fi
else
print_warning "Not in a GitHub repository" "Some features may be limited"
# Log repository detection failure
if [ "$ENABLE_LOGGING" = "true" ]; then
log_warn "main" "No repository context available - running in limited mode"
fi
fi
# Initialize wizard core
init_wizard_core
# Log wizard startup
if [ "$ENABLE_LOGGING" = "true" ]; then
log_info "main" "GitHub Wizard started successfully (Session: $WIZARD_SESSION_ID)"
fi
# Start main menu loop
show_main_menu
}
# === ERROR HANDLING ===
# Handle errors with user guidance
handle_error() {
local error_type="$1"
local error_message="$2"
case "$error_type" in
"dependency")
print_error "Missing Dependency" "$error_message"
echo "Please install the missing dependency and try again."
;;
"auth")
print_error "Authentication Error" "$error_message"
echo "Please run 'gh auth login' to authenticate with GitHub."
;;
"network")
print_error "Network Error" "$error_message"
echo "Please check your internet connection and try again."
;;
"permission")
print_error "Permission Error" "$error_message"
echo "Please check your GitHub permissions and try again."
;;
*)
print_error "Error" "$error_message"
;;
esac
if [ "$ENABLE_LOGGING" = "true" ]; then
log_error "handle_error" "$error_type: $error_message"
fi
}
# === CLEANUP ===
# Cleanup function
cleanup() {
print_info "Exiting GitHub Wizard..."
# Cleanup wizard core if it was initialized
if command -v cleanup_wizard_core >/dev/null 2>&1; then
cleanup_wizard_core
fi
if [ "$ENABLE_LOGGING" = "true" ]; then
log_info "main" "GitHub Wizard session ended"
fi
exit 0
}
# Cleanup on error
cleanup_on_error() {
local exit_code=$?
print_error "An unexpected error occurred (exit code: $exit_code)"
if [ "$ENABLE_LOGGING" = "true" ]; then
log_error "main" "Wizard terminated with error code: $exit_code"
fi
cleanup
}
# === SIGNAL HANDLERS ===
# Set up signal handlers
trap cleanup SIGINT SIGTERM
trap cleanup_on_error ERR
# === ENTRY POINT ===
# Run main function if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi