Skip to content

Instantly share code, notes, and snippets.

@walquis
Last active December 21, 2021 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walquis/39c365de223d9433276e398b19d4b345 to your computer and use it in GitHub Desktop.
Save walquis/39c365de223d9433276e398b19d4b345 to your computer and use it in GitHub Desktop.
Retrieve list of repos for org - demonstrates basic-auth and request pagination
#
# $ token=my-personal-access-token bundle exec ruby repos_for_org.rb my-username some-org
#
require 'httparty' # Probly want a Gemfile for these
require 'json'
token = ENV['token']
repo_fullnames = []
def fullnames_from js
js.map { |i| i['full_name'] }
end
url = "https://github.com/api/v3/orgs/#{ARGV[1]}/repos"
loop do
resp = HTTParty.get( url, basic_auth: {username: ARGV[0], password: token } )
fullnames_from(JSON.parse resp.body).each {|i| repo_fullnames << i }
_next = resp.headers['link'].split(',').grep(/rel="next"/)
break if _next.empty?
url = _next.first.split(';').first.gsub(/<(.*)>/, '\1').strip
end
puts repo_fullnames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment