Sha256: 959b7877da423aa8c13635e06ab304eb82c099ede5067710e3fdaece3ca686cd

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'structured_changelog/core_ext/string'
require 'structured_changelog/core_ext/gem'

class StructuredChangelog
  class ReleasePreview
    def initialize(changelog:, repo:)
      @changelog = changelog
      @repo      = repo
    end

    def to_s
      "## RELEASE #{new_version}\n\n#{release_notes}"
    end

    def empty?
      release_notes.empty?
    end

    private

    attr_reader :changelog, :repo

    def release_notes
      @release_notes ||= git_log_since_last_release.
        split("\n").
        map(&:strip).
        grep(/^\*\ (BREAKING|FEATURE|ENHANCEMENT|FIX|DEPRECATION)\:/).
        join("\n")
    end

    def git_log_since_last_release
      repo.chdir do
        `git log v#{current_version}..HEAD`.chomp
      end
    end

    def current_version
      @current_version ||= changelog.version
    end

    def new_version
      @new_version ||= if release_notes.match?(/^*\ BREAKING:/)
                         current_version.bump_major
                       elsif release_notes.match?(/^*\ FEATURE:/)
                         current_version.bump_minor
                       elsif release_notes.match?(/^*\ FIX:/)
                         current_version.bump_patch
                       elsif release_notes.match?(/^*\ ENHANCEMENT:/)
                         current_version.bump_patch
                       elsif release_notes.match?(/^*\ DEPRECATION:/)
                         current_version.bump_patch
                       end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
structured_changelog-0.11.1 lib/structured_changelog/release_preview.rb