Sha256: 44d3d4a1c3cf52535183c50faea62b0b8b8b9ef34db5932bb6b005a50b704562

Contents?: true

Size: 934 Bytes

Versions: 1

Compression:

Stored size: 934 Bytes

Contents

module Danger
  module SemanticCommit
    class TypeValidator
      def initialize(types)
        @types = types || default_types
      end

      def valid?(commit)
        subject = commit.fetch(:subject)

        type_from(subject) && types.include?(type_from(subject))
      end

      def message(_commit)
        <<~MSG
          Commit is missing a type.  Start your commit with one of the following:
          #{types.join(',')}

          For more information see: http://karma-runner.github.io/1.0/dev/git-commit-msg.html
        MSG
      end

      private

      attr_reader :types

      def type_from(subject)
        if matches = subject.match(/^(?<type>\w+)(\(|:)/)
          matches[:type]
        end
      end

      def default_types
        [
          "chore",
          "docs",
          "feat",
          "fix",
          "refactor",
          "style",
          "test",
        ]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danger-semantic-commit-0.3.0 lib/semantic_commit/type_validator.rb