Save the script as an executable named something like git-github-issues
. Then you can invoke it as git github-issues
.
The script it standalone. hub
not needed.
#!/usr/bin/env ruby -w | |
require 'net/https' | |
require 'json' | |
unless `git config --get-all remote.origin.url` =~ %r{\bgithub\.com[:/]([^/\s]+)/([^/\s]+)} | |
abort "no GitHub URL found in origin remote" | |
end | |
owner = $1 | |
repo = $2.sub(/\.git$/, '') | |
closed = ARGV.include? '-c' | |
uri = URI "https://api.github.com/repos/#{owner}/#{repo}/issues" | |
uri.query = 'state=closed' if closed | |
http = Net::HTTP.new uri.host, uri.port | |
http.use_ssl = true | |
response = http.start { http.get uri.request_uri } | |
unless Net::HTTPSuccess === response | |
abort "GitHub API fetch failed with status #{response.code}" | |
end | |
data = JSON.parse response.body | |
begin | |
data.each do |issue| | |
puts '%3d. %s' % [ issue['number'], issue['title'] ] | |
end | |
rescue Errno::EPIPE | |
# ignore broken pipe | |
end |