Skip to content

Submit annotations directly with HTTP #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
8 changes: 7 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ PATH
remote: .
specs:
rspec-buildkite (0.1.4)
json
net-http
rspec-core (~> 3.0)

GEM
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -41,4 +47,4 @@ DEPENDENCIES
rspec-buildkite!

BUNDLED WITH
1.17.1
2.4.19
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
26 changes: 20 additions & 6 deletions lib/rspec/buildkite/annotation_formatter.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

}
params = {
context: "rspec",
style: "error",
append: true,
body: format_failure(notification),
}

Net::HTTP.post(uri, JSON.dump(params), headers).value
end
end
rescue
Expand Down
4 changes: 3 additions & 1 deletion rspec-buildkite.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ 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"
spec.add_dependency "json"
spec.add_dependency "net-http"

spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake"
Expand Down