Snapshot: starts the conversion to gitea source instance

This commit is contained in:
Max Kratz 2024-01-04 19:02:29 +01:00
parent f8ab644cd5
commit cf3042725c

View file

@ -1,12 +1,11 @@
#!/bin/bash #!/bin/bash
# #
# Script to mirror GitHub repos to a Gitea instance. # Script to mirror Gitea repos to another Gitea instance.
# #
# Modes: # Modes:
# - Mirror a public/private repo # - Mirror a public/private repo
# - Mirror all public/private repos of a user # - Mirror all public/private repos of a user
# - Mirror all starred repos by a user
# - Mirror all public/private repos of an organization # - Mirror all public/private repos of an organization
# #
# Heavily inspired by: # Heavily inspired by:
@ -14,9 +13,10 @@
# #
# ENVs: # ENVs:
# ACCESS_TOKEN = Gitea token # ACCESS_TOKEN_SRC = Gitea token of the source instance
# GITERA_URL = Gitea URL # GITERA_URL_SRC = Gitea URL of the source instance
# GITHUB_TOKEN = GitHub personal access token # ACCESS_TOKEN_TRG = Gitea token of the target instance
# GITEA_URL_TRG = Gitea URL of the target instance
# Displays the given input including "=> " on the console. # Displays the given input including "=> " on the console.
log () { log () {
@ -26,9 +26,9 @@ log () {
CURL="curl -f -S -s" CURL="curl -f -S -s"
# Check for correctly set ENVs # Check for correctly set ENVs
# ACCESS_TOKEN and GITEA_URL are always necessary # ACCESS_TOKEN_TRG and GITEA_URL_TRG are always necessary
if [[ -z "${ACCESS_TOKEN}" || -z "${GITEA_URL}" ]]; then if [[ -z "${ACCESS_TOKEN_TRG}" || -z "${GITEA_URL_TRG}" ]]; then
echo -e "Please set the Gitea access token and URL in environment:\nexport ACCESS_TOKEN=abc\nexport GITEA_URL=http://gitea:3000\n" >&2 echo -e "Please set the Gitea access token and URL in environment:\nexport ACCESS_TOKEN_TRG=abc\nexport GITEA_URL_TRG=http://gitea:3000\n" >&2
echo -e "Don't use a trailing slash in URL!" echo -e "Don't use a trailing slash in URL!"
exit 1 exit 1
fi fi
@ -53,11 +53,11 @@ done
# Prints a message on how to use the script with exit 1 # Prints a message on how to use the script with exit 1
fail_print_usage () { fail_print_usage () {
echo -e "Usage: $0" echo -e "Usage: $0"
echo -e " -m, --mode {org,star,repo,user} Mode to use; either mirror an organization or mirror all starred repositories." echo -e " -m, --mode {org,repo,user} Mode to use; either mirror an organization or an user."
echo -e " -o, --org \$organization GitHub organization to mirror and/or the target organization in Gitea." echo -e " -o, --org \$organization Gitea organization to mirror and/or the target organization in Gitea."
echo -e " -u, --user \$github_user GitHub user to gather the starred repositories from." echo -e " -u, --user \$github_user Gitea user to gather the repositories from."
echo -e " -v, --visibility {public,private} Visibility for the created Gitea organization." echo -e " -v, --visibility {public,private} Visibility for the created Gitea organization."
echo -e " -r, --repo \$repo_url GitHub URL of a single repo to create a mirror for." echo -e " -r, --repo \$repo_url Gitea URL of a single repo to create a mirror for."
echo "" >&2 echo "" >&2
exit 1; exit 1;
} }
@ -78,11 +78,6 @@ if [ "${mode}" == "org" ]; then
echo -e "Visibility not set." echo -e "Visibility not set."
fail_print_usage fail_print_usage
fi fi
elif [ "${mode}" == "star" ]; then
if [[ -z "${gitea_organization}" || -z "${github_user}" ]]; then
echo -e "Organization or GitHub user not set."
fail_print_usage
fi
elif [ "${mode}" == "repo" ]; then elif [ "${mode}" == "repo" ]; then
if [[ -z "${repo}" || -z "${github_user}" ]]; then if [[ -z "${repo}" || -z "${github_user}" ]]; then
echo -e "Repo URL or GitHub user not set." echo -e "Repo URL or GitHub user not set."
@ -102,30 +97,19 @@ fi
#set -euo pipefail #set -euo pipefail
set -eu pipefail set -eu pipefail
header_options=(-H "Authorization: Bearer ${ACCESS_TOKEN}" -H "accept: application/json" -H "Content-Type: application/json") header_options=(-H "Authorization: Bearer ${ACCESS_TOKEN_TRG}" -H "accept: application/json" -H "Content-Type: application/json")
jsonoutput=$(mktemp -d -t github-repos-XXXXXXXX) jsonoutput=$(mktemp -d -t gitea-repos-XXXXXXXX)
trap "rm -rf ${jsonoutput}" EXIT trap "rm -rf ${jsonoutput}" EXIT
# Sets the uid to the specified Gitea organization # Sets the uid to the specified Gitea organization
set_uid() { set_uid() {
uid=$($CURL "${header_options[@]}" $GITEA_URL/api/v1/orgs/${gitea_organization} | jq .id) uid=$($CURL "${header_options[@]}" $GITEA_URL_TRG/api/v1/orgs/${gitea_organization} | jq .id)
} }
# Sets the uid to the specified Gitea user # Sets the uid to the specified Gitea user
set_uid_user() { set_uid_user() {
uid=$($CURL "${header_options[@]}" $GITEA_URL/api/v1/users/${github_user} | jq .id) uid=$($CURL "${header_options[@]}" $GITEA_URL_TRG/api/v1/users/${github_user} | jq .id)
}
# Fetches all starred repos of the given user to JSON files
fetch_starred_repos() {
log "Fetch starred repos."
i=1
# GitHub API just returns empty arrays instead of 404
while $CURL "https://api.github.com/users/${github_user}/starred?page=${i}&per_page=100" >${jsonoutput}/${i}.json \
&& (( $(jq <${jsonoutput}/${i}.json '. | length') > 0 )) ; do
(( i++ ))
done
} }
# Fetches all public/private repos of the given GitHub organization to JSON files # Fetches all public/private repos of the given GitHub organization to JSON files
@ -161,7 +145,7 @@ fetch_one_repo() {
# Creates a specific migration repo on Gitea # Creates a specific migration repo on Gitea
create_migration_repo() { create_migration_repo() {
log "Create migration repo." log "Create migration repo."
if ! $CURL -w "%{http_code}\n" "${header_options[@]}" -d @- -X POST $GITEA_URL/api/v1/repos/migrate > ${jsonoutput}/result.txt 2>${jsonoutput}/stderr.txt; then if ! $CURL -w "%{http_code}\n" "${header_options[@]}" -d @- -X POST $GITEA_URL_TRG/api/v1/repos/migrate > ${jsonoutput}/result.txt 2>${jsonoutput}/stderr.txt; then
local code=$(<${jsonoutput}/result.txt) local code=$(<${jsonoutput}/result.txt)
if (( code != 409 ));then # 409 == repo already exits if (( code != 409 ));then # 409 == repo already exits
cat ${jsonoutput}/stderr.txt >&2 cat ${jsonoutput}/stderr.txt >&2
@ -173,7 +157,7 @@ create_migration_repo() {
create_migration_orga() { create_migration_orga() {
visibility="${1:-}" visibility="${1:-}"
log "Create migration orga with name: ${gitea_organization}" log "Create migration orga with name: ${gitea_organization}"
if ! $CURL -X POST $GITEA_URL/api/v1/orgs "${header_options[@]}" --data '{"username": "'"${gitea_organization}"'", "visibility": "'"${visibility}"'"}' > ${jsonoutput}/result.txt 2>${jsonoutput}/stderr.txt; then if ! $CURL -X POST $GITEA_URL_TRG/api/v1/orgs "${header_options[@]}" --data '{"username": "'"${gitea_organization}"'", "visibility": "'"${visibility}"'"}' > ${jsonoutput}/result.txt 2>${jsonoutput}/stderr.txt; then
local code=$(<${jsonoutput}/result.txt) local code=$(<${jsonoutput}/result.txt)
if (( code != 422 ));then # 422 == orga already exits if (( code != 422 ));then # 422 == orga already exits
cat ${jsonoutput}/stderr.txt >&2 cat ${jsonoutput}/stderr.txt >&2
@ -237,11 +221,6 @@ elif [ "${mode}" == "repo" ]; then
log "Mode = single repo" log "Mode = single repo"
fetch_one_repo fetch_one_repo
one_repo_to_migration one_repo_to_migration
elif [ "${mode}" == "star" ]; then
log "Mode = starred repos"
set_uid
fetch_starred_repos
repos_to_migration
elif [ "${mode}" == "user" ]; then elif [ "${mode}" == "user" ]; then
log "Mode = user" log "Mode = user"
set_uid_user set_uid_user