Skip to content

Commit

Permalink
feat: support deleting branch versions (removing a version from a bra…
Browse files Browse the repository at this point in the history
…nch)
  • Loading branch information
bethesque committed Jun 6, 2022
1 parent 8c5b347 commit 661667b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lib/pact_broker/api/resources/branch_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def content_types_accepted
end

def allowed_methods
["GET", "PUT", "OPTIONS"]
["GET", "PUT", "DELETE", "OPTIONS"]
end

def resource_exists?
Expand All @@ -25,6 +25,11 @@ def to_json
decorator_class(:branch_version_decorator).new(branch_version).to_json(decorator_options)
end

def delete_resource
branch_service.delete_branch_version(branch_version)
true
end

def from_json
already_existed = !!branch_version
@branch_version = branch_service.find_or_create_branch_version(identifier_from_path)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
# Pacticipant branch version

Allowed methods: `GET`, `PUT`
Allowed methods: `GET`, `PUT`, `DELETE`

Path: `/pacticipants/{pacticipant}/branches/{branch}/versions/{version}`

Get or add/create a pacticipant version for a branch.

## Example
## Create

### Example

Add a version to a branch. The pacticipant and branch are automatically created if they do not exist.

curl -XPUT http://broker/pacticipants/Bar/branches/main/versions/1e70030c6579915e5ff56b107a0fd25cf5df7464 \
-H "Content-Type: application/json" -d "{}"


## Delete

Removes a pacticipant version from a branch. Does not delete the actual pacticipant version.

Send a `DELETE` request to the branch version resource.

curl -XDELETE http://broker/pacticipants/Bar/branches/main/versions/1e70030c6579915e5ff56b107a0fd25cf5df7464
7 changes: 7 additions & 0 deletions lib/pact_broker/versions/branch_service.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
require "pact_broker/logging"
require "pact_broker/repositories"
require "pact_broker/messages"
require "forwardable"

module PactBroker
module Versions
class BranchService
extend PactBroker::Repositories

class << self
extend Forwardable
delegate [:delete_branch_version] => :branch_version_repository
end


def self.find_branch_version(pacticipant_name:, branch_name:, version_number:, **)
BranchVersion.where(
version: PactBroker::Domain::Version.where_pacticipant_name_and_version_number(pacticipant_name, version_number),
Expand Down
6 changes: 6 additions & 0 deletions lib/pact_broker/versions/branch_version_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def add_branch(version, branch_name, auto_created: false)
branch_version
end

# Deletes a branch version - that is, removes a version from a branch.
# @param [PactBroker::Versions::BranchVersion] the branch version to delete
def delete_branch_version(branch_version)
branch_version.delete
end

private

def find_or_create_branch(pacticipant, branch_name)
Expand Down
30 changes: 30 additions & 0 deletions spec/features/delete_branch_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe "Deleting a branch version (removing a version from a branch)" do
before do
td.create_consumer("foo")
.create_consumer_version("1234", branch: "main")
.create_consumer_version("1234", branch: "not-main")
.create_consumer_version("555", branch: "main")
.create_consumer("bar")
.create_consumer_version("1234", branch: "main")
end

let(:path) { "/pacticipants/foo/branches/main/versions/1234" }
let(:headers) { { "CONTENT_TYPE" => "application/json" } }
let(:response_body) { JSON.parse(subject.body, symbolize_names: true) }

subject { delete(path, {}, headers) }

it "returns a 204 response" do
expect(subject.status).to be 204
end

it "deletes the branch version" do
expect { subject }.to change { PactBroker::Versions::BranchVersion.count }.by(-1)
end

context "when the branch version does not exist" do
let(:path) { "/pacticipants/foo/branches/main/versions/888" }

its(:status) { is_expected.to eq 404 }
end
end

0 comments on commit 661667b

Please sign in to comment.