Migrate all of your repos from Github to Gitea

So I’ve been thinking about using a self-hosted Gitea instance for a while now, but there was one thing holding me back : the import from Github.

There is an import feature in Gitea but it’s limited to single repositories, and I had around 60 to import. Needless to say I didn’t want to import them one by one, and so I went to my best friend : Google.
I found many tools for importing from Gitlab, from Gogs and other things I didn’t need, but nothing to bulk import from my Github organisation. Well, I did found one but of course it was not working.
So I dug in the Github and Gitea documentations and this is what I came up with, which worked flawlessly !

How to use

You'll need some basic dependencies

sudo apt update && sudo apt install -y git jq curl sed

The script

Here's what's inside.

#!/bin/bash

GITHUB_USERNAME=
GITHUB_TOKEN=
GITHUB_ORGANISATION=

GITEA_USERNAME=
GITEA_TOKEN=
GITEA_DOMAIN=
GITEA_REPO_OWNER=



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')

for URL in $GET_REPOS; do

    REPO_NAME=$(echo $URL | sed 's|https://github.com/$GITHUB_ORGANISATION/||g')

    echo "Found $REPO_NAME, importing..."

    curl -X POST "https://$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

How to exec

  • Copy this and put it in a file named import.sh (or whatever you want)
  • Give the script execution permissions :

    chmod +x import.sh
    
  • You need to fill in the variables at the top before you can run it

  • Exec !

    ./import.sh
    

If all went well, your should have transfered all of your repos to your new Gitea instance.

You made a mistake and don't want to delete the repos all one by one ? Here's the bonus script.

#!/bin/bash

GITHUB_USERNAME=
GITHUB_TOKEN=
GITHUB_ORGANISATION=

GITEA_USERNAME=
GITEA_TOKEN=
GITEA_DOMAIN=
GITEA_REPO_OWNER=

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')

for URL in $GET_REPOS; do

    REPO_NAME=$(echo $URL | sed 's|https://github.com/$GITHUB_ORGANISATION/||g')

    echo "Deleting $REPO_NAME if it exists on Gitea ..."

    curl -X DELETE "https://$GITEA_DOMAIN/api/v1/repos/$GITEA_REPO_OWNER/$name" -u $GITEA_USERNAME:$GITEA_TOKEN -H  "accept: application/json"

done

That's all folks.
Have a good morning, and if I don't see you good afternoon, good evening and good night !

33