Skip to content

Commit

Permalink
[WIP] Use up to date linter configs
Browse files Browse the repository at this point in the history
Right now, what ever is on the "fetched master" for the linter configs
will be what is used by Pronto, and not what is up to date with the
branch being checked against.

This "attempts" to generate a config based on the git blob for the
branch we are checking, but it is still not quite working.
  • Loading branch information
NickLaMuro committed Sep 11, 2020
1 parent 8c70bca commit 5331ffc
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions app/workers/concerns/code_analysis_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require 'pronto/git/patch'
require 'pronto/git/line'

require 'tempfile'

require 'fileutils'
require 'tmpdir'

Expand Down Expand Up @@ -33,6 +35,7 @@ def pronto_result
Pronto::GemNames.new.to_a.each { |gem_name| require "pronto/#{gem_name}" }

branch.repo.git_fetch
generate_linter_configs

git_service = branch.git_service
merge_base = git_service.merge_base
Expand All @@ -42,6 +45,55 @@ def pronto_result
Pronto::Runners.new.run(pronto_patches)
end

# configs in the repo itself most likely will be invalid as the what the
# `MinigitService` has checked out in `.repos/` probably isn't the same SHA
# as what we are working with.
#
# Any specifics per linter that need to be adjusted need to be done here, but
# RuboCop might be the only one that is necessary for now.
#
def generate_linter_configs
# Ensure any tempfiles created here are kept around through
# `.run_all_linters`, and will be unlinked there.
@config_tempfiles = []

# This is config option found in the `proto-rubocop` plugin README:
#
# > You can also specify a custom .rubocop.yml location with the
# > environment variable RUBOCOP_CONFIG.
#
git_service = branch.git_service
rubocop_config_contents = git_service.content_at(".rubocop.yml")
yamllint_config_contents = git_service.content_at(".yamllint")

if rubocop_config_contents
rubocop_yaml_data = YAML.load(rubocop_config_contents)
(rubocop_yaml_data.delete("inherit_from") || []).each do |local_config_path|
next unless inherit_from_data = git_service.content_at(local_config_path)

rubocop_yaml_data["inherit_from"] ||= []
rubocop_yaml_data["inherit_from"] << generate_temp_config(local_config_path, inherit_from_data)
end

ENV["RUBOCOP_CONFIG"] = generate_temp_config(".rubocop.yml", rubocop_yaml_data.to_yaml)
end

if yamllint_config_contents
yamllint_config_path = generate_temp_config(".yamllint", yamllint_config_contents)
ENV["YAMLLINT_OPTS"] = "-c #{yamllint_config_path}"
end
end

def generate_temp_config(config_filepath, file_contents)
temp_config = Tempfile.new(config_filepath)
temp_config.write file_contents
temp_config.close

@config_tempfiles << temp_config

temp_config.path
end

def run_all_linters
pronto_result.group_by(&:runner).values.map do |linted| # group by linter
output = {}
Expand Down Expand Up @@ -70,6 +122,8 @@ def run_all_linters
end
rescue RuboCop::ValidationError => error
[failed_linter_offenses("#{self.class.name} STDERR:\n```\n#{error.message}\n```")]
ensure
@config_tempfiles.each(&:unlink)
end

def failed_linter_offenses(message)
Expand Down

0 comments on commit 5331ffc

Please sign in to comment.