Skip to content

Commit

Permalink
Pronto integration
Browse files Browse the repository at this point in the history
Integration of pronto required to add few new gems and to update some of
the gems. The old method which provided the linter launching was
reworked to launch pronto which provide the linter launching. The output
of pronto is the result of all linters. The pronto result is transformed
into a structure which match the original one. This integration required
to change few tests and add new ones.

Closes #192
  • Loading branch information
europ authored and NickLaMuro committed May 18, 2020
1 parent 85833c8 commit f61fda6
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 2,777 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ gem 'octokit', '~> 4.8.0', :require => false
gem 'faraday', '~> 0.9.2'
gem 'faraday-http-cache', '~> 2.0.0'

gem 'pronto', '~> 0.9.5', :require => false
gem 'pronto-haml', '~> 0.9.0', :require => false
gem 'pronto-rubocop', :require => false
gem 'pronto-yamllint', :require => false

group :development, :test do
gem 'rspec'
gem 'rspec-rails'
Expand Down
36 changes: 34 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ GEM
multi_json (~> 1.0)
net-http-persistent (~> 2.9)
net-http-pipeline
gitlab (4.14.1)
httparty (~> 0.14, >= 0.14.0)
terminal-table (~> 1.5, >= 1.5.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
haml (5.1.2)
Expand All @@ -176,6 +179,9 @@ GEM
sysexits (~> 1.1)
hashdiff (1.0.0)
highline (1.7.10)
httparty (0.18.0)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
i18n (1.8.2)
concurrent-ruby (~> 1.0)
ice_cube (0.14.0)
Expand All @@ -201,6 +207,9 @@ GEM
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.2)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.0425)
mimemagic (0.3.4)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
Expand All @@ -209,6 +218,7 @@ GEM
more_core_extensions (4.0.0)
activesupport
multi_json (1.14.1)
multi_xml (0.6.0)
multipart-post (2.1.1)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
Expand All @@ -223,6 +233,21 @@ GEM
parser (2.7.1.2)
ast (~> 2.4.0)
pg (1.2.2)
pronto (0.9.5)
gitlab (~> 4.0, >= 4.0.0)
httparty (>= 0.13.7)
octokit (~> 4.7, >= 4.7.0)
rainbow (~> 2.1)
rugged (~> 0.24, >= 0.23.0)
thor (~> 0.19.0)
pronto-haml (0.9.0)
haml_lint (~> 0.23)
pronto (~> 0.9.0)
pronto-rubocop (0.9.1)
pronto (~> 0.9.0)
rubocop (~> 0.50, >= 0.49.1)
pronto-yamllint (0.1.1)
pronto (~> 0.9.0)
pry (0.9.12.6)
coderay (~> 1.0)
method_source (~> 0.8)
Expand Down Expand Up @@ -259,7 +284,8 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
rainbow (2.2.2)
rake
rake (12.3.3)
rb-fsevent (0.10.3)
rb-inotify (0.10.1)
Expand Down Expand Up @@ -350,11 +376,13 @@ GEM
sprockets (>= 3.0.0)
sysexits (1.2.0)
temple (0.8.2)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
thin (1.7.2)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (>= 1, < 3)
thor (1.0.1)
thor (0.19.4)
thread_safe (0.3.6)
tilt (2.0.10)
timecop (0.9.1)
Expand Down Expand Up @@ -428,6 +456,10 @@ DEPENDENCIES
more_core_extensions (~> 4.0.0)
octokit (~> 4.8.0)
pg
pronto (~> 0.9.5)
pronto-haml (~> 0.9.0)
pronto-rubocop
pronto-yamllint
rails (~> 5.2.2)
rspec
rspec-rails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def offenses
SEVERITY_MAP[o["severity"]],
format_message(o),
f["path"],
format_locator(f, o)
format_line(f, o)
)
end
end.flatten
Expand All @@ -96,22 +96,13 @@ def format_cop_name(cop_name)
COP_URIS[cop_name] || cop_name
end

def format_locator(file, offense)
[format_line(file, offense), format_column(offense)].compact.join(", ").presence
end

def format_line(file, offense)
line = offense.fetch_path("location", "line")
line = offense.fetch_path("line")
return nil unless line
uri = File.join(line_uri, "blob", commits.last, file["path"]) << "#L#{line}"
"[Line #{line}](#{uri})"
end

