From 21eefa21c17efa0939207e37be51312e32d46a54 Mon Sep 17 00:00:00 2001 From: Suraphan Laokondee Date: Wed, 22 Apr 2020 13:43:44 +0700 Subject: [PATCH 1/8] Remove fail exception. --- lib/iblinter/plugin.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/iblinter/plugin.rb b/lib/iblinter/plugin.rb index 7786ceb..3a0611c 100644 --- a/lib/iblinter/plugin.rb +++ b/lib/iblinter/plugin.rb @@ -43,12 +43,6 @@ def lint(path = Dir.pwd, fail_on_warning: false, inline_mode: true, options: {}) message << markdown_issues(warnings, "Warnings", ":warning:") unless warnings.empty? markdown message end - - if errors.count.positive? - fail "Failed due to IBLinter errors" - elsif fail_on_warning && warnings.count.positive? - fail "Failed due to IBLinter warnings" - end end # Instantiate iblinter From 5fcff31628a6108c0b618282182b36f9d3eae447 Mon Sep 17 00:00:00 2001 From: Suraphan Laokondee Date: Wed, 22 Apr 2020 14:16:33 +0700 Subject: [PATCH 2/8] filter only git diff issue4 --- lib/iblinter/plugin.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/iblinter/plugin.rb b/lib/iblinter/plugin.rb index 3a0611c..bd08cef 100644 --- a/lib/iblinter/plugin.rb +++ b/lib/iblinter/plugin.rb @@ -29,6 +29,8 @@ def lint(path = Dir.pwd, fail_on_warning: false, inline_mode: true, options: {}) end issues = iblinter.lint(path, options) + issues = filter_git_diff_issues(issues) + return if issues.empty? errors = issues.select { |v| v["level"] == "error" } @@ -61,6 +63,16 @@ def iblinter_installed? !`which iblinter`.empty? end + # Filters issues reported against changes in the modified files + # + # @return [Array] swiftlint issues + def filter_git_diff_issues(issues) + modified_files_info = git_modified_files_info() + return issues.select { |i| + modified_files_info["#{i['file']}"] != nil && modified_files_info["#{i['file']}"].include?(i['line'].to_i) + } + end + def markdown_issues(results, heading, emoji) message = "#### #{heading}\n\n" From 3a8c1def01a1f36d42a68502e530dab3db418e15 Mon Sep 17 00:00:00 2001 From: Suraphan Laokondee Date: Wed, 22 Apr 2020 14:19:44 +0700 Subject: [PATCH 3/8] Add git_modified_files_info --- lib/iblinter/plugin.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/iblinter/plugin.rb b/lib/iblinter/plugin.rb index bd08cef..76f88f3 100644 --- a/lib/iblinter/plugin.rb +++ b/lib/iblinter/plugin.rb @@ -73,6 +73,19 @@ def filter_git_diff_issues(issues) } end + # Finds modified files and added files, creates array of files with modified line numbers + # + # @return [Array] Git diff changes for each file + def git_modified_files_info() + modified_files_info = Hash.new + updated_files = (git.modified_files - git.deleted_files) + git.added_files + updated_files.each {|file| + modified_lines = git_modified_lines(file) + modified_files_info[File.expand_path(file)] = modified_lines + } + modified_files_info + end + def markdown_issues(results, heading, emoji) message = "#### #{heading}\n\n" From 55e8f834f761db54925b62ea79f4391f4d0a8ae5 Mon Sep 17 00:00:00 2001 From: Suraphan Laokondee Date: Wed, 22 Apr 2020 14:21:56 +0700 Subject: [PATCH 4/8] Add git_modified_lines --- lib/iblinter/plugin.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/iblinter/plugin.rb b/lib/iblinter/plugin.rb index 76f88f3..f152a13 100644 --- a/lib/iblinter/plugin.rb +++ b/lib/iblinter/plugin.rb @@ -86,6 +86,31 @@ def git_modified_files_info() modified_files_info end + # Gets git patch info and finds modified line numbers, excludes removed lines + # + # @return [Array] Modified line numbers i + def git_modified_lines(file) + git_range_info_line_regex = /^@@ .+\+(?\d+),/ + git_modified_line_regex = /^\+(?!\+|\+)/ + git_removed_line_regex = /^[-]/ + git_not_removed_line_regex = /^[^-]/ + file_info = git.diff_for_file(file) + line_number = 0 + lines = [] + file_info.patch.split("\n").each do |line| + starting_line_number = 0 + case line + when git_range_info_line_regex + starting_line_number = Regexp.last_match[:line_number].to_i + when git_modified_line_regex + lines << line_number + end + line_number += 1 if line_number > 0 + line_number = starting_line_number if line_number == 0 && starting_line_number > 0 + end + lines + end + def markdown_issues(results, heading, emoji) message = "#### #{heading}\n\n" From fa3db7cc3b4ec7474fa13387efad19a128a44b64 Mon Sep 17 00:00:00 2001 From: Suraphan Laokondee Date: Wed, 22 Apr 2020 14:50:52 +0700 Subject: [PATCH 5/8] Remove line condition --- lib/iblinter/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iblinter/plugin.rb b/lib/iblinter/plugin.rb index f152a13..1fac9ba 100644 --- a/lib/iblinter/plugin.rb +++ b/lib/iblinter/plugin.rb @@ -69,7 +69,7 @@ def iblinter_installed? def filter_git_diff_issues(issues) modified_files_info = git_modified_files_info() return issues.select { |i| - modified_files_info["#{i['file']}"] != nil && modified_files_info["#{i['file']}"].include?(i['line'].to_i) + modified_files_info["#{i['file']}"] != nil } end From 6c3e655f643d05013547a8d763ad1669d4601c53 Mon Sep 17 00:00:00 2001 From: Suraphan Laokondee Date: Wed, 22 Apr 2020 15:35:09 +0700 Subject: [PATCH 6/8] change default line to 1 --- lib/iblinter/plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iblinter/plugin.rb b/lib/iblinter/plugin.rb index 1fac9ba..009144b 100644 --- a/lib/iblinter/plugin.rb +++ b/lib/iblinter/plugin.rb @@ -130,7 +130,7 @@ def send_inline_comment(results, method) dir = "#{Dir.pwd}/" results.each do |r| filename = r["file"].gsub(dir, "") - send(method, r["message"], file: filename, line: 0) + send(method, r["message"], file: filename, line: 1) end end end From ed8059aaab5294931a5fd7b179981cf4c5387481 Mon Sep 17 00:00:00 2001 From: "Suraphan(Rawd) Laokondee" Date: Wed, 22 Apr 2020 16:52:42 +0700 Subject: [PATCH 7/8] Revert "Remove fail exception." This reverts commit 21eefa21c17efa0939207e37be51312e32d46a54. --- lib/iblinter/plugin.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/iblinter/plugin.rb b/lib/iblinter/plugin.rb index 009144b..0e1f31d 100644 --- a/lib/iblinter/plugin.rb +++ b/lib/iblinter/plugin.rb @@ -45,6 +45,12 @@ def lint(path = Dir.pwd, fail_on_warning: false, inline_mode: true, options: {}) message << markdown_issues(warnings, "Warnings", ":warning:") unless warnings.empty? markdown message end + + if errors.count.positive? + fail "Failed due to IBLinter errors" + elsif fail_on_warning && warnings.count.positive? + fail "Failed due to IBLinter warnings" + end end # Instantiate iblinter From 7dde271297daefb819d7d5fb0bc5cb81bad813e2 Mon Sep 17 00:00:00 2001 From: Suraphan Laokondee Date: Thu, 23 Apr 2020 10:09:11 +0700 Subject: [PATCH 8/8] fix frozen --- lib/iblinter/plugin.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iblinter/plugin.rb b/lib/iblinter/plugin.rb index 0e1f31d..7501f1f 100644 --- a/lib/iblinter/plugin.rb +++ b/lib/iblinter/plugin.rb @@ -40,7 +40,7 @@ def lint(path = Dir.pwd, fail_on_warning: false, inline_mode: true, options: {}) send_inline_comment(warnings, fail_on_warning ? :fail : :warn) send_inline_comment(errors, :fail) else - message = "### IBLinter found issues\n\n" + message = "### IBLinter found issues\n\n".dup message << markdown_issues(errors, "Errors", ":rotating_light:") unless errors.empty? message << markdown_issues(warnings, "Warnings", ":warning:") unless warnings.empty? markdown message @@ -118,7 +118,7 @@ def git_modified_lines(file) end def markdown_issues(results, heading, emoji) - message = "#### #{heading}\n\n" + message = "#### #{heading}\n\n".dup message << "| | File | Hint |\n" message << "|---| ---- | -----|\n"