From db51d4feee22da91694701c1d303cafcace1b002 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 6 Jun 2022 14:39:03 +1000 Subject: [PATCH] feat: recalculate the branch head if the deleted branch version was the latest for the branch --- .../versions/branch_version_repository.rb | 13 ++++- .../branch_version_repository_spec.rb | 47 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/pact_broker/versions/branch_version_repository.rb b/lib/pact_broker/versions/branch_version_repository.rb index be187f482..7d14f1fc5 100644 --- a/lib/pact_broker/versions/branch_version_repository.rb +++ b/lib/pact_broker/versions/branch_version_repository.rb @@ -17,9 +17,20 @@ def add_branch(version, branch_name, auto_created: false) end # Deletes a branch version - that is, removes a version from a branch. + # Updates the branch head if the deleted branch version was the latest for the branch. + # # @param [PactBroker::Versions::BranchVersion] the branch version to delete def delete_branch_version(branch_version) - branch_version.delete + latest = branch_version.latest? + branch = branch_version.latest? ? branch_version.branch : nil + deleted = branch_version.delete + if latest + new_head_branch_version = BranchVersion.find_latest_for_branch(branch) + if new_head_branch_version + PactBroker::Versions::BranchHead.new(branch: branch, branch_version: new_head_branch_version).upsert + end + end + deleted end private diff --git a/spec/lib/pact_broker/versions/branch_version_repository_spec.rb b/spec/lib/pact_broker/versions/branch_version_repository_spec.rb index c0f84052b..61f401009 100644 --- a/spec/lib/pact_broker/versions/branch_version_repository_spec.rb +++ b/spec/lib/pact_broker/versions/branch_version_repository_spec.rb @@ -76,6 +76,53 @@ module Versions end end end + + describe "delete_branch_version" do + subject { BranchVersionRepository.new.delete_branch_version(branch_version) } + + context "when the branch version was not the branch head" do + before do + td.create_consumer("foo") + .create_consumer_version("1", branch: "main") + .create_consumer_version("2", branch: "main") + end + + let(:branch_version) { BranchVersion.first } + + subject { BranchVersionRepository.new.delete_branch_version(BranchVersion.first) } + + it "does not update the branch head" do + expect { subject }.to_not change { BranchHead.first.version_id } + end + end + + context "when the branch version was the branch head" do + before do + td.create_consumer("foo") + .create_consumer_version("1", branch: "main") + .create_consumer_version("2", branch: "main") + end + + let(:branch_version) { BranchVersion.last } + + it "does updates the branch head" do + expect { subject }.to change { BranchHead.first.version_id } + end + end + + context "when the branch version was the last for its branch" do + before do + td.create_consumer("foo") + .create_consumer_version("1", branch: "main") + end + + let(:branch_version) { BranchVersion.first } + + it "does not create a new branch head" do + expect { subject }.to change { BranchHead.count }.by(-1) + end + end + end end end end