From cf3042725cf96ae8840af8e90b00cba58b7079f1 Mon Sep 17 00:00:00 2001 From: Max Kratz Date: Thu, 4 Jan 2024 19:02:29 +0100 Subject: [PATCH] Snapshot: starts the conversion to gitea source instance --- gitea2gitea-mirror | 59 +++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 40 deletions(-) diff --git a/gitea2gitea-mirror b/gitea2gitea-mirror index 58e2180..846ffe9 100755 --- a/gitea2gitea-mirror +++ b/gitea2gitea-mirror @@ -1,12 +1,11 @@ #!/bin/bash # -# Script to mirror GitHub repos to a Gitea instance. +# Script to mirror Gitea repos to another Gitea instance. # # Modes: # - Mirror a public/private repo # - Mirror all public/private repos of a user -# - Mirror all starred repos by a user # - Mirror all public/private repos of an organization # # Heavily inspired by: @@ -14,9 +13,10 @@ # # ENVs: -# ACCESS_TOKEN = Gitea token -# GITERA_URL = Gitea URL -# GITHUB_TOKEN = GitHub personal access token +# ACCESS_TOKEN_SRC = Gitea token of the source instance +# GITERA_URL_SRC = Gitea URL of the source instance +# 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. log () { @@ -26,9 +26,9 @@ log () { CURL="curl -f -S -s" # Check for correctly set ENVs -# ACCESS_TOKEN and GITEA_URL are always necessary -if [[ -z "${ACCESS_TOKEN}" || -z "${GITEA_URL}" ]]; 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 +# ACCESS_TOKEN_TRG and GITEA_URL_TRG are always necessary +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_TRG=abc\nexport GITEA_URL_TRG=http://gitea:3000\n" >&2 echo -e "Don't use a trailing slash in URL!" exit 1 fi @@ -53,11 +53,11 @@ done # Prints a message on how to use the script with exit 1 fail_print_usage () { 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 " -o, --org \$organization GitHub 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 " -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 " -m, --mode {org,repo,user} Mode to use; either mirror an organization or an user." + echo -e " -o, --org \$organization Gitea organization to mirror and/or the target organization in Gitea." + 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 " -r, --repo \$repo_url Gitea URL of a single repo to create a mirror for." echo "" >&2 exit 1; } @@ -78,11 +78,6 @@ if [ "${mode}" == "org" ]; then echo -e "Visibility not set." fail_print_usage 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 if [[ -z "${repo}" || -z "${github_user}" ]]; then echo -e "Repo URL or GitHub user not set." @@ -102,30 +97,19 @@ fi #set -euo pipefail set -eu pipefail -header_options=(-H "Authorization: Bearer ${ACCESS_TOKEN}" -H "accept: application/json" -H "Content-Type: application/json") -jsonoutput=$(mktemp -d -t github-repos-XXXXXXXX) +header_options=(-H "Authorization: Bearer ${ACCESS_TOKEN_TRG}" -H "accept: application/json" -H "Content-Type: application/json") +jsonoutput=$(mktemp -d -t gitea-repos-XXXXXXXX) trap "rm -rf ${jsonoutput}" EXIT # Sets the uid to the specified Gitea organization 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 set_uid_user() { - uid=$($CURL "${header_options[@]}" $GITEA_URL/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 + uid=$($CURL "${header_options[@]}" $GITEA_URL_TRG/api/v1/users/${github_user} | jq .id) } # 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 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) if (( code != 409 ));then # 409 == repo already exits cat ${jsonoutput}/stderr.txt >&2 @@ -173,7 +157,7 @@ create_migration_repo() { create_migration_orga() { visibility="${1:-}" 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) if (( code != 422 ));then # 422 == orga already exits cat ${jsonoutput}/stderr.txt >&2 @@ -237,11 +221,6 @@ elif [ "${mode}" == "repo" ]; then log "Mode = single repo" fetch_one_repo one_repo_to_migration -elif [ "${mode}" == "star" ]; then - log "Mode = starred repos" - set_uid - fetch_starred_repos - repos_to_migration elif [ "${mode}" == "user" ]; then log "Mode = user" set_uid_user