def format_column(offense)
column = offense.fetch_path("location", "column")
column && "Col #{column}"
end

# TODO: Don't reuse the commit_uri. This should probably be its own URI.
def line_uri
branch.commit_uri.chomp("commit/$commit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def filter_on_diff
@results["files"].each do |f|
f["offenses"].select! do |o|
o["severity"].in?(%w(error fatal)) ||
@diff_details[f["path"]].include?(o["location"]["line"])
@diff_details[f["path"]].include?(o["line"])
end
end
end
Expand Down
62 changes: 57 additions & 5 deletions app/workers/concerns/code_analysis_mixin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
require 'pronto/runners'
require 'pronto/rubocop'
require 'pronto/yamllint'
require 'pronto/haml'
require 'pronto/git/repository'
require 'pronto/git/patches'
require 'pronto/git/patch'
require 'pronto/git/line'

require 'fileutils'
require 'tmpdir'

module CodeAnalysisMixin
def merged_linter_results
results = {
Expand All @@ -19,11 +31,51 @@ def merged_linter_results
results
end

# run linters via pronto and return the pronto result
def pronto_result
p_result = nil

# temporary solution for: download repo, obtain changes, get pronto result about changes
Dir.mktmpdir do |dir|
FileUtils.copy_entry(@branch.repo.path.to_s, dir)
repo = Pronto::Git::Repository.new(dir)
rg = repo.instance_variable_get(:@repo)
rg.fetch('origin', @branch.name.sub(/^prs/, 'pull'))
rg.checkout('FETCH_HEAD')
rg.reset('HEAD', :hard)
patches = repo.diff(@branch.merge_target)
p_result = Pronto::Runners.new.run(patches)
end

p_result
end

def run_all_linters
unmerged_results = []
unmerged_results << Linter::Rubocop.new(branch).run
unmerged_results << Linter::Haml.new(branch).run
unmerged_results << Linter::Yaml.new(branch).run
unmerged_results.tap(&:compact!)
pronto_result.group_by(&:runner).values.map do |linted| # group by linter
output = {}

output["files"] = linted.group_by(&:path).map do |path, value| # group by file in linter
{
"path" => path,
"offenses" => value.map do |msg| # put offenses of file in linter into an array
{
"severity" => msg.level.to_s,
"message" => msg.msg,
"cop_name" => msg.runner,
"corrected" => false,
"line" => msg.line.position
}
end
}
end

output["summary"] = {
"offense_count" => output["files"].sum { |item| item['offenses'].length },
"target_file_count" => output["files"].length,
"inspected_file_count" => 0 # TODO: value cannot be obtained from the result of `pronto_result` method
}

output
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
{
"severity": "error",
"message": "You don't need to use \"- end\" in Haml. Un-indent to close a block:\n- if foo?\n %strong Foo!\n- else\n Not foo.\n%p This line is un-indented, so it isn't part of the \"if\" block",
"location": {
"line": 3
},
"line": 3,
"linter_name": "Syntax"
}
]
Expand All @@ -26,4 +24,4 @@
"target_file_count": 1,
"inspected_file_count": 1
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,23 @@
"cop_name": "Style/MutableConstant",
"corrected": false,
"correctable": true,
"location": {
"start_line": 2,
"start_column": 10,
"last_line": 6,
"last_column": 3,
"length": 78,
"line": 2,
"column": 10
}
"line": 2
},
{
"severity": "convention",
"message": "Align the keys and values of a hash literal if they span more than one line.",
"cop_name": "Layout/HashAlignment",
"corrected": false,
"correctable": true,
"location": {
"start_line": 3,
"start_column": 5,
"last_line": 3,
"last_column": 20,
"length": 16,
"line": 3,
"column": 5
}
"line": 3
},
{
"severity": "convention",
"message": "Align the keys and values of a hash literal if they span more than one line.",
"cop_name": "Layout/HashAlignment",
"corrected": false,
"correctable": true,
"location": {
"start_line": 4,
"start_column": 5,
"last_line": 4,
"last_column": 22,
"length": 18,
"line": 4,
"column": 5
}
"line": 4
}
]
}
Expand All @@ -66,4 +42,4 @@
"target_file_count": 1,
"inspected_file_count": 1
}
}
}
Loading

0 comments on commit f61fda6

Please sign in to comment.