Skip to content

Commit

Permalink
feat(can-i-deploy): support specifying --ignore using the environment…
Browse files Browse the repository at this point in the history
… variable PACT_BROKER_CAN_I_DEPLOY_IGNORE
  • Loading branch information
bethesque committed Dec 1, 2022
1 parent 6447718 commit 04a0894
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,10 @@ Options:
[--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.
specifying a --version after the pacticipant name option. The
environment variable PACT_BROKER_CAN_I_DEPLOY_IGNORE may also
be used to specify a pacticipant name to ignore, with commas to
separate multiple pacticipant names if necessary.
-l, [--latest=[TAG]]
# Use the latest pacticipant version. Optionally specify a TAG
to use the latest version with the specified tag.
Expand Down
10 changes: 7 additions & 3 deletions lib/pact_broker/client/cli/matrix_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module PactBroker
module Client
module CLI
module MatrixCommands

def self.included(thor)
thor.class_eval do

desc "can-i-deploy", ""
long_desc File.read(File.join(__dir__, "can_i_deploy_long_desc.txt"))

method_option :pacticipant, required: true, aliases: "-a", desc: "The pacticipant name. Use once for each pacticipant being checked."
method_option :version, required: false, aliases: "-e", desc: "The pacticipant version. Must be entered after the --pacticipant that it relates to."
method_option :ignore, required: false, desc: "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."
method_option :ignore, required: false, desc: "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. The environment variable PACT_BROKER_CAN_I_DEPLOY_IGNORE may also be used to specify a pacticipant name to ignore, with commas to separate multiple pacticipant names if necessary."
method_option :latest, required: false, aliases: "-l", banner: "[TAG]", desc: "Use the latest pacticipant version. Optionally specify a TAG to use the latest version with the specified tag."
method_option :to_environment, required: false, banner: "ENVIRONMENT", desc: "The environment into which the pacticipant(s) are to be deployed", default: nil
method_option :branch, required: false, desc: "The branch of the version for which you want to check the verification results", default: nil
Expand All @@ -29,7 +29,7 @@ def can_i_deploy(*ignored_but_necessary)

validate_credentials
selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| !s[:ignore] }
ignore_selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| s[:ignore] }
ignore_selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| s[:ignore] } + ignore_selectors_from_environment_variable
validate_can_i_deploy_selectors(selectors)
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, verbose: options.verbose }
Expand Down Expand Up @@ -81,6 +81,10 @@ def validate_can_i_deploy_selectors selectors
pacticipants_without_versions = selectors.select{ |s| s[:version].nil? && s[:latest].nil? && s[:tag].nil? && s[:branch].nil? }.collect{ |s| s[:pacticipant] }
raise ::Thor::RequiredArgumentMissingError, "The version must be specified using `--version VERSION`, `--branch BRANCH` `--latest`, `--latest TAG`, or `--all TAG` for pacticipant #{pacticipants_without_versions.join(", ")}" if pacticipants_without_versions.any?
end

def ignore_selectors_from_environment_variable
ENV.fetch("PACT_BROKER_CAN_I_DEPLOY_IGNORE", "").split(",").collect(&:strip).collect{ |i| { pacticipant: i } }
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/pact_broker/client/string_refinements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ module StringRefinements
def blank?
true
end

def not_blank?
false
end
end

refine String do
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/pact_broker/client/cli/broker_can_i_deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ module CLI
end
end

context "when PACT_BROKER_CAN_I_DEPLOY_IGNORE=Some Service" do
before do
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with("PACT_BROKER_CAN_I_DEPLOY_IGNORE", "").and_return("Some Service, Some Other Service")
end

it "invokes the CanIDeploy service with ignore selectors" do
expect(CanIDeploy).to receive(:call).with(anything, hash_including(ignore_selectors: [ { pacticipant: "Some Service" }, { pacticipant: "Some Other Service" } ]), anything, 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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'webmock/rspec'
require 'rspec/its'

ENV["PACT_BROKER_CAN_I_DEPLOY_IGNORE"] = nil

WebMock.disable_net_connect!(allow_localhost: true)

require "./spec/support/shared_context.rb"
Expand Down

0 comments on commit 04a0894

Please sign in to comment.