Skip to content

Commit

Permalink
feat(cli): allow all versions for a particular tag to be specified wh…
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 19, 2020
1 parent ecdfcbc commit 3690944
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.

Expand Down Expand Up @@ -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:
Expand All @@ -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

```
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,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
Expand Down
35 changes: 19 additions & 16 deletions lib/pact_broker/client/cli/version_selector_options_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" } ]
]
]

Expand Down

0 comments on commit 3690944

Please sign in to comment.