2016-10-24 11:17:51 -04:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
#
|
|
|
|
# Generate a changelog entry file in the correct location.
|
|
|
|
#
|
|
|
|
# Automatically stages the file and amends the previous commit if the `--amend`
|
|
|
|
# argument is used.
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
require 'yaml'
|
|
|
|
|
|
|
|
Options = Struct.new(
|
|
|
|
:amend,
|
|
|
|
:author,
|
|
|
|
:dry_run,
|
2016-11-02 18:15:20 -04:00
|
|
|
:force,
|
2016-10-24 11:17:51 -04:00
|
|
|
:merge_request,
|
2017-05-20 16:49:55 -04:00
|
|
|
:title,
|
|
|
|
:type
|
2016-10-24 11:17:51 -04:00
|
|
|
)
|
2017-05-20 16:49:55 -04:00
|
|
|
INVALID_TYPE = -1
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2018-06-25 17:32:00 -04:00
|
|
|
module ChangelogHelpers
|
|
|
|
Abort = Class.new(StandardError)
|
|
|
|
Done = Class.new(StandardError)
|
|
|
|
|
|
|
|
def capture_stdout(cmd)
|
|
|
|
output = IO.popen(cmd, &:read)
|
|
|
|
fail_with "command failed: #{cmd.join(' ')}" unless $?.success?
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
|
|
|
def fail_with(message)
|
|
|
|
raise Abort, "\e[31merror\e[0m #{message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-24 11:17:51 -04:00
|
|
|
class ChangelogOptionParser
|
2018-06-25 17:32:00 -04:00
|
|
|
extend ChangelogHelpers
|
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
Type = Struct.new(:name, :description)
|
|
|
|
TYPES = [
|
|
|
|
Type.new('added', 'New feature'),
|
|
|
|
Type.new('fixed', 'Bug fix'),
|
|
|
|
Type.new('changed', 'Feature change'),
|
|
|
|
Type.new('deprecated', 'New deprecation'),
|
|
|
|
Type.new('removed', 'Feature removal'),
|
|
|
|
Type.new('security', 'Security fix'),
|
2017-10-13 05:36:00 -04:00
|
|
|
Type.new('performance', 'Performance improvement'),
|
2017-05-20 16:49:55 -04:00
|
|
|
Type.new('other', 'Other')
|
|
|
|
].freeze
|
|
|
|
TYPES_OFFSET = 1
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def parse(argv)
|
|
|
|
options = Options.new
|
|
|
|
|
|
|
|
parser = OptionParser.new do |opts|
|
|
|
|
opts.banner = "Usage: #{__FILE__} [options] [title]\n\n"
|
|
|
|
|
|
|
|
# Note: We do not provide a shorthand for this in order to match the `git
|
|
|
|
# commit` interface
|
|
|
|
opts.on('--amend', 'Amend the previous commit') do |value|
|
|
|
|
options.amend = value
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on('-f', '--force', 'Overwrite an existing entry') do |value|
|
|
|
|
options.force = value
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on('-m', '--merge-request [integer]', Integer, 'Merge Request ID') do |value|
|
|
|
|
options.merge_request = value
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on('-n', '--dry-run', "Don't actually write anything, just print") do |value|
|
|
|
|
options.dry_run = value
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on('-u', '--git-username', 'Use Git user.name configuration as the author') do |value|
|
|
|
|
options.author = git_user_name if value
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on('-t', '--type [string]', String, "The category of the change, valid options are: #{TYPES.map(&:name).join(', ')}") do |value|
|
|
|
|
options.type = parse_type(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.on('-h', '--help', 'Print help message') do
|
|
|
|
$stdout.puts opts
|
2018-06-25 17:32:00 -04:00
|
|
|
raise Done.new
|
2017-05-20 16:49:55 -04:00
|
|
|
end
|
|
|
|
end
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
parser.parse!(argv)
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
# Title is everything that remains, but let's clean it up a bit
|
|
|
|
options.title = argv.join(' ').strip.squeeze(' ').tr("\r\n", '')
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
options
|
|
|
|
end
|
2016-11-02 18:15:20 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
def read_type
|
|
|
|
read_type_message
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
type = TYPES[$stdin.getc.to_i - TYPES_OFFSET]
|
|
|
|
assert_valid_type!(type)
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
type.name
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
def parse_type(name)
|
|
|
|
type_found = TYPES.find do |type|
|
|
|
|
type.name == name
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
2017-05-20 16:49:55 -04:00
|
|
|
type_found ? type_found.name : INVALID_TYPE
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
def read_type_message
|
|
|
|
$stdout.puts "\n>> Please specify the index for the category of your change:"
|
|
|
|
TYPES.each_with_index do |type, index|
|
|
|
|
$stdout.puts "#{index + TYPES_OFFSET}. #{type.description}"
|
|
|
|
end
|
|
|
|
$stdout.print "\n?> "
|
|
|
|
end
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
def assert_valid_type!(type)
|
|
|
|
unless type
|
2018-06-25 17:32:00 -04:00
|
|
|
raise Abort, "Invalid category index, please select an index between 1 and #{TYPES.length}"
|
2017-05-20 16:49:55 -04:00
|
|
|
end
|
|
|
|
end
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
def git_user_name
|
2018-06-25 17:32:00 -04:00
|
|
|
capture_stdout(%w[git config user.name]).strip
|
2017-05-20 16:49:55 -04:00
|
|
|
end
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ChangelogEntry
|
2018-06-25 17:32:00 -04:00
|
|
|
include ChangelogHelpers
|
|
|
|
|
2016-10-24 11:17:51 -04:00
|
|
|
attr_reader :options
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
@options = options
|
|
|
|
|
|
|
|
assert_feature_branch!
|
|
|
|
assert_title!
|
2017-05-20 16:49:55 -04:00
|
|
|
assert_new_file!
|
|
|
|
|
|
|
|
# Read type from $stdin unless is already set
|
|
|
|
options.type ||= ChangelogOptionParser.read_type
|
|
|
|
assert_valid_type!
|
2016-10-24 11:17:51 -04:00
|
|
|
|
|
|
|
$stdout.puts "\e[32mcreate\e[0m #{file_path}"
|
|
|
|
$stdout.puts contents
|
2016-10-29 14:10:47 -04:00
|
|
|
|
2016-10-24 11:17:51 -04:00
|
|
|
unless options.dry_run
|
|
|
|
write
|
|
|
|
amend_commit if options.amend
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-09 06:25:29 -05:00
|
|
|
private
|
|
|
|
|
2016-10-24 11:17:51 -04:00
|
|
|
def contents
|
2016-12-09 06:25:29 -05:00
|
|
|
yaml_content = YAML.dump(
|
2016-10-24 11:17:51 -04:00
|
|
|
'title' => title,
|
|
|
|
'merge_request' => options.merge_request,
|
2017-05-20 16:49:55 -04:00
|
|
|
'author' => options.author,
|
|
|
|
'type' => options.type
|
2016-10-24 11:17:51 -04:00
|
|
|
)
|
2016-12-09 06:25:29 -05:00
|
|
|
remove_trailing_whitespace(yaml_content)
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def write
|
|
|
|
File.write(file_path, contents)
|
|
|
|
end
|
|
|
|
|
|
|
|
def amend_commit
|
2018-06-25 17:32:00 -04:00
|
|
|
fail_with "git add failed" unless system(*%W[git add #{file_path}])
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2018-06-25 17:32:00 -04:00
|
|
|
Kernel.exec(*%w[git commit --amend])
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_feature_branch!
|
|
|
|
return unless branch_name == 'master'
|
|
|
|
|
|
|
|
fail_with "Create a branch first!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_new_file!
|
|
|
|
return unless File.exist?(file_path)
|
2016-11-02 18:15:20 -04:00
|
|
|
return if options.force
|
2016-10-24 11:17:51 -04:00
|
|
|
|
2016-11-02 18:15:20 -04:00
|
|
|
fail_with "#{file_path} already exists! Use `--force` to overwrite."
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_title!
|
|
|
|
return if options.title.length > 0 || options.amend
|
|
|
|
|
|
|
|
fail_with "Provide a title for the changelog entry or use `--amend`" \
|
|
|
|
" to use the title from the previous commit."
|
|
|
|
end
|
|
|
|
|
2017-05-20 16:49:55 -04:00
|
|
|
def assert_valid_type!
|
|
|
|
return unless options.type && options.type == INVALID_TYPE
|
|
|
|
|
|
|
|
fail_with 'Invalid category given!'
|
|
|
|
end
|
|
|
|
|
2016-10-24 11:17:51 -04:00
|
|
|
def title
|
|
|
|
if options.title.empty?
|
|
|
|
last_commit_subject
|
|
|
|
else
|
|
|
|
options.title
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_commit_subject
|
2018-06-25 17:32:00 -04:00
|
|
|
capture_stdout(%w[git log --format=%s -1]).strip
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def file_path
|
|
|
|
File.join(
|
|
|
|
unreleased_path,
|
|
|
|
branch_name.gsub(/[^\w-]/, '-') << '.yml'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unreleased_path
|
2018-02-20 08:16:41 -05:00
|
|
|
path = File.join('changelogs', 'unreleased')
|
|
|
|
path = File.join('ee', path) if ee?
|
|
|
|
|
|
|
|
path
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def ee?
|
|
|
|
@ee ||= File.exist?(File.expand_path('../CHANGELOG-EE.md', __dir__))
|
|
|
|
end
|
|
|
|
|
|
|
|
def branch_name
|
2018-06-25 17:32:00 -04:00
|
|
|
@branch_name ||= capture_stdout(%w[git symbolic-ref --short HEAD]).strip
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
2016-12-09 06:25:29 -05:00
|
|
|
|
|
|
|
def remove_trailing_whitespace(yaml_content)
|
|
|
|
yaml_content.gsub(/ +$/, '')
|
|
|
|
end
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
2018-06-25 17:32:00 -04:00
|
|
|
begin
|
|
|
|
options = ChangelogOptionParser.parse(ARGV)
|
|
|
|
ChangelogEntry.new(options)
|
|
|
|
rescue ChangelogHelpers::Abort => ex
|
|
|
|
$stderr.puts ex.message
|
|
|
|
exit 1
|
|
|
|
rescue ChangelogHelpers::Done
|
|
|
|
exit
|
|
|
|
end
|
2016-10-24 11:17:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# vim: ft=ruby
|