Commit 1ac4ec1b authored by Rémy Coutable's avatar Rémy Coutable

Allow 'static-analysis' to be parallelized generically

Signed-off-by: default avatarRémy Coutable <remy@rymai.me>
parent 7180b0ab
...@@ -5,75 +5,49 @@ require_relative '../lib/gitlab' ...@@ -5,75 +5,49 @@ require_relative '../lib/gitlab'
require_relative '../lib/gitlab/popen' require_relative '../lib/gitlab/popen'
require_relative '../lib/gitlab/popen/runner' require_relative '../lib/gitlab/popen/runner'
def emit_warnings(static_analysis) class StaticAnalysis
static_analysis.warned_results.each do |result| ALLOWED_WARNINGS = [
puts
puts "**** #{result.cmd.join(' ')} had the following warning(s):"
puts
puts result.stderr
puts
end
end
def emit_errors(static_analysis)
static_analysis.failed_results.each do |result|
puts
puts "**** #{result.cmd.join(' ')} failed with the following error(s):"
puts
puts result.stdout
puts result.stderr
puts
end
end
ALLOWED_WARNINGS = [
# https://github.com/browserslist/browserslist/blob/d0ec62eb48c41c218478cd3ac28684df051cc865/node.js#L329 # https://github.com/browserslist/browserslist/blob/d0ec62eb48c41c218478cd3ac28684df051cc865/node.js#L329
# warns if caniuse-lite package is older than 6 months. Ignore this # warns if caniuse-lite package is older than 6 months. Ignore this
# warning message so that GitLab backports don't fail. # warning message so that GitLab backports don't fail.
"Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade`" "Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade`"
].freeze ].freeze
def warning_count(static_analysis) # `gettext:updated_check` and `gitlab:sidekiq:sidekiq_queues_yml:check` will fail on FOSS installations
static_analysis.warned_results # (e.g. gitlab-org/gitlab-foss) since they test against a single
.count { |result| !ALLOWED_WARNINGS.include?(result.stderr.strip) } # file that is generated by an EE installation, which can
end # contain values that a FOSS installation won't find. To work
# around this we will only enable this task on EE installations.
def jobs_to_run(node_index, node_total) TASKS_BY_DURATIONS_SECONDS_DESC = {
all_tasks = [ %w[bin/rake lint:haml] => 338,
%w[bin/rake lint:all], (Gitlab.ee? ? %w[bin/rake gettext:updated_check] : nil) => 308,
%w[bundle exec license_finder], # Most of the time, RuboCop finishes in 30 seconds, but sometimes it can take around 1200 seconds so we set a
%w[yarn run eslint], # duration of 300 to lower the likelihood that it will run in the same job as another long task...
%w[yarn run stylelint], %w[bundle exec rubocop --parallel] => 300,
%w[yarn run prettier-all], %w[yarn run eslint] => 197,
%w[yarn run block-dependencies], %w[yarn run prettier-all] => 124,
%w[bundle exec rubocop --parallel], %w[bin/rake gettext:lint] => 96,
%w[scripts/lint-conflicts.sh], %w[bundle exec license_finder] => 49,
%w[scripts/lint-rugged], %w[bin/rake scss_lint] => 38,
%w[scripts/frontend/check_no_partial_karma_jest.sh], %w[bin/rake lint:static_verification] => 22,
%w[scripts/lint-changelog-filenames], %w[bin/rake gitlab:sidekiq:all_queues_yml:check] => 13,
%w[scripts/gemfile_lock_changed.sh] (Gitlab.ee? ? %w[bin/rake gitlab:sidekiq:sidekiq_queues_yml:check] : nil) => 13,
] %w[bin/rake config_lint] => 11,
%w[yarn run stylelint] => 9,
case node_total %w[scripts/lint-conflicts.sh] => 0.59,
when 1 %w[yarn run block-dependencies] => 0.35,
all_tasks %w[scripts/lint-rugged] => 0.23,
when 2 %w[scripts/gemfile_lock_changed.sh] => 0.02,
rake_lint_all, *rest_jobs = all_tasks %w[scripts/frontend/check_no_partial_karma_jest.sh] => 0.01,
case node_index %w[scripts/lint-changelog-filenames] => 0.01
when 1 }.reject { |k| k.nil? }.sort_by { |a| -a[1] }.to_h.keys.freeze
[rake_lint_all]
else def run_tasks!
rest_jobs tasks = tasks_to_run((ENV['CI_NODE_INDEX'] || 1).to_i, (ENV['CI_NODE_TOTAL'] || 1).to_i)
end
else static_analysis = Gitlab::Popen::Runner.new
raise "Parallelization > 2 (currently set to #{node_total}) isn't supported yet!"
end static_analysis.run(tasks) do |cmd, &run|
end
tasks = jobs_to_run((ENV['CI_NODE_INDEX'] || 1).to_i, (ENV['CI_NODE_TOTAL'] || 1).to_i)
static_analysis = Gitlab::Popen::Runner.new
static_analysis.run(tasks) do |cmd, &run|
puts puts
puts "$ #{cmd.join(' ')}" puts "$ #{cmd.join(' ')}"
...@@ -81,27 +55,68 @@ static_analysis.run(tasks) do |cmd, &run| ...@@ -81,27 +55,68 @@ static_analysis.run(tasks) do |cmd, &run|
puts "==> Finished in #{result.duration} seconds" puts "==> Finished in #{result.duration} seconds"
puts puts
end end
puts puts
puts '===================================================' puts '==================================================='
puts puts
puts puts
if static_analysis.all_success_and_clean? if static_analysis.all_success_and_clean?
puts 'All static analyses passed successfully.' puts 'All static analyses passed successfully.'
elsif static_analysis.all_success? elsif static_analysis.all_success?
puts 'All static analyses passed successfully, but we have warnings:' puts 'All static analyses passed successfully, but we have warnings:'
puts puts
emit_warnings(static_analysis) emit_warnings(static_analysis)
exit 2 if warning_count(static_analysis).nonzero? exit 2 if warning_count(static_analysis).nonzero?
else else
puts 'Some static analyses failed:' puts 'Some static analyses failed:'
emit_warnings(static_analysis) emit_warnings(static_analysis)
emit_errors(static_analysis) emit_errors(static_analysis)
exit 1 exit 1
end
end
def emit_warnings(static_analysis)
static_analysis.warned_results.each do |result|
puts
puts "**** #{result.cmd.join(' ')} had the following warning(s):"
puts
puts result.stderr
puts
end
end
def emit_errors(static_analysis)
static_analysis.failed_results.each do |result|
puts
puts "**** #{result.cmd.join(' ')} failed with the following error(s):"
puts
puts result.stdout
puts result.stderr
puts
end
end
def warning_count(static_analysis)
static_analysis.warned_results
.count { |result| !ALLOWED_WARNINGS.include?(result.stderr.strip) }
end
def tasks_to_run(node_index, node_total)
tasks = []
TASKS_BY_DURATIONS_SECONDS_DESC.each_with_index do |task, i|
tasks << task if i % node_total == (node_index - 1)
end
tasks
end
end
if $0 == __FILE__
StaticAnalysis.new.run_tasks!
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment