Skip to content

Commit

Permalink
fix(pacts for verification): return the pacts for the branch heads wh…
Browse files Browse the repository at this point in the history
…en 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.
  • Loading branch information
bethesque committed Apr 3, 2022
1 parent abbb043 commit 6dac495
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/pact_broker/pacts/pact_publication_dataset_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
22 changes: 22 additions & 0 deletions spec/lib/pact_broker/pacts/pact_publication_dataset_module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

Expand Down

0 comments on commit 6dac495

Please sign in to comment.