Skip to content

Instantly share code, notes, and snippets.

@oriolgual
Created February 28, 2019 09:41
Show Gist options
  • Save oriolgual/c805d1fdd3a65a2d39b8b4e8811a49e4 to your computer and use it in GitHub Desktop.
Save oriolgual/c805d1fdd3a65a2d39b8b4e8811a49e4 to your computer and use it in GitHub Desktop.
Extracts RSpec failed examples from a CircleCI build so you can run them locally
require "open-uri"
require "json"
if ARGV.length < 3
puts "Usage: ruby circle-to-rspec.rb CIRCLE_CI_TOKEN RSPEC_STEP_NAME BUILD_URL"
puts "You can get your CircleCI token at https://circleci.com/account/api"
exit(0)
end
circle_token = ARGV[0]
rspec_step_name = ARGV[1]
build_url = ARGV[2]
api_url = build_url.split("?").first.gsub("circleci.com/gh", "circleci.com/api/v1.1/project/github").concat("?circle-token=#{circle_token}")
json = JSON.parse(open(api_url).read)
rspec_step = json["steps"].find {|step| step["name"] == rspec_step_name }
unless rspec_step
puts "Can't find rspec step with name #{rspec_step_name}"
exit(1)
end
failures = rspec_step["actions"].flat_map do |action|
output_url = action["output_url"]
content = open(output_url).read
content.split("\\r\\n").select { |line|
line.include?("rspec ")
}.map { |line|
match_data = line.match(/rspec ('?\.\/[^#\\]*)/)
match_data[1] if match_data
}.compact
end
puts failures.join(" ").prepend("rspec ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment