Skip to content

Instantly share code, notes, and snippets.

@bparli
Last active May 1, 2019 21:00
Show Gist options
  • Save bparli/6d223685404fb9aa71077c0df946d686 to your computer and use it in GitHub Desktop.
Save bparli/6d223685404fb9aa71077c0df946d686 to your computer and use it in GitHub Desktop.
Scripts to migrate repos from github enterprise to github public
#!/usr/bin/python
from github import Github
import os, subprocess, sys, getopt
from sys import argv
with open("repos") as fp:
repos = [x.strip('\n') for x in fp.readlines()]
user = ''
pw = ''
myopts,args = getopt.getopt(sys.argv[1:],"u:p:")
for o,a in myopts:
if o == '-u':
user = a
elif o == '-p':
pw = a
else:
print{"Usage: %s -u username -p password" % sys.argv[0]}
g = Github(user, pw)
existing = [ e.name for e in g.get_user().get_repos() ]
orgs = g.get_user().get_orgs()
dir = os.getcwd()
for org in orgs:
print org.name
if org.name == <ORG_NAME>:
for team in org.get_teams():
print team
if team.name == 'RepoOwners':
team_id = team
break
for repo in repos:
if repo not in existing:
org.create_repo(repo, private=True, has_wiki=False, team_id = team_id)
proc = subprocess.Popen(dir+'/update_nitro_git.sh', stdout=subprocess.PIPE, \
stdin=subprocess.PIPE)
proc.stdin.write(repo)
proc.communicate()[0]
else:
print "No"
import subprocess
import xmltodict
proc = subprocess.Popen(["java", "-jar", "/Users/bparli/jenkins-cli.jar", "-i", "/Users/bparli/.ssh/id_rsa_jenkins", \
"-noCertificateCheck", "-s", "https://ci.local/", "list-jobs"], stdout=subprocess.PIPE)
for s in iter(proc.stdout.readline, ''):
job = s[:len(s)-1]
if job == "document-service-github-test":
if len(s) > 50:
continue
else:
file = open("job.xml","w")
subprocess.call(["java", "-jar", "/Users/bparli/jenkins-cli.jar", "-i", "/Users/bparli/.ssh/id_rsa_jenkins", \
"-noCertificateCheck", "-s", "https://ci.local/", "get-job", job, ], stdout=file)
file.close()
with open('job.xml', 'r') as fin:
data = fin.read().splitlines(True)
with open('job.xml', 'w') as fout:
fout.writelines(data[1:])
fp = open("job.xml")
data = xmltodict.parse(fp.read())
try:
url = data['project']['scm']['userRemoteConfigs']['hudson.plugins.git.UserRemoteConfig']['url']
repo = url.split("/")[1]
except:
continue
try:
creds = data['project']['scm']['userRemoteConfigs']['hudson.plugins.git.UserRemoteConfig']['credentialsId']
except:
continue
new_data = data
new_data['project']['scm']['userRemoteConfigs']['hudson.plugins.git.UserRemoteConfig']['url'] = \
"git@github.com:<ORG_NAME>/"+repo
new_data['project']['scm']['userRemoteConfigs']['hudson.plugins.git.UserRemoteConfig']['credentialsId'] = '2d1043a5-e7ca-433d-9d25-586c91daa839'
new_data['project']['properties']['com.coravy.hudson.plugins.github.GithubProjectProperty']['projectUrl'] = \
"https://github.com/<COMPANY>/"+repo
print repo, url, creds
print xmltodict.unparse(new_data)
p = subprocess.Popen(["java", "-jar", "/Users/bparli/jenkins-cli.jar", "-i", "/Users/bparli/.ssh/id_rsa_jenkins", \
"-noCertificateCheck", "-s", "https://ci.local/", "update-job", job, ], stdin=subprocess.PIPE)
p.stdin.write(xmltodict.unparse(new_data))
else:
continue
print "DONE!!!"
#!/bin/bash
read repo
echo $repo
git clone --mirror git@<LOCAL_GIT_INSTANCE>:<ORG_NAME>/$repo.git
echo $repo
cd $repo.git
git remote add my_gits git@github.com:<ORG_NAME>/$repo.git
git push --mirror my_gits
cd ../
rm -rf $repo.git
#done
exit 0
#!/bin/bash
REPOS=""
for config in `find . -maxdepth 3 -name config | grep '.git'`; do
egrep "<LOCAL_GIT_INSTANCE>:<ORG_NAME>|<LOCAL_GIT_INSTANCE>/<ORG_NAME>" $config > /dev/null
if [[ $? -eq 0 ]]; then
repo_dir=`dirname $(dirname ${config})`
REPOS="${REPOS} `basename ${repo_dir}`"
fi
done
echo "Processing: $REPOS"
for repo in $REPOS; do
cd $repo > /dev/null
echo "$repo: "
git remote -v show
git remote set-url origin git@github.com:<ORG_NAME>/$repo.git
git remote -v show
cd - > /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment