class Warnings::Reporter

Base reporter class to define attributes and common method to create a report.

Constants

DEFAULT_FAIL
DEFAULT_FILTER
DEFAULT_INLINE
DEFAULT_NAME
ERROR_FILE_NOT_SET
ERROR_HIGH_SEVERITY
ERROR_PARSER_NOT_SET

Attributes

baseline[RW]

Defines the baseline of file paths if needed. @example src/main/java

@return [String] Path baseline for git files.

fail_error[RW]

Whether to fail the PR if any high issue is reported.

@return [Bool] Fail on high issues.

file[RW]

The file path to parse.

@return [String] Path to file.

filter[RW]

Whether to filter and report only for changes files. If this is set to false, all issues are of a report are included in the comment.

@return [Bool] Filter for changes files.

inline[RW]

Whether to comment a markdown report or do an inline comment on the file.

@return [Bool] Use inline comments.

issues[R]
name[W]

The name of this reporter. It is used to identify your report in the comments.

parser[R]

The parser to be used to read issues out of the file.

@return [Symbol] Name of the parser.

parser_impl[R]

The generated implementation of the :parser.

@return [Parser] Parser implementation

Public Class Methods

new(danger) click to toggle source
# File lib/warnings/report/reporter.rb, line 49
def initialize(danger)
  @danger = danger
  @inline = DEFAULT_INLINE
  @filter = DEFAULT_FILTER
  @fail_error = DEFAULT_FAIL
  @issues = []
end

Public Instance Methods

name() click to toggle source

Return the name of this reporter. The name can have 3 values:

- The name set using #name=
- If name is not set, the name of the parser
- If name and parser are not set, a DEFAULT_NAME

@return [String] Name of the reporter.

# File lib/warnings/report/reporter.rb, line 82
def name
  result = @name
  result ||= "#{@parser_impl.name} #{DEFAULT_NAME}" if @parser_impl
  result || DEFAULT_NAME
end
parser=(value) click to toggle source

Define the parser to be used.

@@raise If no implementation can be found for the symbol. @param value [Symbol] A symbol key to match a parser implementation.

# File lib/warnings/report/reporter.rb, line 70
def parser=(value)
  @parser = value
  @parser_impl = ParserFactory.create(value)
end
report() click to toggle source

Start generating the report. Evaluate, parse and comment the found issues.

# File lib/warnings/report/reporter.rb, line 59
def report
  validate
  parse
  filter_issues
  comment
end

Private Instance Methods

comment() click to toggle source
# File lib/warnings/report/reporter.rb, line 118
def comment
  return if @issues.empty?

  inline ? inline_comment : markdown_comment
end
filter_issues() click to toggle source
# File lib/warnings/report/reporter.rb, line 90
def filter_issues
  return unless filter

  git_files = @danger.git.modified_files + @danger.git.added_files
  @issues.select! do |issue|
    git_files.include?(issue_filename(issue))
  end
end
high_issue?(issue) click to toggle source
# File lib/warnings/report/reporter.rb, line 149
def high_issue?(issue)
  issue.severity.eql?(:high)
end
high_issues?() click to toggle source
# File lib/warnings/report/reporter.rb, line 141
def high_issues?
  result = false
  @issues.each do |issue|
    result = true if high_issue?(issue)
  end
  result
end
inline_comment() click to toggle source
# File lib/warnings/report/reporter.rb, line 124
def inline_comment
  @issues.each do |issue|
    text = MessageUtil.inline(issue)
    if fail_error && high_issue?(issue)
      @danger.fail(text, line: issue.line, file: issue.file_name)
    else
      @danger.warn(text, line: issue.line, file: issue.file_name)
    end
  end
end
issue_filename(item) click to toggle source
# File lib/warnings/report/reporter.rb, line 99
def issue_filename(item)
  result = ''
  if baseline
    result << baseline
    result << '/' unless baseline.chars.last == '/'
  end
  result << item.file_name
end
markdown_comment() click to toggle source
# File lib/warnings/report/reporter.rb, line 135
def markdown_comment
  text = MessageUtil.markdown(name, @issues)
  @danger.markdown(text)
  @danger.fail(format(ERROR_HIGH_SEVERITY, name)) if fail_error && high_issues?
end
parse() click to toggle source
# File lib/warnings/report/reporter.rb, line 113
def parse
  @parser_impl.parse(file)
  @issues = @parser_impl.issues
end
validate() click to toggle source
# File lib/warnings/report/reporter.rb, line 108
def validate
  raise ERROR_PARSER_NOT_SET if @parser_impl.nil?
  raise ERROR_FILE_NOT_SET if @file.nil?
end