diff --git a/bin/create-on-github.py b/bin/create-on-github.py new file mode 100644 index 0000000..5173e01 --- /dev/null +++ b/bin/create-on-github.py @@ -0,0 +1,69 @@ +#!/usr/bin/python2 +# +# (C) Copyright 2015, Boudhayan Gupta +# No rights reserved. This file is public domain + +import os +import sys +import requests +import ConfigParser + +try: + import simplejson as json +except ImportError: + import json + +# Load our configuration +config = ConfigParser.ConfigParser() +config.read(os.path.expanduser('~/.github-mirrorrc')) +ACCESS_TOKEN = config.get('Github', 'access_token') +ORGANISATION = config.get('Github', 'organisation') + +# Get the repository name we'll be working with, and the path to the repository (locally) +REPO_NAME = sys.argv[1] +REPO_PATH = sys.argv[2] + +# Set a default description +REPO_DESC = "This repository has no description" + +# Load the description from the path +descriptionFile = REPO_PATH + "/description" + +if os.path.exists(descriptionFile): + REPO_DESC = open( REPO_PATH + "/description", "r").read().strip() + +# Set up the requests session +S = requests.Session() +S.headers.update({"Accept": "application/vnd.github.v3+json"}) +S.headers.update({"Authorization": " ".join(("token", ACCESS_TOKEN))}) + +# Does the repository exist already? +repo_info_url = "/".join(("https://api.github.com/repos/" + ORGANISATION, REPO_NAME)) +r = S.get(repo_info_url) + +if (r.ok) and ("id" in r.json().keys()): + #print "GitHub mirror repository is present" + sys.exit(0) + +# It doesn't, so we need to create the repository +repo_create_payload = { + "name": REPO_NAME, + "description": REPO_DESC, + "private": False, + "has_issues": False, + "has_wiki": False, + "has_downloads": False, + "auto_init": False, +} + +#print "Creating GitHub mirror repository" +repo_create_url = "https://api.github.com/orgs/" + ORGANISATION + "/repos" +r = S.post(repo_create_url, data = json.dumps(repo_create_payload)) + +# Check if the repo was created successfully and exit accordingly +if (r.status_code == 201) and ("id" in r.json().keys()): + #print "GitHub mirror repository created" + sys.exit(0) +else: + #print "GitHub mirror repository creation failed" + sys.exit(1) diff --git a/bin/force-sync-github.sh b/bin/force-sync-github.sh new file mode 100644 index 0000000..a54800b --- /dev/null +++ b/bin/force-sync-github.sh @@ -0,0 +1,26 @@ +repopath=$1 +mgmtdir="/home/git/repo-management/" + +cd $repopath +for line in $( find -mindepth 1 -maxdepth 1 -type d -name "*.git" ); do + cd $repopath/$line/ + + currpath=$(pwd) + urlpath=${currpath#/srv/git/repositories/} + reponame=${urlpath%.git} + + # Gitolite-admin is private, skip it + if [[ "$urlpath" == "gitolite-admin.git" ]]; then + continue + fi + + # Build the remote URL up + remoteurl="git@github.com:kde/$reponame" + # Make sure the repo exists on Github + python $mgmtdir/bin/create-on-github.py "$reponame" "/srv/git/repositories/$urlpath" + # Now we push it up there + python $mgmtdir/helpers/update-repo-mirror.py "$reponame" "$remoteurl" + + # To avoid DoSing them, we now wait 30 seconds + sleep 2s +done diff --git a/bin/sync-to-github.sh b/bin/sync-to-github.sh new file mode 100644 index 0000000..bdaaeca --- /dev/null +++ b/bin/sync-to-github.sh @@ -0,0 +1,13 @@ +# Static Config +mgmtdir="/home/git/repo-management" + +# Grab our input +reponame="$1" +urlpath="$2" + +# Build the remote URL up +remoteurl="git@github.com:kde/$reponame" +# Make sure the repo exists on Github +python $mgmtdir/bin/create-on-github.py "$reponame" "/srv/git/repositories/$urlpath" +# Now we push it up there +python $mgmtdir/helpers/update-repo-mirror.py "$reponame" "$remoteurl"