Sha256: 50794cc22efb703033f48bd8c7fe71ebccc09bc3fe2953c88663f75bfa8f1afd

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module PrChangelog
  # A strategy to get the changes between the two references based on the
  # squash commits
  class SquashCommitStrategy < BaseCommitStrategy
    SQUASH_COMMIT_FORMAT = /^GitHub( Enterprise)?: (?<title>.+) \((?<pr_number>#\d+)\)$/.freeze
    TAGGED_TITLE = /^(?<tag>[^:]+):\s*(?<title>.+)$/.freeze

    attr_reader :base_ref, :current_ref, :git_proxy

    def initialize(base_ref, current_ref, git_proxy = GitProxy.new)
      @base_ref    = base_ref
      @current_ref = current_ref
      @git_proxy   = git_proxy
    end

    def parsed_commits
      commits_not_merged_into_base_ref
        .split('- ')
        .reject(&:empty?)
        .map { |e| e.split("\n") }
        .map(&:first)
        .select { |line| line.match(SQUASH_COMMIT_FORMAT) }
    end

    def format_commit(commit_line)
      pr_number = pull_request_number_for(commit_line)
      commit_title = pull_request_title_for(commit_line)
      commit_title.strip!
      match = commit_title.match(TAGGED_TITLE)
      if match
        ChangeLine.new(pr_number, match[:tag], match[:title])
      else
        ChangeLine.new(pr_number, nil, commit_title)
      end
    end

    private

    def commits_not_merged_into_base_ref
      git_proxy.commits_between(base_ref, current_ref)
    end

    def pull_request_number_for(github_commit_title)
      md = github_commit_title.match(SQUASH_COMMIT_FORMAT)
      md[:pr_number] if md
    end

    def pull_request_title_for(github_commit_title)
      md = github_commit_title.match(SQUASH_COMMIT_FORMAT)
      md[:title] if md
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pr_changelog-0.4.1 lib/pr_changelog/squash_commit_strategy.rb
pr_changelog-0.4.0 lib/pr_changelog/squash_commit_strategy.rb