-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add 'pact-broker can-i-deploy' executable
- Loading branch information
Showing
9 changed files
with
309 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env ruby | ||
require 'pact_broker/client/cli/broker' | ||
|
||
PactBroker::Client::CLI::Broker.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'pact_broker/client/error' | ||
require 'pact_broker/client/pact_broker_client' | ||
|
||
module PactBroker | ||
module Client | ||
class CanIDeploy | ||
|
||
class Result | ||
attr_reader :success, :message | ||
|
||
def initialize success, message = nil | ||
@success = success | ||
@message = message | ||
end | ||
end | ||
|
||
def self.call(pact_broker_base_url, version_selectors, pact_broker_client_options={}) | ||
new(pact_broker_base_url, version_selectors, pact_broker_client_options).call | ||
end | ||
|
||
def initialize(pact_broker_base_url, version_selectors, pact_broker_client_options) | ||
@pact_broker_base_url = pact_broker_base_url | ||
@version_selectors = version_selectors | ||
@pact_broker_client_options = pact_broker_client_options | ||
end | ||
|
||
def call | ||
matrix = pact_broker_client.matrix.get(version_selectors) | ||
if matrix[:matrix].any? | ||
Result.new(true, 'Computer says yes \o/') | ||
This comment has been minimized.
Sorry, something went wrong. |
||
else | ||
Result.new(false, 'Computer says no ¯\_(ツ)_/¯') | ||
end | ||
rescue PactBroker::Client::Error => e | ||
Result.new(false, e.message) | ||
rescue StandardError => e | ||
Result.new(false, "Error retrieving matrix #{e.class} - #{e.message}\n#{e.backtrace.join("\n")}") | ||
end | ||
|
||
private | ||
|
||
attr_reader :pact_broker_base_url, :version_selectors, :pact_broker_client_options | ||
|
||
def pact_broker_client | ||
@pact_broker_client ||= PactBroker::Client::PactBrokerClient.new(base_url: pact_broker_base_url, client_options: pact_broker_client_options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'pact_broker/client/can_i_deploy' | ||
require 'pact_broker/client/version' | ||
require 'thor' | ||
|
||
module PactBroker | ||
module Client | ||
module CLI | ||
class Broker < Thor | ||
desc 'can-i-deploy VERSION_SELECTOR_ONE VERSION_SELECTOR_TWO ...', "Returns exit code 0 or 1, indicating whether or not the specified application versions are compatible.\n\nThe VERSION_SELECTOR format is <pacticipant_name>/version/<version_number>." | ||
|
||
method_option :broker_base_url, required: true, aliases: "-b", desc: "The base URL of the Pact Broker" | ||
method_option :broker_username, aliases: "-n", desc: "Pact Broker basic auth username" | ||
method_option :broker_password, aliases: "-p", desc: "Pact Broker basic auth password" | ||
method_option :verbose, aliases: "-v", desc: "Verbose output", :required => false | ||
|
||
def can_i_deploy(*selectors) | ||
result = CanIDeploy.call(options.broker_base_url, selectors, pact_broker_client_options) | ||
if result.success | ||
$stdout.puts result.message | ||
else | ||
$stdout.puts result.message | ||
exit(1) | ||
end | ||
end | ||
|
||
desc 'version', "Show the pact_broker-client gem version" | ||
def version | ||
$stdout.puts PactBroker::Client::VERSION | ||
end | ||
|
||
no_commands do | ||
def pact_broker_client_options | ||
if options[:username] | ||
{ | ||
username: options[:username], | ||
password: options[:password] | ||
} | ||
else | ||
{} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require_relative 'base_client' | ||
|
||
module PactBroker | ||
module Client | ||
class Matrix < BaseClient | ||
def get selectors | ||
query = {selectors: selectors} | ||
response = self.class.get("/matrix", query: query, headers: default_get_headers) | ||
|
||
handle_response(response) do | ||
JSON.parse(response.body, symbolize_names: true) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
spec/service_providers/pact_broker_client_matrix_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
require_relative 'pact_helper' | ||
require 'pact_broker/client' | ||
|
||
module PactBroker::Client | ||
describe Matrix, :pact => true do | ||
|
||
include_context "pact broker" | ||
|
||
describe "retriving the compatibility matrix" do | ||
let(:matrix_response_body) do | ||
{ | ||
matrix: [{}] | ||
} | ||
end | ||
|
||
context "when results are found" do | ||
before do | ||
pact_broker. | ||
given("the pact for Foo version 1.2.3 has been verified by Bar version 4.5.6"). | ||
upon_receiving("a request for the compatibility matrix for Foo version 1.2.3 and Bar version 4.5.6"). | ||
with( | ||
method: :get, | ||
path: "/matrix", | ||
query: { | ||
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/4.5.6'] | ||
} | ||
). | ||
will_respond_with( | ||
status: 200, | ||
headers: pact_broker_response_headers, | ||
body: matrix_response_body | ||
) | ||
end | ||
|
||
it 'a matrix of compatible versions' do | ||
matrix = pact_broker_client.matrix.get(['Foo/version/1.2.3', 'Bar/version/4.5.6']) | ||
expect(matrix[:matrix].size).to eq 1 | ||
end | ||
end | ||
|
||
context "with only one version selector" do | ||
before do | ||
pact_broker. | ||
given("the pact for Foo version 1.2.3 has been verified by Bar version 4.5.6"). | ||
upon_receiving("a request for the compatibility matrix where only the version of Foo is specified"). | ||
with( | ||
method: :get, | ||
path: "/matrix", | ||
query: { | ||
'selectors[]' => ['Foo/version/1.2.3'] | ||
} | ||
). | ||
will_respond_with( | ||
status: 200, | ||
headers: pact_broker_response_headers, | ||
body: matrix_response_body | ||
) | ||
end | ||
end | ||
|
||
context "when one or more of the versions does not exist" do | ||
before do | ||
pact_broker. | ||
given("the pact for Foo version 1.2.3 has been verified by Bar version 4.5.6"). | ||
upon_receiving("a request for the compatibility matrix where one or more versions does not exist"). | ||
with( | ||
method: :get, | ||
path: "/matrix", | ||
query: { | ||
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/9.9.9'] | ||
} | ||
). | ||
will_respond_with( | ||
status: 400, | ||
headers: pact_broker_response_headers, | ||
body: { | ||
errors: Pact.each_like("an error message") | ||
} | ||
) | ||
end | ||
|
||
it 'returns a list of errors' do | ||
expect { | ||
pact_broker_client.matrix.get(['Foo/version/1.2.3', 'Bar/version/9.9.9']) | ||
}.to raise_error PactBroker::Client::Error, "an error message" | ||
end | ||
end | ||
|
||
context "when results are not found" do | ||
before do | ||
pact_broker. | ||
upon_receiving("a request for the compatibility matrix for a pacticipant that does not exist"). | ||
with( | ||
method: :get, | ||
path: "/matrix", | ||
query: { | ||
'selectors[]' => ['Foo/version/1.2.3', 'Bar/version/9.9.9'] | ||
} | ||
). | ||
will_respond_with( | ||
status: 400, | ||
headers: pact_broker_response_headers, | ||
body: { | ||
errors: Pact.each_like("an error message") | ||
} | ||
) | ||
end | ||
|
||
it 'returns a list of errors' do | ||
expect { | ||
pact_broker_client.matrix.get(['Foo/version/1.2.3', 'Bar/version/9.9.9']) | ||
}.to raise_error PactBroker::Client::Error, "an error message" | ||
end | ||
end | ||
end | ||
end | ||
end |
@bethesque I came all the way just to say thanks for the nice text "emojis" in both yes and no messages 😄