Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 7 additions & 48 deletions lib/manageiq/cross_repo/runner/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,17 @@ def initialize(script_cmd = nil)
end

def build_test_script
load_config!
build_script
end

private

def environment_setup_commands
commands = []

if config["node_js"]
commands << "source ~/.nvm/nvm.sh"
commands += Array(config["node_js"]).map do |node_version|
"nvm install #{node_version}"
end
end

commands.any? ? build_section("environment", *commands) : commands
end
<<~BASH_SCRIPT
#!/bin/bash

def section_commands
sections = %w[before_install install before_script script]
sections.flat_map do |section|
commands = build_section_commands(section)
build_section(section, *commands) if commands.present?
end.compact
#{commands.join("\n")}
BASH_SCRIPT
end

def build_commands
environment_setup_commands + section_commands
end
private

def build_section_commands(section)
# Travis sections can have a single command or an array of commands
Array(config[section]).map { |cmd| "#{cmd} || exit $?" }
def commands
raise NotImplementedError, "must be implemented in a subclass"
end

def build_section(section, *commands)
Expand All @@ -56,25 +33,7 @@ def build_section(section, *commands)
]
end

def build_script
<<~BASH_SCRIPT
#!/bin/bash

#{build_commands.join("\n")}
BASH_SCRIPT
end

def load_config!
ci_config.tap do |config|
# Set missing sections to the proper defaults
config["install"] ||= defaults[config["language"]]["install"]

config["script"] = script_cmd if script_cmd.present?
config["script"] ||= defaults[config["language"]]["script"]
end
end

def ci_config
raise NotImplementedError, "must be implemented in a subclass"
end

Expand Down
37 changes: 24 additions & 13 deletions lib/manageiq/cross_repo/runner/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,32 @@ def self.available?

private

def ci_config
github_config = YAML.load_file(CONFIG_FILE)

steps = github_config["jobs"]["ci"]["steps"]
steps_by_name = steps.index_by { |step| step["name"] }

language = steps.any? { |s| s["uses"] == "ruby/setup-ruby@v1" } ? "ruby" : "node_js"

result = {"language" => language}
def commands
# Append script_cmd to the list of steps if one is present
config["jobs"]["ci"]["steps"] << {"run" => script_cmd, "name" => "script_cmd"} if script_cmd

config.dig("jobs", "ci", "steps").map do |step|
if step["run"].nil?
case step["uses"]
when /ruby\/setup-ruby/
step["run"] = defaults["ruby"]["install"]
when /actions\/setup-node/
step["run"] = defaults["node_js"]["install"]
else
next
end
end

build_section(step["name"], "#{step["run"]} || exit $?")
end.compact
end

result["before_install"] = steps_by_name["Set up system"]["run"] if steps_by_name["Set up system"]
result["before_script"] = steps_by_name["Prepare tests"]["run"] if steps_by_name["Prepare tests"]
result["script"] = steps_by_name["Run tests"]["run"] if steps_by_name["Run tests"]
def load_config!
ci_config
end

result
def ci_config
YAML.load_file(CONFIG_FILE)
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions lib/manageiq/cross_repo/runner/travis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,46 @@ def self.available?

private

def commands
environment_setup_commands + section_commands
end

def environment_setup_commands
commands = []

if config["node_js"]
commands << "source ~/.nvm/nvm.sh"
commands += Array(config["node_js"]).map do |node_version|
"nvm install #{node_version}"
end
end

commands.any? ? build_section("environment", *commands) : commands
end

def section_commands
sections = %w[before_install install before_script script]
sections.flat_map do |section|
commands = build_section_commands(section)
build_section(section, *commands) if commands.present?
end.compact
end

def build_section_commands(section)
# Travis sections can have a single command or an array of commands
Array(config[section]).map { |cmd| "#{cmd} || exit $?" }
end

def load_config!
ci_config.tap do |config|
# Set missing sections to the proper defaults
config["install"] ||= defaults[config["language"]]["install"]

config["script"] = script_cmd if script_cmd.present?
config["script"] ||= defaults[config["language"]]["script"]
end
end

def ci_config
YAML.load_file(CONFIG_FILE)
end
Expand Down
19 changes: 11 additions & 8 deletions spec/manageiq/cross_repo/runner/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@
expected_test_script = <<~SCRIPT
#!/bin/bash

echo '::group::before_install'
echo '::group::Set up system'
bin/before_install || exit $?
echo '::endgroup::'
echo '::group::install'
echo '::group::Set up Ruby'
bundle install --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle} || exit $?
echo '::endgroup::'
echo '::group::before_script'
echo '::group::Prepare tests'
bin/setup || exit $?
echo '::endgroup::'
echo '::group::script'
echo '::group::Run tests'
bundle exec rake || exit $?
echo '::endgroup::'
SCRIPT
Expand All @@ -75,16 +75,19 @@
expected_test_script = <<~SCRIPT
#!/bin/bash

echo '::group::before_install'
echo '::group::Set up system'
bin/before_install || exit $?
echo '::endgroup::'
echo '::group::install'
echo '::group::Set up Ruby'
bundle install --jobs=3 --retry=3 --path=${BUNDLE_PATH:-vendor/bundle} || exit $?
echo '::endgroup::'
echo '::group::before_script'
echo '::group::Prepare tests'
bin/setup || exit $?
echo '::endgroup::'
echo '::group::script'
echo '::group::Run tests'
bundle exec rake || exit $?
echo '::endgroup::'
echo '::group::script_cmd'
cat db/schema.rb || exit $?
echo '::endgroup::'
SCRIPT
Expand Down