Created
June 12, 2017 23:17
-
-
Save jeremygaither/aad351efe578483fd77404a19aff7695 to your computer and use it in GitHub Desktop.
clone or pull all repos in an org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby -w | |
# Script will clone or pull all repos for an org into the current folder | |
require 'json' | |
require 'net/http' | |
require 'uri' | |
abort('Environment variable GITHUB_TOKEN must be set. Go to https://github.com/settings/tokens to create a token.') unless ENV.key?('GITHUB_TOKEN') | |
github_token = ENV['GITHUB_TOKEN'] | |
abort('usage: cloneall.rb ORGNAME') unless ARGV.size == 1 | |
github_org = ARGV[0] | |
uri = URI.parse("https://api.github.com/orgs/#{github_org}/repos?per_page=100") | |
request = Net::HTTP::Get.new(uri) | |
request.basic_auth(github_token, 'x-oauth-basic') | |
req_options = { | |
use_ssl: uri.scheme == 'https', | |
} | |
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| | |
http.request(request) | |
end | |
if response.code == '200' | |
repos = JSON.load(response.body) | |
repos.each do |repo| | |
$stdout.puts "Processing repo #{repo['name']}" | |
if Dir.exist?(repo['name']) | |
`cd #{repo['name']} && git pull & cd ..` | |
else | |
`git clone #{repo['ssh_url']}` | |
end | |
end | |
else | |
$stdout.puts "GitHub API Error: #{response.code} #{response.message}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment