Skip to content

Instantly share code, notes, and snippets.

@rodrigorgs
Created September 23, 2018 14:05
Show Gist options
  • Save rodrigorgs/0fb5fd16166d2dfcf06ab00629d30aad to your computer and use it in GitHub Desktop.
Save rodrigorgs/0fb5fd16166d2dfcf06ab00629d30aad to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'github_api'
def die(msg)
STDERR.puts msg
STDERR.puts
exit 1
end
organization = ENV['ORG'] || die("Please specify the GitHub organization using the environment variable 'ORG'.")
github_token = ENV['GITHUB_TOKEN'] || die("Please specify your GitHub token using the environment variable 'GITHUB_TOKEN'. See https://github.com/settings/tokens")
repo_prefix = ENV['REPO_PREFIX'] || ""
exists = Dir.glob('*')
github = Github.new basic_auth: github_token
pagenum = 1
list = []
while page = github.repos.list(org: organization, page: pagenum, per_page: 100)
break if page.size < 1
list += page.to_a
pagenum += 1
end
list.select! { |repo| repo["clone_url"].split("/")[-1].start_with?(repo_prefix) }
dup = list.select{|repo| exists.include?(repo["name"]) }
puts "skipping #{dup.size} repositories which already exists.\n"
list = list.select{|repo| !exists.include?(repo["name"]) }
puts "cloning #{list.size} repositories.\n"
list.each do |repo|
system "git clone #{repo["clone_url"]}"
sleep 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment