Last active
December 21, 2021 21:54
-
-
Save walquis/39c365de223d9433276e398b19d4b345 to your computer and use it in GitHub Desktop.
Retrieve list of repos for org - demonstrates basic-auth and request pagination
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
# | |
# $ 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