From 392782c0f4dd942ac0487c0552734b8e43d1df57 Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Thu, 7 Sep 2023 22:26:47 +1000 Subject: [PATCH 1/2] Allow ruby 3 --- Gemfile.lock | 2 +- rspec-buildkite.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 55321c7..6901c2b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -41,4 +41,4 @@ DEPENDENCIES rspec-buildkite! BUNDLED WITH - 1.17.1 + 2.4.19 diff --git a/rspec-buildkite.gemspec b/rspec-buildkite.gemspec index 5a1426a..c7cc0a3 100644 --- a/rspec-buildkite.gemspec +++ b/rspec-buildkite.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |spec| spec.files = Dir["README.md", "LICENSE.txt", "lib/**/*"] - spec.required_ruby_version = "~> 2.2" + spec.required_ruby_version = ">= 2.2", "< 4" spec.add_dependency "rspec-core", "~> 3.0" From cbafdc9cfa0ee73429783d5d97adc5db353d518b Mon Sep 17 00:00:00 2001 From: Samuel Cochran Date: Thu, 7 Sep 2023 22:29:38 +1000 Subject: [PATCH 2/2] Submit annotations directly via HTTP Using the buildkite-agent is nice in theory, but becomes a pain in Docker environments. Let's try posting annotations directly to the Buildkite Agent API. We need some more gems, but they're all default/stdlib gems which should be installed anyway. --- Gemfile.lock | 6 +++++ README.md | 2 +- lib/rspec/buildkite/annotation_formatter.rb | 26 ++++++++++++++++----- rspec-buildkite.gemspec | 2 ++ 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6901c2b..29324f7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,8 @@ PATH remote: . specs: rspec-buildkite (0.1.4) + json + net-http rspec-core (~> 3.0) GEM @@ -13,6 +15,9 @@ GEM thor (>= 0.14.0) coderay (1.1.2) diff-lcs (1.3) + json (2.6.3) + net-http (0.3.2) + uri rake (13.0.1) rspec (3.8.0) rspec-core (~> 3.8.0) @@ -28,6 +33,7 @@ GEM rspec-support (~> 3.8.0) rspec-support (3.8.0) thor (0.20.3) + uri (0.12.2) PLATFORMS ruby diff --git a/README.md b/README.md index f7bb036..b0172a2 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Now run your specs on Buildkite! ### Docker & Docker Compose -If you run your RSpec builds inside Docker or Docker Compose then you'll need to make sure that buildkite-agent is available inside your container, and that some environment variables are propagated into the running containers. The buildkite-agent binary can be baked into your image, or mounted in as a volume. If you're using [the docker-compose-buildkite-plugin][dcbp] you can pass the environment using [plugin configuration][dcbp-env]. Or you can add them to the [environment section][dc-env] in your `docker-compose.yml`, or supply [env arguments][d-env] to your docker command. +If you run your RSpec builds inside Docker or Docker Compose then you'll need to make sure that some environment variables are propagated into the running containers. If you're using [the docker-compose-buildkite-plugin][dcbp] you can pass the environment using [plugin configuration][dcbp-env]. Or you can add them to the [environment section][dc-env] in your `docker-compose.yml`, or supply [env arguments][d-env] to your docker command. The following environment variables are required: diff --git a/lib/rspec/buildkite/annotation_formatter.rb b/lib/rspec/buildkite/annotation_formatter.rb index 52789c4..4756787 100644 --- a/lib/rspec/buildkite/annotation_formatter.rb +++ b/lib/rspec/buildkite/annotation_formatter.rb @@ -1,7 +1,10 @@ +require "json" +require "net/http" require "thread" require "rspec/core" require "rspec/buildkite/recolorizer" +require "rspec/buildkite/version" module RSpec::Buildkite # Create a Buildkite annotation for RSpec failures @@ -32,17 +35,28 @@ def example_failed(notification) private + def agent_endpoint + ENV.fetch("BUILDKITE_AGENT_ENDPOINT", "https://agent.buildkite.com") + end + def thread while notification = @queue.pop break if notification == :close if notification - system "buildkite-agent", "annotate", - "--context", "rspec", - "--style", "error", - "--append", - format_failure(notification), - out: :close # only display errors + uri = URI.join(agent_endpoint, "v3/jobs/#{ENV.fetch("BUILDKITE_JOB_ID")}/annotations") + headers = { + "Authorization" => "Token #{ENV.fetch("BUILDKITE_AGENT_ACCESS_TOKEN")}", + "User-Agent" => "rspec-buildkite/#{RSpec::Buildkite::VERSION}", + } + params = { + context: "rspec", + style: "error", + append: true, + body: format_failure(notification), + } + + Net::HTTP.post(uri, JSON.dump(params), headers).value end end rescue diff --git a/rspec-buildkite.gemspec b/rspec-buildkite.gemspec index c7cc0a3..b97da9b 100644 --- a/rspec-buildkite.gemspec +++ b/rspec-buildkite.gemspec @@ -17,6 +17,8 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.2", "< 4" spec.add_dependency "rspec-core", "~> 3.0" + spec.add_dependency "json" + spec.add_dependency "net-http" spec.add_development_dependency "bundler", "~> 1.16" spec.add_development_dependency "rake"