Skip to content

Instantly share code, notes, and snippets.

@mislav
Forked from marksands/ghi.rb
Created April 27, 2010 19:25
Show Gist options
  • Save mislav/381173 to your computer and use it in GitHub Desktop.
Save mislav/381173 to your computer and use it in GitHub Desktop.
GitHub Issues power script
#!/usr/bin/env ruby
require "rubygems"
require "mutter"
require "httparty"
REMOTE = "origin"
REPO = `git config --get remote.#{REMOTE}.url`.chomp[/github\.com[\/\:]([^\/]+\/[^\/]+)\.git/, 1]
USER = `git config --get github.user`.chomp
TOKEN = `git config --get github.token`.chomp
class Issue
include HTTParty
base_uri "https://github.com/api/v2/json/issues"
if USER.empty? or TOKEN.empty?
default_params :output => 'json'
else
default_params :login => USER, :token => TOKEN, :output => 'json'
end
format :json
def self.open(title)
id = post("/open/#{REPO}", :query => { :title => title })["issue"]["number"]
puts "Issue #{id} created."
end
def self.close(*ids)
ids.each do |id|
post("/close/#{REPO}/#{id}")
puts "Issue #{id} closed."
end
end
def self.view(id)
system "open https://github.com/#{REPO}/issues/#issue/#{id}"
end
def self.browse
system "open https://github.com/#{REPO}/issues"
end
def self.list(status="open")
table = Mutter::Table.new do
column :width => 3, :style => [:bold, :green], :align => :right
column :style => :yellow
column
end
data = get("/list/#{REPO}/#{status}")
if data["issues"].empty?
puts "No Issues"
else
data["issues"].each { |issue| table << issue.values_at("number", "user", "title") }
puts table.to_s
end
end
end
case cmd = ARGV.shift
when /^\d+$/
Issue.view cmd
when nil, "list"
Issue.list
when "close", "c"
Issue.close *ARGV
when "browse", "b"
Issue.browse
when "help", "-h", "--help"
abort <<-EOS
GitHub Issues power script
usage:
List issues: ghi
Create an issue: ghi "Your project sucks"
View an issue: ghi 7
Close an issue: ghi close 7 or ghi c 7 or ghi c 7 8 9
Open browser: ghi browse or ghi b
EOS
else
Issue.open cmd
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment