Skip to content

Commit

Permalink
feat: recalculate the branch head if the deleted branch version was t…
Browse files Browse the repository at this point in the history
…he latest for the branch
  • Loading branch information
bethesque committed Jun 6, 2022
1 parent 661667b commit db51d4f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/pact_broker/versions/branch_version_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions spec/lib/pact_broker/versions/branch_version_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit db51d4f

Please sign in to comment.