Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Created May 10, 2012 17:51
Show Gist options
  • Save jeremyf/2654728 to your computer and use it in GitHub Desktop.
Save jeremyf/2654728 to your computer and use it in GitHub Desktop.
A command line tool to help with testing pull requests.
#!/usr/bin/env ruby
# Before you use this:
# git clone git://github.com/some-other-user/their-repo
# This assumes that origin is the repo that you are merging into!
require 'rubygems'
gem 'rest-client'
gem 'crack'
require 'rest-client'
require 'crack'
CURRENT_PATH = ENV['PWD'].freeze
pull_request_number = ARGV[0].freeze
puts "Did not specify pull_request_number" and exit(-1) unless pull_request_number
def execute_commands(*args)
commands = [%(cd #{CURRENT_PATH})]
commands += args
system(commands.join(';'))
end
remote_origin_url = `cd #{CURRENT_PATH}; git config remote.origin.url`.strip
api_remote_repo_url = api_remote_repo_url = remote_origin_url.sub(/(git|https?):\/\//,'https://api.').sub(/\.git$/,'').sub("github.com/",'github.com/repos/')
api_pull_request_url = "#{api_remote_repo_url}/pulls/#{pull_request_number}"
puts api_pull_request_url
response = RestClient.get(api_pull_request_url, :accept => :json)
pull_request = Crack::JSON.parse(response.body)
if pull_request['merged']
puts "Pull request number(#{pull_request_number}) already merged"
else
execute_commands(
"git checkout --track -B #{pull_request['base']['ref']} origin/#{pull_request['base']['ref']}",
"git pull --rebase"
)
return unless execute_commands(%(git checkout -B #{pull_request_number}))
if execute_commands(
%(git remote add #{pull_request_number}-source #{pull_request['head']['repo']['git_url']}),
%(git pull #{pull_request_number}-source #{pull_request['head']['ref']})
)
execute_commands("bundle exec rake")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment