Skip to content

Instantly share code, notes, and snippets.

@simonsickle-old
Last active June 10, 2016 03:23
Show Gist options
  • Save simonsickle-old/6708708 to your computer and use it in GitHub Desktop.
Save simonsickle-old/6708708 to your computer and use it in GitHub Desktop.
This clones every single repository of a user's github (public) to the current directory in a mirrored form. Handy for gerrit instances
package main
import (
"fmt"
"os"
"os/exec"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func getRepos(user string, key string) (error, []github.Repository) {
// Oauth2 to avoid rate limits
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: key},)
tc := oauth2.NewClient(oauth2.NoContext, ts)
// Set up the client
client := github.NewClient(tc)
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
var allRepos []github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg(user, opt)
if err != nil {
return err, nil
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
return nil, allRepos
}
func main() {
if (os.Args == nil) {
fmt.Println("You must use the program in the following way")
fmt.Println(" ./SickleClone GithubAccountName yourAccessToken")
}
if (len(os.Args) != 3) {
fmt.Println("You must use the program in the following way")
fmt.Println(" ./SickleClone GithubAccountName yourAccessToken")
}
err,repos := getRepos(os.Args[1], os.Args[2])
if (err != nil) {
fmt.Println(err)
}
for _, repo := range repos {
cmd := "git"
args := []string{"clone", *repo.CloneURL}
if err := exec.Command(cmd, args...).Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("Finished with this repo")
}
}
<?php
/*
* Copyright (C) 2013 Simon Sickle <simon@simonsickle.com>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//Here will be your Oauth token for github (API Limit workaround)
$AccessToken = "";
//The github account you wish to clone goes here
$GHAccount = "";
function getJson($pagenum,$ghac,$ghtoc) {
$json_url = 'https://api.github.com/users/'.$ghac.'/repos?access_token='.$ghtoc.'&per_page=200&page='.$pagenum;
$ch = curl_init($json_url);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
curl_setopt_array($ch,$options);
curl_setopt($ch, CURLOPT_USERAGENT, "SickleCloner/2.0 PHP");
$result = curl_exec($ch);
return $result;
}
$n = 0;
$p = 1;
do {
$json = getJson($p,$GHAccount,$AccessToken);
if (count(json_decode($json)) < 1) {
echo("No more projects found! \n");
$n = 0;
break;
} else {
$jsonArr = json_decode($json);
foreach ($jsonArr as $i => $repo) {
//Obtain information from each project
$project = $repo->clone_url;
$name = $repo->name;
// Handle old projects, add new ones
if(is_dir($name.".git/")) {
echo("Syncing existing: ". $project . "\n");
system("cd ".$name.".git/; git fetch --all; cd ../");
} else {
echo("Clone new: ". $project . "\n");
system("git clone ".$project." --mirror");
}
}
$n = 1;
$p++;
}
} while ($n > 0);
?>
#!/usr/bin/env python
import json
import netrc
import os
import sys
import subprocess
try:
# python3
import urllib.error
import urllib.parse
import urllib.request
except ImportError:
# python2
import imp
import urllib2
import urlparse
urllib = imp.new_module('urllib')
urllib.error = urllib2
urllib.parse = urlparse
urllib.request = urllib2
if sys.argv[1] == "help":
print("To use the program you can either run and enter info to sync or you may pass the info as an argument")
print("The first is the github account to sync, the second is the Application Authentication Key")
print("Ex. ./SickleClone.py CyanogenMod akeygoesherethatsverylong\n")
print("The auth key can be generated at https://github.com/settings/tokens/new with default selections")
print("For additional support, email webmaster@simonsickle.com")
exit(0)
if not len(sys.argv) == 3:
GHName = raw_input('Enter Gerrit account name: ')
GHToken = raw_input('Enter API Token: ')
else:
GHName = sys.argv[1]
GHToken = sys.argv[2]
page = 1
repositories = []
githubreq = urllib.request.Request("https://api.github.com/users/%s/repos?access_token=%s&per_page=200&page=%d" % (GHName,GHToken,page))
result = json.loads(urllib.request.urlopen(githubreq).read().decode())
print("Finding projects...")
while len(result) > 0:
for res in result:
repositories.append(res)
page = page + 1
githubreq = urllib.request.Request("https://api.github.com/users/%s/repos?access_token=%s&per_page=200&page=%d" % (GHName,GHToken,page))
result = json.loads(urllib.request.urlopen(githubreq).read().decode())
print("No more projects found. Mirroring beginning...")
for repository in repositories:
repo_name = repository['name']
repo_cloneurl = repository['clone_url']
if os.path.isdir("%s.git" % repo_name):
print("Syncing existing project: %s" % repo_name)
subprocess.call(["cd %s.git/; git fetch --all; cd ../" % repo_name], shell=True)
print("Project %s is now updated" % repo_name)
else:
print("Cloning new project: %s" % repo_name)
subprocess.call(["git clone %s --mirror" % repo_cloneurl], shell=True)
print("Project %s completed." % repo_name)
print("Github account %s is now mirrored" % GHName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment