diff --git a/Gemfile.lock b/Gemfile.lock index f38b2cd..e1bda0d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ PATH specs: gridlock_ci (0.1.0) faraday (~> 2.7) + faraday-multipart (~> 1.0) faraday-retry (~> 2.2) rspec-core (~> 3.12) rspec_junit_formatter (~> 0.6) @@ -16,12 +17,15 @@ GEM faraday (2.7.10) faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) + faraday-multipart (1.0.4) + multipart-post (~> 2) faraday-net_http (3.0.2) faraday-retry (2.2.0) faraday (~> 2.0) json (2.6.3) language_server-protocol (3.17.0.3) method_source (1.0.0) + multipart-post (2.3.0) nokogiri (1.15.3-x86_64-darwin) racc (~> 1.4) parallel (1.23.0) diff --git a/README.md b/README.md index 893b280..3d233ae 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,11 @@ Example: `--rspec '--tag=foo --format progress'` ## Junit output Junit output is supported via flag: `--junit-output FilePath`. Do not use the RSpec formatter for the reasons explained above. + +## Upload results + +Results can be uploaded to the server for analysis + +```sh +bundle exec gridlock_ci run --run_id 1 --run_attempt 1 --file_path FilePath +``` diff --git a/exe/gridlock_ci b/exe/gridlock_ci index 9049eb6..36e512e 100755 --- a/exe/gridlock_ci +++ b/exe/gridlock_ci @@ -55,10 +55,32 @@ def enqueue_command options end +def upload_results_command + options = {} + + OptionParser.new do |opts| + opts.banner = 'Usage: gridlock_ci upload_results [flags]' + + opts.on('--run_id RUN_ID', 'Run ID to associate with [required]') do |o| + options[:run_id] = o + end + + opts.on('--run_attempt RUN_ATTEMPT', 'Run attempt number [required]') do |o| + options[:run_attempt] = o + end + + opts.on('--file_path FILE_PATH', 'Path to results file [required]') do |o| + options[:file_path] = o + end + end.parse! + + options +end + global = OptionParser.new do |opts| opts.banner = 'Usage: gridlock_ci [command] [options]' opts.separator '' - opts.separator "Commands:\n run\n enqueue" + opts.separator "Commands:\n run\n enqueue\n upload_results" end global.order! @@ -80,6 +102,10 @@ when 'enqueue' end GridlockCi::Client.new(opts[:run_id], opts[:run_attempt]).send_specs(specs) +when 'upload_results' + opts = upload_results_command + + GridlockCi::Client.new(opts[:run_id], opts[:run_attempt]).upload_results(opts[:file_path]) else puts 'Command not found' exit 1 diff --git a/gridlock_ci.gemspec b/gridlock_ci.gemspec index d9a215e..768ee51 100644 --- a/gridlock_ci.gemspec +++ b/gridlock_ci.gemspec @@ -32,6 +32,7 @@ Gem::Specification.new do |spec| # Uncomment to register a new dependency of your gem spec.add_dependency 'faraday', '~> 2.7' + spec.add_dependency 'faraday-multipart', '~> 1.0' spec.add_dependency 'faraday-retry', '~> 2.2' spec.add_dependency 'rspec-core', '~> 3.12' spec.add_dependency 'rspec_junit_formatter', '~> 0.6' diff --git a/lib/gridlock_ci.rb b/lib/gridlock_ci.rb index fd96608..69867f7 100644 --- a/lib/gridlock_ci.rb +++ b/lib/gridlock_ci.rb @@ -5,6 +5,7 @@ require 'json' require 'faraday' require 'faraday/retry' +require 'faraday/multipart' require_relative 'gridlock_ci/version' require_relative 'gridlock_ci/client' diff --git a/lib/gridlock_ci/client.rb b/lib/gridlock_ci/client.rb index 5746fd5..7b15be8 100644 --- a/lib/gridlock_ci/client.rb +++ b/lib/gridlock_ci/client.rb @@ -13,7 +13,7 @@ def previous_run_completed? previous_run_key = "#{run_id}_#{run_attempt - 1}" results = conn.get("/spec_list/#{previous_run_key}").body['specs'] - return true if results.empty? + true if results.empty? end def next_spec @@ -27,6 +27,13 @@ def send_specs(spec_list) raise result['status'] if result['status'] != 'success' end + def upload_results(file_path) + file = Faraday::Multipart::FilePart.new(file_path, 'text/xml') + payload = { file: file } + + multipart_conn.post("/spec_results/#{run_key}", payload) + end + private def run_key @@ -49,6 +56,14 @@ def conn end end + def multipart_conn + Faraday.new(url: gridlock_ci_endpoint, request: { timeout: 15 }) do |f| + a.request :multipart + f.response :json + f.response :raise_error + end + end + def gridlock_ci_endpoint ENV.fetch('GRIDLOCK_CI_SERVER_ENDPOINT', 'http://localhost:4568') end