# frozen_string_literal: true module NeetoCompliance class SemaphoreciConfigVerifier < Base def local_copy ".semaphore/semaphore.yml" end def auto_correct! end def semaphore_config @_semaphore_config ||= YAML.load(File.open(local_copy).read, aliases: true) end def my_container_setup_commands semaphore_config["global_job_config"]["prologue"]["commands"] end def my_jobs_config semaphore_config["blocks"].map { |block| block["task"]["jobs"] } end def my_env_vars_config semaphore_config["global_job_config"]["env_vars"] end def my_after_pipeline_jobs semaphore_config["after_pipeline"]["task"] end def my_machine_type semaphore_config["agent"]["machine"]["type"] end def machine_type "e1-standard-2" end def container_setup_commands [ "checkout", "sem-version ruby 3.1.2", "sem-version node 18.12", "sem-service start postgres 13", "sem-service start redis", "bundle config path 'vendor/bundle'", "cp config/database.yml.ci config/database.yml", "bundle install --jobs 1", ] end def jobs_config [ [ { "name" => "Checks", "commands" => [ "bundle exec ruby-audit check", "bundle exec rubocop", "bundle exec erblint --lint-all --format compact", "curl -s -L \"https://raw.githubusercontent.com/bigbinary/wheel/main/.semaphore/commands/run_eslint_on_modified_files.sh\" | bash", "bundle exec neeto-audit", "bundle exec rake db:create db:schema:load --trace", "bundle exec rake incinerator:check_for_missing_models", "bundle exec rails test", "bundle exec rake simplecov_coverage:publish", "test-results publish ./test/reports", "bundle exec rake setup" ] } ] ] end def env_vars_config [ { "name" => "TZ", "value" => "UTC" }, { "name" => "RAILS_ENV", "value" => "test" }, { "name" => "RACK_ENV", "value" => "test" } ] end def after_pipeline_jobs { "jobs" => [ { "name" => "Publish Results", "commands" => ["test-results gen-pipeline-report"] } ] } end def standard_semaphore_config_file NeetoCompliance::NeetoCommons.path.join "common_files/semaphore/semaphore.yml" end def semaphore_config_valid? @semaphore_errors = [] @no_errors = true begin container_setup_commands.each do |cmd| unless my_container_setup_commands.include?(cmd) @semaphore_errors << "Missing: \"#{cmd}\" in global_job_config" @no_errors = false end end unless jobs_config == my_jobs_config @semaphore_errors << "Blocks configuration should be same as in #{standard_semaphore_config_file}" @no_errors = false end unless env_vars_config == my_env_vars_config @semaphore_errors << "Env vars configuration should be same as in #{standard_semaphore_config_file}" @no_errors = false end unless after_pipeline_jobs == my_after_pipeline_jobs @semaphore_errors << "after_pipeline jobs configuration should be same as in #{standard_semaphore_config_file}" @no_errors = false end unless machine_type == my_machine_type @semaphore_errors << "Machine type should be #{machine_type}" @no_errors = false end rescue => e p e return false end @no_errors end def autofix_suggestion @error.yellow end def valid? unless semaphore_config_valid? @error = %{SemaphoreCI configuration (.semaphore/semaphore.yml) is incomplete/incorrect. \n#{@semaphore_errors.join("\n")} \n} return false end true end end end