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") }