lib/android_lint/plugin.rb in danger-android_lint-0.0.8 vs lib/android_lint/plugin.rb in danger-android_lint-0.0.9
- old
+ new
@@ -82,10 +82,13 @@
# Enable filtering
# Only show messages within changed files.
attr_accessor :filtering
+ # Only shows messages for the modified lines.
+ attr_accessor :filtering_lines
+
# Calls lint 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]
@@ -111,19 +114,19 @@
issues = read_issues_from_report
filtered_issues = filter_issues_by_severity(issues)
message = ""
-
+
if inline_mode
# Report with inline comment
send_inline_comment(filtered_issues)
else
message = message_for_issues(filtered_issues)
markdown("### AndroidLint found issues\n\n" + message) unless message.to_s.empty?
end
-
+
message
end
private
@@ -193,14 +196,43 @@
filtered = issues.select{|issue| issue.get("severity") == level}
next if filtered.empty?
filtered.each do |r|
location = r.xpath('location').first
filename = location.get('file').gsub(dir, "")
- next unless !filtering || (target_files.include? filename)
+ next unless (!filtering && !filtering_lines) || (target_files.include? filename)
line = (location.get('line') || "0").to_i
+ if filtering_lines
+ added_lines = parseDiff(git.diff[filename].patch)
+ next unless added_lines.include? line
+ end
send(level === "Warning" ? "warn" : "fail", r.get('message'), file: filename, line: line)
end
end
+ end
+
+ # parses git diff of a file and retuns an array of added line numbers.
+ def parseDiff(diff)
+ current_line_number = nil
+ added_lines = []
+ 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
+ if !current_line_number.nil?
+ if line.start_with?('+')
+ # added line
+ added_lines.push current_line_number
+ current_line_number += 1
+ elsif !line.start_with?('-')
+ # unmodified line
+ current_line_number += 1
+ end
+ end
+ end
+ end
+ added_lines
end
def gradlew_exists?
`ls gradlew`.strip.empty? == false
end