Sha256: abb60ab3bc8a1c2385c5a2b806f8064c714e751433b7ac1ede0b8c2d13d8fa6e

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require "versionaire"

module Milestoner
  module Commits
    # Retrieves and categorizes Git repository commit tagged or untagged history.
    class Categorizer
      include Import[:git]

      def initialize(expression: Regexp, **)
        super(**)
        @expression = expression
      end

      def call configuration = Container[:configuration]
        prefixes = configuration.prefixes

        prefixes.reduce({}) { |group, prefix| group.merge prefix => [] }
                .merge("Unknown" => [])
                .then { |groups| group_by_prefix prefixes, groups }
                .each_value { |commits| commits.sort_by!(&:subject) }
                .values
                .flatten
                .uniq(&:subject)
      end

      private

      attr_reader :expression

      def group_by_prefix prefixes, groups
        computed_commits.each.with_object groups do |commit, collection|
          prefix = commit.subject[subject_pattern(prefixes)]
          key = collection.key?(prefix) ? prefix : "Unknown"
          collection[key] << commit
        end
      end

      def subject_pattern prefixes
        prefixes.empty? ? expression.new(//) : expression.union(prefixes)
      end

      def computed_commits = git.tagged? ? tagged_commits : saved_commits

      def tagged_commits
        git.tag_last
           .value_or(nil)
           .then { |tag| git.commits("#{tag}..HEAD") }
           .value_or([])
      end

      def saved_commits = git.commits.value_or([])
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
milestoner-15.3.0 lib/milestoner/commits/categorizer.rb
milestoner-15.2.2 lib/milestoner/commits/categorizer.rb
milestoner-15.2.1 lib/milestoner/commits/categorizer.rb