Skip to content

Instantly share code, notes, and snippets.

@boydaihungst
Created March 17, 2024 13:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boydaihungst/809861ff696deceefbb89a8a075f6b02 to your computer and use it in GitHub Desktop.
Save boydaihungst/809861ff696deceefbb89a8a075f6b02 to your computer and use it in GitHub Desktop.
github_to_gitea_batch
#!/bin/bash
GITHUB_USERNAME=
GITHUB_TOKEN=
GITHUB_ORGANISATION=
GITHUB_API_CALL=
GITHUB_MAX_PER_PAGE=100
GITHUB_CURRENT_PAGE=1
GITEA_TOKEN=
GITEA_DOMAIN=https://git.example.com
GITEA_REPO_OWNER=
FETCHED_PER_PAGE=0
declare -a GET_REPOS=()
declare -a _GET_REPOS=()
fetch_git_url() {
if [ -z "$GITHUB_ORGANISATION" ]; then
echo "\$GITHUB_ORGANISATION is empty"
GITHUB_API_CALL="https://api.github.com/user/repos?per_page=$GITHUB_MAX_PER_PAGE&page=$GITHUB_CURRENT_PAGE"
else
echo "\$GITHUB_ORGANISATION is NOT empty"
GITHUB_API_CALL="https://api.github.com/orgs/$GITHUB_ORGANISATION/repos?per_page=$GITHUB_MAX_PER_PAGE&page=$GITHUB_CURRENT_PAGE&type=all"
fi
_GET_REPOS=$(curl -H 'Accept: application/vnd.github.v3+json' -H "Authorization: token $GITHUB_TOKEN" "$GITHUB_API_CALL" | jq -r '.[].html_url')
FETCHED_PER_PAGE=$(wc -w <<<"$_GET_REPOS")
GET_REPOS+=(${_GET_REPOS[@]})
if [[ $FETCHED_PER_PAGE -eq $GITHUB_MAX_PER_PAGE ]]; then
GITHUB_CURRENT_PAGE=$((GITHUB_CURRENT_PAGE + 1))
fetch_git_url
fi
}
fetch_git_url
read -r -p "Do you want to import ${#GET_REPOS[@]} repositories? " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
echo # (optional) move to a new line
if [[ $confirm =~ ^[Yy]$ ]]; then
for URL in ${GET_REPOS[@]}; do
REPO_NAME=$(echo $URL | sed 's#.*/##')
echo "Found $REPO_NAME, importing..."
curl -X POST "$GITEA_DOMAIN/api/v1/repos/migrate" -H "accept: application/json" -H "Authorization: token $GITEA_TOKEN" -H "Content-Type: application/json" -d "{ \
\"auth_username\": \"$GITHUB_USERNAME\", \
\"auth_password\": \"$GITHUB_TOKEN\", \
\"clone_addr\": \"$URL\", \
\"mirror\": false, \
\"private\": true, \
\"repo_name\": \"$REPO_NAME\", \
\"repo_owner\": \"$GITEA_REPO_OWNER\", \
\"service\": \"git\", \
\"uid\": 0, \
\"wiki\": true}"
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment