Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
GitHubでアクセスのあるレポジトリの数を調べる
#!/usr/bin/ruby
require 'open-uri'
require 'json'
module GitHubApi
User = 'zunda'
Header = {
'Authorization' => 'token <repoとread:orgのトークン>',
'Accept' => 'application/vnd.github.v3+json',
}
def self.uri(path)
"https://api.github.com#{URI.encode path}"
end
def self.get(path)
open(self.uri(path), Header).read
end
def self.data(path)
JSON.parse(self.get(path))
end
def self.repo_full_names(path)
r = GitHubApi.data(path).map{|e| e['full_name']}
$stderr.puts "#{path} #{r.size}"
r
end
end
repos = []
repos += GitHubApi.repo_full_names('/user/repos')
repos += GitHubApi.repo_full_names('/users/zunda/repos')
orgs = GitHubApi.data("/user/orgs").map{|e| e['login']}
orgs.each do |org|
teams = GitHubApi.data("/orgs/#{org}/teams").map{|e| e['id']}
teams.each do |team|
repos += GitHubApi.repo_full_names("/teams/#{team}/repos")
end
end
orgs.each do |org|
repos += GitHubApi.repo_full_names("/users/#{org}/repos")
end
puts "total #{repos.uniq.size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment