lib/kotlin_detekt/plugin.rb in danger-detekt-instacart-0.0.4 vs lib/kotlin_detekt/plugin.rb in danger-detekt-instacart-0.0.5
- old
+ new
@@ -53,10 +53,13 @@
attr_accessor :filtering
# Skip gradle task
attr_accessor :skip_gradle_task
+ # Only shows messages for the modified lines.
+ attr_accessor :filtering_lines
+
# Calls Detekt task of your gradle project.
# It fails if `gradlew` cannot be found inside current directory.
# It fails if `severity` level is not a valid option.
# It fails if `xmlReport` configuration is not set to `true` in your `build.gradle` file.
# @return [void]
@@ -136,14 +139,18 @@
message = ""
results.each do |r|
location = r.parent
filename = location.get("name").gsub(dir, "")
- next unless !filtering || (target_files.include? filename)
- line = r.get("line") || "N/A"
+ next unless (!filtering && !filtering_lines) || (target_files.include? filename)
+ line = r.get("line").to_i || "N/A"
reason = r.get("message")
rule = r.get("source")
+ if filtering_lines
+ added_lines = parse_added_line_numbers(git.diff[filename].patch)
+ next unless added_lines.include? line
+ end
count += 1
message << "`#{filename}` | #{line} | #{reason} | #{rule} \n"
end
if count != 0
header = "#### #{heading} (#{count})\n\n"
@@ -165,14 +172,52 @@
filtered = issues.select { |issue| issue.get("severity") == level }
next if filtered.empty?
filtered.each do |r|
location = r.parent
filename = location.get("name").gsub(dir, "")
- next unless !filtering || (target_files.include? filename)
+ next unless (!filtering && !filtering_lines) || (target_files.include? filename)
line = (r.get("line") || "0").to_i
- send(level == "warning" ? "warn" : "fail", r.get("message"), file: filename, line: line)
+ if filtering_lines
+ added_lines = parse_added_line_numbers(git.diff[filename].patch)
+ next unless added_lines.include? line
+ end
+ reason = r.get("message")
+ rule = r.get("source")
+
+ message = "**Issue:** #{reason}".dup
+ message << "\n"
+ message << "**Rule:** #{rule}"
+ message << "\n"
+ message << "**File:** `#{filename}:#{line}`" # file:line for quick search
+
+ send(level == "warning" ? "warn" : "fail", message, file: filename, line: line)
end
end
+ end
+
+ # Parses git diff of a file and retuns an array of added line numbers.
+ def parse_added_line_numbers(diff)
+ current_line_number = nil
+ added_line_numbers = []
+ diff_lines = diff.strip.split("\n")
+ diff_lines.each_with_index do |line, index|
+ if m = /\+(\d+)(?:,\d+)? @@/.match(line)
+ # (e.g. @@ -32,10 +32,7 @@)
+ current_line_number = Integer(m[1])
+ else
+ unless current_line_number.nil?
+ if line.start_with?("+")
+ # added line
+ added_line_numbers.push current_line_number
+ current_line_number += 1
+ elsif !line.start_with?("-")
+ # unmodified line
+ current_line_number += 1
+ end
+ end
+ end
+ end
+ added_line_numbers
end
def gradlew_exists?
`ls gradlew`.strip.empty? == false
end