From 6dac495ee9aac0af367e7308d11b6fcf21355b2a Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 4 Apr 2022 09:19:50 +1000 Subject: [PATCH] fix(pacts for verification): return the pacts for the branch heads when using the branch selector Rather than returning the latest pacts for the versions that had pacts. The impact of this is that you can now decomission an integration by publishing a consumer version that does not have a pact any more. --- .../pacts/pact_publication_dataset_module.rb | 30 +++++++++++++++++++ .../pact_publication_dataset_module_spec.rb | 22 ++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/pact_broker/pacts/pact_publication_dataset_module.rb b/lib/pact_broker/pacts/pact_publication_dataset_module.rb index a97b5b12e..f8ad0f1a6 100644 --- a/lib/pact_broker/pacts/pact_publication_dataset_module.rb +++ b/lib/pact_broker/pacts/pact_publication_dataset_module.rb @@ -111,7 +111,37 @@ def overall_latest_for_consumer_id_and_provider_id(consumer_id, provider_id) .limit(1) end + # Return the pacts (if they exist) for the branch heads. + # This uses the new logic of finding the branch head and returning any associated pacts, + # rather than the old logic of returning the pact for the latest version + # on the branch that had a pact. + def for_branch_heads(branch_name) + branch_head_join = { + Sequel[:pact_publications][:consumer_version_id] => Sequel[:branch_heads][:version_id], + } + + base_query = self + if no_columns_selected? + base_query = base_query.select_all_qualified.select_append(Sequel[:branch_heads][:branch_name].as(:branch_name)) + end + + base_query + .join(:branch_heads, branch_head_join) do + name_like(Sequel[:branch_heads][:branch_name], branch_name) + end + .remove_overridden_revisions_from_complete_query + end + def latest_for_consumer_branch(branch_name) + # Keep this flag for a little whle in case we need to disable the new logic + if PactBroker.feature_enabled?(:disable_use_branch_heads_for_latest_branch_pacts, true) + old_latest_for_consumer_branch(branch_name) + else + for_branch_heads(branch_name) + end + end + + def old_latest_for_consumer_branch(branch_name) branch_versions_join = { Sequel[:pact_publications][:consumer_version_id] => Sequel[:branch_versions][:version_id] } diff --git a/spec/lib/pact_broker/pacts/pact_publication_dataset_module_spec.rb b/spec/lib/pact_broker/pacts/pact_publication_dataset_module_spec.rb index 45fd6541c..cb5653872 100644 --- a/spec/lib/pact_broker/pacts/pact_publication_dataset_module_spec.rb +++ b/spec/lib/pact_broker/pacts/pact_publication_dataset_module_spec.rb @@ -104,6 +104,28 @@ module Pacts expect(subject.first.values.keys.sort).to eq (PactPublication.columns + [:branch_name]).sort end + context "when there is no pact for the branch head" do + before do + td.create_consumer_version("12", branch: "main") + end + + it "does not return a pact" do + all = subject.all_allowing_lazy_load + expect(all.size).to eq 1 + end + + context "when the new logic is disabled" do + before do + allow(PactBroker). to receive(:feature_enabled?).with(:disable_use_branch_heads_for_latest_branch_pacts, true).and_return(true) + end + + it "does return a pact for the branch" do + all = subject.all_allowing_lazy_load + expect(all.size).to eq 2 + end + end + end + context "when columns are already selected" do subject { PactPublication.select(Sequel[:pact_publications][:id]).latest_for_consumer_branch("main") }