-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmigrate_github_to_codeberg.sh
More file actions
executable file
·207 lines (183 loc) · 6.93 KB
/
migrate_github_to_codeberg.sh
File metadata and controls
executable file
·207 lines (183 loc) · 6.93 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
#!/bin/bash
#
# GitHub to Codeberg Migration Script
# ------------------------------------
#
# Author: Rahul Martim Juliato
# Email: rahul.juliato@gmail.com
# License: GPL-3.0
# Version: 0.1.2
#
# This script migrates GitHub repositories to Codeberg.
#
# User Configuration:
# --------------------
# GitHub username and personal access token
# GITHUB_USERNAME="YourGitHubUsername"
# GITHUB_TOKEN="YourGitHubToken"
#
# Codeberg username and personal access token
# CODEBERG_USERNAME="YourCodebergUsername"
# CODEBERG_TOKEN="YourCodebergToken"
#
# Define the REPOSITORIES array with repository names you want to migrate
# Leave it blank to migrate all repositories, or create a list of repositories
# if you want to select specific ones.
# REPOSITORIES=(
# "repository1"
# "repository2"
# "repository3"
# )
#
# Custom prefix for description
# DESCRIPTION_PREFIX=""
# or something like:
# DESCRIPTION_PREFIX="[MIRROR] "
#
# Usage:
# ------
# 1. Configure the user settings at the beginning of the script.
# 2. Run the script.
# 3. Follow the on-screen instructions to proceed with the migration.
#
# Note: Make sure to have 'curl' and 'jq' installed.
#
# Press ENTER to continue, or C-c to abort.
#
# -----------------------------------------------------------
# USER CONFIGURATION
#---------------------------------------------------------------------------
# GitHub username and personal access token
GITHUB_USERNAME="YourGitHubUsername"
GITHUB_TOKEN="YourGitHubToken"
# Codeberg username and personal access token
CODEBERG_USERNAME="YourCodebergUsername"
CODEBERG_TOKEN="YourCodebergToken"
# Define the REPOSITORIES array with repository names you want to migrate
# Leave it blank so you migrate ALL repositories. Create a one per line list
# if you want to select the repositories to migrate.
REPOSITORIES=()
# REPOSITORIES=(
# "repository1"
# "repository2"
# "repository3"
# )
# Define the OWNERS array with specific user names whose repositories you want to migrate
# Leave it blank so you migrate ALL repositories. Create a one per line list
# if you want to select the repositories to migrate.
OWNERS=()
# OWNERS=(
# "owner1"
# "owner2"
# )
# Custom prefix for description
DESCRIPTION_PREFIX=""
# DESCRIPTION_PREFIX="[Secondary] - "
# DESCRIPTION_PREFIX="Draft: "
# UTILS FUNCTIONS
#---------------------------------------------------------------------------
array_contains() {
local array="$1[@]"
local seeking=$2
local in=1
for element in "${!array}"; do
if [[ $element == "$seeking" ]]; then
in=0
break
fi
done
return $in
}
# PROCESSING
#---------------------------------------------------------------------------
printf "\n ----------------------------------------------"
printf "\n Welcome to Github to Codeberg Migration Script"
printf "\n ----------------------------------------------\n"
printf "\n User on Github : $GITHUB_USERNAME"
printf "\n User on Codeberg : $CODEBERG_USERNAME"
printf "\n Using description prefix: $DESCRIPTION_PREFIX"
if [ ${#OWNERS[@]} -eq 0 ]; then
printf "\n Migrating repos owned by: all users"
else
printf "\n Migrating repos owned by: %s" "${OWNERS[@]}"
fi
if [ ${#REPOSITORIES[@]} -eq 0 ]; then
printf "\n Migrating repo : all"
else
printf "\n Migrating repos : %s" "${REPOSITORIES[@]}"
fi
printf "\n"
printf "\n If you wish to change this, abort and change this script.\n\n"
printf "\n\n Press ENTER to continue, C-c to abort.\n\n"
read
printf ">>> Working...\n"
# NOTE: Github api paginates to max 100 repositories, this calculates how many
# runs (pages) we're going to do to fetch all user data.
GITHUB_PAGINATION=100
github_total_repos=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/user" | jq '.public_repos + .total_private_repos')
github_needed_pages=$((($github_total_repos + $GITHUB_PAGINATION - 1) / $GITHUB_PAGINATION))
# Start Migration to Codeberg
for ((github_page_counter = 1; github_page_counter <= github_needed_pages; github_page_counter++)); do
repos=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/user/repos?per_page=${GITHUB_PAGINATION}&page=${github_page_counter}")
echo "$repos" | jq -c '.[]' | while read -r row; do
repo_name=$(echo "$row" | jq -r '.name')
# Skips current processing repo if
# A) it is not on the wanted list
# or B) and you're migrating 'all'
if ! array_contains REPOSITORIES "$repo_name" && [ ${#REPOSITORIES[@]} -ne 0 ]; then
continue
fi
repo_owner=$(echo "$row" | jq -r '.owner.login')
# Skips current processing repo if
# A) it is not owned by a targeted user
# B) no specific users are targeted
if ! array_contains OWNERS "$repo_owner" && [ ${#OWNERS[@]} -ne 0 ]; then
continue
fi
repo_clone_url=$(echo "$row" | jq -r '.clone_url')
repo_description="$DESCRIPTION_PREFIX$(echo "$row" | jq -r '.description')"
repo_is_private=$(echo "$row" | jq -r '.private')
printf ">>> Migrating: $repo_name ($([ "$repo_is_private" = "true" ] && echo "private" || echo "public"))...\n"
json_payload=$(jq -n \
--arg auth_username "$GITHUB_USERNAME" \
--arg auth_token "$GITHUB_TOKEN" \
--arg clone_addr "$repo_clone_url" \
--argjson private "$repo_is_private" \
--arg repo_name "$repo_name" \
--arg repo_owner "$CODEBERG_USERNAME" \
--arg description "$repo_description" \
'{
auth_username: $auth_username,
auth_token: $auth_token,
clone_addr: $clone_addr,
private: $private,
repo_name: $repo_name,
repo_owner: $repo_owner,
service: "github",
description: $description
}')
response=$(curl -s -w "\n%{http_code}" -X POST -H "Content-Type: application/json" -H "Authorization: token $CODEBERG_TOKEN" -d "$json_payload" "https://codeberg.org/api/v1/repos/migrate")
response_body=$(echo "$response" | head -n -1)
http_status=$(echo "$response" | tail -n 1)
case $http_status in
201)
printf " Success!\n"
;;
409)
printf " Error! Already exists on Codeberg.\n"
;;
403)
printf "Error! Forbidden.\n"
;;
*)
error_message=$(echo "$response_body" | jq -r '.message // empty' 2>/dev/null)
if [ -n "$error_message" ]; then
printf "Error: %s (HTTP %s)\n" "$error_message" "$http_status"
else
printf "Error: Unknown! %s\n" "$http_status"
fi
;;
esac
done
done
echo ">>> Migration script completed!"