Skip to content

Instantly share code, notes, and snippets.

@markuskreitzer
Created October 26, 2021 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markuskreitzer/20c1def17cdc37a1cf3defcf8d572f06 to your computer and use it in GitHub Desktop.
Save markuskreitzer/20c1def17cdc37a1cf3defcf8d572f06 to your computer and use it in GitHub Desktop.
Migrate all Github Repos to a self-hosted Gitea server
#!/bin/bash
# Adapted from Nicolas Boyer by elec3647
# Original post: https://dev.to/nicolasboyer/migrate-all-of-your-repos-from-github-to-gitea-3fk
# Changelog:
# 1. Added ability to pull a user's personal repos or organization.
# 2. Updated `sed` command to work on Linux.
# TODO: Add some logic to pick between org or user.
# TODO: Give ownership to org in Gitea if pulling org.
GITHUB_USERNAME=""
GITHUB_TOKEN=""
GITHUB_ORGANISATION=""
GITEA_USERNAME=""
GITEA_TOKEN=""
GITEA_DOMAIN=""
GITEA_REPO_OWNER=""
# Pull organization
GET_REPOS=$(curl -H 'Accept: application/vnd.github.v3+json' -u $GITHUB_USERNAME:$GITHUB_TOKEN -s "https://api.github.com/orgs/$GITHUB_ORGANISATION/repos?per_page=200&type=all" | jq -r '.[].html_url')
# Pull User
# GET_REPOS=$(curl -H 'Accept: application/vnd.github.v3+json' -u $GITHUB_USERNAME:$GITHUB_TOKEN -s "https://api.github.com/users/${GITHUB_USERNAME}/repos?per_page=200&type=all" | jq -r '.[].html_url')
for URL in $GET_REPOS; do
REPO_NAME=$(echo $URL | sed -e "s|https://github.com/$GITHUB_ORGANISATION/||g")
#REPO_NAME=$(echo $URL | sed -e "s|https://github.com/$GITHUB_USERNAME/||g")
echo "Found $REPO_NAME, importing..."
curl -k -X POST "http://$GITEA_DOMAIN/api/v1/repos/migrate" -u $GITEA_USERNAME:$GITEA_TOKEN -H "accept: application/json" -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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment