forked from pact-foundation/pact_broker-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add create-environment command
- Loading branch information
Showing
5 changed files
with
325 additions
and
1 deletion.
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
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,78 @@ | ||
require 'pact_broker/client/hal_client_methods' | ||
require 'pact_broker/client/error' | ||
require 'pact_broker/client/command_result' | ||
require 'term/ansicolor' | ||
|
||
module PactBroker | ||
module Client | ||
module Environments | ||
class CreateEnvironment | ||
include PactBroker::Client::HalClientMethods | ||
|
||
NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support creation of environments. Please upgrade to version 2.80.0 or later." | ||
|
||
def self.call(params, pact_broker_base_url, pact_broker_client_options) | ||
new(params, pact_broker_base_url, pact_broker_client_options).call | ||
end | ||
|
||
def initialize(params, pact_broker_base_url, pact_broker_client_options) | ||
@params = params | ||
@pact_broker_base_url = pact_broker_base_url | ||
@pact_broker_client_options = pact_broker_client_options | ||
end | ||
|
||
def call | ||
check_if_command_supported | ||
create_environment | ||
rescue PactBroker::Client::Error => e | ||
PactBroker::Client::CommandResult.new(false, ::Term::ANSIColor.red(e.message)) | ||
end | ||
|
||
private | ||
|
||
attr_reader :params | ||
attr_reader :pact_broker_base_url, :pact_broker_client_options | ||
|
||
def create_environment | ||
index_resource | ||
._link!("pb:environments") | ||
.post!(request_body) | ||
PactBroker::Client::CommandResult.new(true, result_message) | ||
end | ||
|
||
def request_body | ||
{ | ||
name: params[:name], | ||
displayName: params[:display_name], | ||
production: params[:production], | ||
contacts: contacts | ||
}.compact | ||
end | ||
|
||
def contacts | ||
if params[:contact_name] || params[:contact_email_address] | ||
contact = {} | ||
contact[:name] = params[:contact_name] || "unknown" | ||
if params[:contact_email_address] | ||
contact[:details] = { emailAddress: params[:contact_email_address] } | ||
end | ||
[contact] | ||
else | ||
nil | ||
end | ||
end | ||
|
||
def result_message | ||
prod_label = params[:production] ? "production" : "non-production" | ||
::Term::ANSIColor.green("Created #{prod_label} environment #{params[:name]} in #{pact_broker_name}") | ||
end | ||
|
||
def check_if_command_supported | ||
unless index_resource.can?("pb:environments") | ||
raise PactBroker::Client::Error.new(NOT_SUPPORTED_MESSAGE) | ||
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
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,78 @@ | ||
require 'service_providers/pact_helper' | ||
require 'pact_broker/client/environments/create_environment' | ||
|
||
RSpec.describe "create an environment", pact: true do | ||
include_context "pact broker" | ||
include PactBrokerPactHelperMethods | ||
|
||
let(:params) do | ||
{ | ||
name: "test", | ||
display_name: "Test", | ||
production: false, | ||
contact_name: "Foo team", | ||
contact_email_address: "foo@bar.com" | ||
|
||
} | ||
end | ||
let(:pact_broker_client_options) { {} } | ||
let(:request_body) do | ||
{ | ||
name: "test", | ||
displayName: "Test", | ||
production: false, | ||
contacts: [{ | ||
name: "Foo team", | ||
details: { | ||
emailAddress: "foo@bar.com" | ||
} | ||
}] | ||
} | ||
end | ||
|
||
subject { PactBroker::Client::Environments::CreateEnvironment.call(params, broker_base_url, pact_broker_client_options) } | ||
|
||
def mock_index | ||
pact_broker | ||
.given("the pb:environments relation exists in the index resource") | ||
.upon_receiving("a request for the index resource") | ||
.with( | ||
method: "GET", | ||
path: '/', | ||
headers: get_request_headers). | ||
will_respond_with( | ||
status: 200, | ||
headers: pact_broker_response_headers, | ||
body: { | ||
_links: { | ||
:'pb:environments' => { | ||
href: placeholder_url_term("pb:environments") | ||
} | ||
} | ||
} | ||
) | ||
end | ||
|
||
def mock_environment_creation_request | ||
pact_broker | ||
.upon_receiving("a request to create an environment") | ||
.with( | ||
method: "POST", | ||
path: "/HAL-REL-PLACEHOLDER-PB-ENVIRONMENTS", | ||
headers: post_request_headers, | ||
body: request_body | ||
) | ||
.will_respond_with( | ||
status: 201, | ||
headers: pact_broker_response_headers, | ||
body: request_body | ||
) | ||
end | ||
|
||
it "returns a success result" do | ||
mock_index | ||
mock_environment_creation_request | ||
expect(subject.success).to be true | ||
end | ||
end | ||
|