2017-04-28 11:31:18 -04:00
|
|
|
#!/usr/bin/env ruby
|
2021-02-12 07:09:02 -05:00
|
|
|
# frozen_string_literal: true
|
2017-04-28 11:31:18 -04:00
|
|
|
|
2018-01-23 07:12:51 -05:00
|
|
|
# We don't have auto-loading here
|
2020-03-18 20:09:27 -04:00
|
|
|
require_relative '../lib/gitlab'
|
2018-01-23 07:12:51 -05:00
|
|
|
require_relative '../lib/gitlab/popen'
|
|
|
|
require_relative '../lib/gitlab/popen/runner'
|
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
class StaticAnalysis
|
|
|
|
ALLOWED_WARNINGS = [
|
|
|
|
# https://github.com/browserslist/browserslist/blob/d0ec62eb48c41c218478cd3ac28684df051cc865/node.js#L329
|
|
|
|
# warns if caniuse-lite package is older than 6 months. Ignore this
|
|
|
|
# warning message so that GitLab backports don't fail.
|
|
|
|
"Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade`"
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
# `gettext:updated_check` and `gitlab:sidekiq:sidekiq_queues_yml:check` will fail on FOSS installations
|
|
|
|
# (e.g. gitlab-org/gitlab-foss) since they test against a single
|
|
|
|
# file that is generated by an EE installation, which can
|
|
|
|
# contain values that a FOSS installation won't find. To work
|
|
|
|
# around this we will only enable this task on EE installations.
|
|
|
|
TASKS_BY_DURATIONS_SECONDS_DESC = {
|
|
|
|
%w[bin/rake lint:haml] => 338,
|
|
|
|
(Gitlab.ee? ? %w[bin/rake gettext:updated_check] : nil) => 308,
|
|
|
|
# Most of the time, RuboCop finishes in 30 seconds, but sometimes it can take around 1200 seconds so we set a
|
|
|
|
# duration of 300 to lower the likelihood that it will run in the same job as another long task...
|
|
|
|
%w[bundle exec rubocop --parallel] => 300,
|
|
|
|
%w[yarn run eslint] => 197,
|
|
|
|
%w[yarn run prettier-all] => 124,
|
|
|
|
%w[bin/rake gettext:lint] => 96,
|
|
|
|
%w[bundle exec license_finder] => 49,
|
|
|
|
%w[bin/rake scss_lint] => 38,
|
|
|
|
%w[bin/rake lint:static_verification] => 22,
|
|
|
|
%w[bin/rake gitlab:sidekiq:all_queues_yml:check] => 13,
|
|
|
|
(Gitlab.ee? ? %w[bin/rake gitlab:sidekiq:sidekiq_queues_yml:check] : nil) => 13,
|
|
|
|
%w[bin/rake config_lint] => 11,
|
|
|
|
%w[yarn run stylelint] => 9,
|
|
|
|
%w[scripts/lint-conflicts.sh] => 0.59,
|
|
|
|
%w[yarn run block-dependencies] => 0.35,
|
|
|
|
%w[scripts/lint-rugged] => 0.23,
|
|
|
|
%w[scripts/gemfile_lock_changed.sh] => 0.02,
|
|
|
|
%w[scripts/frontend/check_no_partial_karma_jest.sh] => 0.01,
|
|
|
|
%w[scripts/lint-changelog-filenames] => 0.01
|
|
|
|
}.reject { |k| k.nil? }.sort_by { |a| -a[1] }.to_h.keys.freeze
|
|
|
|
|
|
|
|
def run_tasks!
|
|
|
|
tasks = tasks_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 "$ #{cmd.join(' ')}"
|
|
|
|
|
|
|
|
result = run.call
|
|
|
|
|
|
|
|
puts "==> Finished in #{result.duration} seconds"
|
|
|
|
puts
|
|
|
|
end
|
2018-01-23 07:12:51 -05:00
|
|
|
|
|
|
|
puts
|
2020-05-28 14:08:37 -04:00
|
|
|
puts '==================================================='
|
2018-01-23 07:12:51 -05:00
|
|
|
puts
|
|
|
|
puts
|
2017-04-28 11:31:18 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
if static_analysis.all_success_and_clean?
|
|
|
|
puts 'All static analyses passed successfully.'
|
|
|
|
elsif static_analysis.all_success?
|
|
|
|
puts 'All static analyses passed successfully, but we have warnings:'
|
|
|
|
puts
|
2020-01-05 04:08:10 -05:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
emit_warnings(static_analysis)
|
2020-01-05 04:08:10 -05:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
exit 2 if warning_count(static_analysis).nonzero?
|
2019-10-31 17:06:28 -04:00
|
|
|
else
|
2020-05-28 14:08:37 -04:00
|
|
|
puts 'Some static analyses failed:'
|
2017-04-28 11:31:18 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
emit_warnings(static_analysis)
|
|
|
|
emit_errors(static_analysis)
|
2017-04-28 11:31:18 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
2017-04-28 11:31:18 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
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
|
2017-11-03 12:00:49 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
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
|
2018-01-23 07:12:51 -05:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
def warning_count(static_analysis)
|
|
|
|
static_analysis.warned_results
|
|
|
|
.count { |result| !ALLOWED_WARNINGS.include?(result.stderr.strip) }
|
|
|
|
end
|
2018-01-23 07:12:51 -05:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
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
|
2017-04-28 11:31:18 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
tasks
|
|
|
|
end
|
|
|
|
end
|
2017-04-28 11:31:18 -04:00
|
|
|
|
2020-05-28 14:08:37 -04:00
|
|
|
if $0 == __FILE__
|
|
|
|
StaticAnalysis.new.run_tasks!
|
2017-04-28 11:31:18 -04:00
|
|
|
end
|