Skip to content

Commit

Permalink
feat(can-i-deploy): allow dry-run to be enabled by setting ACT_BROKER…
Browse files Browse the repository at this point in the history
…_CAN_I_DEPLOY_DRY_RUN=true
  • Loading branch information
bethesque committed Jun 16, 2021
1 parent 9d90c79 commit 0562436
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
14 changes: 5 additions & 9 deletions README.md
Expand Up @@ -84,7 +84,8 @@ Options:
# The pacticipant version. Must be entered after the --pacticipant that it relates to.
[--ignore=IGNORE]
# The pacticipant name to ignore. Use once for each pacticipant being ignored.
A specific version can be ignored by also specifying a --version after the pacticipant name option.
A specific version can be ignored by also specifying a --version after the
pacticipant name option.
-l, [--latest=[TAG]]
# Use the latest pacticipant version. Optionally specify a TAG to use the
latest version with the specified tag.
Expand All @@ -103,6 +104,9 @@ Options:
# The time between retries in seconds. Use in conjuction with --retry-while-unknown
# Default: 10
[--dry-run], [--no-dry-run]
# When enabled, always exits process with a success code. Can also be enabled by setting
the environment variable PACT_BROKER_CAN_I_DEPLOY_DRY_RUN=true.
-b, --broker-base-url=BROKER_BASE_URL
# The base URL of the Pact Broker
-u, [--broker-username=BROKER_USERNAME]
Expand All @@ -113,14 +117,6 @@ Options:
# Pact Broker bearer token
-v, [--verbose], [--no-verbose]
# Verbose output. Default: false
Description:
Returns exit code 0 or 1, indicating whether or not the specified application (pacticipant) versions are
compatible (ie. safe to deploy). Prints out the relevant pact/verification details, indicating any
missing or failed verification results.
The environment variables PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD may be used
instead of their respective command line options.
```

Returns exit code 0 or 1, indicating whether or not the specified application (pacticipant) versions are compatible (ie. safe to deploy). Prints out the relevant pact/verification details, indicating any missing or failed verification results.
Expand Down
8 changes: 6 additions & 2 deletions lib/pact_broker/client/can_i_deploy.rb
Expand Up @@ -70,10 +70,14 @@ def failure_message(matrix)

def computer_says(success)
if success
Term::ANSIColor.green('Computer says yes \o/ ')
if dry_run?
"Computer says yes \\o/ (and maybe you don't need to enable dry run)"
else
Term::ANSIColor.green('Computer says yes \o/ ')
end
else
if dry_run?
"Computer says no ¯\_(ツ)_/¯ (but you're ignoring this with --dry-run)"
"Computer says no ¯\\_(ツ)_/¯ (but you're ignoring this by enabling dry run)"
else
Term::ANSIColor.red("Computer says no ¯\_(ツ)_/¯")
end
Expand Down
5 changes: 3 additions & 2 deletions lib/pact_broker/client/cli/broker.rb
Expand Up @@ -37,7 +37,7 @@ class Broker < CustomThor
method_option :retry_interval, banner: 'SECONDS', type: :numeric, default: 10, required: false, desc: "The time between retries in seconds. Use in conjuction with --retry-while-unknown"
# Allow limit to be set manually until https://github.com/pact-foundation/pact_broker-client/issues/53 is fixed
method_option :limit, hide: true
method_option :dry_run, type: :boolean, default: false, desc: "When enabled, always exits process with a success code"
method_option :dry_run, type: :boolean, default: false, desc: "When dry-run is enabled, always exit process with a success code. Can also be enabled by setting the environment variable PACT_BROKER_CAN_I_DEPLOY_DRY_RUN=true."
shared_authentication_options

def can_i_deploy(*ignored_but_necessary)
Expand All @@ -52,7 +52,8 @@ def can_i_deploy(*ignored_but_necessary)
[]
end
validate_can_i_deploy_selectors(selectors)
can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval, dry_run: options.dry_run }
dry_run = options.dry_run || ENV["PACT_BROKER_CAN_I_DEPLOY_DRY_RUN"] == "true"
can_i_deploy_options = { output: options.output, retry_while_unknown: options.retry_while_unknown, retry_interval: options.retry_interval, dry_run: dry_run }
result = CanIDeploy.call(options.broker_base_url, selectors, { to_tag: options.to, to_environment: options.to_environment, limit: options.limit, ignore_selectors: ignore_selectors }, can_i_deploy_options, pact_broker_client_options)
$stdout.puts result.message
$stdout.flush
Expand Down
@@ -1,4 +1,4 @@
[dry-run] Computer says no ¯_(ツ)_/¯ (but you're ignoring this with --dry-run)
[dry-run] Computer says no ¯\_(ツ)_/¯ (but you're ignoring this by enabling dry run)
[dry-run] 
[dry-run] text matrix
[dry-run] 
Expand Down
@@ -1,4 +1,4 @@
[dry-run] Computer says yes \o/
[dry-run] Computer says yes \o/ (and maybe you don't need to enable dry run)
[dry-run] 
[dry-run] text matrix
[dry-run] 
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb
Expand Up @@ -95,6 +95,18 @@ module CLI
end
end

context "when PACT_BROKER_CAN_I_DEPLOY_DRY_RUN=true" do
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with("PACT_BROKER_CAN_I_DEPLOY_DRY_RUN").and_return("true")
end

it "invokes the CanIDeploy service with dry_run set to true" do
expect(CanIDeploy).to receive(:call).with(anything, anything, anything, hash_including(dry_run: true), anything)
invoke_can_i_deploy
end
end

context "when successful" do
it "prints the message to stdout" do
expect($stdout).to receive(:puts).with(message)
Expand Down

0 comments on commit 0562436

Please sign in to comment.