Skip to content

Instantly share code, notes, and snippets.

@mrk21
Last active August 29, 2015 14:04
Show Gist options
  • Save mrk21/7980b97c14994b06eb1c to your computer and use it in GitHub Desktop.
Save mrk21/7980b97c14994b06eb1c to your computer and use it in GitHub Desktop.
Git log と GitHub Issues から Changelog を作成する。
require 'open3'
require 'pp'
require 'json'
require 'date'
require 'rubygems'
require 'faraday'
require 'ostruct'
require 'erb'
require 'time'
repo = ARGV[0]
out, err, status = Open3.capture3('git tag')
version_tags = out.split(/\n/).select{|v| v =~ /^v[\d.]+$/}.sort
out, err, status = Open3.capture3('git log #{version_tags.first}...HEAD --oneline')
is = out.split(/\n/)
is.map! do |line|
hash, *message = line.split(/\s+/)
message = message.join(' ')
matches, issue_no = message.match(/^.*#(\d+).*$/).to_a
next if issue_no.nil?
issue_no.to_i
end
is = is.compact.uniq
connection = Faraday::Connection.new(url: 'https://api.github.com')
response = connection.get "/repos/mrk21/#{repo}/issues?state=closed"
issues = JSON.parse(response.body)
issues = issues.find_all{|v| is.include?(v['number'].to_i)}
change_log = issues.reduce({}) do |m,v|
labels = v['labels'].map{|v| v['name']}
type = labels.include?('enhancement') ? 'Future' : labels.include?('bug') ? 'Bugfix' : nil
next m if type.nil?
next m if v['milestone'].nil?
milestone = v['milestone']['title'].match(/^[\d.]+$/).to_a.first
next m if milestone.nil?
milestone = (milestone.split('.') + [0])[0..2].join('.')
m[milestone] ||= OpenStruct.new(
info: OpenStruct.new(
milestone: milestone,
due_on: Time.parse(v['milestone']['due_on'], Time.now.utc).getlocal.strftime('%Y-%m-%d'),
),
commits: []
)
m[milestone].commits.push OpenStruct.new(
id: v['number'].to_i,
url: v['html_url'].strip,
title: v['title'].strip.gsub(/\.$/,''),
type: type,
)
m
end
puts ERB.new(<<-MD,nil,'-').result(binding)
# Changelog
<% change_log.each do |k,v| %>
## [<%= k %>](https://github.com/mrk21/#{repo}/tree/v<%= k %>) - <%= v.info.due_on %>
<% v.commits.each do |v| -%>
* <%= v.type %> [#<%= v.id %>](<%= v.url %>): <%= v.title %>
<% end -%>
<% end -%>
MD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment