class Danger::DangerSuggester

Public Instance Methods

suggest() click to toggle source
# File lib/danger/danger_suggester.rb, line 8
def suggest
  suggestions_for_added_lines.each do |suggestion|
    markdown(
      suggestion.message,
      file: suggestion.path,
      line: suggestion.line
    )
  end
end

Private Instance Methods

added_lines_in_commits() click to toggle source

@return [Hash{String => Array<Integer>}]

# File lib/danger/danger_suggester.rb, line 21
def added_lines_in_commits
  @added_lines_in_commits ||= begin
    origin = ::Hash.new do |hash, key|
      hash[key] = []
    end
    ::GitDiff.from_string(diff_string_from_commits).files.each_with_object(origin) do |file, result|
      result[file.b_path] = file.hunks.flat_map do |hunk|
        hunk.lines.grep(::GitDiff::Line::Addition).map do |line|
          line.line_number.right
        end
      end
    end
  end
end
diff_string_from_commits() click to toggle source

@return [String]

# File lib/danger/danger_suggester.rb, line 37
def diff_string_from_commits
  git.diff.patch
end
diff_string_from_unstaged_changes() click to toggle source

@return [String]

# File lib/danger/danger_suggester.rb, line 42
def diff_string_from_unstaged_changes
  `git diff`
end
files() click to toggle source

@return [Array<Danger::Suggester::File>]

# File lib/danger/danger_suggester.rb, line 47
def files
  ::GitDiff.from_string(diff_string_from_unstaged_changes).files.map do |file|
    ::Danger::Suggester::File.new(file)
  end
end
suggestions_for_added_lines() click to toggle source

@return [Array<Danger::Suggester::Suggestion>]

# File lib/danger/danger_suggester.rb, line 54
def suggestions_for_added_lines
  suggestions_for_unstaged_changes.select do |suggestion|
    added_lines_in_commits[suggestion.path].include?(suggestion.line)
  end
end
suggestions_for_unstaged_changes() click to toggle source

@return [Array<Danger::Suggester::Suggestion>]

# File lib/danger/danger_suggester.rb, line 61
def suggestions_for_unstaged_changes
  files.select(&:suggestible?).flat_map do |file|
    file.changes.select(&:suggestible?).map do |change|
      ::Danger::Suggester::Suggestion.new(
        content: change.added_content,
        line: change.first_line_number,
        path: file.a_path
      )
    end
  end
end