Sha256: 42c8b450bc33feca6a52a2639aff8109e9c3061b458b9bff1370ce3070f3827f

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Danger
  # Run each commit in the MR through the check.
  #
  # GFSM Commit Tralier will make sure that at least one commit
  # inside the MR has a 'CHANGELOG: ' trailer.
  #
  # @example Warn instead of fail
  #
  #          gfsm_commit_trailer.check(:warn: true)
  #
  # @see danger/danger
  class DangerGfsmCommitTrailer < Plugin
    ERROR_MESSAGE = "This MR has no commits with a CHANGELOG trailer."
    CHANGELOG_TRAILER_REGEX = /^(?<name>Changelog):\s*(?<category>.+)$/i.freeze

    # Checks the commits using the config passed by the user
    #
    # Passing in a hash which contains the following keys
    #
    # * `warn` - boolean value to tell whether or not eventual errors
    #            should be reported as warnings
    #
    # @param [Hash] config
    #
    # @return [void]
    def check(config = {})
      unless changelog_trailers?
        if config.key?(:warn) && config[:warn]
          messaging.warn ERROR_MESSAGE
        else
          messaging.fail ERROR_MESSAGE
        end
      end
    end

    private

    def changelog_trailers?
      git.commits.each do |commit|
        trailer = commit.message.match(CHANGELOG_TRAILER_REGEX)
        return true if trailer
      end

      false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
danger-gfsm_commit_trailer-0.1.1 lib/gfsm_commit_trailer/plugin.rb
danger-gfsm_commit_trailer-0.1.0 lib/gfsm_commit_trailer/plugin.rb