-
-
Save hwatkins/473726 to your computer and use it in GitHub Desktop.
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 | |
# | |
# Discover the contribution status of a user for all their watched repos: owner, collaborator, or watcher | |
# Returns JSON { "repo" => { ... }, "owner" => is project owner?, "collaborator" => has collaborator access? } | |
# | |
# Returns either Array of JSON above; or streams each JSON result as discovered (--stream flag) | |
# | |
# Usage: | |
# | |
# Save to file github_user_collaborations.rb | |
# ./github_user_collaborations.rb drnic --stream # lots of results streamed | |
# ./github_user_collaborations.rb kef # 20+ results in an Array | |
# | |
# Or: | |
# | |
# ruby -rubygems -ropen-uri -e 'eval open("http://gist.github.com/raw/473707/github_user_collaborations.rb").read' drnic --stream | |
require "json" | |
require "open-uri" | |
def collaborators(user, project) | |
begin | |
url = "http://github.com/api/v2/json/repos/show/#{user}/#{project}/collaborators" | |
$stderr.puts "open #{url}" if ENV['DEBUG'] | |
JSON.parse(open(url).read)["collaborators"] | |
rescue OpenURI::HTTPError => e | |
$stderr.puts "404 on #{url}" if ENV['DEBUG'] | |
[] | |
end | |
end | |
def watched_repos(user) | |
begin | |
url = "http://github.com/api/v2/json/repos/watched/#{user}" | |
$stderr.puts "open #{url}" if ENV['DEBUG'] | |
JSON.parse(open(url).read)["repositories"] | |
rescue OpenURI::HTTPError => e | |
$stderr.puts "404 on #{url}" if ENV['DEBUG'] | |
[] | |
end | |
end | |
def contributions(user, &block) | |
if block | |
watched_repos(user).each do |repo| | |
is_owner = repo["owner"] == user | |
yield({ "repo" => repo, "owner" => is_owner, "collaborator" => (is_owner || collaborators(repo["owner"], repo["name"]).include?(user)) }) | |
end | |
else | |
watched_repos(user).map do |repo| | |
is_owner = repo["owner"] == user | |
{ "repo" => repo, "owner" => is_owner, "collaborator" => (is_owner || collaborators(repo["owner"], repo["name"]).include?(user)) } | |
end | |
end | |
end | |
options = {} | |
parser = OptionParser.new do |opts| | |
opts.banner = <<-BANNER.gsub(/^ /, '') | |
Discover the contribution status of a user for all their watched repositories: owner, collaborator, or watcher | |
Returns JSON { "repo" => { ... }, "owner" => is project owner?, "collaborator" => has collaborator access? } | |
Returns either Array of JSON above; or streams each JSON result as discovered (--stream flag) | |
Usage: #{File.basename($0)} user [options] | |
Options are: | |
BANNER | |
opts.separator "" | |
opts.on("-s", "--stream", | |
"Stream each watched repository result on a line of its own." | |
) { |arg| options[:stream] = true } | |
opts.on("-h", "--help", | |
"Show this help message.") { $stdout.puts opts; exit } | |
opts.parse!(ARGV) | |
end | |
if user = ARGV.shift | |
if options[:stream] | |
contributions(user) do |contribution| | |
puts contribution.to_json | |
$stdout.flush | |
end | |
else | |
print contributions(user).to_json | |
end | |
else | |
puts parser | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment