Skip to content

Instantly share code, notes, and snippets.

@ramontayag
Last active October 14, 2015 23:49
Show Gist options
  • Save ramontayag/7494149b9b740f0e72d9 to your computer and use it in GitHub Desktop.
Save ramontayag/7494149b9b740f0e72d9 to your computer and use it in GitHub Desktop.
Get a markdown list of the pull requests merged into master since production's HEAD
#!/usr/bin/env ruby
raise 'I require Ruby 2.0+!' unless RUBY_VERSION[/^2/]
def get_production_remote_name
ENV["PRODUCTION"] || "production"
end
def extract_hashes(production_remote_name:)
command = [
"git log #{production_remote_name}/master..origin/master --merges --pretty=oneline",
%Q(grep "Merge pull request"),
"awk '{print $1}'",
].join(" | ")
`#{command}`.split("\n").reverse
end
def extract_pull_request_number(hash)
command = "git show #{hash} --pretty=full"
message = `#{command}`
match_data = message.match(/Merge pull request #(\d+)/)
match_data[1]
end
def extract_merge_title(hash)
command = "git show #{hash} --pretty=full | tail -2 | head -1"
`#{command}`.strip
end
def extract_github_owner_and_project_name
command = "cat .git/config | grep github"
result = `#{command}`.strip
match_data = result.match(/github.com:([\w-]+)\/([\w-]+)\.git/)
match_data[1..-1]
end
def has_migrations?(hash)
command = "git show #{hash} --format=fuller --stat=1000"
result = `#{command}`
result.include?("db/migrate")
end
def build_link_for(hash, github_owner, github_project_name)
pull_request_number = extract_pull_request_number(hash)
"https://github.com/#{github_owner}/#{github_project_name}/pull/#{pull_request_number}"
end
def build_markdown_link_for(hash, github_owner, github_project_name)
url = build_link_for(hash, github_owner, github_project_name)
title = extract_merge_title(hash)
str = "[#{title}](#{url})"
str = "#{str} (has migrations)" if has_migrations?(hash)
str
end
def build_markdown_links_from(hashes, github_owner, github_project_name)
hashes.map do |hash|
build_markdown_link_for(hash, github_owner, github_project_name)
end
end
def build_markdown_list(hashes, github_owner, github_project_name)
links = build_markdown_links_from(hashes, github_owner, github_project_name)
links.map do |link|
"- #{link}"
end.join("\n")
end
def update_remotes
[
"git fetch",
"git rebase origin/master",
"git fetch #{get_production_remote_name}",
].each do |update_command|
`#{update_command}`
end
end
update_remotes
hashes = extract_hashes(production_remote_name: get_production_remote_name)
github_owner, github_project_name = extract_github_owner_and_project_name
links = build_markdown_list(hashes, github_owner, github_project_name)
puts links
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment