-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_repo_manager.sh
executable file
·753 lines (656 loc) · 27.7 KB
/
github_repo_manager.sh
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
#!/bin/bash
# GitHub Repository Manager
# A tool to manage GitHub repositories, including visibility and archive status
#
# This script provides a menu-driven interface to manage GitHub repositories,
# allowing users to toggle visibility, archive status, and perform other
# repository management tasks.
#
# Features:
# - List all repositories
# - Toggle visibility for selected repositories
# - Toggle archive status for selected repositories
# - Save and load repository status
# - Search repositories
# - Show detailed repository information
#
# Requirements:
# - GitHub CLI (gh) must be installed and authenticated
# - jq must be installed for JSON parsing
# - dialog must be installed for the interactive menu interface
#
# Usage: ./github_repo_manager.sh
# Set up logging
LOG_FILE="github_repo_manager.log"
log() {
echo "$(date): $1" >> "$LOG_FILE"
}
log "Script started"
# Dialog color settings
export DIALOGRC="/tmp/dialogrc"
cat > "$DIALOGRC" << EOF
use_shadow = OFF
use_colors = ON
screen_color = (BLACK,BLACK,OFF)
dialog_color = (CYAN,BLACK,OFF)
title_color = (YELLOW,BLACK,OFF)
border_color = (CYAN,BLACK,OFF)
button_active_color = (BLACK,CYAN,OFF)
button_inactive_color = (CYAN,BLACK,OFF)
button_key_active_color = (BLACK,CYAN,OFF)
button_key_inactive_color = (CYAN,BLACK,OFF)
button_label_active_color = (BLACK,CYAN,OFF)
button_label_inactive_color = (CYAN,BLACK,OFF)
inputbox_color = (CYAN,BLACK,OFF)
inputbox_border_color = (CYAN,BLACK,OFF)
searchbox_color = (CYAN,BLACK,OFF)
searchbox_title_color = (YELLOW,BLACK,OFF)
searchbox_border_color = (CYAN,BLACK,OFF)
position_indicator_color = (YELLOW,BLACK,OFF)
menubox_color = (CYAN,BLACK,OFF)
menubox_border_color = (CYAN,BLACK,OFF)
item_color = (CYAN,BLACK,OFF)
item_selected_color = (BLACK,CYAN,OFF)
tag_color = (YELLOW,BLACK,OFF)
tag_selected_color = (BLACK,YELLOW,OFF)
tag_key_color = (YELLOW,BLACK,OFF)
tag_key_selected_color = (BLACK,YELLOW,OFF)
check_color = (CYAN,BLACK,OFF)
check_selected_color = (BLACK,CYAN,OFF)
uarrow_color = (CYAN,BLACK,OFF)
darrow_color = (CYAN,BLACK,OFF)
EOF
# Colors for terminal output
GREEN='\033[0;32m'
BRIGHT_GREEN='\033[1;32m'
DARK_GREEN='\033[2;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
GRAY='\033[0;90m'
NC='\033[0m' # No Color
# Function to check if a command is available
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check if required commands are installed
check_required_commands() {
local missing_commands=()
if ! command_exists gh; then
missing_commands+=("GitHub CLI (gh)")
fi
if ! command_exists dialog; then
missing_commands+=("dialog")
fi
if ! command_exists jq; then
missing_commands+=("jq")
fi
if [ ${#missing_commands[@]} -ne 0 ]; then
echo "Error: The following required commands are not installed:"
for cmd in "${missing_commands[@]}"; do
echo "- $cmd"
done
echo "Please install the missing commands and try again."
echo "Installation instructions:"
echo "- GitHub CLI: https://cli.github.com/"
echo "- dialog: Use your system's package manager (e.g., apt-get install dialog)"
echo "- jq: Use your system's package manager (e.g., apt-get install jq)"
exit 1
fi
}
# Run the check for required commands
check_required_commands
# Function to toggle repo visibility
toggle_repo_visibility() {
local repo="$1"
local current_visibility
local new_visibility
local output
log "Checking current visibility of $repo"
# Get current visibility using GitHub CLI
if ! current_visibility=$(gh repo view "$repo" --json visibility --jq '.visibility' 2>&1 | tr '[:upper:]' '[:lower:]'); then
log "Failed to get visibility status for $repo. Error: $current_visibility"
dialog --title "Error" --msgbox "Failed to get visibility status for $repo.\nError: $current_visibility" $(calculate_dialog_size)
return 1
fi
log "Current visibility of $repo: $current_visibility"
# Determine new visibility (toggle between public and private)
new_visibility=$([ "$current_visibility" = "public" ] && echo "private" || echo "public")
# Attempt to change visibility using GitHub CLI
log "Attempting to change visibility of $repo from $current_visibility to $new_visibility"
if output=$(gh repo edit "$repo" --visibility "$new_visibility" 2>&1); then
log "Successfully changed $repo from $current_visibility to $new_visibility"
dialog --title "Success" --msgbox "Changed $repo from $current_visibility to $new_visibility" $(calculate_dialog_size)
return 0
else
log "Failed to change $repo from $current_visibility to $new_visibility. Error: $output"
# Handle specific error cases
case "$output" in
*"API rate limit exceeded"*)
log "Error: GitHub API rate limit exceeded"
dialog --title "Error" --msgbox "GitHub API rate limit exceeded. Please try again later." $(calculate_dialog_size)
;;
*"Could not resolve to a Repository"*)
log "Error: Repository $repo not found or no permission to modify"
dialog --title "Error" --msgbox "Repository $repo not found or you don't have permission to modify it." $(calculate_dialog_size)
;;
*"is archived and cannot be edited"*)
log "Error: Repository $repo is archived and cannot be edited"
dialog --title "Error" --msgbox "Repository $repo is archived and cannot be edited.\nPlease unarchive the repository first." $(calculate_dialog_size)
;;
*)
log "Unhandled error occurred: $output"
dialog --title "Error" --msgbox "Failed to change $repo visibility.\nError: $output" $(calculate_dialog_size)
;;
esac
return 1
fi
}
# Function to toggle repo archive status
toggle_repo_archive_status() {
local repo="$1"
local current_status
local new_status
local archive_action
local output
log "Starting toggle_repo_archive_status for $repo"
log "Checking current archive status of $repo"
if ! current_status=$(gh repo view "$repo" --json isArchived --jq '.isArchived' 2>&1); then
log "Failed to get archive status for $repo. Error: $current_status"
return 1
fi
log "Current archive status of $repo: $current_status"
# Determine new status and action
if [ "$current_status" = "true" ]; then
new_status="unarchived"
archive_action="unarchive"
else
new_status="archived"
archive_action="archive"
fi
log "New status will be: $new_status (archive_action: $archive_action)"
log "Attempting to change archive status of $repo from $current_status to $new_status"
log "Executing command: gh repo $archive_action \"$repo\" --yes"
if output=$(gh repo $archive_action "$repo" --yes 2>&1); then
log "Successfully changed $repo to $new_status"
log "Repository status change successful. Refreshing cache..."
CACHED_REPOS=""
get_all_repositories >/dev/null
log "Cache refreshed"
return 0
else
log "Failed to change $repo to $new_status. Error: $output"
return 1
fi
}
# Function to validate repository name
validate_repo_name() {
local repo="$1"
if [[ $repo =~ ^[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+$ ]]; then
return 0
else
return 1
fi
}
# Function to check for empty repository list
check_empty_repo_list() {
local repos="$1"
if [ -z "$repos" ]; then
dialog --msgbox "No repositories found. Please check your GitHub authentication and try again." $(calculate_dialog_size)
return 1
fi
return 0
}
# Global variable to store cached repository list
CACHED_REPOS=""
# Function to get all repositories
get_all_repositories() {
# If the cache is empty, fetch repositories from GitHub CLI
if [ -z "$CACHED_REPOS" ]; then
log "Fetching repositories from GitHub CLI"
CACHED_REPOS=$(gh repo list --json nameWithOwner,visibility,isArchived --limit 1000 --jq '.[] | "\(.nameWithOwner)|\(.visibility)|\(.isArchived)"' 2>&1)
if [ $? -ne 0 ]; then
log "Error fetching repositories: $CACHED_REPOS"
dialog --title "Error" $(calculate_dialog_size) --msgbox "Failed to fetch repositories from GitHub. Error: $CACHED_REPOS"
return 1
fi
if [ -z "$CACHED_REPOS" ]; then
log "No repositories found or empty response from GitHub CLI"
dialog --title "Warning" $(calculate_dialog_size) --msgbox "No repositories found or empty response from GitHub CLI. Please check your GitHub authentication and permissions."
return 1
fi
log "Successfully fetched repositories"
fi
echo "$CACHED_REPOS"
}
# Function to display repository selection menu
show_repo_selection_menu() {
local repos="$1"
local menu_items=()
local i=1
log "Starting show_repo_selection_menu with repos: $repos"
while IFS='|' read -r repo visibility archived; do
archived_status=$([ "$archived" = "true" ] && echo "[Archived]" || echo "")
menu_items+=("$i" "$repo ($visibility) $archived_status" "off")
((i++))
done <<< "$repos"
log "Menu items prepared: ${menu_items[*]}"
local dialog_output=$(dialog --clear --title "Select Repositories" \
--backtitle "GitHub Repository Manager" \
--ok-label "Select" \
--extra-button \
--extra-label "Back" \
--no-cancel \
--checklist "Choose repositories:" $(calculate_dialog_size) \
"${menu_items[@]}" 2>&1 >/dev/tty)
local return_value=$?
log "Dialog return value: $return_value"
log "Dialog output: $dialog_output"
if [ $return_value -eq 3 ]; then
echo "BACK"
elif [ $return_value -eq 0 ]; then
local selected_repos=""
for selection in $dialog_output; do
selected_repos+="${menu_items[$(( (selection-1) * 3 + 1 ))]}\n"
done
echo -e "$selected_repos"
else
echo ""
fi
}
# Function to process selected repositories
process_selected_repos() {
local selected_repos="$1"
local all_repos="$2"
local repo
local visibility
local archived
if [ "$selected_repos" = "BACK" ]; then
return
fi
if [ -z "$selected_repos" ]; then
dialog --msgbox "No repositories selected." $(calculate_dialog_size)
return
fi
local repos_to_change=""
while IFS= read -r repo_info; do
repo=$(echo "$repo_info" | cut -d' ' -f1)
repos_to_change+="$repo\n"
done <<< "$selected_repos"
dialog --title "Confirm Visibility Changes" --yesno "Are you sure you want to change the visibility of these repositories?\n\n$repos_to_change" $(calculate_dialog_size)
if [ $? -ne 0 ]; then
dialog --msgbox "Operation cancelled." $(calculate_dialog_size)
return
fi
while IFS= read -r repo_info; do
repo=$(echo "$repo_info" | cut -d' ' -f1)
if toggle_repo_visibility "$repo"; then
dialog --msgbox "Successfully toggled visibility for $repo" $(calculate_dialog_size)
else
dialog --msgbox "Failed to toggle visibility for $repo. Check the log file for details." $(calculate_dialog_size)
fi
done <<< "$selected_repos"
}
# Function to process selected repositories for archive status toggling
process_selected_repos_archive() {
local selected_repos="$1"
local all_repos="$2"
local repo
local visibility
local archived
local refresh_needed=false
log "Starting process_selected_repos_archive"
log "Selected repos: $selected_repos"
log "All repos: $all_repos"
if [ "$selected_repos" = "BACK" ]; then
log "BACK selected, returning"
return
fi
if [ -z "$selected_repos" ]; then
log "No repositories selected"
dialog --msgbox "No repositories selected." $(calculate_dialog_size)
return
fi
local repos_to_change=""
while IFS= read -r repo_info; do
repo=$(echo "$repo_info" | cut -d' ' -f1)
repos_to_change+="$repo\n"
done <<< "$selected_repos"
dialog --title "Confirm Archive Status Changes" --yesno "Are you sure you want to change the archive status of these repositories?\n\n$repos_to_change" $(calculate_dialog_size)
if [ $? -ne 0 ]; then
log "Operation cancelled by user"
dialog --msgbox "Operation cancelled." $(calculate_dialog_size)
return
fi
while IFS= read -r repo_info; do
repo=$(echo "$repo_info" | cut -d' ' -f1)
log "Processing repo: $repo"
if toggle_repo_archive_status "$repo"; then
log "Successfully changed archive status for $repo"
dialog --msgbox "Successfully changed archive status for $repo" $(calculate_dialog_size)
refresh_needed=true
else
log "Failed to change archive status for $repo"
dialog --msgbox "Failed to change archive status for $repo. Check the log file for details." $(calculate_dialog_size)
fi
done <<< "$selected_repos"
if [ "$refresh_needed" = true ]; then
log "Refreshing repository cache"
CACHED_REPOS=""
get_all_repositories >/dev/null
dialog --msgbox "Repository cache has been refreshed." $(calculate_dialog_size)
fi
log "Finished process_selected_repos_archive"
}
# Function to list all repositories
list_repositories() {
echo "$(date): Listing all repositories" >> "$LOG_FILE"
local repo_list
repo_list=$(gh repo list --json nameWithOwner,visibility,isArchived --jq '.[] | "\(.nameWithOwner)|\(.visibility)|\(.isArchived)"' 2>&1)
if [ $? -ne 0 ]; then
log "Error fetching repository list: $repo_list"
dialog --title "Error" --msgbox "Failed to fetch repository list. Error: $repo_list" $(calculate_dialog_size)
return 1
fi
if [ -z "$repo_list" ]; then
log "No repositories found or empty response from GitHub CLI"
dialog --title "Warning" --msgbox "No repositories found or empty response from GitHub CLI. Please check your GitHub authentication and permissions." $(calculate_dialog_size)
return 1
fi
local formatted_list=""
while IFS='|' read -r repo visibility archived; do
archived_status=$([ "$archived" = "true" ] && echo "[Archived]" || echo "")
formatted_list+="• $repo (Visibility: $visibility)$archived_status\n"
echo "$(date): $repo | $visibility | $archived_status" >> "$LOG_FILE"
done <<< "$repo_list"
dialog --title "Repository List" --msgbox "Repositories and their visibility:\n\n$formatted_list" $(calculate_dialog_size)
}
# Function to save repository status
save_repo_status() {
local default_file="repo_status_$(date +%Y%m%d_%H%M%S).csv"
local output_file=$(dialog --title "Save Repository Status" --inputbox "Enter filename to save status (default: $default_file):" 10 60 "$default_file" 2>&1 >/dev/tty)
if [ $? -ne 0 ] || [ -z "$output_file" ]; then
log "Operation cancelled or empty filename provided for saving repository status"
dialog --msgbox "Operation cancelled or empty filename provided." $(calculate_dialog_size)
return
fi
# Validate filename
if [[ ! $output_file =~ ^[a-zA-Z0-9_.-]+\.csv$ ]]; then
log "Invalid filename provided: $output_file"
dialog --msgbox "Invalid filename. Please use only letters, numbers, underscores, hyphens, and periods, and end with .csv" $(calculate_dialog_size)
return
fi
# Ensure the directory exists
mkdir -p "$(dirname "$output_file")"
log "Attempting to save repository status to $output_file"
if ! gh repo list --json nameWithOwner,visibility,isArchived --jq '.[] | "\(.nameWithOwner),\(.visibility),\(.isArchived)"' > "$output_file"; then
log "Failed to save repository status to $output_file"
dialog --msgbox "Failed to save repository status. Please check your GitHub authentication and try again." $(calculate_dialog_size)
return
fi
log "Successfully saved repository status to $output_file"
dialog --msgbox "Repository status saved to $output_file" $(calculate_dialog_size)
}
# Function to load and apply repository status
load_and_apply_repo_status() {
local default_file=$(ls -t repo_status_*.csv 2>/dev/null | head -n1)
local input_file=$(dialog --title "Load Repository Status" --inputbox "Enter filename to load status (default: $default_file):" 10 60 "$default_file" 2>&1 >/dev/tty)
if [ $? -ne 0 ] || [ -z "$input_file" ]; then
log "Operation cancelled or empty filename provided for loading repository status"
dialog --msgbox "Operation cancelled or empty filename provided." $(calculate_dialog_size)
return
fi
if [ ! -f "$input_file" ]; then
log "File not found: $input_file"
dialog --msgbox "File not found: $input_file" $(calculate_dialog_size)
return
fi
# Check if the file is readable
if [ ! -r "$input_file" ]; then
log "Cannot read file: $input_file. Please check permissions."
dialog --msgbox "Cannot read file: $input_file. Please check permissions." $(calculate_dialog_size)
return
fi
# Validate file content
if ! grep -qE '^[^,]+,(public|private),(true|false)$' "$input_file"; then
log "Invalid file format in $input_file"
dialog --msgbox "Invalid file format. Each line should be 'repo,visibility,isArchived'." $(calculate_dialog_size)
return
fi
log "Starting to apply repository status from $input_file"
while IFS=',' read -r repo visibility is_archived; do
if ! validate_repo_name "$repo"; then
log "Invalid repository name: $repo. Skipping."
dialog --msgbox "Invalid repository name: $repo. Skipping." $(calculate_dialog_size)
continue
fi
dialog --yesno "Change $repo to $visibility and archive status to $is_archived?" $(calculate_dialog_size)
if [ $? -eq 0 ]; then
log "Attempting to change $repo to $visibility and archive status to $is_archived"
if toggle_repo_visibility "$repo"; then
if [ "$is_archived" = "true" ]; then
if gh repo edit "$repo" --archived; then
log "Successfully changed $repo to $visibility and archived"
dialog --msgbox "Successfully changed $repo to $visibility and archived" $(calculate_dialog_size)
else
log "Changed $repo to $visibility but failed to archive"
dialog --msgbox "Changed $repo to $visibility but failed to archive" $(calculate_dialog_size)
fi
else
if gh repo edit "$repo" --unarchived; then
log "Successfully changed $repo to $visibility and unarchived"
dialog --msgbox "Successfully changed $repo to $visibility and unarchived" $(calculate_dialog_size)
else
log "Changed $repo to $visibility but failed to unarchive"
dialog --msgbox "Changed $repo to $visibility but failed to unarchive" $(calculate_dialog_size)
fi
fi
else
log "Failed to change $repo to $visibility"
dialog --msgbox "Failed to change $repo to $visibility" $(calculate_dialog_size)
fi
else
log "User skipped changing $repo"
fi
done < "$input_file"
log "Finished applying repository status from $input_file"
dialog --msgbox "Finished applying repository status from $input_file" $(calculate_dialog_size)
}
# Function to change repository visibility
change_repo_visibility() {
local repo="$1"
local new_visibility="$2"
local current_visibility
# Get current visibility
current_visibility=$(gh repo view "$repo" --json isPrivate --jq 'if .isPrivate then "private" else "public" end')
if [ "$current_visibility" = "$new_visibility" ]; then
log "Repository $repo is already $new_visibility"
return 0
fi
# Change visibility
if gh repo edit "$repo" --visibility "$new_visibility"; then
log "Successfully changed $repo to $new_visibility"
return 0
else
log "Failed to change $repo to $new_visibility"
return 1
fi
}
# Set PAGER to 'cat' to prevent pagination
export PAGER=cat
# Function to search repositories
search_repos() {
local keyword=$(dialog --inputbox "Enter search keyword:" 8 40 2>&1 >/dev/tty)
if [ -z "$keyword" ]; then
return
fi
local repos=$(gh repo list --json nameWithOwner,visibility,isArchived --jq ".[] | select(.nameWithOwner | contains(\"$keyword\")) | \"\(.nameWithOwner)|\(.visibility)|\(.isArchived)\"")
if [ -z "$repos" ]; then
dialog --msgbox "No repositories found matching the keyword: $keyword" $(calculate_dialog_size)
return
fi
show_repo_selection_menu "$repos"
}
# Function to calculate dialog size
calculate_dialog_size() {
local min_height=${1:-8}
local min_width=${2:-40}
local term_lines=$(tput lines)
local term_cols=$(tput cols)
local dialog_height=$((term_lines * 80 / 100))
local dialog_width=$((term_cols * 80 / 100))
dialog_height=$((dialog_height < min_height ? min_height : dialog_height))
dialog_width=$((dialog_width < min_width ? min_width : dialog_width))
echo "$dialog_height $dialog_width"
}
# Function to display detailed repository information
show_repo_details() {
local repo="$1"
local repo_info
local error_message
local commit_info
log "Fetching repository information for $repo"
if ! repo_info=$(gh repo view "$repo" --json name,description,url,homepageUrl,defaultBranchRef,isPrivate,isArchived,createdAt,updatedAt,pushedAt,diskUsage,languages,licenseInfo,stargazerCount,forkCount,issues,pullRequests 2>&1); then
error_message="Failed to fetch repository information for $repo. Error: $repo_info"
log "$error_message"
dialog --title "Error" $(calculate_dialog_size) --msgbox "$error_message"
return
fi
if [ -z "$repo_info" ]; then
error_message="No information retrieved for repository $repo"
log "$error_message"
dialog --title "Error" $(calculate_dialog_size) --msgbox "$error_message"
return
fi
log "Successfully fetched repository information for $repo"
log "Raw repository info: $repo_info"
# Use a single jq command to extract all information
local repo_details=$(echo "$repo_info" | jq -r '
"Name: \(.name // "N/A")
Description: \(.description // "N/A")
URL: \(.url // "N/A")
Homepage: \(.homepageUrl // "N/A")
Default Branch: \(.defaultBranchRef.name // "N/A")
Visibility: \(if .isPrivate then "Private" else "Public" end)
Archived: \(if .isArchived then "Yes" else "No" end)
Created: \(.createdAt[0:10] // "N/A")
Last Updated: \(.updatedAt[0:10] // "N/A")
Last Pushed: \(.pushedAt[0:10] // "N/A")
Disk Usage: \(.diskUsage // "N/A") KB
Primary Language: \((.languages | keys)[0] // "N/A")
License: \(.licenseInfo.name // "N/A")
Stars: \(.stargazerCount // "N/A")
Forks: \(.forkCount // "N/A")
Open Issues: \(.issues.totalCount // "N/A")
Open Pull Requests: \(.pullRequests.totalCount // "N/A")"
')
# Fetch commit count
local commit_count=$(gh api "repos/$repo/commits?per_page=1" --jq 'total_count' 2>/dev/null || echo "N/A")
# Fetch committer count
local committer_count=$(gh api "repos/$repo/contributors?per_page=100" --jq 'length' 2>/dev/null || echo "N/A")
log "Fetched commit count: $commit_count, committer count: $committer_count"
local details="${repo_details}
Commit Count: $commit_count
Committer Count: $committer_count"
log "Formatted repository details for $repo: $details"
dialog --title "Repository Details: $repo" --msgbox "$details" $(calculate_dialog_size)
}
# Function to display the main menu
show_main_menu() {
local refresh_cache=$1
# Refresh the cache if needed
if [ "$refresh_cache" = "true" ] || [ -z "$CACHED_REPOS" ]; then
CACHED_REPOS=""
get_all_repositories >/dev/null
fi
# Fetch user information from GitHub API
local user_info=$(gh api user --jq '{login: .login, name: .name, public_repos: .public_repos}')
log "API response: $user_info"
# Parse user information
local username=$(echo "$user_info" | jq -r '.login')
local name=$(echo "$user_info" | jq -r '.name')
local public_repos=$(echo "$user_info" | jq -r '.public_repos')
local total_repos=$(echo "$CACHED_REPOS" | wc -l)
local private_repos=$((total_repos - public_repos))
log "Parsed values: username=$username, name=$name, public_repos=$public_repos, private_repos=$private_repos, total_repos=$total_repos"
# Display the main menu using dialog
dialog --clear --title "GitHub Repository Manager" \
--no-cancel \
--menu "User: $username ($name)\nPublic Repos: $public_repos | Private Repos: $private_repos\n\nChoose an operation:" 23 70 10 \
1 "List all repositories" \
2 "Toggle visibility for selected repositories" \
3 "Toggle archive status for selected repositories" \
4 "Save current repository status" \
5 "Load and apply repository status" \
6 "Search repositories" \
7 "Show detailed repository information" \
8 "Refresh repository cache" \
9 "Exit" 2>&1 >/dev/tty
}
# Main script
echo "GitHub Repository Manager"
refresh_cache=false
while true; do
choice=$(show_main_menu "$refresh_cache")
refresh_cache=false
case $choice in
1)
if ! all_repos=$(get_all_repositories); then
continue
fi
if check_empty_repo_list "$all_repos"; then
list_repositories
fi
;;
2)
all_repos=$(get_all_repositories)
if check_empty_repo_list "$all_repos"; then
selected_repos=$(show_repo_selection_menu "$all_repos")
if [ "$selected_repos" != "BACK" ]; then
process_selected_repos "$selected_repos" "$all_repos"
refresh_cache=true
fi
fi
;;
3)
all_repos=$(get_all_repositories)
if check_empty_repo_list "$all_repos"; then
selected_repos=$(show_repo_selection_menu "$all_repos")
if [ "$selected_repos" != "BACK" ]; then
process_selected_repos_archive "$selected_repos" "$all_repos"
refresh_cache=true
fi
fi
;;
4)
save_repo_status
;;
5)
load_and_apply_repo_status
refresh_cache=true
;;
6)
search_repos
;;
7)
all_repos=$(get_all_repositories)
if check_empty_repo_list "$all_repos"; then
repo=$(dialog --menu "Select a repository:" 22 76 16 $(echo "$all_repos" | awk -F'|' '{print NR " " $1}') 2>&1 >/dev/tty)
if [ -n "$repo" ]; then
selected_repo=$(echo "$all_repos" | sed -n "${repo}p" | cut -d'|' -f1)
show_repo_details "$selected_repo"
fi
fi
;;
8)
refresh_cache=true
;;
9)
clear
echo -e "${BRIGHT_GREEN}Exiting...${NC}"
exit 0
;;
*)
dialog --msgbox "Invalid option. Please try again." $(calculate_dialog_size)
;;
esac
done