Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stan is hate can i merge ignore #177

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 32 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,9 +27,41 @@ 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 "exits with failure if one pacticipant has a failing result" 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=5").
with(
headers: {
'Accept'=>'application/hal+json',
}).
to_return(status: 200, body: File.read("spec/support/matrix_with_failure.json"), headers: { "Content-Type" => "application/hal+json" })
stub_const("ARGV", %w[--pacticipant Bar --version 5])
expect($stdout).to receive(:puts).with(/Computer says no.*(success).*(failure)/m)
expect { invoke_can_i_merge }.to raise_error(SystemExit)
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=5&ignore%5B%5D%5Bpacticipant%5D=Foo").
with(
headers: {
'Accept'=>'application/hal+json',
}).
to_return(status: 200, body: File.read("spec/support/matrix_with_ignored_failure.json"), headers: { "Content-Type" => "application/hal+json" })
stub_const("ARGV", %w[--pacticipant Bar --version 5 --ignore Foo])
expect($stdout).to receive(:puts).with(/Computer says yes.*false \[ignored\]/m)
invoke_can_i_merge
end
end
end
end
Expand Down
61 changes: 61 additions & 0 deletions spec/support/matrix_with_failure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"summary": {
"deployable": false,
"ignored": 0,
"unknown": 0
},
"matrix": [
{
"consumer": {
"name": "Baz",
"version": {
"number": "4"
}
},
"provider": {
"name": "Bar",
"version": {
"number": "5"
}
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
}
},
{
"consumer": {
"name": "Foo",
"version": {
"number": "4"
}
},
"provider": {
"name": "Bar",
"version": {
"number": "5"
}
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": false,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
}
}
]
}
62 changes: 62 additions & 0 deletions spec/support/matrix_with_ignored_failure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"summary": {
"deployable": true,
"ignored": 1,
"unknown": 0
},
"matrix": [
{
"consumer": {
"name": "Baz",
"version": {
"number": "4"
}
},
"provider": {
"name": "Bar",
"version": {
"number": "5"
}
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": true,
"_links": {
"self": {
"href": "http://result"
}
}
},
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
}
},
{
"consumer": {
"name": "Foo",
"version": {
"number": "4"
}
},
"provider": {
"name": "Bar",
"version": {
"number": "5"
}
},
"verificationResult": {
"verifiedAt": "2017-10-10T12:49:04+11:00",
"success": false,
"_links": {
"self": {
"href": "http://result"
}
}
},
"ignored": true,
"pact": {
"createdAt": "2017-10-10T12:49:04+11:00"
}
}
]
}