-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgh-repo-export
executable file
·289 lines (235 loc) · 8.84 KB
/
gh-repo-export
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
#!/usr/bin/env bash
# shellcheck disable=SC2213,SC2214,SC2155
set -e
ARCHIVE=
ARCHIVE_PER_REPO=false
EXCLUDE_ATTACHMENTS=false
EXCLUDE_GIT_DATA=false
EXCLUDE_METADATA=false
EXCLUDE_OWNER_PROJECTS=false
EXCLUDE_RELEASES=false
LOCK_REPOSITORIES=false
SKIP_DOWNLOAD=false
__USAGE="
Bulk exports a list of Git repositories from an organization
USAGE
gh repo-export [flags] <organization> <repo1> <repo2> ... <repoN>
gh repo-export [flags] <organization> <path/to/repos file>
FLAGS
--archive=string Name of archive excluding extension; default 'migration-archive-<migration_id>'
--archive-per-repo Generates 1 archive per repository instead of 1 archive for all repositories
-d, --debug Enable debugging
--exclude-attachments Indicates attachments should be excluded from the migration
--exclude-git-data Indicates git data should be excluded from the migration
--exclude-metadata Indicates metadata should be excluded from the migration
--exclude-owner-projects Indicates projects owned by the organization or users should be excluded from the migration
--exclude-releases Indicates releases should be excluded from the migration
-h, --help Display help usage
--hostname=string Hostname of the GitHub instance to authenticate with
--lock-repositories Indicates repositories should be locked (to prevent manipulation) while migrating data
--skip-download Skip downloading the archive(s), instead outputting the URL(s) to stdout (curl and sed required)
EXAMPLES
# Generate 1 archive containing test1 repository, reading repositories from arguments
$ gh repo-export tinyfists test1
# Generate 1 archive containing both test1 and test2 repositories, reading repositories from arguments
$ gh repo-export tinyfists test1 test2
# Generate 1 archive containing both test1 and test2 repositories, reading repositories from arguments, and output the archive URL instead of downloading
$ gh repo-export --skip-download tinyfists test1 test2
# Generate 2 archives, 1 containing test1 repository and 1 containing test2 repository, reading repositories from arguments
$ gh repo-export --archive-per-repo tinyfists test1 test2
# Generate 2 archives, 1 containing test1 repository and 1 containing test2 repository, reading repositories from 'repos.txt' file
$ gh repo-export --archive-per-repo tinyfists repos.txt
";
die() {
printf "\nError: %s\n" "$1"
echo "$__USAGE"
exit 1
}
if ! type -p gh > /dev/null; then
die "'gh' could not be found"
fi
if ! type -p jq > /dev/null; then
die "'jq' could not be found"
fi
while getopts "dh-:" OPT; do
if [ "$OPT" = "-" ]; then # long option: reformulate OPT and OPTARG
OPT="${OPTARG%%=*}" # extract long option name
OPTARG="${OPTARG#"$OPT"}" # extract long option argument (may be empty)
OPTARG="${OPTARG#=}" # if long option argument, remove assigning `=`
fi
case "$OPT" in
archive)
ARCHIVE="${OPTARG}"
;;
archive-per-repo)
ARCHIVE_PER_REPO=true
;;
debug | d)
set -x
;;
exclude-attachments)
EXCLUDE_ATTACHMENTS=true
;;
exclude-git-data)
EXCLUDE_GIT_DATA=true
;;
exclude-metadata)
EXCLUDE_METADATA=true
;;
exclude-owner-projects)
EXCLUDE_OWNER_PROJECTS=true
;;
exclude-releases)
EXCLUDE_RELEASES=true
;;
help | h)
echo "$__USAGE"
exit 0
;;
hostname)
export GH_HOST="${OPTARG}"
;;
lock-repositories)
LOCK_REPOSITORIES=true
;;
skip-download)
SKIP_DOWNLOAD=true
;;
*)
die "Unknown flag '$OPT'"
;;
esac
done
# shift so that $@, $1, etc. refer to the non-option arguments
shift "$((OPTIND-1))"
if test "$#" -lt 2; then
die "Must provide organization and repositories arguments"
fi
# Process arguments for organization and repository information
ORGANIZATION="$1"
declare -a REPOSITORIES
if [[ "$#" -eq 2 && -f "$2" ]]; then
# If argument is valid file, read repositories from file
REPOSITORIES_FILE="$2"
printf "Reading repositories from file: %s\n" "$REPOSITORIES_FILE"
while IFS= read -r REPOSITORY; do
REPOSITORIES+=("$REPOSITORY")
done < "$REPOSITORIES_FILE"
else
# Otherwise, assume arguments are repositories
REPOSITORIES=("${@:2}")
printf "Reading repositories from arguments: %s\n" "${REPOSITORIES[*]}"
fi
# Start migration of specified GitHub repositories
declare -a MIGRATIONS
start_migration() {
local REPOSITORIES=("$@")
# shellcheck disable=SC2016
local MIGRATION_JQ='{
"lock_repositories": $lock_repositories,
"exclude_attachments": $exclude_attachments,
"exclude_git_data": $exclude_git_data,
"exclude_metadata": $exclude_metadata,
"exclude_owner_projects": $exclude_owner_projects,
"exclude_releases": $exclude_releases,
"repositories": $ARGS.positional,
}'
local MIGRATION_INPUTS=$(
jq -n "$MIGRATION_JQ" \
--argjson exclude_attachments "$EXCLUDE_ATTACHMENTS" \
--argjson exclude_git_data "$EXCLUDE_GIT_DATA" \
--argjson exclude_metadata "$EXCLUDE_METADATA" \
--argjson exclude_owner_projects "$EXCLUDE_OWNER_PROJECTS" \
--argjson exclude_releases "$EXCLUDE_RELEASES" \
--argjson lock_repositories "$LOCK_REPOSITORIES" \
--args "${REPOSITORIES[@]}")
local MIGRATION_ID=$(echo "$MIGRATION_INPUTS" | gh api "/orgs/$ORGANIZATION/migrations" -X POST -p wyandotte --input - --jq '.id')
printf "Starting migration %s for repositories: %s\n" "$MIGRATION_ID" "${REPOSITORIES[*]}"
# shellcheck disable=SC2004
MIGRATIONS[$MIGRATION_ID]="pending"
}
if $SKIP_DOWNLOAD; then
if ! type -p curl >/dev/null; then
die "'curl' is required for --skip-download option and could not be found"
fi
if ! type -p sed >/dev/null; then
die "'sed' is required for --skip-download option and could not be found"
fi
if [[ $ARCHIVE != "" ]]; then
die "--archive cannot be used with --skip-download"
fi
fi
if $ARCHIVE_PER_REPO; then
for REPOSITORY in "${REPOSITORIES[@]}"; do
start_migration "$REPOSITORY"
done
else
start_migration "${REPOSITORIES[@]}"
fi
# Watch migrations that are waiting to run or complete, sleeping periodically to give them time and avoid rate limiting
is_waiting() {
local MIGRATION_ID="$1"
local MIGRATION_STATE="${MIGRATIONS[$MIGRATION_ID]}"
[[ "$MIGRATION_STATE" == "pending" || "$MIGRATION_STATE" == "exporting" ]]
}
MIGRATION_WATCH=true
while $MIGRATION_WATCH; do
MIGRATION_WATCH=false
for MIGRATION_ID in "${!MIGRATIONS[@]}"; do
if is_waiting "$MIGRATION_ID"; then
# shellcheck disable=SC2004
MIGRATIONS[$MIGRATION_ID]=$(gh api "/orgs/$ORGANIZATION/migrations/$MIGRATION_ID" -p wyandotte --jq '.state')
printf "Watching migration %s with '%s' state\n" "$MIGRATION_ID" "${MIGRATIONS[$MIGRATION_ID]}"
if is_waiting "$MIGRATION_ID"; then
MIGRATION_WATCH=true
fi
fi
done
if $MIGRATION_WATCH; then
sleep 15
fi
done
# Download migration archives based upon how repositories were bundled
#
# If all repositories in 1 migration archive, we can allow user to fully specify archive name.
# Otherwise, we need to differentiate with migration IDs
download_migration() {
local MIGRATION_ID="$1"
local MIGRATION_FILE="$2"
local MIGRATION_STATE="${MIGRATIONS[$MIGRATION_ID]}"
if [[ "$MIGRATION_STATE" == "exported" ]]; then
printf "Downloading migration %s archive to %s\n" "$MIGRATION_ID" "$MIGRATION_FILE"
gh api "/orgs/$ORGANIZATION/migrations/$MIGRATION_ID/archive" -p wyandotte > "$MIGRATION_FILE"
else
printf "Skipping migration %s due to '%s' state; please investigate\n" "$MIGRATION_ID" "$MIGRATION_STATE"
fi
}
print_archive_url() {
local MIGRATION_ID="$1"
local MIGRATION_STATE="${MIGRATIONS[$MIGRATION_ID]}"
if [[ $GH_HOST != "" ]]; then
if [[ "$GH_HOST" == "github.com" ]]; then
BASE_URL="https://api.github.com"
else
BASE_URL="https://$GH_HOST/api/v3"
fi
else
BASE_URL="https://api.github.com"
fi
local TOKEN=$(gh auth token)
if [[ "$MIGRATION_STATE" == "exported" ]]; then
local ARCHIVE_URL=$(curl -l --no-progress-meter -H "Authorization: bearer $TOKEN" "$BASE_URL/orgs/$ORGANIZATION/migrations/$MIGRATION_ID/archive")
printf "Archive URL for migration %s: %s\n" "$MIGRATION_ID" "$ARCHIVE_URL"
else
printf "Skipping migration %s due to '%s' state; please investigate\n" "$MIGRATION_ID" "$MIGRATION_STATE"
fi
}
for MIGRATION_ID in "${!MIGRATIONS[@]}"; do
if $SKIP_DOWNLOAD; then
print_archive_url "$MIGRATION_ID"
elif $ARCHIVE_PER_REPO; then
download_migration "$MIGRATION_ID" "${ARCHIVE:-migration-archive}-$MIGRATION_ID.tar.gz"
else
download_migration "$MIGRATION_ID" "${ARCHIVE:-migration-archive-$MIGRATION_ID}.tar.gz"
fi
done