Skip to content

Instantly share code, notes, and snippets.

@tbnorth
Last active February 9, 2021 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbnorth/aeab2c2414504fca89c921669741d10d to your computer and use it in GitHub Desktop.
Save tbnorth/aeab2c2414504fca89c921669741d10d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""Create a repo auto_{dirname} on GitHub and add it as a remote for the
current repo. called 'auto'.
Expects GITHUB_USER and GITHUB_TOKEN in ~/.gh_auto:
GITHUB_USER = "gituser"
GITHUB_TOKEN = "647e67457e8e5e49ba56583fb6623cf0"
"""
import json
import os
import subprocess
import urllib
from urllib import request
# get GITHUB_USER and GITHUB_TOKEN
exec(open(os.path.expanduser("~/.gh_auto")).read())
top = subprocess.Popen(
"git rev-parse --show-toplevel", shell=True, stdout=subprocess.PIPE
).communicate()[0]
name = 'auto_' + os.path.basename(top.decode('utf8')).strip()
url = f"https://api.github.com/user/repos"
data = json.dumps({'name': name, 'private': True}).encode('utf8')
url = request.Request(
url, data=data, headers={'Authorization': f"token {GITHUB_TOKEN}"}
)
try:
request.urlopen(url)
except urllib.error.HTTPError:
print("(Request failed)")
cmd = (
"git remote add auto "
f"https://{GITHUB_USER}:{GITHUB_TOKEN}@github.com/{GITHUB_USER}/{name}"
)
subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate()
print("git push auto --tags refs/remotes/origin/*:refs/heads/*")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment