Skip to content

Instantly share code, notes, and snippets.

@zph
Created June 26, 2014 17:17
Show Gist options
  • Select an option

  • Save zph/107fc0d873bbd5b91d9c to your computer and use it in GitHub Desktop.

Select an option

Save zph/107fc0d873bbd5b91d9c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def usage_and_exit
usage = <<-USAGE
Usage: git issue-patch [ISSUE_OR_PR_NUMBER]
Retrieves raw diff patch for an issue/pr on Github and executes `git apply [patch]`
(Requires either gh or hub in $PATH)
(Choose by setting export GITHUB_API_TOOL='hub' in environment)
USAGE
warn usage
exit(1)
end
def binary_full_path
default_bin = 'gh' # Can be replaced with 'hub'
gh_bin = ENV.fetch('GITHUB_API_TOOL') { default_bin }
usage_and_exit unless system("which #{gh_bin} > /dev/null")
`which #{gh_bin}`.chomp
end
def remotes
`git remote -v`.chomp.split("\n").map { |l| l.split[1] }.grep(/github/i)
end
def extract_issue(args = ARGV.dup)
begin
args.first[/\d+/]
rescue NoMethodError
usage_and_exit
end
end
def full_repo_name(remote)
if remote
repo_regex = %r{github\.com[:/](?<full_repo_name>.*)\.git}
remote.match(repo_regex)[:full_repo_name]
else
usage_and_exit
end
end
def execute_patch_apply(gh, url)
exec "#{gh} apply #{url} > /dev/null"
end
def fetch_patch(url)
# If using as standalone, ie just fetching patch not applying
patch_url = "#{url}.patch"
patch = `curl --silent -L #{patch_url}`.chomp
end
def main(io = STDOUT)
github = binary_full_path
issue_no = extract_issue
remote = remotes.first
full_name = full_repo_name(remote)
url = "https://github.com/#{full_name}/pull/#{issue_no}"
io.puts url
execute_patch_apply(github, url)
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment