Skip to content

Instantly share code, notes, and snippets.

@darkdarkdragon
Forked from ryanfb/clone_github_network.rb
Last active September 22, 2020 09:24
Show Gist options
  • Save darkdarkdragon/a74ec33fbf16864440001a06681d09fb to your computer and use it in GitHub Desktop.
Save darkdarkdragon/a74ec33fbf16864440001a06681d09fb to your computer and use it in GitHub Desktop.
Clone all forks in a GitHub repository's network.
#!/usr/bin/env ruby
require 'json'
require 'open-uri'
require 'fileutils'
token = ''
token_arg = '?access_token='+token
def clone_repo(repo)
puts repo['full_name']
owner, path = repo['full_name'].split('/')
FileUtils.mkdir_p(owner)
unless File.directory?(repo['full_name'])
`git clone "#{repo['clone_url']}" "#{repo['full_name']}"`
end
end
def clone_repo_branch(repo, branch)
puts repo['full_name']
owner, path = repo['full_name'].split('/')
FileUtils.mkdir_p(owner)
full_dir = repo["full_name"]+'.'+branch
unless File.directory?(full_dir)
`git clone "#{repo['clone_url']}" "#{full_dir}" -b #{branch}`
end
end
github_api = "https://api.github.com/repos"
master = File.join(ARGV[0],ARGV[1])
FileUtils.mkdir_p(master)
Dir.chdir(master)
clone_repo(JSON.parse(open("#{github_api}/#{ARGV[0]}/#{ARGV[1]}").read))
page = 1
while page < 100 do
puts "================= page #{page}"
forks = JSON.parse(open("#{github_api}/#{ARGV[0]}/#{ARGV[1]}/forks#{token_arg}&per_page=100&page=#{page}").read)
forks.each do |fork|
puts fork["full_name"]
full_name = fork["full_name"]
begin
branches = JSON.parse(open("#{github_api}/#{full_name}/branches#{token_arg}").read)
branches.each do |branch|
puts "#{full_name}, #{branch["name"]}"
clone_repo_branch(fork, branch['name'])
end
rescue Exception => e
puts e.message
puts e.backtrace.inspect
end
end
page += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment