Class: Gitlab::Danger::CommitLinter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/danger/commit_linter.rb

Constant Summary collapse

MIN_SUBJECT_WORDS_COUNT =
3
MAX_LINE_LENGTH =
72
MAX_CHANGED_FILES_IN_COMMIT =
3
MAX_CHANGED_LINES_IN_COMMIT =
30
SHORT_REFERENCE_REGEX =
%r{([\w\-\/]+)?(#|!|&|%)\d+\b}.freeze
DEFAULT_SUBJECT_DESCRIPTION =
'commit subject'
WIP_PREFIX =
'WIP: '
PROBLEMS =
{
  subject_too_short: "The %s must contain at least #{MIN_SUBJECT_WORDS_COUNT} words",
  subject_too_long: "The %s may not be longer than #{MAX_LINE_LENGTH} characters",
  subject_starts_with_lowercase: "The %s must start with a capital letter",
  subject_ends_with_a_period: "The %s must not end with a period",
  separator_missing: "The commit subject and body must be separated by a blank line",
  details_too_many_changes: "Commits that change #{MAX_CHANGED_LINES_IN_COMMIT} or more lines across " \
    "at least #{MAX_CHANGED_FILES_IN_COMMIT} files must describe these changes in the commit body",
  details_line_too_long: "The commit body should not contain more than #{MAX_LINE_LENGTH} characters per line",
  message_contains_text_emoji: "Avoid the use of Markdown Emoji such as `:+1:`. These add limited value " \
    "to the commit message, and are displayed as plain text outside of GitLab",
  message_contains_unicode_emoji: "Avoid the use of Unicode Emoji. These add no value to the commit " \
    "message, and may not be displayed properly everywhere",
  message_contains_short_reference: "Use full URLs instead of short references (`gitlab-org/gitlab#123` or " \
    "`!123`), as short references are displayed as plain text outside of GitLab"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commit) ⇒ CommitLinter

Returns a new instance of CommitLinter.


35
36
37
38
39
# File 'lib/gitlab/danger/commit_linter.rb', line 35

def initialize(commit)
  @commit = commit
  @problems = {}
  @linted = false
end

Instance Attribute Details

#commitObject (readonly)

Returns the value of attribute commit


33
34
35
# File 'lib/gitlab/danger/commit_linter.rb', line 33

def commit
  @commit
end

#problemsObject (readonly)

Returns the value of attribute problems


33
34
35
# File 'lib/gitlab/danger/commit_linter.rb', line 33

def problems
  @problems
end

Instance Method Details

#add_problem(problem_key, *args) ⇒ Object


65
66
67
# File 'lib/gitlab/danger/commit_linter.rb', line 65

def add_problem(problem_key, *args)
  @problems[problem_key] = sprintf(PROBLEMS[problem_key], *args)
end

#failed?Boolean

Returns:

  • (Boolean)

61
62
63
# File 'lib/gitlab/danger/commit_linter.rb', line 61

def failed?
  problems.any?
end

#fixup?Boolean

Returns:

  • (Boolean)

41
42
43
# File 'lib/gitlab/danger/commit_linter.rb', line 41

def fixup?
  commit.message.start_with?('fixup!', 'squash!')
end

#lint(subject_description = "commit subject") ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gitlab/danger/commit_linter.rb', line 69

def lint(subject_description = "commit subject")
  return self if @linted

  @linted = true
  lint_subject(subject_description)
  lint_separator
  lint_details
  lint_message

  self
end

#lint_subject(subject_description) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gitlab/danger/commit_linter.rb', line 81

def lint_subject(subject_description)
  if subject_too_short?
    add_problem(:subject_too_short, subject_description)
  end

  if subject_too_long?
    add_problem(:subject_too_long, subject_description)
  end

  if subject_starts_with_lowercase?
    add_problem(:subject_starts_with_lowercase, subject_description)
  end

  if subject_ends_with_a_period?
    add_problem(:subject_ends_with_a_period, subject_description)
  end

  self
end

#merge?Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/gitlab/danger/commit_linter.rb', line 49

def merge?
  commit.message.start_with?('Merge branch')
end

#multi_line?Boolean

Returns:

  • (Boolean)

57
58
59
# File 'lib/gitlab/danger/commit_linter.rb', line 57

def multi_line?
  !details.nil? && !details.empty?
end

#revert?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/gitlab/danger/commit_linter.rb', line 53

def revert?
  commit.message.start_with?('Revert "')
end

#suggestion?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/gitlab/danger/commit_linter.rb', line 45

def suggestion?
  commit.message.start_with?('Apply suggestion to')
end