Skip to content

Commit 3690944

Browse files
committed
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
1 parent ecdfcbc commit 3690944

File tree

4 files changed

+46
-24
lines changed

4 files changed

+46
-24
lines changed

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Returns exit code 0 or 1, indicating whether or not the specified application (p
9494

9595
The environment variables PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD may be used instead of their respective command line options.
9696

97-
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.
97+
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.
9898

9999
#### Specifying an application version
100100

@@ -105,6 +105,7 @@ To specify an application (pacticipant) version you need to provide:
105105
* `--version VERSION` to specify a known application version (recommended)
106106
* `--latest` to specify the latest version
107107
* `--latest TAG` to specify the latest version that has a particular tag
108+
* `--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).
108109

109110
Using a specific version is the easiest way to ensure you get an accurate response that won't be affected by race conditions.
110111

@@ -148,13 +149,20 @@ Can I deploy the latest version of the application Foo that has the tag "test" t
148149

149150

150151

151-
#### Alternate usage - specifying all dependencies explicitly
152+
#### Alternate usage - specifying dependencies explicitly
152153

153-
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.
154+
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.
154155

155-
$ pact-broker can-i-deploy --pacticipant PACTICIPANT_1 [--version VERSION_1 | --latest [TAG]] \
156-
--pacticipant PACTICIPANT_2 [--version VERSION_2 | --latest [TAG_2]] \
157-
--to ENVIRONMENT \
156+
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.)
157+
158+
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.
159+
160+
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.
161+
162+
163+
$ pact-broker can-i-deploy --pacticipant PACTICIPANT_1 [--version VERSION_1 | --latest [TAG_1] | --all TAG_1] \
164+
--pacticipant PACTICIPANT_2 [--version VERSION_2 | --latest [TAG_2] | --all TAG_2] \
165+
[--to ENVIRONMENT] \
158166
--broker-base-url BROKER_BASE_URL
159167

160168
Examples:
@@ -175,6 +183,14 @@ Can I deploy the latest version of Foo with tag "master" and the latest version
175183
--broker-base-url BROKER_BASE_URL
176184

177185

186+
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.)
187+
188+
189+
$ pact-broker can-i-deploy --pacticipant Bar --version b80e7b1b \
190+
--pacticipant Foo --all prod \
191+
--to prod \
192+
--broker-base-url BROKER_BASE_URL
193+
178194
### create-webhook
179195

180196
```

lib/pact_broker/client/cli/broker.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ def validate_pact_files pact_files
190190
end
191191

192192
def validate_can_i_deploy_selectors selectors
193-
pacticipants_without_versions = selectors.select{ |s| s[:version].nil? && s[:latest].nil? }.collect{ |s| s[:pacticipant] }
194-
raise ::Thor::RequiredArgumentMissingError, "The version must be specified using --version or --latest [TAG] for pacticipant #{pacticipants_without_versions.join(", ")}" if pacticipants_without_versions.any?
193+
pacticipants_without_versions = selectors.select{ |s| s[:version].nil? && s[:latest].nil? && s[:tag].nil? }.collect{ |s| s[:pacticipant] }
194+
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?
195195
end
196196

197197
def publish_pacts pact_files

lib/pact_broker/client/cli/version_selector_options_parser.rb

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@ module PactBroker
22
module Client
33
module CLI
44
class VersionSelectorOptionsParser
5-
def self.call options
6-
versions = []
7-
last_flag = nil
8-
options.each do | option |
9-
case option
5+
def self.call words
6+
selectors = []
7+
previous_option = nil
8+
words.each do | word |
9+
case word
1010
when "--pacticipant", "-a"
11-
versions << {}
11+
selectors << {}
1212
when "--latest", "-l"
13-
versions << {pacticipant: nil} unless versions.last
14-
versions.last[:latest] = true
13+
selectors << { pacticipant: nil } if selectors.empty?
14+
selectors.last[:latest] = true
1515
when /^\-/
1616
nil
1717
else
18-
case last_flag
18+
case previous_option
1919
when "--pacticipant", "-a"
20-
versions.last[:pacticipant] = option
20+
selectors.last[:pacticipant] = word
2121
when "--version", "-e"
22-
versions << {pacticipant: nil} unless versions.last
23-
versions.last[:version] = option
22+
selectors << { pacticipant: nil } if selectors.empty?
23+
selectors.last[:version] = word
2424
when "--latest", "-l"
25-
versions << {pacticipant: nil} unless versions.last
26-
versions.last[:tag] = option
25+
selectors << { pacticipant: nil } if selectors.empty?
26+
selectors.last[:tag] = word
27+
when "--all"
28+
selectors << { pacticipant: nil } if selectors.empty?
29+
selectors.last[:tag] = word
2730
end
2831
end
29-
last_flag = option if option.start_with?("-")
32+
previous_option = word if word.start_with?("-")
3033
end
31-
versions
34+
selectors
3235
end
3336
end
3437
end

spec/lib/pact_broker/client/cli/version_selector_options_parser_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ module CLI
3636
],[
3737
["--pacticipant", "Foo", "--latest", "prod", "--pacticipant", "Bar"],
3838
[{ pacticipant: "Foo", latest: true, tag: "prod"}, { pacticipant: "Bar" } ]
39+
],[
40+
["--pacticipant", "Foo", "--all", "prod", "--pacticipant", "Bar"],
41+
[{ pacticipant: "Foo", tag: "prod"}, { pacticipant: "Bar" } ]
3942
]
4043
]
4144

0 commit comments

Comments
 (0)