From d35d9080fdaab2dbd6a7ddb6bc7cdc68395d2ee9 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 10 May 2022 16:10:18 +1000 Subject: [PATCH] feat: add --verification-exit-code to publish-provider-contract command --- .../client/cli/provider_contract_commands.rb | 67 ++++++++++++------- script/publish-provider-contract.sh | 37 +++++----- 2 files changed, 63 insertions(+), 41 deletions(-) diff --git a/lib/pactflow/client/cli/provider_contract_commands.rb b/lib/pactflow/client/cli/provider_contract_commands.rb index c4955f1..53697f1 100644 --- a/lib/pactflow/client/cli/provider_contract_commands.rb +++ b/lib/pactflow/client/cli/provider_contract_commands.rb @@ -20,7 +20,8 @@ def self.included(thor) #method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag consumer version with the name of the current git branch. Default: false" method_option :specification, default: "oas", desc: "The contract specification" method_option :content_type, desc: "The content type. eg. application/yml" - method_option :verification_success, type: :boolean + method_option :verification_success, type: :boolean, desc: "Whether or not the self verification passed successfully." + method_option :verification_exit_code, type: :numeric, desc: "The exit code of the verification process. Can be used instead of --verificaiton-success|--no-verification-success for a simpler build script." method_option :verification_results, desc: "The path to the file containing the output from the verification process" method_option :verification_results_content_type, desc: "The content type of the verification output eg. text/plain, application/yaml" method_option :verification_results_format, desc: "The format of the verification output eg. junit, text" @@ -34,31 +35,51 @@ def self.included(thor) def publish_provider_contract(provider_contract_path) require "pactflow/client/provider_contracts/publish" - params = params = { - provider_name: options.provider.strip, - provider_version_number: options.provider_app_version.strip, - branch_name: options.branch && options.branch.strip, - tags: (options.tag && options.tag.collect(&:strip)) || [], - contract: { - content: File.read(provider_contract_path), - content_type: options.content_type, - specification: options.specification - }, - verification_results: { - success: options.verification_success, - content: options.verification_results ? File.read(options.verification_results) : nil, - content_type: options.verification_results_content_type, - format: options.verification_results_format, - verifier: options.verifier, - verifier_version: options.verifier_version - } - } - - command_options = { verbose: options.verbose, output: options.output } - result = ::Pactflow::Client::ProviderContracts::Publish.call(params, command_options, pact_broker_client_options) + validate_publish_provider_contract_options(provider_contract_path) + result = ::Pactflow::Client::ProviderContracts::Publish.call( + publish_provider_contract_command_params(provider_contract_path), + command_options, + pact_broker_client_options + ) $stdout.puts result.message exit(1) unless result.success end + + no_commands do + def command_options + { verbose: options.verbose, output: options.output } + end + + def validate_publish_provider_contract_options(provider_contract_path) + if !options.verification_success.nil? && options.verification_exit_code + raise Thor::Error, "Cannot use both --verification-success|--no-verification-success and --verification-exit-code" + end + end + + def publish_provider_contract_command_params(provider_contract_path) + success = !options.verification_success.nil? ? options.verification_success : ( options.verification_exit_code && options.verification_exit_code == 0 ) + + { + provider_name: options.provider.strip, + provider_version_number: options.provider_app_version.strip, + branch_name: options.branch && options.branch.strip, + tags: (options.tag && options.tag.collect(&:strip)) || [], + contract: { + content: File.read(provider_contract_path), + content_type: options.content_type, + specification: options.specification + }, + verification_results: { + success: success, + content: options.verification_results ? File.read(options.verification_results) : nil, + content_type: options.verification_results_content_type, + format: options.verification_results_format, + verifier: options.verifier, + verifier_version: options.verifier_version + } + } + end + end end end end diff --git a/script/publish-provider-contract.sh b/script/publish-provider-contract.sh index c365b6b..59985a2 100755 --- a/script/publish-provider-contract.sh +++ b/script/publish-provider-contract.sh @@ -1,5 +1,22 @@ -export PACT_BROKER_BASE_URL="http://localhost:9292" +export PACT_BROKER_BASE_URL=${PACT_BROKER_BASE_URL:-"http://localhost:9292"} export PACTFLOW_FEATURES=publish-provider-contract +bundle exec bin/pactflow publish-provider-contract \ + script/oas.yml \ + --provider Foo \ + --provider-app-version 1013b5650d61214e19f10558f97fb5a3bb082d44 \ + --branch main \ + --tag dev \ + --specification oas \ + --content-type application/yml \ + --verification-exit-code 0 \ + --verification-results script/verification-results.txt \ + --verification-results-content-type text/plain \ + --verification-results-format text \ + --verifier my-custom-tool \ + --verifier-version "1.0" \ + --verbose + + # bundle exec bin/pactflow publish-provider-contract \ # script/oas.yml \ # --provider Foo \ @@ -7,21 +24,5 @@ export PACTFLOW_FEATURES=publish-provider-contract # --branch main \ # --tag dev \ # --specification oas \ -# --content-type application/yml \ -# --no-verification-success \ -# --verification-results script/verification-results.txt \ -# --verification-results-content-type text/plain \ -# --verification-results-format text \ -# --verifier my-custom-tool \ -# --verifier-version "1.0" \ -# --verbose - +# --content-type application/yml - bundle exec bin/pactflow publish-provider-contract \ - script/oas.yml \ - --provider Foo \ - --provider-app-version 1013b5650d61214e19f10558f97fb5a3bb082d44 \ - --branch main \ - --tag dev \ - --specification oas \ - --content-type application/yml