Skip to content

Commit

Permalink
refactor: move deployment commands into separate module and fix versi…
Browse files Browse the repository at this point in the history
…on superclass issues
  • Loading branch information
bethesque committed Jun 4, 2021
1 parent 5506138 commit 75f06b8
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 167 deletions.
40 changes: 18 additions & 22 deletions lib/pact_broker/client/cli/deployment_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,42 @@ def self.included(thor)
shared_authentication_options

def record_deployment
require 'pact_broker/client/versions/record_deployment'
params = {
pacticipant_name: options.pacticipant,
version_number: options.version,
environment_name: options.environment,
target: options.target,
output: options.output
target: options.target
}
result = PactBroker::Client::Versions::RecordDeployment.call(
params,
options.broker_base_url,
pact_broker_client_options
)
$stdout.puts result.message
exit(1) unless result.success
execute_deployment_command(params, "RecordDeployment")
end

ignored_and_hidden_potential_options_from_environment_variables
desc "record-undeployment", "Record undeployment of (or the end of support for) a pacticipant version from an environment"
method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was deployed."
method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was deployed."
method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was deployed to."
method_option :pacticipant, required: true, aliases: "-a", desc: "The name of the pacticipant that was undeployed."
method_option :version, required: true, aliases: "-e", desc: "The pacticipant version number that was undeployed."
method_option :environment, required: true, desc: "The name of the environment that the pacticipant version was undeployed from."
method_option :target, default: nil, required: false, desc: "Optional. The target that the application is being undeployed from - a logical identifer required to differentiate deployments when there are multiple instances of the same application in an environment."
output_option_json_or_text
shared_authentication_options

def record_undeployment
require 'pact_broker/client/versions/record_undeployment'
params = {
pacticipant_name: options.pacticipant,
version_number: options.version,
environment_name: options.environment,
output: options.output
target: options.target
}
result = PactBroker::Client::Versions::RecordUndeployment.call(
params,
options.broker_base_url,
pact_broker_client_options
)
$stdout.puts result.message
exit(1) unless result.success
execute_deployment_command(params, "RecordUndeployment")
end

no_commands do
def execute_deployment_command(params, command_class_name)
require 'pact_broker/client/deployments'
command_options = { verbose: options.verbose, output: options.output }
result = PactBroker::Client::Deployments.const_get(command_class_name).call(params, command_options, pact_broker_client_options)
$stdout.puts result.message
exit(1) unless result.success
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/pact_broker/client/deployments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dir.glob(File.join(__FILE__.gsub(".rb", "/**/*.rb"))).sort.each do | path |
require path
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module PactBroker
module Client
class Versions
module Deployments
class RecordDeployment < PactBroker::Client::BaseCommand

NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support recording deployments. Please upgrade to version 2.80.0 or later."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
require 'pact_broker/client/hal_client_methods'
require 'pact_broker/client/error'
require 'pact_broker/client/command_result'

# TODO
# --limit 1
# order by date so that the oldest one gets undeployed first
require 'pact_broker/client/base_command'

module PactBroker
module Client
class Versions
class RecordUndeployment
include PactBroker::Client::HalClientMethods
module Deployments
class RecordUndeployment < PactBroker::Client::BaseCommand

NOT_SUPPORTED_MESSAGE = "This version of the Pact Broker does not support recording undeployments. 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)
@pact_broker_base_url = pact_broker_base_url
super
@pacticipant_name = params.fetch(:pacticipant_name)
@version_number = params.fetch(:version_number)
@environment_name = params.fetch(:environment_name)
@output = params.fetch(:output)
@pact_broker_client_options = pact_broker_client_options
@target = params.fetch(:target)
end

def call
private

def do_call
check_if_command_supported
if deployed_version_links_for_environment.any?
@undeployment_entities = deployed_version_links_for_environment.collect do | deployed_version_link |
Expand All @@ -39,13 +29,8 @@ def call
end

PactBroker::Client::CommandResult.new(true, "foo")
rescue PactBroker::Client::Error => e
PactBroker::Client::CommandResult.new(false, e.message)
end

private

attr_reader :pact_broker_base_url, :pact_broker_client_options
attr_reader :pacticipant_name, :version_number, :environment_name, :target, :output
attr_reader :deployed_version_resource, :undeployment_entities

Expand Down Expand Up @@ -82,12 +67,10 @@ def deployed_version_not_found_message
end

def result_message
if output == "text"
message = "Recorded undeployment of #{pacticipant_name} version #{version_number} from #{environment_name} in #{pact_broker_name}."
elsif output == "json"
if output_json?
undeployment_entities.last.response.raw_body
else
""
green("Recorded undeployment of #{pacticipant_name} version #{version_number} from #{environment_name} in #{pact_broker_name}.")
end
end

Expand Down
6 changes: 6 additions & 0 deletions lib/pact_broker/client/versions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require_relative 'base_client'

Dir.glob(File.join(__FILE__.gsub(".rb", "/**/*.rb"))).sort.each do | path |
require path
end

# Old code
require 'pact_broker/client/pacts'

module PactBroker
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/client/versions/describe.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Need Versions class to extend BaseClient until we can remove the old Versions code
require 'pact_broker/client/base_client'
require 'pact_broker/client/pact_broker_client'
require 'pact_broker/client/versions/formatter'

module PactBroker
module Client
class Versions
class Versions < BaseClient
class Describe

class Result
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/client/versions/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Need Versions class to extend BaseClient until we can remove the old Versions code
require 'pact_broker/client/base_client'
require 'pact_broker/client/versions/json_formatter'
require 'pact_broker/client/versions/text_formatter'

module PactBroker
module Client
class Versions
class Versions < BaseClient
class Formatter
def self.call(matrix_lines, format)
formatter = case format
Expand Down
8 changes: 5 additions & 3 deletions lib/pact_broker/client/versions/json_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Need Versions class to extend BaseClient until we can remove the old Versions code
require 'pact_broker/client/base_client'
require 'table_print'

module PactBroker
module Client
class Versions
class Versions < BaseClient
class JsonFormatter
def self.call(matrix)
JSON.pretty_generate(matrix)
def self.call(version)
JSON.pretty_generate(version)
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/pact_broker/client/versions/text_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Need Versions class to extend BaseClient until we can remove the old Versions code
require 'pact_broker/client/base_client'
require 'table_print'

module PactBroker
module Client
class Versions
class Versions < BaseClient
class TextFormatter

Line = Struct.new(:number, :tags)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'pact_broker/client/versions/record_deployment'
require 'pact_broker/client/deployments/record_deployment'

module PactBroker
module Client
class Versions
module Deployments
describe RecordDeployment do
describe ".call" do
let(:broker_base_url) { "http://broker" }
Expand Down
107 changes: 0 additions & 107 deletions spec/pacts/pact_broker_client-pact_broker.json
Original file line number Diff line number Diff line change
Expand Up @@ -1638,113 +1638,6 @@
}
}
},
{
"description": "a request for the index resource",
"providerState": "the pb:publish-contracts relations exists in the index resource",
"request": {
"method": "GET",
"path": "/",
"headers": {
"Accept": "application/hal+json"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pb:publish-contracts": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PUBLISH-CONTRACTS"
}
}
},
"matchingRules": {
"$.body._links.pb:publish-contracts.href": {
"match": "regex",
"regex": "http:\\/\\/.*"
}
}
}
},
{
"description": "a request to publish contracts",
"request": {
"method": "POST",
"path": "/HAL-REL-PLACEHOLDER-PB-PUBLISH-CONTRACTS",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"pacticipantName": "Foo",
"pacticipantVersionNumber": "5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
"branch": "main",
"tags": [
"dev"
],
"buildUrl": "http://build",
"contracts": [
{
"consumerName": "Foo",
"providerName": "Bar",
"specification": "pact",
"contentType": "application/json",
"content": "eyJjb25zdW1lciI6eyJuYW1lIjoiRm9vIn0sInByb3ZpZGVyIjp7Im5hbWUiOiJCYXIifSwiaW50ZXJhY3Rpb25zIjpbeyJkZXNjcmlwdGlvbiI6ImFuIGV4YW1wbGUgcmVxdWVzdCIsInByb3ZpZGVyU3RhdGUiOiJhIHByb3ZpZGVyIHN0YXRlIiwicmVxdWVzdCI6eyJtZXRob2QiOiJHRVQiLCJwYXRoIjoiLyIsImhlYWRlcnMiOnt9fSwicmVzcG9uc2UiOnsic3RhdHVzIjoyMDAsImhlYWRlcnMiOnsiQ29udGVudC1UeXBlIjoiYXBwbGljYXRpb24vaGFsK2pzb24ifX19XSwibWV0YWRhdGEiOnsicGFjdFNwZWNpZmljYXRpb24iOnsidmVyc2lvbiI6IjIuMC4wIn19fQ==",
"writeMode": "overwrite",
"onConflict": "overwrite"
}
]
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_embedded": {
"pacticipant": {
"name": "Foo"
},
"version": {
"number": "5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
"buildUrl": "http://build"
}
},
"logs": [
{
"level": "info",
"message": "some message"
}
],
"_links": {
"pb:pacticipant-version-tags": [
{
"name": "dev"
}
],
"pb:contracts": [
{
"href": "http://some-pact"
}
]
}
},
"matchingRules": {
"$.body.logs": {
"min": 1
},
"$.body.logs[*].*": {
"match": "type"
},
"$.body._links.pb:contracts[0].href": {
"match": "type"
}
}
}
},
{
"description": "a request for the index resource",
"providerState": "the pb:pacticipant-version and pb:environments relations exist in the index resource",
Expand Down
4 changes: 2 additions & 2 deletions spec/service_providers/record_deployment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'service_providers/pact_helper'
require 'pact_broker/client/versions/record_deployment'
require 'pact_broker/client/deployments/record_deployment'

deployment_feature_on = ENV.fetch('PACT_BROKER_FEATURES', '').include?("deployments")

Expand Down Expand Up @@ -27,7 +27,7 @@
end
let(:pact_broker_client_options) { { pact_broker_base_url: broker_base_url } }

subject { PactBroker::Client::Versions::RecordDeployment.call(params, options, pact_broker_client_options) }
subject { PactBroker::Client::Deployments::RecordDeployment.call(params, options, pact_broker_client_options) }

def mock_index
pact_broker
Expand Down

0 comments on commit 75f06b8

Please sign in to comment.