Skip to content

Instantly share code, notes, and snippets.

@twhite96
Last active May 1, 2024 02:49
Show Gist options
  • Save twhite96/abf22f1eb5851bc30c79bfd0edc77ab4 to your computer and use it in GitHub Desktop.
Save twhite96/abf22f1eb5851bc30c79bfd0edc77ab4 to your computer and use it in GitHub Desktop.
Migrate certain repos to Gitea

This script is a work in progress

I found this script on an article at DEV. It's perfect for what I want to do... for the most part.

My specific use case is paring down extraneous GitHub repos by exporting and then importing them to my Gitea server however I don't want all of my repos imported and then deleted, just some.

So the migrate.sh script will be updated as I rummage through the GitHub REST API to figure out the appropriate endpoint for my use case.

Requirements

  • A Linux machine
  • Familiarity with GitHub and REST APIs
  • Comfort with the command line
sudo apt update && sudo apt install -y git jq curl sed
#!/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/users/$GITHUB_USERNAME/repos?per_page=100&type=all" | jq -r '.[].html_url')
for URL in $GET_REPOS; do
REPO_NAME=$(echo $URL | sed 's|https://github.com/$GITHUB_USERNAME/||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
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment