Skip to content

Instantly share code, notes, and snippets.

@kijart
Created March 27, 2020 08:50
Show Gist options
  • Save kijart/d7b0dcfea14f5348f45401397151c2b0 to your computer and use it in GitHub Desktop.
Save kijart/d7b0dcfea14f5348f45401397151c2b0 to your computer and use it in GitHub Desktop.
GitHub organization cloner (API v3)
[GITHUB]
MAX_PAGES = 7
ORG_NAME = org-name
PASSWORD = password
USERNAME = username
# https://developer.github.com/v3/repos/#list-organization-repositories
import http.client
import json
import mimetypes
import subprocess
import base64
import configparser
print("\nGitHub ORG cloner\n")
config = configparser.ConfigParser()
config.read("env.ini")
"GITHUB" in config
MAX_PAGES = int(config["GITHUB"]["MAX_PAGES"])
ORG_NAME = str(config["GITHUB"]["ORG_NAME"])
PASSWORD = str(config["GITHUB"]["PASSWORD"])
USERNAME = str(config["GITHUB"]["USERNAME"])
headers = {
"Accept": "application/vnd.github.nebula-preview+json",
"Authorization": "Basic %s" % str(base64.b64encode(("%s:%s" % (USERNAME, PASSWORD)).encode("utf-8")), "utf-8"),
"User-Agent": "application/json"
}
connection = http.client.HTTPSConnection("api.github.com")
for i in range(MAX_PAGES):
url = "/orgs/%s/repos?page=%s" % (ORG_NAME, i+1)
connection.request("GET", url, '', headers)
res = connection.getresponse()
data = res.read()
json_data = json.loads(data)
urls = [repo["ssh_url"] for repo in json_data]
for url in urls:
cmd = 'git clone --mirror ' + url
pipe = subprocess.Popen(cmd, shell=True)
pipe.wait()
connection.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment