Skip to content

Commit

Permalink
fix: do not allow an empty environment name to be used in the can-i-d…
Browse files Browse the repository at this point in the history
…eploy command
  • Loading branch information
bethesque committed Mar 29, 2023
1 parent 2aab2d5 commit a3bfe8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/pact_broker/client/cli/matrix_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def can_i_deploy(*ignored_but_necessary)
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)
validate_can_i_deploy_options
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 }
result = CanIDeploy.call(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)
Expand Down Expand Up @@ -82,6 +83,12 @@ def validate_can_i_deploy_selectors selectors
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 validate_can_i_deploy_options
if options[:to_environment] && options[:to_environment].blank?
raise ::Thor::RequiredArgumentMissingError, "The environment name cannot be blank"
end
end

def ignore_selectors_from_environment_variable
ENV.fetch("PACT_BROKER_CAN_I_DEPLOY_IGNORE", "").split(",").collect(&:strip).collect{ |i| { pacticipant: i } }
end
Expand Down
10 changes: 10 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 @@ -70,6 +70,16 @@ module CLI
expect(CanIDeploy).to receive(:call).with(anything, {to_tag: nil, to_environment: 'prod', limit: 1000, ignore_selectors: []}, anything, anything)
invoke_can_i_deploy
end

context "when the environment is an empty string" do
before do
subject.options.to_environment = ' '
end

it "raises an error" do
expect { invoke_can_i_deploy }.to raise_error ::Thor::RequiredArgumentMissingError
end
end
end

context "with basic auth" do
Expand Down

0 comments on commit a3bfe8a

Please sign in to comment.