Class: CodeclimateDiff::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/codeclimate_diff/runner.rb

Class Method Summary collapse

Class Method Details

.calculate_changed_filenames(pattern) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/codeclimate_diff/runner.rb', line 13

def self.calculate_changed_filenames(pattern)
  extra_grep_filter = pattern ? " | grep '#{pattern}'" : ""
  branch_name = CodeclimateDiff.configuration["main_branch_name"] || "main"
  all_files_changed_str = `git diff --name-only #{branch_name} | grep --extended-regexp '.js$|.rb$'#{extra_grep_filter}`
  all_files_changed = all_files_changed_str.split("\n")
                                           .filter { |filename| File.exist?(filename) }

  # load the exclude patterns list from .codeclimate.yml
  exclude_patterns = []
  if File.exist?(".codeclimate.yml")
    config = YAML.load_file(".codeclimate.yml")
    exclude_patterns = config["exclude_patterns"]
  end

  files_and_directories_excluded = exclude_patterns.map { |exclude_pattern| Dir.glob(exclude_pattern) }.flatten

  # filter out any files that match the excluded ones
  all_files_changed.filter do |filename|
    next if files_and_directories_excluded.include? filename

    next if files_and_directories_excluded.any? { |excluded_filename| filename.start_with?(excluded_filename) }

    true
  end
end

.calculate_issues_in_changed_files(changed_filenames, always_analyze_all_files) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/codeclimate_diff/runner.rb', line 39

def self.calculate_issues_in_changed_files(changed_filenames, always_analyze_all_files)
  changed_file_issues = []

  threshold_to_run_on_all_files = CodeclimateDiff.configuration["threshold_to_run_on_all_files"] || 8
  analyze_all_files = always_analyze_all_files || changed_filenames.count > threshold_to_run_on_all_files
  if analyze_all_files
    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..."
    puts message

    result = CodeclimateWrapper.new.run_codeclimate
    JSON.parse(result).each do |issue|
      next if issue["type"] != "issue"
      next unless changed_filenames.include? issue["location"]["path"]

      changed_file_issues.append(issue)
    end

  else
    changed_filenames.each do |filename|
      puts "Analysing '#{filename}'..."
      result = CodeclimateWrapper.new.run_codeclimate(filename)
      JSON.parse(result).each do |issue|
        next if issue["type"] != "issue"

        changed_file_issues.append(issue)
      end
    end
  end

  changed_file_issues
end

.calculate_preexisting_issues_in_changed_files(changed_filenames) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/codeclimate_diff/runner.rb', line 71

def self.calculate_preexisting_issues_in_changed_files(changed_filenames)
  Downloader.refresh_baseline_if_configured

  puts "Extracting relevant preexisting issues..."
  all_issues = JSON.parse(File.read("./codeclimate_diff_baseline.json"))

  all_issues.filter { |issue| issue.key?("location") && changed_filenames.include?(issue["location"]["path"]) }
end

.generate_baselineObject



80
81
82
83
84
85
86
87
# File 'lib/codeclimate_diff/runner.rb', line 80

def self.generate_baseline
  CodeclimateWrapper.new.pull_latest_image

  puts "Generating the baseline.  Should take about 5 minutes..."
  result = CodeclimateWrapper.new.run_codeclimate
  File.write("codeclimate_diff_baseline.json", result)
  puts "Done!"
end

.run_diff_on_branch(pattern, always_analyze_all_files: false, show_preexisting: true) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/codeclimate_diff/runner.rb', line 89

def self.run_diff_on_branch(pattern, always_analyze_all_files: false, show_preexisting: true)
  CodeclimateWrapper.new.pull_latest_image

  changed_filenames = calculate_changed_filenames(pattern)

  changed_file_issues = calculate_issues_in_changed_files(changed_filenames, always_analyze_all_files)

  preexisting_issues = calculate_preexisting_issues_in_changed_files(changed_filenames)

  sorted_issues = IssueSorter.sort_issues(preexisting_issues, changed_file_issues)

  ResultPrinter.print_result(sorted_issues, show_preexisting)
  ResultPrinter.print_call_to_action(sorted_issues)
end