4
4
require "colorize"
5
5
require_relative "./codeclimate_wrapper"
6
6
require_relative "./result_printer"
7
+ require_relative "./issue_sorter"
7
8
8
9
module CodeclimateDiff
9
10
class Runner
10
-
11
-
12
11
def self . calculate_changed_filenames ( pattern )
13
12
extra_grep_filter = pattern ? " | grep '#{ pattern } '" : ""
14
13
files_changed = `git diff --name-only main | grep --invert-match spec/ | grep --extended-regexp '.js$|.rb$'#{ extra_grep_filter } `
@@ -40,76 +39,6 @@ def self.calculate_preexisting_issues_in_changed_files(changed_filenames)
40
39
all_issues . filter { |issue | issue . key? ( "location" ) && changed_filenames . include? ( issue [ "location" ] [ "path" ] ) }
41
40
end
42
41
43
- def self . remove_closest_match_from_list ( issue_to_match , list )
44
- # check for exact match first
45
- index = list . index do |issue |
46
- issue [ "fingerprint" ] == issue_to_match [ "fingerprint" ] &&
47
- issue [ "location" ] [ "lines" ] [ "begin" ] == issue_to_match [ "location" ] [ "lines" ] [ "begin" ] &&
48
- issue [ "description" ] == issue_to_match [ "description" ]
49
- end
50
-
51
- if index
52
- list . delete_at ( index )
53
- return
54
- end
55
-
56
- # check for same method name (description often has method name or variable name in it)
57
- index = list . index do |issue |
58
- issue [ "fingerprint" ] == issue_to_match [ "fingerprint" ] &&
59
- issue [ "description" ] == issue_to_match [ "description" ]
60
- end
61
-
62
- if index
63
- list . delete_at ( index )
64
- return
65
- end
66
-
67
- # otherwise just remove the first one
68
- list . pop
69
- end
70
-
71
- def self . sort_issues ( preexisting_issues , changed_file_issues )
72
- puts "Sorting into :preexisting, :new and :fixed lists..."
73
-
74
- result = { }
75
- result [ :preexisting ] = [ ]
76
- result [ :new ] = [ ]
77
- result [ :fixed ] = [ ]
78
-
79
- # fingerprints are unique per issue type and file
80
- # so there could be multiple if the same issue shows up multiple times
81
- # plus line numbers and method names could have changed
82
- unique_fingerprints = ( preexisting_issues + changed_file_issues ) . map { |issue | issue [ "fingerprint" ] } . uniq
83
-
84
- unique_fingerprints . each do |fingerprint |
85
- baseline_issues = preexisting_issues . filter { |issue | issue [ "fingerprint" ] == fingerprint }
86
- current_issues = changed_file_issues . filter { |issue | issue [ "fingerprint" ] == fingerprint }
87
-
88
- if baseline_issues . count == current_issues . count
89
- # current issues are most up to date (line numbers could have changed etc.)
90
- result [ :preexisting ] += current_issues
91
- elsif current_issues . count < baseline_issues . count
92
- # less issues than there were before
93
- current_issues . each do |issue_to_match |
94
- CodeclimateDiff . remove_closest_match_from_list ( issue_to_match , baseline_issues )
95
- end
96
- result [ :fixed ] += baseline_issues
97
- else
98
- # more issues than there were before
99
- baseline_issues . each do |issue_to_match |
100
- CodeclimateDiff . remove_closest_match_from_list ( issue_to_match , current_issues )
101
- end
102
- result [ :new ] += current_issues
103
- end
104
- end
105
-
106
- # do a check to make sure the maths works out
107
- puts "#{ preexisting_issues . count } issues in matching files in baseline"
108
- puts "#{ changed_file_issues . count } current issues in matching files"
109
-
110
- result
111
- end
112
-
113
42
def self . generate_baseline
114
43
puts "Generating the baseline. Should take about 5 minutes..."
115
44
result = CodeclimateWrapper . new . run_codeclimate
@@ -124,7 +53,7 @@ def self.run_diff_on_branch(pattern, show_preexisting: true)
124
53
125
54
preexisting_issues = calculate_preexisting_issues_in_changed_files ( changed_filenames )
126
55
127
- sorted_issues = sort_issues ( preexisting_issues , changed_file_issues )
56
+ sorted_issues = IssueSorter . sort_issues ( preexisting_issues , changed_file_issues )
128
57
129
58
ResultPrinter . print_result ( sorted_issues , show_preexisting )
130
59
ResultPrinter . print_call_to_action ( sorted_issues )
0 commit comments