From da54dc1d05c1bf6f4dcaf5459ba4f809efce20e9 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 20 Jan 2020 09:04:42 +1100 Subject: [PATCH] feat(cli): allow all versions for a particular tag to be specified when determining if it safe to deploy (mobile provider use case) https://pact.canny.io/feature-requests/p/ability-to-label-multiple-versions-or-a-range-of-versions-with-the-same-tag --- README.md | 28 +++++++++++---- lib/pact_broker/client/cli/broker.rb | 4 +-- .../cli/version_selector_options_parser.rb | 35 ++++++++++--------- .../version_selector_options_parser_spec.rb | 3 ++ 4 files changed, 46 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 66460b3d..51dbeb77 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ Returns exit code 0 or 1, indicating whether or not the specified application (p The environment variables PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD may be used instead of their respective command line options. -There are two ways to use `can-i-deploy`. The first (recommended and most common) approach is to specify just the application version you want to deploy and let the Pact Broker work out the dependencies for you. The second approach is to specify each application version explicitly. This would generally only be used if there were limitations that stopped you being able to use the first approach. +There are two ways to use `can-i-deploy`. The first (recommended and most commonly used) approach is to specify just the application version you want to deploy and let the Pact Broker work out the dependencies for you. The second approach is to specify each application version explicitly. This would generally only be used if there were limitations that stopped you being able to use the first approach. #### Specifying an application version @@ -105,6 +105,7 @@ To specify an application (pacticipant) version you need to provide: * `--version VERSION` to specify a known application version (recommended) * `--latest` to specify the latest version * `--latest TAG` to specify the latest version that has a particular tag + * `--all TAG` to specify all the versions that have a particular tag (eg. "all prod" versions). This would be used when ensuring you have backwards compatiblity with all production mobile clients for a provider. Note, when using this option, you need to specify dependency explicitly (see the second usage option). Using a specific version is the easiest way to ensure you get an accurate response that won't be affected by race conditions. @@ -148,13 +149,20 @@ Can I deploy the latest version of the application Foo that has the tag "test" t -#### Alternate usage - specifying all dependencies explicitly +#### Alternate usage - specifying dependencies explicitly -If you are unable to use tags, or there is some other limitation that stops you from using the recommended approach, you can specify each of the application versions explictly. You can specify as many application versions as you like. +If you are unable to use tags, or there is some other limitation that stops you from using the recommended approach, you can specify one or more of the dependencies explictly. You must also do this if you want to use the `--all TAG` option for any of the pacticipants. - $ pact-broker can-i-deploy --pacticipant PACTICIPANT_1 [--version VERSION_1 | --latest [TAG]] \ - --pacticipant PACTICIPANT_2 [--version VERSION_2 | --latest [TAG_2]] \ - --to ENVIRONMENT \ +You can specify as many application versions as you like, and you can even specify multiple versions of the same application (repeat the `--pacticipant` name and supply a different version.) + +You can use explictly declared dependencies with or without the `--to ENVIRONMENT`. For example, if you declare two (or more) application versions with no `--to ENVIRONMENT`, then only the applications you specify will be taken into account when determining if it is safe to deploy. If you declare two (or more) application versions _as well as_ a `--to ENVIRONMENT`, then the Pact Broker will work out what integrations your declared applications will have in that environment when determining if it safe to deploy. When using this script for a production release, and you are using tags, it is always the most future-proof option to use the `--to` if possible, as it will catch any newly added consumers or providers. + +If you are finding that your dependencies are not being automatically included when you supply multiple pacticipant versions, please upgrade to the latest version of the Pact Broker, as this is a more recently added feature. + + + $ pact-broker can-i-deploy --pacticipant PACTICIPANT_1 [--version VERSION_1 | --latest [TAG_1] | --all TAG_1] \ + --pacticipant PACTICIPANT_2 [--version VERSION_2 | --latest [TAG_2] | --all TAG_2] \ + [--to ENVIRONMENT] \ --broker-base-url BROKER_BASE_URL Examples: @@ -175,6 +183,14 @@ Can I deploy the latest version of Foo with tag "master" and the latest version --broker-base-url BROKER_BASE_URL +Mobile provider use case - can I deploy version b80e7b1b of Bar, all versions of Foo with tag "prod", and the latest version tagged "prod" of any other automatically calculated dependencies together? (Eg. where Bar is a provider and Foo is a mobile consumer with multiple versions in production, and Bar also has its own providers it needs to be compatible with.) + + + $ pact-broker can-i-deploy --pacticipant Bar --version b80e7b1b \ + --pacticipant Foo --all prod \ + --to prod \ + --broker-base-url BROKER_BASE_URL + ### create-webhook ``` diff --git a/lib/pact_broker/client/cli/broker.rb b/lib/pact_broker/client/cli/broker.rb index 8994c678..9eadaeca 100644 --- a/lib/pact_broker/client/cli/broker.rb +++ b/lib/pact_broker/client/cli/broker.rb @@ -144,8 +144,8 @@ def validate_pact_files pact_files end def validate_can_i_deploy_selectors selectors - pacticipants_without_versions = selectors.select{ |s| s[:version].nil? && s[:latest].nil? }.collect{ |s| s[:pacticipant] } - raise ::Thor::RequiredArgumentMissingError, "The version must be specified using --version or --latest [TAG] for pacticipant #{pacticipants_without_versions.join(", ")}" if pacticipants_without_versions.any? + pacticipants_without_versions = selectors.select{ |s| s[:version].nil? && s[:latest].nil? && s[:tag].nil? }.collect{ |s| s[:pacticipant] } + raise ::Thor::RequiredArgumentMissingError, "The version must be specified using `--version VERSION`, `--latest`, `--latest TAG`, or `--all TAG` for pacticipant #{pacticipants_without_versions.join(", ")}" if pacticipants_without_versions.any? end def publish_pacts pact_files diff --git a/lib/pact_broker/client/cli/version_selector_options_parser.rb b/lib/pact_broker/client/cli/version_selector_options_parser.rb index ee24badf..68a7f0d7 100644 --- a/lib/pact_broker/client/cli/version_selector_options_parser.rb +++ b/lib/pact_broker/client/cli/version_selector_options_parser.rb @@ -2,33 +2,36 @@ module PactBroker module Client module CLI class VersionSelectorOptionsParser - def self.call options - versions = [] - last_flag = nil - options.each do | option | - case option + def self.call words + selectors = [] + previous_option = nil + words.each do | word | + case word when "--pacticipant", "-a" - versions << {} + selectors << {} when "--latest", "-l" - versions << {pacticipant: nil} unless versions.last - versions.last[:latest] = true + selectors << { pacticipant: nil } if selectors.empty? + selectors.last[:latest] = true when /^\-/ nil else - case last_flag + case previous_option when "--pacticipant", "-a" - versions.last[:pacticipant] = option + selectors.last[:pacticipant] = word when "--version", "-e" - versions << {pacticipant: nil} unless versions.last - versions.last[:version] = option + selectors << { pacticipant: nil } if selectors.empty? + selectors.last[:version] = word when "--latest", "-l" - versions << {pacticipant: nil} unless versions.last - versions.last[:tag] = option + selectors << { pacticipant: nil } if selectors.empty? + selectors.last[:tag] = word + when "--all" + selectors << { pacticipant: nil } if selectors.empty? + selectors.last[:tag] = word end end - last_flag = option if option.start_with?("-") + previous_option = word if word.start_with?("-") end - versions + selectors end end end diff --git a/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb b/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb index cec00813..b3fa2619 100644 --- a/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb +++ b/spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb @@ -36,6 +36,9 @@ module CLI ],[ ["--pacticipant", "Foo", "--latest", "prod", "--pacticipant", "Bar"], [{ pacticipant: "Foo", latest: true, tag: "prod"}, { pacticipant: "Bar" } ] + ],[ + ["--pacticipant", "Foo", "--all", "prod", "--pacticipant", "Bar"], + [{ pacticipant: "Foo", tag: "prod"}, { pacticipant: "Bar" } ] ] ]