-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-disable-wiki
More file actions
executable file
路173 lines (154 loc) 路 5.91 KB
/
Copy pathgithub-disable-wiki
File metadata and controls
executable file
路173 lines (154 loc) 路 5.91 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
#!/usr/bin/env bash
# Usage:
# github-disable-wiki -u <username> # all non-fork repos of a user
# github-disable-wiki -u <username> -v public # only public
# github-disable-wiki -u <username> -v private # only private
# github-disable-wiki -u <username> -F # include forks
# github-disable-wiki -f links.txt # repos from a file
# echo "https://github.com/o/r" | github-disable-wiki # repos from stdin
# github-disable-wiki owner/repo another/repo # positional slugs
# github-disable-wiki -u <username> --force # disable wiki even if it has pages
# DRY_RUN=1 github-disable-wiki -u <username> # show what would be done
# github-disable-wiki --dry-run -u <username> # same as DRY_RUN=1
# github-disable-wiki -l run.tsv # also log what happened per repo (TSV)
# github-disable-wiki --version # show version
# github-disable-wiki --help # show help
#
# For each repo on the list: if the wiki feature is enabled but the wiki has no
# pages (empty wiki), disables the wiki feature. Repos whose wiki is already off,
# or whose wiki HAS pages, are skipped - unless --force, which disables the wiki
# feature regardless of content (the pages are NOT deleted; re-enabling the wiki
# brings them back).
#
# Pairs with github-find-repos-with-wiki: `github-find-repos-with-wiki -u me -e -o w.txt`
# then `github-disable-wiki -f w.txt`.
#
# Accepts, one entry per line (lines starting with # are ignored, duplicates
# collapsed, unrecognized lines reported as SKIP):
# - https://github.com/owner/repo
# - owner/repo
#
# Exit codes: 0 = no failures, 1 = at least one repo ended in a FAIL row.
#
# Requires: gh CLI (authenticated) with admin access on the repos, git.
set -u
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source-path=SCRIPTDIR
# shellcheck source=../shared/__shared.sh
source "$script_dir/../shared/__shared.sh"
normalize_dry_run
if [[ $# -eq 0 && -t 0 ]]; then
show_usage
exit 0
fi
while [[ $# -gt 0 ]]; do
case "$1" in
-u|--user)
require_value --user "${2:-}" "a username"
GH_USER="$2"; shift 2 ;;
-v|--visibility)
require_value --visibility "${2:-}" "a value"
VISIBILITY="$2"; shift 2 ;;
-F|--include-forks)
INCLUDE_FORKS=1; shift ;;
-f|--file)
require_value --file "${2:-}" "a path"
INPUT_FILE="$2"; shift 2 ;;
--force)
FORCE=1; shift ;;
--dry-run)
DRY_RUN=1; shift ;;
-l|--log)
require_value --log "${2:-}" "a path"
LOG_FILE="$2"; shift 2 ;;
-V|--version)
show_version ;;
-h|--help)
show_usage; exit 0 ;;
-*)
echo "Unknown flag: $1 (use -f <file>, -u <username>, --force, --dry-run, -l <path> or --help)" >&2; exit 1 ;;
*)
POSITIONAL+=("$1"); shift ;;
esac
done
require_cmd gh "brew install gh"
require_cmd git
validate_visibility "$VISIBILITY"
# Token for the wiki content check - passed to git via the git_authed
# credential helper that reads an env var, never on the command line.
require_gh_token
# Echoes the wiki state: "pages" (has >=1 page), "empty" (no wiki repo -
# GitHub reports "Repository not found"), or "error" (inconclusive: network,
# auth or rate limit). Auth comes from the GH_HELPER_TOKEN credential helper.
wiki_state() {
local slug="$1" out rc
out=$(git_authed ls-remote "https://github.com/$slug.wiki.git" 2>&1)
rc=$?
if [[ $rc -eq 0 ]]; then
[[ -n "$out" ]] && echo "pages" || echo "empty"
elif grep -qi 'not found' <<< "$out"; then
echo "empty"
else
echo "error"
fi
}
disable_wiki() {
local url="$1"
local slug
slug=$(url_to_slug "$url")
if [[ -z "$slug" ]]; then
print_row "SKIP" "$url" "unrecognized URL"
return
fi
local enabled
if ! enabled=$(gh api "repos/$slug" --jq '.has_wiki' 2>/dev/null); then
FAILED=$((FAILED + 1)); print_row "FAIL" "$slug" "cannot read repo (404 or no access)"
return
fi
if [[ "$enabled" != "true" ]]; then
print_row "KEEP" "$slug" "wiki feature already disabled"
return
fi
# Wiki feature is on - decide based on its content (unless forced).
if [[ "$FORCE" != "1" ]]; then
local state
state=$(wiki_state "$slug")
case "$state" in
pages)
print_row "KEEP" "$slug" "wiki has pages - not empty (use --force to disable anyway)"
return ;;
error)
FAILED=$((FAILED + 1)); print_row "FAIL" "$slug" "wiki check inconclusive (network/auth?) - skipped for safety"
return ;;
esac
fi
if [[ "$DRY_RUN" == "1" ]]; then
local note="would disable wiki feature"
[[ "$FORCE" == "1" ]] && note="$note [forced - may have pages]"
print_row "DRY" "$slug" "$note"
return
fi
local out
if out=$(gh repo edit "$slug" --enable-wiki=false 2>&1); then
print_row "OK" "$slug" "wiki feature disabled"
else
FAILED=$((FAILED + 1)); print_row "FAIL" "$slug" "$(echo "$out" | head -n 1)"
fi
}
[[ "$DRY_RUN" == "1" ]] && echo "${C_YELLOW}== DRY RUN - nothing will be changed ==${C_RESET}"
[[ "$FORCE" == "1" ]] && echo "${C_RED}== FORCE mode - will disable wiki even if it has pages ==${C_RESET}"
validate_input_source
start_log
FAILED=0
while read -r url; do
disable_wiki "$url"
done < <(input_source "hasWikiEnabled" ".hasWikiEnabled==true" "(wiki enabled)" \
| normalize_input)
# Exit 1 when the repo listing failed or any repo ended in a FAIL row
# (useful for chaining and CI).
if input_source_failed; then
echo "ERROR: could not list repositories - nothing was processed" >&2
exit 1
fi
[[ "$FAILED" -gt 0 ]] && exit 1
exit 0