Class: Gitlab::Dangerfiles::BaseLinter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/dangerfiles/base_linter.rb

Direct Known Subclasses

CommitLinter, MergeRequestLinter

Constant Summary collapse

MIN_SUBJECT_WORDS_COUNT =
3
MAX_LINE_LENGTH =
72

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commit) ⇒ BaseLinter

Returns a new instance of BaseLinter.



27
28
29
30
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 27

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

Instance Attribute Details

#commitObject (readonly)

Returns the value of attribute commit.



11
12
13
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 11

def commit
  @commit
end

#problemsObject (readonly)

Returns the value of attribute problems.



11
12
13
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 11

def problems
  @problems
end

Class Method Details

.problems_mappingObject



13
14
15
16
17
18
19
20
21
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 13

def self.problems_mapping
  {
    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_a_space: "The %s must not start with a space",
    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"
  }
end

.subject_descriptionObject



23
24
25
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 23

def self.subject_description
  "commit subject"
end

Instance Method Details

#add_problem(problem_key, *args) ⇒ Object



40
41
42
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 40

def add_problem(problem_key, *args)
  @problems[problem_key] = sprintf(self.class.problems_mapping[problem_key], *args)
end

#failed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 36

def failed?
  problems.any?
end

#inspectObject



32
33
34
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 32

def inspect
  "#{self.class}:#{object_id} @commit=#{@commit} @problems=#{@problems}"
end

#lint_subjectObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gitlab/dangerfiles/base_linter.rb', line 44

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

  if subject_too_long?
    add_problem(:subject_too_long, self.class.subject_description)
  end

  if subject_starts_with_a_space?
    add_problem(:subject_starts_with_a_space, self.class.subject_description)
  end

  if subject_starts_with_lowercase?
    add_problem(:subject_starts_with_lowercase, self.class.subject_description)
  end

  if subject_ends_with_a_period?
    add_problem(:subject_ends_with_a_period, self.class.subject_description)
  end

  self
end