changelog 3.62 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/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,
15
  :force,
Robert Speicher's avatar
Robert Speicher committed
16 17 18 19 20 21 22 23 24
  :merge_request,
  :title
)

class ChangelogOptionParser
  def self.parse(argv)
    options = Options.new

    parser = OptionParser.new do |opts|
25
      opts.banner = "Usage: #{__FILE__} [options] [title]\n\n"
Robert Speicher's avatar
Robert Speicher committed
26 27 28 29 30 31 32

      # 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

33 34 35 36
      opts.on('-f', '--force', 'Overwrite an existing entry') do |value|
        options.force = value
      end

Robert Speicher's avatar
Robert Speicher committed
37 38 39 40 41 42 43 44 45 46 47 48 49
      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('-h', '--help', 'Print help message') do
50
        $stdout.puts opts
Robert Speicher's avatar
Robert Speicher committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        exit
      end
    end

    parser.parse!(argv)

    # Title is everything that remains, but let's clean it up a bit
    options.title = argv.join(' ').strip.squeeze(' ').tr("\r\n", '')

    options
  end

  def self.git_user_name
    %x{git config user.name}.strip
  end
end

class ChangelogEntry
  attr_reader :options

  def initialize(options)
    @options = options

    assert_feature_branch!
    assert_new_file!
    assert_title!

    $stdout.puts "\e[32mcreate\e[0m #{file_path}"
    $stdout.puts contents
80

Robert Speicher's avatar
Robert Speicher committed
81 82 83 84 85 86
    unless options.dry_run
      write
      amend_commit if options.amend
    end
  end

87 88
  private

Robert Speicher's avatar
Robert Speicher committed
89
  def contents
90
    yaml_content = YAML.dump(
Robert Speicher's avatar
Robert Speicher committed
91 92 93 94
      'title'         => title,
      'merge_request' => options.merge_request,
      'author'        => options.author
    )
95
    remove_trailing_whitespace(yaml_content)
Robert Speicher's avatar
Robert Speicher committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  end

  def write
    File.write(file_path, contents)
  end

  def amend_commit
    %x{git add #{file_path}}
    exec("git commit --amend")
  end

  def fail_with(message)
    $stderr.puts "\e[31merror\e[0m #{message}"
    exit 1
  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)
120
    return if options.force
Robert Speicher's avatar
Robert Speicher committed
121

122
    fail_with "#{file_path} already exists! Use `--force` to overwrite."
Robert Speicher's avatar
Robert Speicher committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  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

  def title
    if options.title.empty?
      last_commit_subject
    else
      options.title
    end
  end

  def last_commit_subject
    %x{git log --format="%s" -1}.strip
  end

  def file_path
    File.join(
      unreleased_path,
      branch_name.gsub(/[^\w-]/, '-') << '.yml'
    )
  end

  def unreleased_path
    File.join('changelogs', 'unreleased').tap do |path|
      path << '-ee' if ee?
    end
  end

  def ee?
    @ee ||= File.exist?(File.expand_path('../CHANGELOG-EE.md', __dir__))
  end

  def branch_name
162
    @branch_name ||= %x{git symbolic-ref --short HEAD}.strip
Robert Speicher's avatar
Robert Speicher committed
163
  end
164 165 166 167

  def remove_trailing_whitespace(yaml_content)
    yaml_content.gsub(/ +$/, '')
  end
Robert Speicher's avatar
Robert Speicher committed
168 169 170 171 172 173 174 175
end

if $0 == __FILE__
  options = ChangelogOptionParser.parse(ARGV)
  ChangelogEntry.new(options)
end

# vim: ft=ruby