Skip to content

Instantly share code, notes, and snippets.

@luisehk
Last active September 5, 2017 16:17
Show Gist options
  • Save luisehk/279b10b65ec4f873ba00b31868feb1df to your computer and use it in GitHub Desktop.
Save luisehk/279b10b65ec4f873ba00b31868feb1df to your computer and use it in GitHub Desktop.
Migrate from github to self-hosted gitlab
import subprocess
import requests
import os
def call(cmd):
print('Executing', cmd)
subprocess.call(cmd, shell=True)
def cd(d):
print('Changing dir', d)
os.chdir(os.path.join(os.getcwd(), d))
with open('repos.txt') as f:
# every line is a repo name
content = f.readlines()
content = [x.strip() for x in content]
# save initial working dir
cwd = os.getcwd()
for repo in content:
# always start from initial working dir
os.chdir(cwd)
# clone from github
clone_cmd = 'git clone git@github.com:<YOUR_ORGANIZATION>/{}.git'.format(repo)
call(clone_cmd)
# get into the repo folder
cd(repo)
# remove gitlab remote, in case you're running this script twice
remote_remove_cmd = 'git remote remove gitlab'
call(remote_remove_cmd)
# track all remote branches
track_cmd = "git branch -r | grep -v '\->' | while read remote; do git branch --track \"${remote#origin/}\" \"$remote\"; done" # noqa
call(track_cmd)
# pull all github branches
fetch_cmd = 'git fetch --all'
pull_cmd = 'git pull --all'
call(fetch_cmd)
call(pull_cmd)
# create gitlab repo
# Gather namespace_id from https://gitlab.com/api/v4/namespaces?search=<YOUR_ORGANIZATION>
gitlab_endpoint = 'https://gitlab.com/api/v4/projects?name={}&namespace_id=<NAMESPACE_ID>' # noqa
gitlab_access_token = '<YOUR_GITLAB_ACCESS_TOKEN>'
gitlab_response = requests.post(
gitlab_endpoint.format(repo),
headers={
'PRIVATE-TOKEN': gitlab_access_token,
})
print(gitlab_response.json())
# add gitlab remote
remote_cmd = 'git remote add gitlab ssh://git@gitlab.com:22/<YOUR_ORGANIZATION>/{}.git'.format(repo) # noqa
call(remote_cmd)
# push to gitlab
push_cmd = 'git push --all gitlab'
call(push_cmd)
repo1
repo2
repo3
repo4
repo5
@luisehk
Copy link
Author

luisehk commented Sep 5, 2017

It could be improved by:

  • connecting to the Github API and gathering the repos instead of using a text file
  • gathering the namespace_id from the Gitlab API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment