Skip to content

Commit 880a7c0

Browse files
feat: you can now choose a threshold for changed files. Past the threshhold, we analyze all files rather than each file one by one. You can also pass in the --all parameter to get this behaviour, regardless of threshold.
1 parent 235804f commit 880a7c0

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
codeclimate_diff (0.1.5)
4+
codeclimate_diff (0.1.6)
55
colorize
66
json
77
optparse

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
8787
4. Add a `.codecimate_diff.yml` configuration file
8888
```
8989
main_branch_name: master # defaults to main
90+
threshold_to_run_on_all_files: 8 # when you reach a certain number of files changed, it becomes faster to analyze all files rather than analyze them one by one.
9091
```
9192
9293
5. Install the gem
@@ -123,18 +124,23 @@ NOTE: similar code will only work correctly if you run a diff on all the files i
123124
3. Check if you've added any issues (about 10 secs per code file changed on your branch):
124125

125126
```bash
126-
# runs on all code files changed in your branch
127+
# runs on each file changed in your branch (about 10 secs per code file changed on your branch)
127128
./bin/codeclimate_diff
128129
129130
OR
130131
131-
# filters the changed files in your branch futher
132+
# filters the changed files in your branch futher by a grep pattern
132133
./bin/codeclimate_diff --pattern places
133134
134135
OR
135136
136137
# only shows the new and fixed issues
137138
./bin/codeclimate_diff --new-only
139+
140+
OR
141+
142+
# always analyzes all files rather than the changed files one by one, even if below the 'threshold_to_run_on_all_files' setting.
143+
./bin/codeclimate_diff --all
138144
```
139145

140146
4. Now you have time to fix the issues, horray!

exe/codeclimate_diff

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ OptionParser.new do |opts|
1616
opts.on("-n", "--new-only",
1717
"It will only show what you have changed and not existing issues in files you have touched.")
1818

19+
opts.on("-a", "--all",
20+
"It will always analyze all files, and not the changed files one by one, even if below the 'threshold_to_run_on_all_files' setting.")
21+
1922
opts.on("-pPATTERN", "--pattern=PATTERN",
2023
"Grep pattern to filter files. If provided, will filter the files changed on your branch further.")
2124
end.parse!(into: options)
2225

2326
if options[:baseline]
2427
CodeclimateDiff::Runner.generate_baseline
2528
elsif options[:"new-only"]
26-
CodeclimateDiff::Runner.run_diff_on_branch(options[:pattern], show_preexisting: false)
29+
CodeclimateDiff::Runner.run_diff_on_branch(options[:pattern], always_analyze_all_files: options[:all], show_preexisting: false)
2730
else
28-
CodeclimateDiff::Runner.run_diff_on_branch(options[:pattern], show_preexisting: true)
31+
CodeclimateDiff::Runner.run_diff_on_branch(options[:pattern], always_analyze_all_files: options[:all], show_preexisting: true)
2932
end

lib/codeclimate_diff/runner.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,35 @@ def self.calculate_changed_filenames(pattern)
1616
files_changed_str.split("\n")
1717
end
1818

19-
def self.calculate_issues_in_changed_files(changed_filenames)
19+
def self.calculate_issues_in_changed_files(changed_filenames, always_analyze_all_files)
2020
changed_file_issues = []
2121

22-
changed_filenames.each do |filename|
23-
next if filename == "codeclimate_diff.rb" # TODO: fix this file's code quality issues when we make a Gem!
22+
threshold_to_run_on_all_files = CodeclimateDiff.configuration["threshold_to_run_on_all_files"] || 8
23+
analyze_all_files = always_analyze_all_files || changed_filenames.count > threshold_to_run_on_all_files
24+
if analyze_all_files
25+
message = always_analyze_all_files ? "Analyzing all files..." : "The number of changed files is greater than the threshold '#{threshold_to_run_on_all_files}', so analyzing all files..."
26+
puts message
2427

25-
puts "Analysing '#{filename}'..."
26-
result = CodeclimateWrapper.new.run_codeclimate(filename)
28+
result = CodeclimateWrapper.new.run_codeclimate
2729
JSON.parse(result).each do |issue|
2830
next if issue["type"] != "issue"
31+
next unless changed_filenames.include? issue["location"]["path"]
2932

3033
changed_file_issues.append(issue)
3134
end
35+
36+
else
37+
changed_filenames.each do |filename|
38+
next if filename == "codeclimate_diff.rb" # TODO: fix this file's code quality issues when we make a Gem!
39+
40+
puts "Analysing '#{filename}'..."
41+
result = CodeclimateWrapper.new.run_codeclimate(filename)
42+
JSON.parse(result).each do |issue|
43+
next if issue["type"] != "issue"
44+
45+
changed_file_issues.append(issue)
46+
end
47+
end
3248
end
3349

3450
changed_file_issues
@@ -52,12 +68,12 @@ def self.generate_baseline
5268
puts "Done!"
5369
end
5470

55-
def self.run_diff_on_branch(pattern, show_preexisting: true)
71+
def self.run_diff_on_branch(pattern, always_analyze_all_files: false, show_preexisting: true)
5672
CodeclimateWrapper.new.pull_latest_image
5773

5874
changed_filenames = calculate_changed_filenames(pattern)
5975

60-
changed_file_issues = calculate_issues_in_changed_files(changed_filenames)
76+
changed_file_issues = calculate_issues_in_changed_files(changed_filenames, always_analyze_all_files)
6177

6278
preexisting_issues = calculate_preexisting_issues_in_changed_files(changed_filenames)
6379

lib/codeclimate_diff/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module CodeclimateDiff
4-
VERSION = "0.1.5"
4+
VERSION = "0.1.6"
55
end

0 commit comments

Comments
 (0)