Skip to content

Commit

Permalink
feat: support --ignore flag in can-i-merge command
Browse files Browse the repository at this point in the history
  • Loading branch information
stan-is-hate authored and YOU54F committed Oct 23, 2024
1 parent d475041 commit 1349bd0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,13 @@ Options:
-e, [--version=VERSION]
# The pacticipant version. Must be entered after the
--pacticipant that it relates to.
[--ignore=PACTICIPANT]
# 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_MERGE_IGNORE may also be
used to specify a pacticipant name to ignore, with commas to
separate multiple pacticipant names if necessary.
-o, [--output=OUTPUT]
# json or table
# Default: table
Expand Down
9 changes: 7 additions & 2 deletions lib/pact_broker/client/cli/matrix_commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def can_i_deploy(*ignored_but_necessary)
long_desc "Checks if the specified pacticipant version is compatible with the configured main branch of each of the pacticipants with which it is integrated."
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, banner: "PACTICIPANT", 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_MERGE_IGNORE may also be used to specify a pacticipant name to ignore, with commas to separate multiple pacticipant names if necessary."
method_option :output, aliases: "-o", desc: "json or table", default: "table"
method_option :retry_while_unknown, banner: "TIMES", type: :numeric, default: 0, required: false, desc: "The number of times to retry while there is an unknown verification result (ie. the provider verification is likely still running)"
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"
Expand All @@ -59,11 +60,12 @@ def can_i_merge(*ignored_but_necessary)
require "pact_broker/client/can_i_deploy"

validate_credentials
selectors = VersionSelectorOptionsParser.call(ARGV)
selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| !s[:ignore] }
ignore_selectors = VersionSelectorOptionsParser.call(ARGV).select { |s| s[:ignore] } + ignore_merge_selectors_from_environment_variable
validate_can_i_deploy_selectors(selectors)
dry_run = options.dry_run || ENV["PACT_BROKER_CAN_I_MERGE_DRY_RUN"] == "true"
can_i_merge_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, { with_main_branches: true }, can_i_merge_options, pact_broker_client_options)
result = CanIDeploy.call(selectors, { with_main_branches: true, ignore_selectors: ignore_selectors}, can_i_merge_options, pact_broker_client_options)
$stdout.puts result.message
$stdout.flush
exit(1) unless result.success
Expand Down Expand Up @@ -121,6 +123,9 @@ def validate_can_i_deploy_options
def ignore_selectors_from_environment_variable
ENV.fetch("PACT_BROKER_CAN_I_DEPLOY_IGNORE", "").split(",").collect(&:strip).collect{ |i| { pacticipant: i } }
end
def ignore_merge_selectors_from_environment_variable
ENV.fetch("PACT_BROKER_CAN_I_MERGE_IGNORE", "").split(",").collect(&:strip).collect{ |i| { pacticipant: i } }
end
end
end
end
Expand Down
29 changes: 21 additions & 8 deletions spec/integration/can_i_merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ module CLI
allow($stdout).to receive(:puts)
allow($stderr).to receive(:puts)
allow(Retry).to receive(:sleep)

stub_const("ARGV", %w[--pacticipant Foo --version 1])
stub_request(:get, "http://pact-broker/matrix?latest=true&latestby=cvp&mainBranch=true&q%5B%5D%5Bpacticipant%5D=Foo&q%5B%5D%5Bversion%5D=1").
with(
headers: {
'Accept'=>'application/hal+json',
}).
to_return(status: 200, body: File.read("spec/support/matrix.json"), headers: { "Content-Type" => "application/hal+json" })
end

let(:minimum_valid_options) do
Expand All @@ -35,6 +27,27 @@ module CLI
let(:invoke_can_i_merge) { subject.can_i_merge }

it "sends a matrix query" do
stub_request(:get, "http://pact-broker/matrix?latest=true&latestby=cvp&mainBranch=true&q%5B%5D%5Bpacticipant%5D=Foo&q%5B%5D%5Bversion%5D=1").
with(
headers: {
'Accept'=>'application/hal+json',
}).
to_return(status: 200, body: File.read("spec/support/matrix.json"), headers: { "Content-Type" => "application/hal+json" })

stub_const("ARGV", %w[--pacticipant Foo --version 1])
expect($stdout).to receive(:puts).with(/Computer says yes/)
invoke_can_i_merge
end


it "ignores pacticipant if --ignore flag is provided" do
stub_request(:get, "http://pact-broker/matrix?latest=true&latestby=cvp&mainBranch=true&q%5B%5D%5Bpacticipant%5D=Bar&q%5B%5D%5Bversion%5D=3.4.5&ignore%5B%5D%5Bpacticipant%5D=Foo").
with(
headers: {
'Accept'=>'application/hal+json',
}).
to_return(status: 200, body: File.read("spec/support/matrix.json"), headers: { "Content-Type" => "application/hal+json" })
stub_const("ARGV", %w[--pacticipant Bar --version 3.4.5 --ignore Foo])
expect($stdout).to receive(:puts).with(/Computer says yes/)
invoke_can_i_merge
end
Expand Down

0 comments on commit 1349bd0

Please sign in to comment.