class Danger::DangerCommitLint

Run each commit in the PR through a message linting.

Commit lint will check each commit in the PR to ensure the following is
true:

* Commit subject begins with a capital letter (`subject_cap`)
* Commit subject is more than one word (`subject_word`)
* Commit subject is no longer than 50 characters (`subject_length`)
* Commit subject does not end in a period (`subject_period`)
* Commit subject and body are separated by an empty line (`empty_line`)

By default, Commit Lint fails, but you can configure this behavior.

@example Lint all commits using defaults

commit_lint.check

@example Warn instead of fail

commit_lint.check warn: :all

@example Disable a particular check

commit_lint.check disable: [:subject_period]

@see danger/danger @tags commit linting

Constants

NOOP_MESSAGE

Public Instance Methods

check(config = {}) click to toggle source

Checks the commits with whatever config the user passes.

Passing in a hash which contain the following keys:

* `disable` - array of checks to skip
* `fail` - array of checks to fail on
* `warn` - array of checks to warn on

The current check types are:

* `subject_cap`
* `subject_word`
* `subject_length`
* `subject_period`
* `empty_line`

Note: you can pass :all instead of an array to target all checks.

@param [Hash] config

@return [void]

# File lib/commit_lint/plugin.rb, line 56
def check(config = {})
  @config = config

  if all_checks_disabled?
    messaging.warn NOOP_MESSAGE
  else
    check_messages
  end
end

Private Instance Methods

all_checks_disabled?() click to toggle source
# File lib/commit_lint/plugin.rb, line 106
def all_checks_disabled?
  @config[:disable] == :all || disabled_checks.count == checkers.count
end
check_messages() click to toggle source
# File lib/commit_lint/plugin.rb, line 68
def check_messages
  for message in messages
    for klass in warning_checkers
      issue_warning(klass::MESSAGE, message[:sha]) if klass.fail? message
    end

    for klass in failing_checkers
      issue_failure(klass::MESSAGE, message[:sha]) if klass.fail? message
    end
  end
end
checkers() click to toggle source
# File lib/commit_lint/plugin.rb, line 80
def checkers
  [
    SubjectCapCheck,
    SubjectWordsCheck,
    SubjectLengthCheck,
    SubjectPeriodCheck,
    EmptyLineCheck
  ]
end
checks() click to toggle source
# File lib/commit_lint/plugin.rb, line 90
def checks
  checkers.map(&:type)
end
disabled_checks() click to toggle source
# File lib/commit_lint/plugin.rb, line 110
def disabled_checks
  @config[:disable] || []
end
enabled_checkers() click to toggle source
# File lib/commit_lint/plugin.rb, line 94
def enabled_checkers
  checkers.reject { |klass| disabled_checks.include? klass.type }
end
failing_checkers() click to toggle source
# File lib/commit_lint/plugin.rb, line 102
def failing_checkers
  enabled_checkers - warning_checkers
end
issue_failure(message, sha) click to toggle source
# File lib/commit_lint/plugin.rb, line 135
def issue_failure(message, sha)
  messaging.fail [message, sha].join("\n")
end
issue_warning(message, sha) click to toggle source
# File lib/commit_lint/plugin.rb, line 131
def issue_warning(message, sha)
  messaging.warn [message, sha].join("\n")
end
messages() click to toggle source
# File lib/commit_lint/plugin.rb, line 120
def messages
  git.commits.map do |commit|
    (subject, empty_line) = commit.message.split("\n")
    {
      subject: subject,
      empty_line: empty_line,
      sha: commit.sha
    }
  end
end
warning_checkers() click to toggle source
# File lib/commit_lint/plugin.rb, line 98
def warning_checkers
  enabled_checkers.select { |klass| warning_checks.include? klass.type }
end
warning_checks() click to toggle source
# File lib/commit_lint/plugin.rb, line 114
def warning_checks
  return checks if @config[:warn] == :all

  @config[:warn] || []
end