Sha256: 080191086008c12103375598af312d23fcbf68b1114ab8b8104025c9a3fe0776

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require "refinements/array"
require "versionaire"

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

      using Refinements::Array

      def initialize(collector: Collector.new, expression: Regexp, **)
        @collector = collector
        @expression = expression
        super(**)
      end

      def call configuration = Container[:configuration]
        categories = configuration.commit_categories.pluck :label

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

      private

      attr_reader :collector, :expression

      def group_by_category categories, groups
        collector.call.value_or([]).each.with_object groups do |commit, collection|
          category = commit.subject[subject_pattern(categories)]
          key = collection.key?(category) ? category : "Unknown"
          collection[key] << commit
        end
      end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
milestoner-17.6.0 lib/milestoner/commits/categorizer.rb
milestoner-17.5.0 lib/milestoner/commits/categorizer.rb
milestoner-17.4.0 lib/milestoner/commits/categorizer.rb
milestoner-17.3.0 lib/milestoner/commits/categorizer.rb
milestoner-17.2.0 lib/milestoner/commits/categorizer.rb
milestoner-17.1.0 lib/milestoner/commits/categorizer.rb
milestoner-17.0.0 lib/milestoner/commits/categorizer.rb