Skip to content

Instantly share code, notes, and snippets.

@nowells
Last active December 25, 2015 17:09
Show Gist options
  • Save nowells/7010858 to your computer and use it in GitHub Desktop.
Save nowells/7010858 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
begin
require 'rubygems'
require 'shellwords'
require 'json'
require 'colorize'
rescue
abort("Please run 'gem install json colorize'")
end
def shell(command, allow_fail=false)
result = %x[#{command}]
abort("Failed to run command: ".red + "#{command}") if $?.exitstatus != 0 && !allow_fail
result.strip
end
$origin = shell("git config --get github.remote", true)
if $origin.empty?
$origin = 'origin'
end
$url, _, $org, $repo = shell("git config --get remote.#{$origin}.url").match(/([^\/:@]+)(\/|:)([\w\.\-_]+)\/([\w\-_\.]+)\.git/).captures rescue nil
$user = shell("git config --get github.user", true)
$access_token = shell("git config --get github.token", true)
unless shell("git config --get remote.#{$origin}.fetch").match(/#{$origin}\/pr\/\*/)
shell("git config --add remote.#{$origin}.fetch +refs/pull/*/head:refs/remotes/#{$origin}/pr/*")
end
abort("Invalid github #{$origin} remote".red) unless $org && $repo
def github(path)
if $url == 'github.com'
url = 'https://api.github.com'
else
url = "https://#{$url}/api/v3"
end
auth = ''
if !$user.empty? && !$access_token.empty?
auth = " -u '#{$user}:#{$access_token}' "
end
result = JSON.parse(shell("curl #{auth} #{url}#{path} 2>/dev/null"))
result
end
pull_request_id = ARGV[0]
shell("git remote update > /dev/null 2>&1")
$pull_requests = github("/repos/#{$org}/#{$repo}/pulls")
def merge_pull_request(pull_request_id)
pull_request = $pull_requests.find { |pr| pr['number'].to_s == pull_request_id.to_s }
abort("Unknown Pull Request: #{pull_request_id}".red) unless pull_request
abort("Pull request is not open".red) if pull_request['state'] != 'open'
comments = github("/repos/#{$org}/#{$repo}/pulls/#{pull_request_id}/comments")
abort("Pull request must have a :shipit: comment".red) unless comments.map { |x| x['body'] }.join("\n").match(/:shipit:/)
commit_message = "#{pull_request['title']}\n\n#{pull_request['body']}\n\n- Fixes ##{pull_request['number']}"
shell(%Q[git merge --squash #{$origin}/pr/#{pull_request['number']}])
shell(%Q[git commit --no-verify -m #{commit_message.shellescape}])
puts "\nSuccessfully Merged Pull Request: ".green + "##{pull_request['number']} - #{pull_request['title']}".yellow
puts " You can now run ".light_black + "git push".yellow
end
if pull_request_id
merge_pull_request(pull_request_id)
else
puts "# Available Pull Requests".yellow
$pull_requests.each do |pull_request|
next unless pull_request['state'] === 'open'
puts " #{pull_request['number']})".green + " #{pull_request['title']}"
end
puts ""
print "Which Pull Request would you like to merge? ".yellow
begin
input = gets.strip
puts "\n"
rescue SystemExit, Interrupt
puts "\n"
abort("Exiting...".red)
end
merge_pull_request(input)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment