-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgh-issue-manager.sh
More file actions
executable file
·587 lines (500 loc) · 20.7 KB
/
gh-issue-manager.sh
File metadata and controls
executable file
·587 lines (500 loc) · 20.7 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
#!/bin/bash
set -euo pipefail
# --- MCP Validation Header ---
# [MCP:REQUIRED] shellcheck validation
# [MCP:REQUIRED] POSIX compliance check
# [MCP:RECOMMENDED] Error handling coverage
# === LOGGING SYSTEM ===
# Initialize logging system
log_init() {
# Set default values if not provided
export ENABLE_LOGGING=${ENABLE_LOGGING:-false}
export LOG_LEVEL=${LOG_LEVEL:-INFO}
export LOG_FILE=${LOG_FILE:-./logs/gh-issue-manager.log}
# Create log directory if logging is enabled
if [ "$ENABLE_LOGGING" = "true" ]; then
mkdir -p "$(dirname "$LOG_FILE")"
log_info "log_init" "Logging initialized - Level: $LOG_LEVEL, File: $LOG_FILE"
fi
}
# Initialize logging when script is sourced
log_init
# Main logging function
log_message() {
local level="$1"
local function_name="$2"
local message="$3"
local timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Check if logging is enabled
if [ "$ENABLE_LOGGING" != "true" ]; then
return 0
fi
# Check log level
case "$LOG_LEVEL" in
"DEBUG") allowed_levels="DEBUG INFO WARN ERROR" ;;
"INFO") allowed_levels="INFO WARN ERROR" ;;
"WARN") allowed_levels="WARN ERROR" ;;
"ERROR") allowed_levels="ERROR" ;;
*) allowed_levels="INFO WARN ERROR" ;;
esac
if [[ " $allowed_levels " =~ $level ]]; then
echo "[$timestamp] [$level] [$function_name] $message" >> "$LOG_FILE"
# Also output to console for ERROR and WARN
if [ "$level" = "ERROR" ] || [ "$level" = "WARN" ]; then
echo "[$timestamp] [$level] [$function_name] $message" >&2
fi
fi
}
# Logging wrapper functions
log_error() { log_message "ERROR" "$1" "$2"; }
log_warn() { log_message "WARN" "$1" "$2"; }
log_info() { log_message "INFO" "$1" "$2"; }
log_debug() { log_message "DEBUG" "$1" "$2"; }
# Performance timing function
log_timing() {
local function_name="$1"
local start_time="$2"
local end_time
end_time=$(date +%s.%N)
local duration
# Try to calculate duration with bc, fallback to awk if bc is not available
if command -v bc >/dev/null 2>&1; then
duration=$(echo "$end_time - $start_time" | bc -l 2>/dev/null || echo "N/A")
elif command -v awk >/dev/null 2>&1; then
duration=$(awk "BEGIN {printf \"%.3f\", $end_time - $start_time}" 2>/dev/null || echo "N/A")
else
duration="N/A"
fi
log_info "$function_name" "Execution time: ${duration}s"
}
# === ORIGINAL FUNCTIONS WITH LOGGING ===
validate_input() {
local start_time
start_time=$(date +%s.%N)
log_debug "validate_input" "Starting input validation with $# arguments"
for var in "$@"; do
log_debug "validate_input" "Checking argument: '${var:0:50}...'"
if [ -z "$var" ] || [[ "$var" =~ ^[[:space:]]*$ ]]; then
log_error "validate_input" "Argument is empty or contains only whitespace"
return 1
fi
done
log_info "validate_input" "Input validation successful"
log_timing "validate_input" "$start_time"
}
check_dependencies() {
local start_time
start_time=$(date +%s.%N)
log_debug "check_dependencies" "Checking required dependencies"
local missing_deps=()
if ! command -v gh >/dev/null 2>&1; then
missing_deps+=("gh (GitHub CLI)")
log_warn "check_dependencies" "GitHub CLI not found"
else
log_debug "check_dependencies" "GitHub CLI found: $(gh --version | head -1)"
fi
if ! command -v jq >/dev/null 2>&1; then
missing_deps+=("jq")
log_warn "check_dependencies" "jq not found"
else
log_debug "check_dependencies" "jq found: $(jq --version)"
fi
if [ ${#missing_deps[@]} -gt 0 ]; then
log_error "check_dependencies" "Missing dependencies: ${missing_deps[*]}"
echo "Error: Missing required dependencies:" >&2
printf ' - %s\n' "${missing_deps[@]}" >&2
return 1
fi
log_info "check_dependencies" "All dependencies satisfied"
log_timing "check_dependencies" "$start_time"
}
load_environment() {
local start_time
start_time=$(date +%s.%N)
log_debug "load_environment" "Loading environment configuration"
if [ -f ".env" ]; then
set -o allexport
# shellcheck disable=SC1091
source .env
set +o allexport
log_info "load_environment" "Loaded configuration from .env file"
echo "Loaded configuration from .env file"
else
log_warn "load_environment" "No .env file found"
echo "No .env file found, using current repo context"
fi
log_timing "load_environment" "$start_time"
}
get_repo_context() {
local start_time
start_time=$(date +%s.%N)
log_debug "get_repo_context" "Getting repository context"
if ! REPO_OWNER=$(gh repo view --json owner -q .owner.login 2>/dev/null); then
log_error "get_repo_context" "Failed to get repository owner"
echo "Error: Failed to get repository owner. Are you in a git repository with GitHub remote?" >&2
return 1
fi
if [ -z "$REPO_OWNER" ]; then
log_error "get_repo_context" "Repository owner is empty"
echo "Error: Could not determine repository owner. The 'gh' command returned no output." >&2
return 1
fi
if ! REPO_NAME=$(gh repo view --json name -q .name 2>/dev/null); then
log_error "get_repo_context" "Failed to get repository name"
echo "Error: Failed to get repository name. Are you in a git repository with GitHub remote?" >&2
return 1
fi
if [ -z "$REPO_NAME" ]; then
log_error "get_repo_context" "Repository name is empty"
echo "Error: Could not determine repository name. The 'gh' command returned no output." >&2
return 1
fi
log_info "get_repo_context" "Repository context: $REPO_OWNER/$REPO_NAME"
echo "Repository context: $REPO_OWNER/$REPO_NAME"
log_timing "get_repo_context" "$start_time"
}
create_issues() {
local start_time
start_time=$(date +%s.%N)
local parent_title="$1"
local parent_body="$2"
local child_title="$3"
local child_body="$4"
log_info "create_issues" "Creating parent issue: '$parent_title'"
log_debug "create_issues" "Parent body length: ${#parent_body} characters"
local parent_output
if ! parent_output=$(gh issue create --title "$parent_title" --body "$parent_body" 2>/dev/null); then
log_error "create_issues" "Failed to create parent issue"
echo "Error: Failed to create parent issue" >&2
return 1
fi
if [ -z "$parent_output" ]; then
log_error "create_issues" "Parent issue creation returned no output"
echo "Error: Failed to get parent issue details from creation command." >&2
return 1
fi
PARENT_ISSUE=$(echo "$parent_output" | awk -F'/' '{print $NF}')
PARENT_ID="$(gh api graphql -f query='query{repository(owner:"'"$REPO_OWNER"'",name:"'"$REPO_NAME"'"){issue(number:'"$PARENT_ISSUE"'){id}}}' -q .data.repository.issue.id)"
if [ -z "$PARENT_ISSUE" ] || [ "$PARENT_ISSUE" = "null" ]; then
log_error "create_issues" "Failed to parse parent issue number"
echo "Error: Could not parse parent issue number from output." >&2
return 1
fi
log_info "create_issues" "Parent issue created: #$PARENT_ISSUE"
log_info "create_issues" "Creating child issue: '$child_title'"
log_debug "create_issues" "Child body length: ${#child_body} characters"
local child_output
if ! child_output=$(gh issue create --title "$child_title" --body "$child_body" 2>/dev/null); then
log_error "create_issues" "Failed to create child issue"
echo "Error: Failed to create child issue" >&2
return 1
fi
if [ -z "$child_output" ]; then
log_error "create_issues" "Child issue creation returned no output"
echo "Error: Failed to get child issue details from creation command." >&2
return 1
fi
CHILD_ISSUE=$(echo "$child_output" | awk -F'/' '{print $NF}')
CHILD_ID="$(gh api graphql -f query='query{repository(owner:"'"$REPO_OWNER"'",name:"'"$REPO_NAME"'"){issue(number:'"$CHILD_ISSUE"'){id}}}' -q .data.repository.issue.id)"
if [ -z "$CHILD_ISSUE" ] || [ "$CHILD_ISSUE" = "null" ]; then
log_error "create_issues" "Failed to parse child issue number"
echo "Error: Could not parse child issue number from output." >&2
return 1
fi
log_info "create_issues" "Child issue created: #$CHILD_ISSUE"
echo "Created issues: Parent #$PARENT_ISSUE, Child #$CHILD_ISSUE"
log_timing "create_issues" "$start_time"
}
link_sub_issue() {
local start_time
start_time=$(date +%s.%N)
log_info "link_sub_issue" "Linking child issue #$CHILD_ISSUE to parent #$PARENT_ISSUE"
log_debug "link_sub_issue" "Parent ID: $PARENT_ID, Child ID: $CHILD_ID"
# Link child to parent using GraphQL
if ! gh api graphql \
-H "GraphQL-Features: sub_issues" \
-f query="
mutation {
addSubIssue(input: {issueId: \"$PARENT_ID\", subIssueId: \"$CHILD_ID\"}) {
clientMutationId
}
}" >/dev/null 2>&1; then
log_warn "link_sub_issue" "Failed to create sub-issue relationship"
echo "Warning: Failed to create sub-issue relationship. Feature may not be available."
else
log_info "link_sub_issue" "Successfully linked child #$CHILD_ISSUE to parent #$PARENT_ISSUE"
echo "Linked child issue #$CHILD_ISSUE to parent #$PARENT_ISSUE"
fi
log_timing "link_sub_issue" "$start_time"
}
add_to_project() {
local start_time
start_time=$(date +%s.%N)
log_debug "add_to_project" "PROJECT_URL: ${PROJECT_URL:-'not set'}"
if [ -n "${PROJECT_URL:-}" ]; then
echo "Adding issues to project: $PROJECT_URL"
log_info "add_to_project" "Adding issues to project: $PROJECT_URL"
# Extract project number from URL
PROJECT_NUMBER=$(echo "$PROJECT_URL" | sed 's/.*\/projects\///')
log_debug "add_to_project" "Project number: $PROJECT_NUMBER"
# Add parent issue to project using URL format
PARENT_ISSUE_URL="https://github.com/$REPO_OWNER/$REPO_NAME/issues/$PARENT_ISSUE"
log_debug "add_to_project" "Adding parent issue: $PARENT_ISSUE_URL"
if ! gh project item-add "$PROJECT_NUMBER" --owner "$REPO_OWNER" --url "$PARENT_ISSUE_URL" 2>/dev/null; then
log_warn "add_to_project" "Failed to add parent issue #$PARENT_ISSUE to project"
echo "Warning: Failed to add parent issue to project board"
else
log_info "add_to_project" "Added parent issue #$PARENT_ISSUE to project"
echo "✅ Added parent issue #$PARENT_ISSUE to project"
fi
# Add child issue to project using URL format
CHILD_ISSUE_URL="https://github.com/$REPO_OWNER/$REPO_NAME/issues/$CHILD_ISSUE"
log_debug "add_to_project" "Adding child issue: $CHILD_ISSUE_URL"
if ! gh project item-add "$PROJECT_NUMBER" --owner "$REPO_OWNER" --url "$CHILD_ISSUE_URL" 2>/dev/null; then
log_warn "add_to_project" "Failed to add child issue #$CHILD_ISSUE to project"
echo "Warning: Failed to add child issue to project board"
else
log_info "add_to_project" "Added child issue #$CHILD_ISSUE to project"
echo "✅ Added child issue #$CHILD_ISSUE to project"
fi
else
log_info "add_to_project" "No PROJECT_URL configured, skipping project board assignment"
echo "No PROJECT_URL configured, skipping project board assignment"
fi
log_timing "add_to_project" "$start_time"
}
# Function to update a GitHub issue
update_issue() {
local start_time
start_time=$(date +%s.%N)
log_debug "update_issue" "Updating issue #$1..."
local issue_number="$1"
local update_args=()
shift # Remove issue_number from arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--title)
update_args+=(--title "$2")
log_debug "update_issue" "Setting title to: $2"
shift 2
;;
--body)
update_args+=(--body "$2")
log_debug "update_issue" "Setting body to: $2"
shift 2
;;
--state)
update_args+=(--state "$2")
log_debug "update_issue" "Setting state to: $2"
shift 2
;;
--add-label)
update_args+=(--add-label "$2")
log_debug "update_issue" "Adding label: $2"
shift 2
;;
--remove-label)
update_args+=(--remove-label "$2")
log_debug "update_issue" "Removing label: $2"
shift 2
;;
--milestone)
update_args+=(--milestone "$2")
log_debug "update_issue" "Setting milestone to: $2"
shift 2
;;
*)
log_warn "update_issue" "Unknown or unsupported argument for update_issue: $1"
shift
;;
esac
done
if [ ${#update_args[@]} -eq 0 ]; then
log_warn "update_issue" "No update arguments provided for issue #$issue_number."
log_timing "update_issue" "$start_time"
return 0
fi
log_info "update_issue" "Executing: gh issue edit $issue_number ${update_args[*]}"
if ! gh issue edit "$issue_number" "${update_args[@]}"; then
log_error "update_issue" "Failed to update issue #$issue_number."
echo "Error: Failed to update issue #$issue_number." >&2
log_timing "update_issue" "$start_time"
return 1
fi
log_info "update_issue" "Successfully updated issue #$issue_number."
echo "✅ Successfully updated issue #$issue_number."
log_timing "update_issue" "$start_time"
}
# Function to process "Files to Create" in an issue body
process_files_to_create_in_issue() {
local start_time
start_time=$(date +%s.%N)
log_debug "process_files_to_create_in_issue" "Processing files to create for issue #$1..."
local issue_number="$1"
local issue_body
issue_body=$(gh issue view "$issue_number" --json body --jq .body)
if [ -z "$issue_body" ]; then
log_warn "process_files_to_create_in_issue" "Issue #$issue_number has no body or failed to fetch."
log_timing "process_files_to_create_in_issue" "$start_time"
return 0
fi
local files_to_create_section
files_to_create_section=$(echo "$issue_body" | sed -n '/^Files to Create:/,/^$/p')
if [ -z "$files_to_create_section" ]; then
log_info "process_files_to_create_in_issue" "No 'Files to Create:' section found in issue #$issue_number body."
log_timing "process_files_to_create_in_issue" "$start_time"
return 0
fi
local new_body="$issue_body"
local files_modified=false
# Extract filenames, skipping the header line
echo "$files_to_create_section" | tail -n +2 | while IFS= read -r line;
do
local filename
filename=$(echo "$line" | sed -e 's/^[[:space:]]*- //' -e 's/^[[:space:]]*//')
if [ -z "$filename" ]; then
continue
fi
if [ -f "$filename" ]; then
local timestamp
timestamp=$(date +%Y%m%d_%H%M%S)
local dirname
dirname=$(dirname "$filename")
local basename
basename=$(basename "$filename")
local name_part="${basename%.*}"
local extension_part="${basename##*.}"
local new_filename
if [ "$name_part" != "$basename" ]; then # Has an extension
new_filename="${dirname}/${name_part}_${timestamp}.${extension_part}"
else
new_filename="${dirname}/${basename}_${timestamp}"
fi
log_warn "process_files_to_create_in_issue" "File '$filename' already exists. Proposing new name: '$new_filename'."
new_body=${new_body//"$filename"/"$new_filename"}
files_modified=true
fi
done
if [ "$files_modified" = true ]; then
log_info "process_files_to_create_in_issue" "Updating issue #$issue_number body with new file names."
if ! update_issue "$issue_number" --body "$new_body"; then
log_error "process_files_to_create_in_issue" "Failed to update issue body for #$issue_number."
return 1
fi
else
log_info "process_files_to_create_in_issue" "No existing files found for issue #$issue_number. No update needed."
fi
log_timing "process_files_to_create_in_issue" "$start_time"
}
main() {
local start_time
start_time=$(date +%s.%N)
log_init
log_info "main" "GitHub Issue Manager started"
local MODE="CREATE"
local issue_number=""
local update_args=()
local PROCESS_FILES_MODE=false
if [[ "$1" == "--update" ]]; then
MODE="UPDATE"
shift
if [ -z "$1" ]; then
log_error "main" "Missing issue number for --update."
echo "Error: Missing issue number for --update." >&2
show_usage
exit 1
fi
issue_number="$1"
shift
update_args=("$@")
elif [[ "$1" == "--process-files" ]]; then
PROCESS_FILES_MODE=true
shift
if [ -z "$1" ]; then
log_error "main" "Missing issue number for --process-files."
echo "Error: Missing issue number for --process-files." >&2
show_usage
exit 1
fi
issue_number="$1"
shift
fi
if ! check_dependencies; then exit 1; fi
load_environment
if ! get_repo_context; then exit 1; fi
if [ "$MODE" == "CREATE" ]; then
if [ $# -ne 4 ]; then
log_error "main" "Invalid argument count for create mode: expected 4, got $#"
echo "Error: Invalid argument count for create mode." >&2
show_usage
exit 1
fi
local parent_title="$1"
local parent_body="$2"
local child_title="$3"
local child_body="$4"
if ! validate_input "$parent_title" "$parent_body" "$child_title" "$child_body"; then exit 1; fi
if ! create_issues "$parent_title" "$parent_body" "$child_title" "$child_body"; then
exit 1
fi
link_sub_issue
add_to_project
log_info "main" "Successfully completed: Parent #$PARENT_ISSUE, Child #$CHILD_ISSUE"
echo "✅ Successfully created parent issue #$PARENT_ISSUE and child issue #$CHILD_ISSUE"
elif [ "$MODE" == "UPDATE" ]; then
if ! validate_input "$issue_number" "${update_args[@]}"; then exit 1; fi
if ! update_issue "$issue_number" "${update_args[@]}"; then
exit 1
fi
elif [ "$PROCESS_FILES_MODE" = true ]; then
if ! validate_input "$issue_number"; then exit 1; fi
if ! process_files_to_create_in_issue "$issue_number"; then
exit 1
fi
else
log_error "main" "Invalid mode or arguments provided."
show_usage
exit 1
fi
log_timing "main" "$start_time"
}
# Show usage information
show_usage() {
echo "Usage: $0 [OPTIONS] PARENT_TITLE PARENT_BODY CHILD_TITLE CHILD_BODY"
echo ""
echo "Create GitHub issues with parent-child relationships"
echo ""
echo "Options:"
echo " --update ISSUE_NUMBER [OPTIONS] Update an existing issue"
echo " --process-files ISSUE_NUMBER Process 'Files to Create' section"
echo " --help Show this help message"
echo ""
echo "Update options:"
echo " --title TITLE Update issue title"
echo " --body BODY Update issue body"
echo " --state STATE Update issue state (open/closed)"
echo " --add-label LABEL Add label to issue"
echo " --remove-label LABEL Remove label from issue"
echo " --milestone MILESTONE Set milestone"
echo ""
echo "Examples:"
echo " $0 \"Parent Issue\" \"Description\" \"Child Issue\" \"Child description\""
echo " $0 --update 123 --title \"New Title\" --add-label \"bug\""
echo " $0 --process-files 456"
echo ""
echo "Environment variables:"
echo " PROJECT_URL GitHub project URL for auto-assignment"
echo " ENABLE_LOGGING Enable logging (true/false)"
echo " LOG_LEVEL Log level (DEBUG/INFO/WARN/ERROR)"
echo " LOG_FILE Log file path"
}
# Only run main if script is executed directly (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
# Handle help option
if [[ "${1:-}" == "--help" ]] || [[ "${1:-}" == "-h" ]]; then
show_usage
exit 0
fi
main "$@"
fi